From 2892ea11e2aa52f30a451753b4a061cb6e3bd57f Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 15 Jun 2019 16:31:00 +0300 Subject: [PATCH] Proper test environment --- .../kotlin/scientifik/kmath/prob/MCScope.kt | 4 +- .../scientifik/kmath/prob/MCScopeTest.kt | 43 +++++++++++++------ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/MCScope.kt b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/MCScope.kt index 1cbecb206..e65a589f3 100644 --- a/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/MCScope.kt +++ b/kmath-prob/src/jvmMain/kotlin/scientifik/kmath/prob/MCScope.kt @@ -33,9 +33,7 @@ val MCScope.random get() = coroutineContext.random!! * Launches a supervised Monte-Carlo scope */ suspend fun mc(generator: RandomGenerator, block: suspend MCScope.() -> T): T = - supervisorScope { - MCScope.init(generator).block() - } + MCScope.init(generator).block() suspend fun mc(seed: Long = -1, block: suspend MCScope.() -> T): T = mc(SplitRandomWrapper(seed), block) diff --git a/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/MCScopeTest.kt b/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/MCScopeTest.kt index a740661b3..69480ec22 100644 --- a/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/MCScopeTest.kt +++ b/kmath-prob/src/jvmTest/kotlin/scientifik/kmath/prob/MCScopeTest.kt @@ -1,28 +1,45 @@ package scientifik.kmath.prob +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.newSingleThreadContext import kotlinx.coroutines.runBlocking +import java.util.* +import kotlin.collections.HashSet import kotlin.test.Test import kotlin.test.assertEquals +data class RandomResult(val branch: String, val order: Int, val value: Int) + +typealias ATest = suspend CoroutineScope.() -> Set + class MCScopeTest { - @Test - fun testGenerator(){ - runBlocking(Dispatchers.Default) { - mc(1122){ - launch { - repeat(9){ - println("first:${random.nextInt()}") - } - assertEquals(690819834,random.nextInt()) + val test: ATest = { + mc(1122) { + val res = Collections.synchronizedSet(HashSet()) + + val job = launch { + repeat(10) { + res.add(RandomResult("first", it, random.nextInt())) } launch { - repeat(9){ - println("second:${random.nextInt()}") - } - assertEquals(691997530,random.nextInt()) + "empty fork" } } + 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) + } } \ No newline at end of file