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 | |
| } |
Author
Author
TODO: Maybe allow people to store and run them in other ways? Maybe even parallel huehuehue
Eh writing a new test framework isn't really worthwhile
Author
Author
package TestUtil
data class TestResult(var score: Double, var maxScore: Double)
data class Case(val name: String, val weight: Double = 1.0, val code: () -> Boolean) {
fun test(): TestResult {
val result = code()
val resultDisplay = if (result) "PASSED" else "FAILED"
println("TEST $name -> $resultDisplay ($weight)")
val score = if (result) weight else 0.0
return TestResult(score, weight)
}
}
// exmaple
fun main() {
val cases = 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
}
)
runCases(cases)
}
fun runCases(cases: List<Case>) {
println("Running tests")
val (score, totalScore) = sumScore(cases)
println("Got $score / $totalScore")
}
fun sumScore(cases: List<Case>): Pair<Double, Double> {
var score: Double = 0.0
var totalScore: Double = 0.0
cases.forEach {
val result = it.test()
score += result.score
totalScore += result.maxScore
}
return Pair(score, totalScore)
}
// User code from here
fun add(a: Int, b: Int): Int {
return a + b
}Even fancier version
Author
MIT License
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Sum scores using reduce like
Performs about the same but far less readable