@@ -107,6 +107,124 @@ class PersistentHashMapBuilderTest : ExecutionTimeMeasuringTest() {
107107 }
108108 }
109109
110+ private fun testAfterRandomPut (block : (MutableMap <IntWrapper , Int >, PersistentMap <IntWrapper , Int >) -> Unit ) {
111+ val elementsToAdd = NForAlgorithmComplexity .O_NNlogN
112+
113+ var map = persistentHashMapOf<IntWrapper , Int >()
114+ val expected = hashMapOf<IntWrapper , Int >()
115+
116+ repeat(times = elementsToAdd) {
117+ val keyValue = Random .nextInt()
118+ val keyHash = Random .nextInt(elementsToAdd) // to have collisions
119+ val key = IntWrapper (keyValue, keyHash)
120+
121+ expected[key] = keyValue
122+ map = map.put(key, keyValue)
123+
124+ val shouldTest = Random .nextDouble() < 0.1
125+ if (shouldTest) {
126+ block(expected, map)
127+ }
128+ }
129+ }
130+
131+ @Test
132+ fun keysIteratorTests () {
133+ fun testKeysIterator (expected : MutableMap <IntWrapper , Int >, actual : PersistentMap .Builder <IntWrapper , Int >) {
134+ var expectedSize = actual.size
135+ while (expectedSize > 0 ) {
136+ assertEquals(expectedSize, actual.size)
137+
138+ val iterator = actual.keys.iterator()
139+ repeat(expectedSize) {
140+ assertTrue(iterator.hasNext())
141+
142+ val nextKey = iterator.next()
143+ assertEquals(expected[nextKey], actual[nextKey])
144+
145+ val shouldRemove = Random .nextDouble() < 0.2
146+ if (shouldRemove) {
147+ iterator.remove()
148+ expectedSize--
149+ }
150+ }
151+ assertFalse(iterator.hasNext())
152+ }
153+
154+ assertTrue(actual.isEmpty())
155+ }
156+
157+ testAfterRandomPut { expected, map ->
158+ testKeysIterator(expected, map.builder())
159+ }
160+ }
161+
162+ @Test
163+ fun valuesIteratorTests () {
164+ fun testValuesIterator (actual : PersistentMap .Builder <IntWrapper , Int >) {
165+ var expectedSize = actual.size
166+ while (expectedSize > 0 ) {
167+ assertEquals(expectedSize, actual.size)
168+
169+ val iterator = actual.values.iterator()
170+ repeat(expectedSize) {
171+ assertTrue(iterator.hasNext())
172+
173+ iterator.next()
174+
175+ val shouldRemove = Random .nextDouble() < 0.2
176+ if (shouldRemove) {
177+ iterator.remove()
178+ expectedSize--
179+ }
180+ }
181+ assertFalse(iterator.hasNext())
182+ }
183+
184+ assertTrue(actual.isEmpty())
185+ }
186+
187+ testAfterRandomPut { _, map ->
188+ testValuesIterator(map.builder())
189+ }
190+ }
191+
192+ @Test
193+ fun entriesIteratorTests () {
194+ fun testEntriesIterator (expected : MutableMap <IntWrapper , Int >, actual : PersistentMap .Builder <IntWrapper , Int >) {
195+ var expectedSize = actual.size
196+ while (expectedSize > 0 ) {
197+ assertEquals(expectedSize, actual.size)
198+
199+ val iterator = actual.entries.iterator()
200+ repeat(expectedSize) {
201+ assertTrue(iterator.hasNext())
202+
203+ val nextEntry = iterator.next()
204+ assertEquals(expected[nextEntry.key], actual[nextEntry.key])
205+
206+ val shouldUpdate = Random .nextDouble() < 0.1
207+ if (shouldUpdate) {
208+ val newValue = Random .nextInt()
209+ assertEquals(expected.put(nextEntry.key, newValue), nextEntry.setValue(newValue))
210+ }
211+ val shouldRemove = Random .nextDouble() < 0.2
212+ if (shouldRemove) {
213+ iterator.remove()
214+ expectedSize--
215+ }
216+ }
217+ assertFalse(iterator.hasNext())
218+ }
219+
220+ assertTrue(actual.isEmpty())
221+ }
222+
223+ testAfterRandomPut { expected, map ->
224+ testEntriesIterator(expected.toMutableMap(), map.builder())
225+ }
226+ }
227+
110228 @Test
111229 fun removeTests () {
112230 val builder = persistentHashMapOf<Int , String >().builder()
0 commit comments