Skip to content

Commit 2f00671

Browse files
author
Abduqodiri Qurbonzoda
committed
Typealias IntWrapper = ObjectWrapper<Int>
1 parent e8e8353 commit 2f00671

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

core/commonTest/src/stress/ObjectWrapper.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ class ObjectWrapper<K: Comparable<K>>(
3838
override fun compareTo(other: ObjectWrapper<K>): Int {
3939
return obj.compareTo(other.obj)
4040
}
41-
}
41+
}
42+
43+
typealias IntWrapper = ObjectWrapper<Int>

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import tests.NForAlgorithmComplexity
2222
import tests.distinctStringValues
2323
import tests.remove
2424
import tests.stress.ExecutionTimeMeasuringTest
25-
import tests.stress.ObjectWrapper
25+
import tests.stress.IntWrapper
2626
import tests.stress.WrapperGenerator
2727
import kotlin.random.Random
2828
import kotlin.test.*
@@ -213,15 +213,15 @@ class PersistentHashMapBuilderTest : ExecutionTimeMeasuringTest() {
213213

214214
@Test
215215
fun collisionTests() {
216-
val builder = persistentHashMapOf<ObjectWrapper<Int>, Int>().builder()
216+
val builder = persistentHashMapOf<IntWrapper, Int>().builder()
217217

218218
repeat(times = 2) { removeEntryPredicate ->
219219

220220
val elementsToAdd = NForAlgorithmComplexity.O_NlogN
221221

222222
val maxHashCode = elementsToAdd / 5 // should be less than `elementsToAdd`
223223
val keyGen = WrapperGenerator<Int>(maxHashCode)
224-
fun key(key: Int): ObjectWrapper<Int> {
224+
fun key(key: Int): IntWrapper {
225225
return keyGen.wrapper(key)
226226
}
227227

@@ -259,7 +259,7 @@ class PersistentHashMapBuilderTest : ExecutionTimeMeasuringTest() {
259259

260260
assertTrue(builder.remove(key, key.obj))
261261
} else {
262-
val nonExistingKey = ObjectWrapper(Int.MIN_VALUE, key.hashCode)
262+
val nonExistingKey = IntWrapper(Int.MIN_VALUE, key.hashCode)
263263
assertNull(builder.remove(nonExistingKey))
264264
assertEquals(key.obj, builder[key])
265265

@@ -276,8 +276,8 @@ class PersistentHashMapBuilderTest : ExecutionTimeMeasuringTest() {
276276

277277
@Test
278278
fun randomOperationsTests() {
279-
val mapGen = mutableListOf(List(20) { persistentHashMapOf<ObjectWrapper<Int>, Int>() })
280-
val expected = mutableListOf(List(20) { mapOf<ObjectWrapper<Int>, Int>() })
279+
val mapGen = mutableListOf(List(20) { persistentHashMapOf<IntWrapper, Int>() })
280+
val expected = mutableListOf(List(20) { mapOf<IntWrapper, Int>() })
281281

282282
repeat(times = 5) {
283283

@@ -297,7 +297,7 @@ class PersistentHashMapBuilderTest : ExecutionTimeMeasuringTest() {
297297
val shouldRemove = Random.nextDouble() < 0.3
298298
val shouldOperateOnExistingKey = map.isNotEmpty() && Random.nextDouble().let { if (shouldRemove) it < 0.8 else it < 0.2 }
299299

300-
val key = if (shouldOperateOnExistingKey) map.keys.first() else ObjectWrapper(Random.nextInt(), hashCodes.random())
300+
val key = if (shouldOperateOnExistingKey) map.keys.first() else IntWrapper(Random.nextInt(), hashCodes.random())
301301

302302
val shouldRemoveByKey = shouldRemove && Random.nextBoolean()
303303
val shouldRemoveByKeyAndValue = shouldRemove && !shouldRemoveByKey
@@ -343,9 +343,9 @@ class PersistentHashMapBuilderTest : ExecutionTimeMeasuringTest() {
343343
}
344344

345345
private fun testAfterOperation(
346-
expected: Map<ObjectWrapper<Int>, Int>,
347-
actual: Map<ObjectWrapper<Int>, Int>,
348-
operationKey: ObjectWrapper<Int>
346+
expected: Map<IntWrapper, Int>,
347+
actual: Map<IntWrapper, Int>,
348+
operationKey: IntWrapper
349349
) {
350350
assertEquals(expected.size, actual.size)
351351
assertEquals(expected[operationKey], actual[operationKey])

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import tests.NForAlgorithmComplexity
2222
import tests.distinctStringValues
2323
import tests.remove
2424
import tests.stress.ExecutionTimeMeasuringTest
25-
import tests.stress.ObjectWrapper
25+
import tests.stress.IntWrapper
2626
import tests.stress.WrapperGenerator
2727
import kotlin.random.Random
2828
import kotlin.test.*
@@ -222,10 +222,10 @@ class PersistentHashMapTest : ExecutionTimeMeasuringTest() {
222222

223223
@Test
224224
fun collisionTests() {
225-
var map = persistentHashMapOf<ObjectWrapper<Int>, Int>()
225+
var map = persistentHashMapOf<IntWrapper, Int>()
226226

227-
val oneWrapper = ObjectWrapper(1, 1)
228-
val twoWrapper = ObjectWrapper(2, 1)
227+
val oneWrapper = IntWrapper(1, 1)
228+
val twoWrapper = IntWrapper(2, 1)
229229
assertEquals(1, map.put(oneWrapper, 1).put(twoWrapper, 2)[oneWrapper])
230230
assertEquals(2, map.put(oneWrapper, 1).put(twoWrapper, 2)[twoWrapper])
231231

@@ -235,7 +235,7 @@ class PersistentHashMapTest : ExecutionTimeMeasuringTest() {
235235

236236
val maxHashCode = elementsToAdd / 5 // should be less than `elementsToAdd`
237237
val keyGen = WrapperGenerator<Int>(maxHashCode)
238-
fun key(key: Int): ObjectWrapper<Int> {
238+
fun key(key: Int): IntWrapper {
239239
return keyGen.wrapper(key)
240240
}
241241

@@ -274,7 +274,7 @@ class PersistentHashMapTest : ExecutionTimeMeasuringTest() {
274274

275275
map.remove(key, key.obj)
276276
} else {
277-
val nonExistingKey = ObjectWrapper(Int.MIN_VALUE, key.hashCode)
277+
val nonExistingKey = IntWrapper(Int.MIN_VALUE, key.hashCode)
278278
val sameMap = map.remove(nonExistingKey)
279279
assertEquals(map.size, sameMap.size)
280280
assertEquals(key.obj, sameMap[key])
@@ -294,8 +294,8 @@ class PersistentHashMapTest : ExecutionTimeMeasuringTest() {
294294
fun randomOperationsTests() {
295295
repeat(times = 1) {
296296

297-
val mutableMaps = List(10) { hashMapOf<ObjectWrapper<Int>?, Int?>() }
298-
val immutableMaps = MutableList(10) { persistentHashMapOf<ObjectWrapper<Int>?, Int?>() }
297+
val mutableMaps = List(10) { hashMapOf<IntWrapper?, Int?>() }
298+
val immutableMaps = MutableList(10) { persistentHashMapOf<IntWrapper?, Int?>() }
299299

300300
val operationCount = NForAlgorithmComplexity.O_NlogN
301301

@@ -313,7 +313,7 @@ class PersistentHashMapTest : ExecutionTimeMeasuringTest() {
313313
val key = when {
314314
shouldOperateOnExistingKey -> mutableMap.keys.first()
315315
Random.nextDouble() < 0.001 -> null
316-
else -> ObjectWrapper(Random.nextInt(), hashCodes.random())
316+
else -> IntWrapper(Random.nextInt(), hashCodes.random())
317317
}
318318

319319
val shouldRemoveByKey = shouldRemove && Random.nextBoolean()
@@ -363,9 +363,9 @@ class PersistentHashMapTest : ExecutionTimeMeasuringTest() {
363363
}
364364

365365
private fun testAfterOperation(
366-
expected: Map<ObjectWrapper<Int>?, Int?>,
367-
actual: Map<ObjectWrapper<Int>?, Int?>,
368-
operationKey: ObjectWrapper<Int>?
366+
expected: Map<IntWrapper?, Int?>,
367+
actual: Map<IntWrapper?, Int?>,
368+
operationKey: IntWrapper?
369369
) {
370370
assertEquals(expected.size, actual.size)
371371
assertEquals(expected[operationKey], actual[operationKey])

core/commonTest/src/stress/set/PersistentHashSetBuilderTest.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import kotlinx.collections.immutable.persistentHashSetOf
2020
import tests.NForAlgorithmComplexity
2121
import tests.distinctStringValues
2222
import tests.stress.ExecutionTimeMeasuringTest
23-
import tests.stress.ObjectWrapper
23+
import tests.stress.IntWrapper
2424
import tests.stress.WrapperGenerator
2525
import kotlin.random.Random
2626
import kotlin.test.Test
@@ -221,13 +221,13 @@ class PersistentHashSetBuilderTest : ExecutionTimeMeasuringTest() {
221221

222222
@Test
223223
fun collisionTests() {
224-
val builder = persistentHashSetOf<ObjectWrapper<Int>>().builder()
224+
val builder = persistentHashSetOf<IntWrapper>().builder()
225225

226226
val elementsToAdd = NForAlgorithmComplexity.O_NlogN
227227

228228
val numberOfDistinctHashCodes = elementsToAdd / 5 // should be less than `elementsToAdd`
229229
val eGen = WrapperGenerator<Int>(numberOfDistinctHashCodes)
230-
fun wrapper(element: Int): ObjectWrapper<Int> {
230+
fun wrapper(element: Int): IntWrapper {
231231
return eGen.wrapper(element)
232232
}
233233

@@ -258,7 +258,7 @@ class PersistentHashSetBuilderTest : ExecutionTimeMeasuringTest() {
258258
for (wrapper in collisions) {
259259
assertTrue(builder.contains(wrapper))
260260

261-
val nonExistingElement = ObjectWrapper(wrapper.obj.inv(), wrapper.hashCode)
261+
val nonExistingElement = IntWrapper(wrapper.obj.inv(), wrapper.hashCode)
262262
assertFalse(builder.remove(nonExistingElement))
263263
assertTrue(builder.contains(wrapper))
264264
assertTrue(builder.remove(wrapper))
@@ -271,8 +271,8 @@ class PersistentHashSetBuilderTest : ExecutionTimeMeasuringTest() {
271271

272272
@Test
273273
fun randomOperationsTests() {
274-
val setGen = mutableListOf(List(20) { persistentHashSetOf<ObjectWrapper<Int>>() })
275-
val expected = mutableListOf(List(20) { setOf<ObjectWrapper<Int>>() })
274+
val setGen = mutableListOf(List(20) { persistentHashSetOf<IntWrapper>() })
275+
val expected = mutableListOf(List(20) { setOf<IntWrapper>() })
276276

277277
repeat(times = 5) {
278278

@@ -292,7 +292,7 @@ class PersistentHashSetBuilderTest : ExecutionTimeMeasuringTest() {
292292
val shouldRemove = Random.nextDouble() < 0.3
293293
val shouldOperateOnExistingElement = set.isNotEmpty() && Random.nextDouble().let { if (shouldRemove) it < 0.8 else it < 0.001 }
294294

295-
val element = if (shouldOperateOnExistingElement) set.first() else ObjectWrapper(Random.nextInt(), hashCodes.random())
295+
val element = if (shouldOperateOnExistingElement) set.first() else IntWrapper(Random.nextInt(), hashCodes.random())
296296

297297
when {
298298
shouldRemove -> {
@@ -328,11 +328,7 @@ class PersistentHashSetBuilderTest : ExecutionTimeMeasuringTest() {
328328
}
329329
}
330330

331-
private fun testAfterOperation(
332-
expected: Set<ObjectWrapper<Int>>,
333-
actual: Set<ObjectWrapper<Int>>,
334-
element: ObjectWrapper<Int>
335-
) {
331+
private fun testAfterOperation(expected: Set<IntWrapper>, actual: Set<IntWrapper>, element: IntWrapper) {
336332
assertEquals(expected.size, actual.size)
337333
assertEquals(expected.contains(element), actual.contains(element))
338334
// assertEquals(expected, actual)

core/commonTest/src/stress/set/PersistentHashSetTest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import kotlinx.collections.immutable.persistentHashSetOf
2020
import tests.NForAlgorithmComplexity
2121
import tests.distinctStringValues
2222
import tests.stress.ExecutionTimeMeasuringTest
23-
import tests.stress.ObjectWrapper
23+
import tests.stress.IntWrapper
2424
import tests.stress.WrapperGenerator
2525
import kotlin.random.Random
2626
import kotlin.test.Test
@@ -186,15 +186,15 @@ class PersistentHashSetTest : ExecutionTimeMeasuringTest() {
186186

187187
@Test
188188
fun collisionTests() {
189-
var set = persistentHashSetOf<ObjectWrapper<Int>>()
189+
var set = persistentHashSetOf<IntWrapper>()
190190

191-
assertTrue(set.add(ObjectWrapper(1, 1)).contains(ObjectWrapper(1, 1)))
191+
assertTrue(set.add(IntWrapper(1, 1)).contains(IntWrapper(1, 1)))
192192

193193
val elementsToAdd = NForAlgorithmComplexity.O_NlogN
194194

195195
val numberOfDistinctHashCodes = elementsToAdd / 5 // should be less than `elementsToAdd`
196196
val eGen = WrapperGenerator<Int>(numberOfDistinctHashCodes)
197-
fun wrapper(element: Int): ObjectWrapper<Int> {
197+
fun wrapper(element: Int): IntWrapper {
198198
return eGen.wrapper(element)
199199
}
200200

@@ -225,7 +225,7 @@ class PersistentHashSetTest : ExecutionTimeMeasuringTest() {
225225
for (wrapper in collisions) {
226226
assertTrue(set.contains(wrapper))
227227

228-
val nonExistingElement = ObjectWrapper(wrapper.obj.inv(), wrapper.hashCode)
228+
val nonExistingElement = IntWrapper(wrapper.obj.inv(), wrapper.hashCode)
229229
val sameSet = set.remove(nonExistingElement)
230230
assertEquals(set.size, sameSet.size)
231231
assertTrue(sameSet.contains(wrapper))
@@ -242,8 +242,8 @@ class PersistentHashSetTest : ExecutionTimeMeasuringTest() {
242242
fun randomOperationsTests() {
243243
repeat(times = 1) {
244244

245-
val mutableSets = List(10) { hashSetOf<ObjectWrapper<Int>?>() }
246-
val immutableSets = MutableList(10) { persistentHashSetOf<ObjectWrapper<Int>?>() }
245+
val mutableSets = List(10) { hashSetOf<IntWrapper?>() }
246+
val immutableSets = MutableList(10) { persistentHashSetOf<IntWrapper?>() }
247247

248248
val operationCount = NForAlgorithmComplexity.O_NlogN
249249

@@ -261,7 +261,7 @@ class PersistentHashSetTest : ExecutionTimeMeasuringTest() {
261261
val element = when {
262262
shouldOperateOnExistingElement -> mutableSet.first()
263263
Random.nextDouble() < 0.001 -> null
264-
else -> ObjectWrapper(Random.nextInt(), hashCodes.random())
264+
else -> IntWrapper(Random.nextInt(), hashCodes.random())
265265
}
266266

267267
val newImmutableSet = when {
@@ -299,7 +299,7 @@ class PersistentHashSetTest : ExecutionTimeMeasuringTest() {
299299
}
300300
}
301301

302-
private fun testAfterOperation(expected: Set<ObjectWrapper<Int>?>, actual: Set<ObjectWrapper<Int>?>, element: ObjectWrapper<Int>?) {
302+
private fun testAfterOperation(expected: Set<IntWrapper?>, actual: Set<IntWrapper?>, element: IntWrapper?) {
303303
assertEquals(expected.size, actual.size)
304304
assertEquals(expected.contains(element), actual.contains(element))
305305
// assertEquals(expected, actual)

0 commit comments

Comments
 (0)