Files
kotlin-fork/kotlin-native/backend.native/tests/runtime/basic/random.kt
T
Stanislav Erokhin f624800b84 Move everything under kotlin-native folder
I was forced to manually do update the following files, because otherwise
they would be ignored according .gitignore settings. Probably they
should be deleted from repo.

Interop/.idea/compiler.xml
Interop/.idea/gradle.xml
Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_3.xml
Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_3.xml
Interop/.idea/modules.xml
Interop/.idea/modules/Indexer/Indexer.iml
Interop/.idea/modules/Runtime/Runtime.iml
Interop/.idea/modules/StubGenerator/StubGenerator.iml
backend.native/backend.native.iml
backend.native/bc.frontend/bc.frontend.iml
backend.native/cli.bc/cli.bc.iml
backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt
backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt
backend.native/tests/link/lib/foo.kt
backend.native/tests/link/lib/foo2.kt
backend.native/tests/teamcity-test.property
2020-10-27 21:00:28 +03:00

64 lines
1.8 KiB
Kotlin

/*
* 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) })
}