Skip to content

Commit b130378

Browse files
author
Abduqodiri Qurbonzoda
committed
Fix collision only map iterator initialization crash (#57)
1 parent 48837a1 commit b130378

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

core/commonMain/src/implementations/immutableMap/PersistentHashMapContentIterators.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ internal abstract class PersistentHashMapBaseIterator<K, V, T>(node: TrieNode<K,
105105
private var hasNext = true
106106

107107
init {
108-
path[0].reset(node.buffer, ENTRY_SIZE * node.entryCount())
108+
if (node.isCollision()) {
109+
path[0].reset(node.buffer, node.buffer.size)
110+
} else {
111+
path[0].reset(node.buffer, ENTRY_SIZE * node.entryCount())
112+
}
109113
pathLastIndex = 0
110114
ensureNextEntryIsReady()
111115
}

core/commonTest/src/stress/map/PersistentHashMapTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ class PersistentHashMapTest : ExecutionTimeMeasuringTest() {
109109
}
110110
}
111111

112+
@Test
113+
fun iteratorTests() {
114+
val emptyIterator = persistentHashMapOf<Int, String>().iterator()
115+
assertFalse(emptyIterator.hasNext())
116+
assertFails { emptyIterator.next() }
117+
118+
val collisionIterator = persistentHashMapOf<IntWrapper, String>().put(IntWrapper(1, 1), "a").put(IntWrapper(2, 1), "b").iterator()
119+
var aFlag = false
120+
var bFlag = false
121+
for (entry in collisionIterator) {
122+
aFlag = aFlag || entry.value == "a"
123+
bFlag = bFlag || entry.value == "b"
124+
}
125+
assertTrue(aFlag && bFlag)
126+
}
127+
112128
@Test
113129
fun removeTests() {
114130
var map = persistentHashMapOf<Int, String>()

0 commit comments

Comments
 (0)