[atomicfu-K/N] Tests for K/N atomicfu-compiler-plugin

* `nativeTest` task now allows to provide compiler plugins that may be enabled during test compilation
* test sets for JVM and K/N backends are equal

KT-60800 describes all the issues with native tests that were solved in this commit.

Co-authored-by: Dmitriy Dolovov <Dmitriy.Dolovov@jetbrains.com>

Merge-request: KT-MR-11401
Merged-by: Maria Sokolova <maria.sokolova@jetbrains.com>
This commit is contained in:
mvicsokolova
2023-08-16 09:41:29 +00:00
committed by Space Team
parent 8f03eb9314
commit 9ff0e0b046
38 changed files with 2644 additions and 33 deletions
@@ -0,0 +1,61 @@
import kotlinx.atomicfu.*
import kotlin.test.*
class LockFreeLongCounterTest {
private inline fun testWith(g: LockFreeLongCounter.() -> Long) {
val c = LockFreeLongCounter()
assertEquals(0L, c.g())
assertEquals(1L, c.increment())
assertEquals(1L, c.g())
assertEquals(2L, c.increment())
assertEquals(2L, c.g())
}
fun testBasic() = testWith { get() }
fun testGetInner() = testWith { getInner() }
fun testAdd2() {
val c = LockFreeLongCounter()
c.add2()
assertEquals(2L, c.get())
c.add2()
assertEquals(4L, c.get())
}
fun testSetM2() {
val c = LockFreeLongCounter()
c.setM2()
assertEquals(-2L, c.get())
}
}
class LockFreeLongCounter {
private val counter = atomic(0L)
fun get(): Long = counter.value
fun increment(): Long = counter.incrementAndGet()
fun add2() = counter.getAndAdd(2)
fun setM2() {
counter.value = -2L // LDC instruction here
}
fun getInner(): Long = Inner().getFromOuter()
// testing how an inner class can get access to it
private inner class Inner {
fun getFromOuter(): Long = counter.value
}
}
@Test
fun box() {
val testClass = LockFreeLongCounterTest()
testClass.testBasic()
testClass.testAdd2()
testClass.testSetM2()
testClass.testGetInner()
}