Jacoco 1 of 4 branch is missing

1.4k Views Asked by At

I am using Jacoco to check the coverage of my test, i tried lots of way but it still warning 1 of 4 branches missed

fun countingDeleteDemo() : Int {
    return list.count { it.isDeleted() }
}

How can I know what branch is missed? I have read some posts about the logic true && false, but is there any documents or official link about the mistake of the coverage tools?

1

There are 1 best solutions below

0
On

Jacoco checks the code coverage on byte code and not on source code. As far as I know Jacoco does not show which branch is missing.

If you use IntelliJ IDEA, you can check the kotlin bytecode.

Here, the missing branch in my IDE.

// ... omitted
INVOKESTATIC kotlin/collections/CollectionsKt.throwCountOverflow ()V
// ... omitted

Kotlin's has many builtin extension function count for Iterable, Array ...

I presume that your list is a simple List, so an Iterable. You do not have much more elements than Int.MAX_VALUE which will make the count negative. But one can pass an iterable which may have beyond Int.MAX_VALUE elements to count.

As a good citizen, the kotlin jvm implementation checks count overflow.

You can check out the implementation details.

public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Int {
    if (this is Collection && isEmpty()) return 0
    var count = 0
    for (element in this) if (predicate(element)) checkCountOverflow(++count)
    return count
}
@PublishedApi
@SinceKotlin("1.3")
@InlineOnly
internal actual inline fun checkCountOverflow(count: Int): Int {
    if (count < 0) {
        if (apiVersionIsAtLeast(1, 3, 0))
            throwCountOverflow()
        else
            throw ArithmeticException("Count overflow has happened.")
    }
    return count
}

https://github.com/JetBrains/kotlin/blob/b8ea48fdc29678b6d99cb1b7bad312f917ea9529/libraries/stdlib/jvm/src/kotlin/collections/CollectionsJVM.kt#L111