Skip to content

Commit

Permalink
Fix NullPointerException in treapListOf(1).subString(1, 1) (and many …
Browse files Browse the repository at this point in the history
…other cases) (#16)
  • Loading branch information
ericeil authored May 23, 2024
1 parent 840d5eb commit 1bb0beb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions collect/src/main/kotlin/com/certora/collect/TreapListNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ internal class TreapListNode<E> private constructor(
private fun split(index: Int): Pair<TreapListNode<E>?, TreapListNode<E>?> = stepToIndex(
index,
found = { left to this.with(left = null) },
goLeft = { left!!.split(it).let { it.first to this.with(left = it.second) }},
goRight = { right!!.split(it).let { this.with(right = it.first) to it.second }},
goLeft = { left?.split(it).let { it?.first to this.with(left = it?.second) }},
goRight = { right?.split(it).let { this.with(right = it?.first) to it?.second }},
)

private fun removeNode(element: E): TreapListNode<E>? {
Expand Down
25 changes: 25 additions & 0 deletions collect/src/test/kotlin/com/certora/collect/TreapListTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,29 @@ class TreapListTest {
val large = (1..100000).toTreapList()
large.removeAt(large.size / 2)
}

@Test
fun subList() {
assertEquals(listOf<Int>(), treapListOf<Int>().subList(0, 0))
assertFailsWith<IndexOutOfBoundsException> { treapListOf<Int>().subList(0, 1) }
assertFailsWith<IndexOutOfBoundsException> { treapListOf<Int>().subList(1, 1) }

assertEquals(listOf<Int>(), treapListOf(1).subList(0, 0))
assertEquals(listOf<Int>(), treapListOf(1).subList(1, 1))
assertEquals(listOf(1), treapListOf(1).subList(0, 1))
assertFailsWith<IndexOutOfBoundsException> { treapListOf<Int>().subList(1, 2) }
assertFailsWith<IndexOutOfBoundsException> { treapListOf<Int>().subList(2, 2) }

assertEquals(listOf<Int>(), treapListOf(1, 2).subList(2, 2))
assertEquals(listOf(1), treapListOf(1, 2).subList(0, 1))
assertEquals(listOf(2), treapListOf(1, 2).subList(1, 2))
assertEquals(listOf(1, 2), treapListOf(1, 2).subList(0, 2))

assertEquals(listOf(1), treapListOf(1, 2, 3).subList(0, 1))
assertEquals(listOf(1, 2), treapListOf(1, 2, 3).subList(0, 2))
assertEquals(listOf(2, 3), treapListOf(1, 2, 3).subList(1, 3))
assertEquals(listOf(3), treapListOf(1, 2, 3).subList(2, 3))
assertEquals(listOf<Int>(), treapListOf(1, 2, 3).subList(3, 3))
assertEquals(listOf(1, 2, 3), treapListOf(1, 2, 3).subList(0, 3))
}
}

0 comments on commit 1bb0beb

Please sign in to comment.