Skip to content

Commit

Permalink
Fix stack overflow in TreapMapBuilder.clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
ericeil committed Jan 12, 2024
1 parent 9ccf2b2 commit e64c9b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal class TreapMapBuilder<@Treapable K, V>(
override fun get(key: K): V? = map.get(key)
override fun getOrDefault(key: K, defaultValue: @UnsafeVariance V): V = map.getOrDefault(key, defaultValue)

override fun clear() { map = map.clear() }

override fun remove(key: K): V? {
val oldMap = map
map = map.remove(key)
Expand Down
13 changes: 13 additions & 0 deletions collect/src/test/kotlin/com/certora/collect/TreapMapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ abstract class TreapMapTest {
}
}

@Test
fun clear() {
val m = makeMap()
val k = makeKey(1)
val v = Object()
m.put(k, v)
assertEquals(1, m.size)
assertEquals(v, m[k])
m.clear()
assertEquals(0, m.size)
assertNull(m[k])
}

@Test
fun addMany() {
val b = makeBaseline()
Expand Down
12 changes: 12 additions & 0 deletions collect/src/test/kotlin/com/certora/collect/TreapSetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ abstract class TreapSetTest {
}
}

@Test
fun clear() {
val s = makeSet()
val k = makeKey(1)
s.add(k)
assertEquals(1, s.size)
assertTrue(k in s)
s.clear()
assertEquals(0, s.size)
assertFalse(k in s)
}

fun randomHashObjectMaybeNull(rand: Random): TestKey? {
val r = rand.nextInt()
if (nullKeysAllowed) {
Expand Down

0 comments on commit e64c9b0

Please sign in to comment.