[K/N] Migrate some runtime/basic tests to new testing infra ^KT-61259

This commit is contained in:
Alexander Shabalin
2023-10-25 18:15:18 +02:00
committed by Space Team
parent 4fa121071a
commit fa9c620261
35 changed files with 448 additions and 636 deletions
@@ -1,13 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.empty_substring
import kotlin.test.*
@Test fun runTest() {
val hello = "Hello world"
println(hello.subSequence(1, 1).toString())
}
@@ -1,26 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.hash0
import kotlin.test.*
@Test fun runTest() {
println(239.hashCode())
println((-1L).hashCode())
println('a'.hashCode())
println(1.0f.hashCode())
println(1.0.hashCode())
println(true.hashCode())
println(false.hashCode())
println(Any().hashCode() != Any().hashCode())
val a = CharArray(5)
a[0] = 'H'
a[1] = 'e'
a[2] = 'l'
a[3] = 'l'
a[4] = 'o'
println("Hello".hashCode() == a.concatToString(0, 5).hashCode())
}
@@ -1,9 +0,0 @@
239
0
97
1065353216
1072693248
1231
1237
true
true
@@ -1,28 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlin.test.*
import kotlin.math.*
fun main() {
val hf = hypot(Float.NEGATIVE_INFINITY, Float.NaN)
println("float hypot: $hf ${hf.toRawBits().toString(16)}")
println("Float.NaN: ${Float.NaN.toRawBits().toString(16)}")
println("Float.+Inf: ${Float.POSITIVE_INFINITY} ${Float.POSITIVE_INFINITY.toRawBits().toString(16)}")
println("Float.-Inf: ${Float.NEGATIVE_INFINITY} ${Float.NEGATIVE_INFINITY.toRawBits().toUInt().toString(16)}")
println(Float.POSITIVE_INFINITY == Float.NEGATIVE_INFINITY)
assertEquals(Float.POSITIVE_INFINITY, hypot(Float.NEGATIVE_INFINITY, Float.NaN))
val hd = hypot(Double.NEGATIVE_INFINITY, Double.NaN)
println("Double hypot: $hd ${hd.toRawBits().toString(16)}")
println("Double.NaN: ${Double.NaN.toRawBits().toString(16)}")
println("Double.+Inf: ${Double.POSITIVE_INFINITY} ${Double.POSITIVE_INFINITY.toRawBits().toString(16)}")
println("Double.-Inf: ${Double.NEGATIVE_INFINITY} ${Double.NEGATIVE_INFINITY.toRawBits().toUInt().toString(16)}")
println(Double.POSITIVE_INFINITY == Double.NEGATIVE_INFINITY)
assertEquals(Double.POSITIVE_INFINITY, hypot(Double.NEGATIVE_INFINITY, Double.NaN))
println("hypot NaN, 0: ${hypot(Double.NaN, 0.0).toRawBits().toString(16)}")
assertTrue(hypot(Double.NaN, 0.0).isNaN())
}
@@ -1,33 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.ieee754
import kotlin.test.*
@Test fun runTest() {
val v = Float.POSITIVE_INFINITY
val i = v.toInt()
println("$v $i ${i.toShort()}")
val a = 42
val b = Float.MAX_VALUE
println("${a + b}")
val s = Float.NaN.toInt().toShort()
println("NAN2SHORT:: $s")
val d: Float = Float.MAX_VALUE
val d2i = d.toInt()
println("$d2i ${d2i.toShort()}")
for (f in arrayOf(Float.POSITIVE_INFINITY, Float.MAX_VALUE / 2, Float.MAX_VALUE,
3.14f, Float.NaN, -33333.12312f, Float.MIN_VALUE, Float.NEGATIVE_INFINITY,
-1.2f, -12.6f, 2.3f)) {
println("FLOAT:: $f INT:: ${f.toInt()}")
}
println("OK")
}
@@ -1,16 +0,0 @@
Infinity 2147483647 -1
3.4028235E38
NAN2SHORT:: 0
2147483647 -1
FLOAT:: Infinity INT:: 2147483647
FLOAT:: 1.7014117E38 INT:: 2147483647
FLOAT:: 3.4028235E38 INT:: 2147483647
FLOAT:: 3.14 INT:: 3
FLOAT:: NaN INT:: 0
FLOAT:: -33333.125 INT:: -33333
FLOAT:: 1.4E-45 INT:: 0
FLOAT:: -Infinity INT:: -2147483648
FLOAT:: -1.2 INT:: -1
FLOAT:: -12.6 INT:: -12
FLOAT:: 2.3 INT:: 2
OK
@@ -1,63 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.random
import kotlin.collections.*
import kotlin.random.*
import kotlin.system.*
import kotlin.test.*
/**
* Tests that setting the same seed make random generate the same sequence
*/
private inline fun <reified T> testReproducibility(seed: Long, generator: Random.() -> T) {
// Reset seed. This will make Random to start a new sequence
val r1 = Random(seed)
val first = Array<T>(50, { i -> r1.generator() }).toList()
// Reset seed and try again
val r2 = Random(seed)
val second = Array<T>(50, { i -> r2.generator() }).toList()
assertTrue(first == second, "FAIL: got different sequences of generated values " +
"first: $first, second: $second")
}
/**
* Tests that setting seed makes random generate different sequence.
*/
private inline fun <reified T> testDifference(generator: Random.() -> T) {
val r1 = Random(12345678L)
val first = Array<T>(100, { i -> r1.generator() }).toList()
val r2 = Random(87654321L)
val second = Array<T>(100, { i -> r2.generator() }).toList()
assertTrue(first != second, "FAIL: got the same sequence of generated values " +
"first: $first, second: $second")
}
@Test
fun testInts() {
testReproducibility(getTimeMillis(), { nextInt() })
testReproducibility(Long.MAX_VALUE, { nextInt() })
}
@Test
fun testLong() {
testReproducibility(getTimeMillis(), { nextLong() })
testReproducibility(Long.MAX_VALUE, { nextLong() })
}
@Test
fun testDiffInt() = testDifference { nextInt() }
@Test
fun testDiffLong() = testDifference { nextLong() }
@Test
fun testNextInt() {
testReproducibility(getTimeMillis(), { nextInt(1000) })
testReproducibility(1000L, { nextInt(1024) })
}
@@ -1,107 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
package runtime.basic.simd
import kotlin.test.*
import kotlinx.cinterop.Vector128
import kotlinx.cinterop.vectorOf
@Test fun runTest() {
testBoxingSimple()
testBoxing()
testSetGet()
testString()
testOOB()
testEquals()
testHash()
testDefaultValue()
}
class Box<T>(t: T) {
var value = t
}
class UnalignedC(t: Vector128) {
var smth = 1
var value = t
}
fun testBoxingSimple() {
val v = vectorOf(1f, 3.162f, 10f, 31f)
val box: Box<Vector128> = Box<Vector128>(v)
assertEquals(v, box.value, "testBoxingSimple FAILED")
}
fun testBoxing() {
var u = UnalignedC(vectorOf(0, 1, 2, 3))
assertEquals(3, u.value.getIntAt(3))
u.value = vectorOf(0f, 1f, 2f, 3f)
assertEquals(vectorOf(0f, 1f, 2f, 3f), u.value, "testBoxing FAILED")
}
fun testSetGet() {
var v4any = vectorOf(0, 1, 2, 3)
(0 until 4).forEach { assertEquals(it, v4any.getIntAt(it), "testSetGet FAILED for <4 x i32>") }
// type punning: set the variable to another runtime type
val a = arrayOf(1f, 3.162f, 10f, 31f)
v4any = vectorOf(a[0], a[1], a[2], a[3])
(0 until 4).forEach { assertEquals(a[it], v4any.getFloatAt(it), "testSetGet FAILED for <4 x float>") }
}
fun testString() {
val v4i = vectorOf(100, 1024, Int.MAX_VALUE, Int.MIN_VALUE)
assertEquals("(0x64, 0x400, 0x7fffffff, 0x80000000)", v4i.toString(), "testString FAILED")
}
fun testOOB() {
val f = vectorOf(1f, 3.162f, 10f, 31f)
println("f.getByteAt(15) is OK ${f.getByteAt(15)}")
assertFailsWith<IndexOutOfBoundsException>("f.getByteAt(16) should fail") {
f.getByteAt(16)
}
assertFailsWith<IndexOutOfBoundsException>("f.getIntAt(-1) should fail") {
f.getIntAt(-1)
}
assertFailsWith<IndexOutOfBoundsException>("f.getFloatAt(4) should fail") {
f.getFloatAt(4)
}
}
fun testEquals() {
var v1 = vectorOf(-1f, 0f, 0f, -7f)
var v2 = vectorOf(1f, 4f, 3f, 7f)
assertEquals(false, v1 == v2)
assertEquals(false, v1.equals(v2))
assertEquals(false, v1.equals(Any()))
assertEquals(true, v2 == v2)
v1 = v2
assertEquals(true, v1 == v2)
}
fun testHash() {
val h1 = vectorOf(1f, 4f, 3f, 7f).hashCode()
val h2 = vectorOf(3f, 7f, 1f, 4f).hashCode()
assertEquals(false, h1 == h2)
val i = 654687
assertEquals(true, vectorOf(0, 0, i, 0).hashCode() == i.hashCode()) // exploit little endianness
assertEquals(true, vectorOf(1, 0, 0, 0).hashCode() == vectorOf(0, 0, 31, 0).hashCode())
}
private fun funDefaultValue(v: Vector128 = vectorOf(1.0f, 2.0f, 3.0f, 4.0f)) = v
fun testDefaultValue() {
assertEquals(vectorOf(1.0f, 2.0f, 3.0f, 4.0f), funDefaultValue())
}
@@ -1,32 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.standard
import kotlin.test.*
class Foo(val bar: Int)
fun <T> assertEquals(actual: T, expected: T) {
if (actual != expected) throw AssertionError("Assertion failed. Expected value: $expected, actual value: $actual")
}
@Test fun runTest() {
try {
TODO()
throw AssertionError("TODO() doesn't throw an exception")
} catch(e: NotImplementedError) {}
val foo = Foo(42)
assertEquals(run { 42 }, 42)
assertEquals(foo.run { bar }, 42)
assertEquals(with(foo) { bar }, 42)
assertEquals(foo.apply { bar }, foo)
assertEquals(foo.also { it.bar }, foo)
assertEquals(foo.let { it.bar }, 42)
var i = 0
repeat(10) { i++ }
assertEquals(i, 10)
}
@@ -1,27 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.tostring0
import kotlin.test.*
@Test fun runTest() {
println(127.toByte().toString())
println(255.toByte().toString())
println(239.toShort().toString())
println('A'.toString())
println('Ё'.toString())
println('ト'.toString())
println(1122334455.toString())
println(112233445566778899.toString())
// Here we differ from Java, as have no dtoa() yet.
println(3.14159265358.toString())
// Here we differ from Java, as have no dtoa() yet.
println(1e27.toFloat().toString())
println(1e7.toString())
println(1e-300.toDouble().toString())
println(true.toString())
println(false.toString())
}
@@ -1,14 +0,0 @@
127
-1
239
A
Ё
1122334455
112233445566778899
3.14159265358
1.0E27
1.0E7
1.0E-300
true
false
@@ -1,13 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.tostring1
import kotlin.test.*
@Test fun runTest() {
val hello = "Hello world"
println(hello.subSequence(1, 5).toString())
}
@@ -1 +0,0 @@
ello
@@ -1,19 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.tostring2
import kotlin.test.*
@Test fun runTest() {
val hello = "Hello"
val array = hello.toCharArray()
for (ch in array) {
print(ch)
print(" ")
}
println()
println(array.concatToString(0, array.size))
}
@@ -1,2 +0,0 @@
H e l l o
Hello
@@ -1,78 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.tostring3
import kotlin.test.*
fun testByte() {
val values = ByteArray(2)
values[0] = Byte.MIN_VALUE
values[1] = Byte.MAX_VALUE
for (v in values) {
println(v)
}
}
fun testShort() {
val values = ShortArray(2)
values[0] = Short.MIN_VALUE
values[1] = Short.MAX_VALUE
for (v in values) {
println(v)
}
}
fun testInt() {
val values = IntArray(2)
values[0] = Int.MIN_VALUE
values[1] = Int.MAX_VALUE
for (v in values) {
println(v)
}
}
fun testLong() {
val values = LongArray(2)
values[0] = Long.MIN_VALUE
values[1] = Long.MAX_VALUE
for (v in values) {
println(v)
}
}
fun testFloat() {
val values = FloatArray(5)
values[0] = Float.MIN_VALUE
values[1] = Float.MAX_VALUE
values[2] = Float.NEGATIVE_INFINITY
values[3] = Float.POSITIVE_INFINITY
values[4] = Float.NaN
for (v in values) {
println(v)
}
}
fun testDouble() {
val values = DoubleArray(5)
values[0] = Double.MIN_VALUE
values[1] = Double.MAX_VALUE
values[2] = Double.NEGATIVE_INFINITY
values[3] = Double.POSITIVE_INFINITY
values[4] = Double.NaN
for (v in values) {
println(v)
}
}
@Test fun runTest() {
testByte()
testShort()
testInt()
testLong()
testFloat()
testDouble()
}
@@ -1,18 +0,0 @@
-128
127
-32768
32767
-2147483648
2147483647
-9223372036854775808
9223372036854775807
1.4E-45
3.4028235E38
-Infinity
Infinity
NaN
4.9E-324
1.7976931348623157E308
-Infinity
Infinity
NaN
@@ -1,33 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package runtime.basic.tostring4
import kotlin.test.*
class TopLevel
@Test fun runTest() {
assertEquals("runtime.basic.tostring4.TopLevel", TopLevel().toStringWithoutHashCode())
class Local1
assertEquals("runtime.basic.tostring4.runTest\$Local1", Local1().toStringWithoutHashCode())
assertEquals("runtime.basic.tostring4.runTest\$1", object {}.toStringWithoutHashCode())
fun localFun() {
class Local2
assertEquals("runtime.basic.tostring4.runTest\$localFun\$Local2", Local2().toStringWithoutHashCode())
assertEquals("runtime.basic.tostring4.runTest\$localFun\$1", object {}.toStringWithoutHashCode())
}
localFun()
}
private fun Any.toStringWithoutHashCode(): String {
val string = toString()
assertEquals(1, string.count { it == '@' }, "Invalid toString() value: $string")
return string.substringBefore('@')
}
@@ -1,43 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(ObsoleteWorkersApi::class)
package runtime.basic.worker_random
import kotlin.native.concurrent.*
import kotlin.collections.*
import kotlin.random.*
import kotlin.system.*
import kotlin.test.*
@Test
fun testRandomWorkers() {
val seed = getTimeMillis()
val workers = Array(5, { _ -> Worker.start() })
val attempts = 3
val results = Array(attempts, { ArrayList<Int>() } )
for (attempt in 0 until attempts) {
// Produce a list of random numbers in each worker
val futures = Array(workers.size, { workerIndex ->
workers[workerIndex].execute(TransferMode.SAFE, { workerIndex }) {
input ->
Array(10, { Random.nextInt() }).toList()
}
})
// Now collect all results into current attempt's list
val futureSet = futures.toSet()
var finished = 0
while (finished < futureSet.size) {
val ready = waitForMultipleFutures(futureSet, 10000)
ready.forEach { results[attempt].addAll(it.result) }
finished += ready.size
}
}
workers.forEach {
it.requestTermination().result
}
}