Skip to content

Commit f92e96d

Browse files
committed
Make tests buildable in Kotlin/JS and /Native
1 parent 8311487 commit f92e96d

16 files changed

+202
-111
lines changed

core/commonTest/src/contract/ComparisonDSL.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package kotlinx.collections.immutable.contractTests
1818

19+
import kotlinx.collections.immutable.tests.assertTypeEquals
1920
import kotlin.test.*
2021

2122
public fun <T> compare(expected: T, actual: T, block: CompareContext<T>.() -> Unit) {
@@ -49,8 +50,4 @@ public class CompareContext<out T>(public val expected: T, public val actual: T)
4950
assertTypeEquals(expectedFail, actualFail)
5051
}
5152
}
52-
53-
public fun assertTypeEquals(expected: Any?, actual: Any?) {
54-
assertEquals(expected?.javaClass, actual?.javaClass)
55-
}
5653
}

core/commonTest/src/contract/list/ImmutableListTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package kotlinx.collections.immutable.contractTests.immutableList
1919
import kotlinx.collections.immutable.*
2020
import kotlinx.collections.immutable.contractTests.compare
2121
import kotlinx.collections.immutable.contractTests.listBehavior
22+
import kotlinx.collections.immutable.tests.isDigit
23+
import kotlinx.collections.immutable.tests.isUpperCase
2224
import kotlin.test.*
2325

2426
class ImmutableListTest {
@@ -200,6 +202,6 @@ class ImmutableListTest {
200202
val listSN: PersistentList<String?> = listS + (null as String?)
201203
val listAny: PersistentList<Any?> = listSN + 1
202204

203-
assertEquals(listOf("x", null, 1), listAny)
205+
assertEquals<List<*>>(listOf("x", null, 1), listAny)
204206
}
205207
}

core/commonTest/src/contract/map/ImmutableMapTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,6 @@ abstract class ImmutableMapTest {
214214
val mapSNI: PersistentMap<String, Int?> = mapSI + mapOf("y" to null)
215215
val mapANA: PersistentMap<Any, Any?> = mapSNI + listOf(1 to "x")
216216

217-
assertEquals(mapOf(1 to "x", "x" to 1, "y" to null), mapANA)
217+
assertEquals<Map<*, *>>(mapOf(1 to "x", "x" to 1, "y" to null), mapANA)
218218
}
219219
}

core/commonTest/src/contract/set/ImmutableSetTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package kotlinx.collections.immutable.contractTests.immutableSet
1919
import kotlinx.collections.immutable.*
2020
import kotlinx.collections.immutable.contractTests.compare
2121
import kotlinx.collections.immutable.contractTests.setBehavior
22+
import kotlinx.collections.immutable.tests.isDigit
23+
import kotlinx.collections.immutable.tests.isUpperCase
2224
import kotlin.test.*
2325

2426
class ImmutableHashSetTest : ImmutableSetTestBase() {

core/commonTest/src/stress/ObjectWrapper.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616

1717
package kotlinx.collections.immutable.stressTests
1818

19-
class ObjectWrapper<K: Comparable<K>>(val obj: K, val hashCode: Int) : Comparable<ObjectWrapper<K>> {
19+
import kotlinx.collections.immutable.internal.assert
20+
import kotlin.js.JsName
21+
22+
class ObjectWrapper<K: Comparable<K>>(
23+
val obj: K,
24+
@JsName("_hashCode") val hashCode: Int
25+
) : Comparable<ObjectWrapper<K>> {
2026
override fun hashCode(): Int {
2127
return hashCode
2228
}

core/commonTest/src/stress/WrapperGenerator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616

1717
package kotlinx.collections.immutable.stressTests
1818

19-
import java.util.*
19+
import kotlin.random.Random
20+
2021

2122
class WrapperGenerator<K: Comparable<K>>(private val hashCodeUpperBound: Int) {
2223
private val elementMap = hashMapOf<K, ObjectWrapper<K>>()
2324
private val hashCodeMap = hashMapOf<Int, MutableList<ObjectWrapper<K>>>()
24-
private val random = Random()
2525

2626
fun wrapper(element: K): ObjectWrapper<K> {
2727
val existing = elementMap[element]
2828
if (existing != null) {
2929
return existing
3030
}
31-
val hashCode = random.nextInt(hashCodeUpperBound)
31+
val hashCode = Random.nextInt(hashCodeUpperBound)
3232
val wrapper = ObjectWrapper(element, hashCode)
3333
elementMap[element] = wrapper
3434

core/commonTest/src/stress/list/PersistentListBuilderTest.kt

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import kotlinx.collections.immutable.PersistentList
2020
import kotlinx.collections.immutable.contractTests.compare
2121
import kotlinx.collections.immutable.contractTests.listIteratorProperties
2222
import kotlinx.collections.immutable.persistentListOf
23-
import org.junit.Test
24-
import org.junit.Assert.*
25-
import java.util.*
26-
import kotlin.test.assertFailsWith
23+
import kotlin.random.Random
24+
import kotlin.test.*
2725

2826
class PersistentListBuilderTest {
2927

@@ -107,11 +105,11 @@ class PersistentListBuilderTest {
107105
assertEquals(emptyList<Int>(), builder)
108106

109107
val elementsToAdd = 1000
110-
val list = LinkedList<Int>()
108+
val list = mutableListOf<Int>()
111109
repeat(times = elementsToAdd) { index ->
112110
list.add(index)
113111
builder.add(index)
114-
assertEquals(list, builder)
112+
assertEquals<List<*>>(list, builder)
115113
}
116114
}
117115

@@ -301,9 +299,8 @@ class PersistentListBuilderTest {
301299
actualIterator: MutableListIterator<E>,
302300
maxIterationCount: Int,
303301
afterIteration: () -> Unit) {
304-
val random = Random()
305-
val towardStart = random.nextBoolean()
306-
val iterationCount = random.nextInt(maxIterationCount)
302+
val towardStart = Random.nextBoolean()
303+
val iterationCount = Random.nextInt(maxIterationCount)
307304

308305
if (towardStart) {
309306
repeat(iterationCount) {
@@ -332,11 +329,10 @@ class PersistentListBuilderTest {
332329
var expectedIterator = expected.listIterator()
333330
compare(expectedIterator, builderIterator) { listIteratorProperties() }
334331

335-
val random = Random()
336332
repeat(times = 100) {
337-
val createNew = random.nextDouble() < 0.2
333+
val createNew = Random.nextDouble() < 0.2
338334
if (createNew) {
339-
val index = random.nextInt(expected.size)
335+
val index = Random.nextInt(expected.size)
340336
builderIterator = builder.listIterator(index)
341337
expectedIterator = expected.listIterator(index)
342338
compare(expectedIterator, builderIterator) { listIteratorProperties() }
@@ -356,20 +352,19 @@ class PersistentListBuilderTest {
356352
var expectedIterator = expected.listIterator()
357353
compare(expectedIterator, builderIterator) { listIteratorProperties() }
358354

359-
val random = Random()
360355
repeat(times = 100) {
361-
val createNew = random.nextDouble() < 0.1
356+
val createNew = Random.nextDouble() < 0.1
362357
if (createNew) {
363-
val index = random.nextInt(expected.size)
358+
val index = Random.nextInt(expected.size)
364359
builderIterator = builder.listIterator(index)
365360
expectedIterator = expected.listIterator(index)
366361
compare(expectedIterator, builderIterator) { listIteratorProperties() }
367362
}
368363

369-
val shouldSet = random.nextBoolean()
364+
val shouldSet = Random.nextBoolean()
370365
iterateWith(expectedIterator, builderIterator, expected.size) {
371366
if (shouldSet) {
372-
val elementToSet = random.nextInt()
367+
val elementToSet = Random.nextInt()
373368
expectedIterator.set(elementToSet)
374369
builderIterator.set(elementToSet)
375370
}
@@ -387,21 +382,20 @@ class PersistentListBuilderTest {
387382
var expectedIterator = expected.listIterator(builder.size)
388383
compare(expectedIterator, builderIterator) { listIteratorProperties() }
389384

390-
val random = Random()
391385
repeat(times = 100) {
392-
val createNew = random.nextDouble() < 0.1
386+
val createNew = Random.nextDouble() < 0.1
393387
if (createNew) {
394-
val index = random.nextInt(expected.size)
388+
val index = Random.nextInt(expected.size)
395389
builderIterator = builder.listIterator(index)
396390
expectedIterator = expected.listIterator(index)
397391
compare(expectedIterator, builderIterator) { listIteratorProperties() }
398392
}
399393

400-
val shouldAdd = random.nextBoolean()
394+
val shouldAdd = Random.nextBoolean()
401395
if (shouldAdd) {
402-
val addCount = random.nextInt(2000)
396+
val addCount = Random.nextInt(2000)
403397
repeat(addCount) {
404-
val elementToAdd = random.nextInt()
398+
val elementToAdd = Random.nextInt()
405399
expectedIterator.add(elementToAdd)
406400
builderIterator.add(elementToAdd)
407401
compare(expectedIterator, builderIterator) { listIteratorProperties() }
@@ -422,24 +416,23 @@ class PersistentListBuilderTest {
422416
var expectedIterator = expected.listIterator()
423417
compare(expectedIterator, builderIterator) { listIteratorProperties() }
424418

425-
val random = Random()
426419
repeat(times = 100) {
427-
val createNew = random.nextDouble() < 0.1
420+
val createNew = Random.nextDouble() < 0.1
428421
if (createNew) {
429-
val index = random.nextInt(expected.size)
422+
val index = Random.nextInt(expected.size)
430423
builderIterator = builder.listIterator(index)
431424
expectedIterator = expected.listIterator(index)
432425
compare(expectedIterator, builderIterator) { listIteratorProperties() }
433426
}
434427

435-
val shouldAddOrRemove = random.nextBoolean()
428+
val shouldAddOrRemove = Random.nextBoolean()
436429
if (shouldAddOrRemove) {
437-
val actionCount = random.nextInt(2000)
438-
val shouldAdd = random.nextBoolean()
430+
val actionCount = Random.nextInt(2000)
431+
val shouldAdd = Random.nextBoolean()
439432

440433
if (shouldAdd) {
441434
repeat(actionCount) {
442-
val elementToAdd = random.nextInt()
435+
val elementToAdd = Random.nextInt()
443436
expectedIterator.add(elementToAdd)
444437
builderIterator.add(elementToAdd)
445438
compare(expectedIterator, builderIterator) { listIteratorProperties() }
@@ -463,17 +456,16 @@ class PersistentListBuilderTest {
463456

464457
repeat(times = 10) {
465458

466-
val random = Random()
467459
val builders = vectorGen.last().map { it.builder() }
468460
val lists = builders.map { it.toMutableList() }
469461

470462
repeat(times = 100000) {
471-
val index = random.nextInt(lists.size)
463+
val index = Random.nextInt(lists.size)
472464
val list = lists[index]
473465
val builder = builders[index]
474466

475-
val operationType = random.nextDouble()
476-
val operationIndex = if (list.size > 1) random.nextInt(list.size) else 0
467+
val operationType = Random.nextDouble()
468+
val operationIndex = if (list.size > 1) Random.nextInt(list.size) else 0
477469

478470
val shouldRemove = operationType < 0.15
479471
val shouldSet = operationType > 0.15 && operationType < 0.3
@@ -482,12 +474,12 @@ class PersistentListBuilderTest {
482474
assertEquals(list.removeAt(operationIndex),
483475
builder.removeAt(operationIndex))
484476
} else if (!list.isEmpty() && shouldSet) {
485-
val value = random.nextInt()
477+
val value = Random.nextInt()
486478
assertEquals(list.set(operationIndex, value),
487479
builder.set(operationIndex, value))
488480

489481
} else {
490-
val value = random.nextInt()
482+
val value = Random.nextInt()
491483
list.add(operationIndex, value)
492484
builder.add(operationIndex, value)
493485
}

core/commonTest/src/stress/list/PersistentListTest.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ package kotlinx.collections.immutable.stressTests.immutableList
1818

1919
import kotlinx.collections.immutable.PersistentList
2020
import kotlinx.collections.immutable.persistentListOf
21-
import org.junit.Test
22-
import org.junit.Assert.*
23-
import java.util.*
24-
import kotlin.test.assertFailsWith
21+
import kotlin.random.Random
22+
import kotlin.test.*
2523

2624

2725
class PersistentListTest {
@@ -130,7 +128,7 @@ class PersistentListTest {
130128
)
131129

132130
val elementsToAdd = 1000
133-
val list = LinkedList<Int>()
131+
val list = mutableListOf<Int>()
134132
repeat(times = elementsToAdd) { index ->
135133
list.add(index)
136134
vector = vector.add(index)
@@ -297,17 +295,16 @@ class PersistentListTest {
297295
fun randomOperationsTests() {
298296
repeat(times = 1) {
299297

300-
val random = Random()
301298
val lists = List(20) { mutableListOf<Int>() }
302299
val vectors = MutableList(20) { persistentListOf<Int>() }
303300

304301
repeat(times = 1000000) {
305-
val index = random.nextInt(lists.size)
302+
val index = Random.nextInt(lists.size)
306303
val list = lists[index]
307304
val vector = vectors[index]
308305

309-
val operationType = random.nextDouble()
310-
val operationIndex = if (list.size > 1) random.nextInt(list.size) else 0
306+
val operationType = Random.nextDouble()
307+
val operationIndex = if (list.size > 1) Random.nextInt(list.size) else 0
311308

312309
val shouldRemove = operationType < 0.15
313310
val shouldSet = operationType > 0.15 && operationType < 0.3
@@ -316,11 +313,11 @@ class PersistentListTest {
316313
list.removeAt(operationIndex)
317314
vector.removeAt(operationIndex)
318315
} else if (!list.isEmpty() && shouldSet) {
319-
val value = random.nextInt()
316+
val value = Random.nextInt()
320317
list[operationIndex] = value
321318
vector.set(operationIndex, value)
322319
} else {
323-
val value = random.nextInt()
320+
val value = Random.nextInt()
324321
list.add(operationIndex, value)
325322
vector.add(operationIndex, value)
326323
}
@@ -337,7 +334,7 @@ class PersistentListTest {
337334
var vector = vectors[index]
338335

339336
while (!list.isEmpty()) {
340-
val removeIndex = random.nextInt(list.size)
337+
val removeIndex = Random.nextInt(list.size)
341338
list.removeAt(removeIndex)
342339
vector = vector.removeAt(removeIndex)
343340

0 commit comments

Comments
 (0)