-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from JetBrains-Research/dev-testing
Add a few unit tests
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
core/src/linuxTest/kotlin/org/jetbrains/litmuskt/AffinityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.jetbrains.litmuskt | ||
|
||
import platform.posix.sleep | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class AffinityTest { | ||
|
||
@Test | ||
fun testBasic() { | ||
val manager = affinityManager ?: return | ||
|
||
val t = PthreadThreadlike() | ||
val future = t.start(Unit) { | ||
sleep(2u) | ||
} | ||
val cpus = setOf(0) | ||
manager.setAffinity(t, cpus) | ||
assertEquals(cpus, manager.getAffinity(t)) | ||
|
||
val moreCpus = setOf(1, 3, 5, 7, 9) | ||
assertEquals(true, manager.setAffinityAndCheck(t, moreCpus)) | ||
|
||
future.await() | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
core/src/nativeTest/kotlin/org/jetbrains/litmuskt/BarrierTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.jetbrains.litmuskt | ||
|
||
import org.jetbrains.litmuskt.barriers.CinteropSpinBarrier | ||
import org.jetbrains.litmuskt.barriers.KNativeSpinBarrier | ||
import platform.posix.sleep | ||
import kotlin.concurrent.Volatile | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class BarrierTest { | ||
|
||
fun testBarrier(barrierProducer: (Int) -> Barrier) { | ||
val t1 = PthreadThreadlike() | ||
val t2 = PthreadThreadlike() | ||
|
||
class Context( | ||
val barrier: Barrier, | ||
var x: Int = 0, | ||
var y: Int = 0, | ||
@Volatile | ||
var flush: Int = 0 // ensure that changes are visible | ||
) | ||
|
||
val ctx = Context(barrierProducer(2)) | ||
|
||
val f1 = t1.start(ctx) { | ||
it.barrier.await() // sync #1 | ||
it.x = 1 | ||
it.flush++ | ||
|
||
sleep(2u) | ||
assertEquals(0, it.y) // thread 2 is waiting | ||
it.barrier.await() // sync #2 | ||
sleep(1u) | ||
assertEquals(1, it.y) // thread 2 has continued | ||
} | ||
val f2 = t2.start(ctx) { | ||
sleep(1u) | ||
assertEquals(0, it.x) // thread 1 is waiting | ||
it.barrier.await() // sync #1 | ||
sleep(1u) | ||
assertEquals(1, it.x) // thread 1 has continued | ||
|
||
it.barrier.await() // sync #2 | ||
it.y = 1 | ||
it.flush++ | ||
} | ||
|
||
f1.await() | ||
f2.await() | ||
} | ||
|
||
@Test | ||
fun testCinteropSpinBarrier() = testBarrier { CinteropSpinBarrier(it) } | ||
|
||
@Test | ||
fun testKNativeSpinBarrier() = testBarrier { KNativeSpinBarrier(it) } | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
core/src/nativeTest/kotlin/org/jetbrains/litmuskt/IntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.jetbrains.litmuskt | ||
|
||
import org.jetbrains.litmuskt.autooutcomes.LitmusIIOutcome | ||
import org.jetbrains.litmuskt.autooutcomes.accept | ||
import org.jetbrains.litmuskt.barriers.CinteropSpinBarrier | ||
import kotlin.test.Test | ||
import kotlin.test.assertNotEquals | ||
|
||
class IntegrationTest { | ||
|
||
private val sampleLitmusTest = litmusTest({ | ||
object : LitmusIIOutcome() { | ||
var x = 0 | ||
} | ||
}) { | ||
// TODO: reset | ||
thread { | ||
x = 1 | ||
} | ||
thread { | ||
r1 = x | ||
x = 2 | ||
r2 = x | ||
} | ||
spec { | ||
accept(0, 2) | ||
accept(1, 2) | ||
accept(0, 1) // r1 = 0; x = 2; x = 1 (t1); r2 = 1 | ||
} | ||
} | ||
|
||
@Test | ||
fun testBasic() { | ||
val runner = PthreadRunner() | ||
val result = runner.runTests( | ||
tests = listOf(sampleLitmusTest), | ||
params = LitmusRunParams( | ||
batchSize = 1_000_000, | ||
syncPeriod = 10, | ||
affinityMap = null, | ||
barrierProducer = ::CinteropSpinBarrier | ||
) | ||
).first() | ||
assertNotEquals(LitmusOutcomeType.FORBIDDEN, result.overallStatus()) | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
core/src/nativeTest/kotlin/org/jetbrains/litmuskt/ThreadlikeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.jetbrains.litmuskt | ||
|
||
import platform.posix.sleep | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ThreadlikeTest { | ||
|
||
private fun testThreadlike(t: Threadlike) { | ||
class IntHolder(var x: Int) | ||
|
||
val holder = IntHolder(0) | ||
val future = t.start(holder) { | ||
sleep(1u) | ||
it.x = 1 | ||
} | ||
assertEquals(0, holder.x) // checking parallelism | ||
future.await() | ||
assertEquals(1, holder.x) | ||
} | ||
|
||
@Test | ||
fun testWorkerThreadlike() = testThreadlike(WorkerThreadlike()) | ||
|
||
@Test | ||
fun testPthreadThreadlike() = testThreadlike(PthreadThreadlike()) | ||
} |