Skip to content

Instantly share code, notes, and snippets.

@harsh183
Last active May 29, 2026 04:08
Show Gist options
  • Select an option

  • Save harsh183/fb32427bb283b5b546bab7974859060b to your computer and use it in GitHub Desktop.

Select an option

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
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
}
@harsh183

Copy link
Copy Markdown
Author

Sum scores using reduce like

val (score, totalScore) = results.reduce() { sum, result ->
    TestResult(sum.score+result.score, sum.maxScore+result.maxScore)
}

Performs about the same but far less readable

@harsh183

Copy link
Copy Markdown
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

@harsh183

Copy link
Copy Markdown
Author

image
250-300ms on jeed plain

@harsh183

Copy link
Copy Markdown
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

@harsh183

harsh183 commented Apr 18, 2020

Copy link
Copy Markdown
Author

MIT License

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment