Proper test environment

This commit is contained in:
Alexander Nozik 2019-06-15 16:31:00 +03:00
parent c267372c70
commit 2892ea11e2
2 changed files with 31 additions and 16 deletions

View File

@ -33,9 +33,7 @@ val MCScope.random get() = coroutineContext.random!!
* Launches a supervised Monte-Carlo scope * Launches a supervised Monte-Carlo scope
*/ */
suspend fun <T> mc(generator: RandomGenerator, block: suspend MCScope.() -> T): T = suspend fun <T> mc(generator: RandomGenerator, block: suspend MCScope.() -> T): T =
supervisorScope { MCScope.init(generator).block()
MCScope.init(generator).block()
}
suspend fun <T> mc(seed: Long = -1, block: suspend MCScope.() -> T): T = suspend fun <T> mc(seed: Long = -1, block: suspend MCScope.() -> T): T =
mc(SplitRandomWrapper(seed), block) mc(SplitRandomWrapper(seed), block)

View File

@ -1,28 +1,45 @@
package scientifik.kmath.prob package scientifik.kmath.prob
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.util.*
import kotlin.collections.HashSet
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
data class RandomResult(val branch: String, val order: Int, val value: Int)
typealias ATest = suspend CoroutineScope.() -> Set<RandomResult>
class MCScopeTest { class MCScopeTest {
@Test val test: ATest = {
fun testGenerator(){ mc(1122) {
runBlocking(Dispatchers.Default) { val res = Collections.synchronizedSet(HashSet<RandomResult>())
mc(1122){
launch { val job = launch {
repeat(9){ repeat(10) {
println("first:${random.nextInt()}") res.add(RandomResult("first", it, random.nextInt()))
}
assertEquals(690819834,random.nextInt())
} }
launch { launch {
repeat(9){ "empty fork"
println("second:${random.nextInt()}")
}
assertEquals(691997530,random.nextInt())
} }
} }
launch {
repeat(10) {
res.add(RandomResult("second", it, random.nextInt()))
}
}
res
} }
} }
@Test
fun testGenerator() {
val res1 = runBlocking(Dispatchers.Default) { test() }
val res2 = runBlocking(newSingleThreadContext("test")) {test()}
assertEquals(res1,res2)
}
} }