vendredi 11 août 2017

What is test(0) in this snippet from kotlinlang.org?

I'm trying to solve the problem of "runs" in the KotlinKoans page here

Here is my implementation

package runs

fun runs(a: IntArray): Int {
    var numberOfRuns = 1;

    for (index in 0..a.size-2) {
        if (a[index+1] != a[index]) numberOfRuns++
    }

    return numberOfRuns
}

Here is the test suite that runs against the implementation

class Tests {

    @Test fun testRuns1() {
        test(0)
    }

    @Test fun testRuns2() {
        test(1, 1)
    }

    @Test fun testRuns3() {
        test(3, 1, 2, 3)
    }

    @Test fun testRuns4() {
        test(3, 1, 2, 2, 3)
    }

    @Test fun testRuns5() {
        test(3, 1, 1, 2, 3)
    }

    @Test fun testRuns6() {
        test(1, 1, 1, 1, 1)
    }

    @Test fun testRuns7() {
        test(3, 1, 1, 1, 0, 1, 1)
    }

    @Test fun testRuns8() {
        test(3, 1, 1, 1, 0, 1)
    }

    @Test fun testRuns9() {
        test(5, 1, 0, 1, 0, 1)
    }
}

fun test(expected: Int, vararg data: Int) {
    assertEquals(expected, runs(data), "\ndata = ${Arrays.toString(data)}")
}

All test passes except testRuns1(), I don't understand what test(0) does, given that the test() function takes two parametrs as input, an expected value and the actual value. What am I missing?

Aucun commentaire:

Enregistrer un commentaire