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