Last active
May 29, 2026 04:08
-
-
Save harsh183/fb32427bb283b5b546bab7974859060b to your computer and use it in GitHub Desktop.
A very, very budget cut test running framework that also works with grade weights. Expanded from hackyTest.kt. It eventually got used in https://github.com/daviskeene/KTeach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data class TestResult(var score: Double, var maxScore: Double); | |
| fun main() { | |
| println("Running stuff") | |
| val results = listOf( | |
| case("5 + 10", 2.0) { | |
| var x = 15; | |
| x = x + 10 | |
| add(5, 10) == x | |
| }, | |
| case("10 + 15", weight=0.25) { | |
| add(10, 15) == 25 | |
| }, | |
| case("Negative") { | |
| add(10, -15) == -5 | |
| }, | |
| case("Zero", 10.0) { | |
| add(10, 0) == 10 | |
| } | |
| ) | |
| val (score, totalScore) = sumScore(results) | |
| println("Got $score / $totalScore") | |
| } | |
| fun sumScore(results: List<TestResult>): Pair<Double, Double> { | |
| var score: Double = 0.0 | |
| var totalScore: Double = 0.0 | |
| for ((pointsScored, possiblePoints) in results) { | |
| score += pointsScored | |
| totalScore += possiblePoints | |
| } | |
| return Pair(score, totalScore) | |
| } | |
| fun case(name: String, weight: Double = 1.0, code: () -> Boolean): TestResult { | |
| val result = code() | |
| val resultDisplay = if (result) "PASSED" else "FAILED" | |
| println("TEST " + name + " -> " + resultDisplay) | |
| val score = if (result) weight else 0.0 | |
| return TestResult(score, weight) | |
| } | |
| // User code from here | |
| fun add(a: Int, b: Int): Int { | |
| return a + b | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Even fancier version