-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better diagnostics for map iteration while mutating
If one obtains a `MutableMap.Entry` from a mutable map / builder, and then removes the underlying value from the map, we currently return `null` from the entry's `value` property, rather than throwing. Either behavior is allowed by the `MutableMap.Entry` spec (which specifies `IllegalStateException` *or* undefined behavior in this case), but it's much easier to debug this if an exception is thrown.
- Loading branch information
Showing
2 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
collect/src/test/kotlin/com/certora/collect/MapEntryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.certora.collect | ||
|
||
import kotlin.test.* | ||
|
||
/** Tests for map entries. */ | ||
class MapEntryTest { | ||
@Test | ||
fun getValue() { | ||
val map = treapMapBuilderOf(1 to 2, 3 to 4) | ||
val e = map.entries.first() | ||
assertEquals(1, e.key) | ||
assertEquals(2, e.value) | ||
map.remove(1) | ||
assertEquals(1, e.key) | ||
assertFailsWith<IllegalStateException> { e.value } | ||
} | ||
|
||
@Test | ||
fun setValue() { | ||
val map = treapMapBuilderOf(1 to 2, 3 to 4) | ||
val e = map.entries.first() | ||
assertEquals(2, e.setValue(5)) | ||
assertEquals(1, e.key) | ||
assertEquals(5, e.value) | ||
assertEquals(5, map[1]) | ||
map.remove(1) | ||
assertEquals(1, e.key) | ||
assertFailsWith<IllegalStateException> { e.setValue(10) } | ||
} | ||
|
||
@Test | ||
fun getAndSetNullValue() { | ||
val map = treapMapBuilderOf(1 to null, 3 to 4) | ||
val e = map.entries.first() | ||
assertEquals(1, e.key) | ||
assertEquals(null, e.value) | ||
assertEquals(null, e.setValue(5)) | ||
assertEquals(5, e.value) | ||
assertEquals(5, map[1]) | ||
assertEquals(5, e.setValue(null)) | ||
assertEquals(null, e.value) | ||
assertEquals(null, map[1]) | ||
} | ||
} |