forked from kscience/kmath
Proper test environment
This commit is contained in:
parent
c267372c70
commit
2892ea11e2
@ -33,9 +33,7 @@ val MCScope.random get() = coroutineContext.random!!
|
||||
* Launches a supervised Monte-Carlo scope
|
||||
*/
|
||||
suspend fun <T> mc(generator: RandomGenerator, block: suspend MCScope.() -> T): T =
|
||||
supervisorScope {
|
||||
MCScope.init(generator).block()
|
||||
}
|
||||
|
||||
suspend fun <T> mc(seed: Long = -1, block: suspend MCScope.() -> T): T =
|
||||
mc(SplitRandomWrapper(seed), block)
|
||||
|
@ -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<RandomResult>
|
||||
|
||||
class MCScopeTest {
|
||||
val test: ATest = {
|
||||
mc(1122) {
|
||||
val res = Collections.synchronizedSet(HashSet<RandomResult>())
|
||||
|
||||
val job = launch {
|
||||
repeat(10) {
|
||||
res.add(RandomResult("first", it, random.nextInt()))
|
||||
}
|
||||
launch {
|
||||
"empty fork"
|
||||
}
|
||||
}
|
||||
launch {
|
||||
repeat(10) {
|
||||
res.add(RandomResult("second", it, random.nextInt()))
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun testGenerator() {
|
||||
runBlocking(Dispatchers.Default) {
|
||||
mc(1122){
|
||||
launch {
|
||||
repeat(9){
|
||||
println("first:${random.nextInt()}")
|
||||
}
|
||||
assertEquals(690819834,random.nextInt())
|
||||
}
|
||||
launch {
|
||||
repeat(9){
|
||||
println("second:${random.nextInt()}")
|
||||
}
|
||||
assertEquals(691997530,random.nextInt())
|
||||
}
|
||||
}
|
||||
}
|
||||
val res1 = runBlocking(Dispatchers.Default) { test() }
|
||||
val res2 = runBlocking(newSingleThreadContext("test")) {test()}
|
||||
assertEquals(res1,res2)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user