[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
@@ -0,0 +1,94 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.kotlinx.cinterop
import kotlin.test.*
import kotlinx.cinterop.Vector128
import kotlinx.cinterop.vectorOf
class VectorTest {
@Test
fun box() {
class Box(val value: Vector128)
val v = vectorOf(1f, 3.162f, 10f, 31f)
val box = Box(v)
assertEquals(v, box.value)
}
@Test
fun boxWithExtraFields() {
class Box(v: Vector128) {
val extraField: Int = 1
var value: Vector128 = v
}
val box = Box(vectorOf(0, 1, 2, 3))
assertEquals(vectorOf(0, 1, 2, 3), box.value)
box.value = vectorOf(0.1f, 1.1f, 2.1f, 3.1f)
assertEquals(vectorOf(0.1f, 1.1f, 2.1f, 3.1f), box.value)
}
@Test
fun getIntAt() {
val a = arrayOf(0, 1, 2, 3)
val v = vectorOf(a[0], a[1], a[2], a[3])
(0 until 4).forEach { assertEquals(a[it], v.getIntAt(it)) }
assertFailsWith<IndexOutOfBoundsException> { v.getIntAt(-1) }
assertFailsWith<IndexOutOfBoundsException> { v.getIntAt(4) }
}
@Test
fun getFloatAt() {
val a = arrayOf(1f, 3.162f, 10f, 31f)
val v = vectorOf(a[0], a[1], a[2], a[3])
(0 until 4).forEach { assertEquals(a[it], v.getFloatAt(it)) }
assertFailsWith<IndexOutOfBoundsException> { v.getFloatAt(-1) }
assertFailsWith<IndexOutOfBoundsException> { v.getFloatAt(4) }
}
@Test
fun getByteAt() {
val a = arrayOf(0, 1, 2, 3)
val v = vectorOf(a[0], a[1], a[2], a[3])
(0 until 4).forEach {
assertEquals(if (Platform.isLittleEndian) a[it].toByte() else 0, v.getByteAt(it * 4))
assertEquals(0, v.getByteAt(it * 4 + 1))
assertEquals(0, v.getByteAt(it * 4 + 2))
assertEquals(if (!Platform.isLittleEndian) a[it].toByte() else 0, v.getByteAt(it * 4 + 3))
}
assertFailsWith<IndexOutOfBoundsException> { v.getByteAt(-1) }
assertFailsWith<IndexOutOfBoundsException> { v.getByteAt(16) }
}
@Test
fun updateVector() {
var v = vectorOf(0, 1, 2, 3)
val a = arrayOf(1f, 3.162f, 10f, 31f)
// Used to be vector of ints, now a vector of floats.
v = vectorOf(a[0], a[1], a[2], a[3])
(0 until 4).forEach { assertEquals(a[it], v.getFloatAt(it)) }
}
@Test
fun testToString() {
val v = vectorOf(100, 1024, Int.MAX_VALUE, Int.MIN_VALUE)
assertEquals("(0x64, 0x400, 0x7fffffff, 0x80000000)", v.toString())
}
@Test
fun testEquals() {
assertNotEquals(vectorOf(-1f, 0f, 0f, -7f), vectorOf(1f, 4f, 3f, 7f))
assertNotEquals(vectorOf(-1f, 0f, 0f, -7f), Any())
assertEquals(vectorOf(-1f, 0f, 0f, -7f), vectorOf(-1f, 0f, 0f, -7f))
}
@Test
fun testHashCode() {
assertNotEquals(vectorOf(1f, 4f, 3f, 7f).hashCode(), vectorOf(3f, 7f, 1f, 4f).hashCode())
assertEquals(vectorOf(1f, 4f, 3f, 7f).hashCode(), vectorOf(1f, 4f, 3f, 7f).hashCode())
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.numbers
import kotlin.test.*
// Native-specific part of stdlib/test/numbers/ConversionTest.kt
class ConversionNativeTest {
@Test
fun floatToInt() {
fun testEquals(expected: Int, v: Float) = assertEquals(expected, v.toInt())
testEquals(3, 3.14f)
testEquals(-33333, -33333.12312f)
testEquals(-1, -1.2f)
testEquals(-12, -12.6f)
testEquals(2, 2.3f)
}
@Test
fun intToShort() {
fun testEquals(expected: Short, v: Int) = assertEquals(expected, v.toShort())
testEquals(-1, Int.MAX_VALUE)
}
}
@@ -138,4 +138,8 @@ class FloatMathNativeTest {
assertTrue(2.0f.IEEErem(0.0f).isNaN())
assertEquals(PI.toFloat(), PI.toFloat().IEEErem(Float.NEGATIVE_INFINITY))
}
@Test fun maxValue() {
assertEquals(Float.MAX_VALUE, Float.MAX_VALUE + 42)
}
}
@@ -0,0 +1,75 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.random
import kotlin.concurrent.AtomicInt
import kotlin.native.concurrent.*
import kotlin.random.*
import kotlin.test.*
// Native-specific part of stdlib/test/random/RandomTest.kt
class SeededRandomSmokeNativeTest {
val subject: Random get() = seededRandomSmokeTestSubject
@Test
fun sameIntSeedNextLong() {
val v = subject.nextInt(1..Int.MAX_VALUE)
for (seed in listOf(v, -v)) {
testSameSeededRandoms(Random(seed), Random(seed), seed) { nextLong() }
}
}
@Test
fun sameIntSeedNextIntWithLimit() {
val v = subject.nextInt(1..Int.MAX_VALUE)
for (seed in listOf(v, -v)) {
testSameSeededRandoms(Random(seed), Random(seed), seed) { nextInt(1000) }
}
}
private inline fun <reified T> testSameSeededRandoms(r1: Random, r2: Random, seed: Any, generator: Random.() -> T) {
val seq1 = List(10) { r1.generator() }
val seq2 = List(10) { r2.generator() }
assertEquals(seq1, seq2, "Generators seeded with $seed should produce the same output")
}
}
class MultiThreadedRandomSmokeTest {
val subject: Random get() = Random
@Test
fun nextInt() {
val workers = Array(10) { Worker.start() }
val canStart = AtomicInt(0)
val futures = workers.map {
it.execute(TransferMode.SAFE, { subject to canStart }) { (subject, canStart) ->
var result1 = 0
var result2 = -1
while (canStart.value == 0) {}
repeat(100) {
val r = subject.nextInt()
result1 = result1 or r
result2 = result2 and r
}
result1 to result2
}
}
canStart.value = 1
var result1 = 0
var result2 = -1
futures.forEach {
val (r1, r2) = it.result
result1 = result1 or r1
result2 = result2 and r2
}
assertEquals(-1, result1, "All one bits should present")
assertEquals(0, result2, "All zero bits should present")
workers.forEach {
it.requestTermination().result
}
}
}
@@ -118,4 +118,11 @@ class CharNativeTest {
assertTrue('_' in CharCategory.CONNECTOR_PUNCTUATION)
assertTrue('$' in CharCategory.CURRENCY_SYMBOL)
}
@Test
fun testToString() {
assertEquals("A", 'A'.toString())
assertEquals("Ё", 'Ё'.toString())
assertEquals("", 'ト'.toString())
}
}
@@ -151,4 +151,10 @@ class StringNativeTest {
assertEquals(expected = "\u1FFFString", actual = "\u00A0 \u1FFFString".trim(),
message = "Trim special whitespace but should left a unicode symbol")
}
@Test
fun subSequence() {
assertEquals("ello", "Hello world".subSequence(1, 5).toString())
assertEquals("", "Hello world".subSequence(1, 1).toString())
}
}
@@ -127,4 +127,61 @@ class StringNumberConversionNativeTest {
assertFailsWith<NumberFormatException> { "\uDC0012".toFloat() }
assertFailsWith<NumberFormatException> { "12\uD800".toFloat() }
}
@Test
fun byteToString() {
assertEquals("13", 13.toByte().toString())
assertEquals("-1", (-1).toByte().toString())
assertEquals("-128", Byte.MIN_VALUE.toString())
assertEquals("127", Byte.MAX_VALUE.toString())
}
@Test
fun shortToString() {
assertEquals("239", 239.toShort().toString())
assertEquals("-32768", Short.MIN_VALUE.toString())
assertEquals("32767", Short.MAX_VALUE.toString())
}
@Test
fun intToString() {
assertEquals("1122334455", 1122334455.toString())
assertEquals("-2147483648", Int.MIN_VALUE.toString())
assertEquals("2147483647", Int.MAX_VALUE.toString())
}
@Test
fun longToString() {
assertEquals("112233445566778899", 112233445566778899L.toString())
assertEquals("-9223372036854775808", Long.MIN_VALUE.toString())
assertEquals("9223372036854775807", Long.MAX_VALUE.toString())
}
@Test
fun floatToString() {
assertEquals("1.0E27", 1e27.toFloat().toString())
assertEquals("1.4E-45", Float.MIN_VALUE.toString())
assertEquals("3.4028235E38", Float.MAX_VALUE.toString())
assertEquals("-Infinity", Float.NEGATIVE_INFINITY.toString())
assertEquals("Infinity", Float.POSITIVE_INFINITY.toString())
assertEquals("NaN", Float.NaN.toString())
}
@Test
fun doubleToString() {
assertEquals("3.14159265358", 3.14159265358.toString())
assertEquals("1.0E7", 1e7.toString())
assertEquals("1.0E-300", 1e-300.toDouble().toString())
assertEquals("4.9E-324", Double.MIN_VALUE.toString())
assertEquals("1.7976931348623157E308", Double.MAX_VALUE.toString())
assertEquals("-Infinity", Double.NEGATIVE_INFINITY.toString())
assertEquals("Infinity", Double.POSITIVE_INFINITY.toString())
assertEquals("NaN", Double.NaN.toString())
}
@Test
fun booleanToString() {
assertEquals("true", true.toString())
assertEquals("false", false.toString())
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.utils
import kotlin.test.*
private class TopLevel
private fun Any.toStringWithoutHashCode(): String {
val string = toString()
assertEquals(1, string.count { it == '@' }, "Invalid toString() value: $string")
return string.substringBefore('@')
}
class AnyTest {
@Test
fun testToString() {
assertEquals("test.utils.TopLevel", TopLevel().toStringWithoutHashCode())
class Local1
assertEquals("test.utils.AnyTest\$testToString\$Local1", Local1().toStringWithoutHashCode())
assertEquals("test.utils.AnyTest\$testToString\$1", object {}.toStringWithoutHashCode())
fun localFun() {
class Local2
assertEquals("test.utils.AnyTest\$testToString\$localFun\$Local2", Local2().toStringWithoutHashCode())
assertEquals("test.utils.AnyTest\$testToString\$localFun\$1", object {}.toStringWithoutHashCode())
}
localFun()
}
}
@@ -0,0 +1,54 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.utils
import kotlin.test.*
// Native-specific part of stdlib/test/utils/HashCodeTest.kt
class HashCodeNativeTest {
@Test
fun hashCodeOfInt() {
assertEquals(239, 239.hashCode())
}
@Test
fun hashCodeOfLong() {
assertEquals(0, (-1L).hashCode())
}
@Test
fun hashCodeOfChar() {
assertEquals(97, 'a'.hashCode())
}
@Test
fun hashCodeOfFloat() {
assertEquals(1065353216, 1.0f.hashCode())
}
@Test
fun hashCodeOfDouble() {
assertEquals(1072693248, 1.0.hashCode())
}
@Test
fun hashCodeOfBool() {
assertEquals(1231, true.hashCode())
assertEquals(1237, false.hashCode())
}
@Test
fun hashCodeOfAny() {
assertNotEquals(Any().hashCode(), Any().hashCode())
}
@Test
fun hashCodeOfString() {
val str = "Hello"
val charArray = charArrayOf('H', 'e', 'l', 'l', 'o')
assertEquals(str.hashCode(), charArray.concatToString(0, 5).hashCode())
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.utils
import kotlin.test.*
class RepeatTest {
@Test
fun testRepeat() {
var i = 0
repeat(10) { i++ }
assertEquals(10, i)
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package test.utils
import kotlin.test.*
private class Box(val value: Int)
class ScopeFunctionsTest {
@Test
fun testLet() {
val box = Box(42)
assertEquals(42, box.let { it.value })
}
@Test
fun testWith() {
val box = Box(42)
assertEquals(42, with(box) { value })
}
@Test
fun testRun() {
assertEquals(42, run { 42 })
val box = Box(42)
assertEquals(42, box.run { value })
}
@Test
fun testApply() {
val box = Box(42)
assertEquals(box, box.apply { value })
}
@Test
fun testAlso() {
val box = Box(42)
assertEquals(box, box.also { it.value })
}
}