[K/N] Add more tests on associated object handling ^KT-56233

This commit is contained in:
Alexander Shabalin
2023-03-31 14:20:20 +02:00
committed by Space Team
parent dbe14a0a90
commit 87da670319
13 changed files with 227 additions and 0 deletions
@@ -0,0 +1,4 @@
enum class SimpleEnum {
ONE,
TWO
}
@@ -0,0 +1,44 @@
import Kt56233
func threadRoutine(pointer: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
autoreleasepool {
let f = pointer.bindMemory(to: (() -> ()).self, capacity: 1).pointee
f()
}
return nil
}
func launchThreads(
_ f: @convention(c) () -> (),
threadCount: Int = 4
) throws {
var threads: [pthread_t] = []
for _ in 0..<threadCount {
let fPtr = UnsafeMutablePointer<() -> ()>.allocate(capacity: 1)
fPtr.initialize(to: f)
var thread: pthread_t? = nil
let result = pthread_create(&thread, nil, threadRoutine, fPtr)
try assertEquals(actual: result, expected: 0)
threads.append(thread!)
}
for thread in threads {
pthread_join(thread, nil)
}
}
func kt56233() {
// Stress testing for race conditions.
for _ in 0..<50000000 {
_ = Kt56233.SimpleEnum.two.ordinal
}
}
// -------- Execution of the test --------
class TestTests : SimpleTestProvider {
override init() {
super.init()
test("Kt56233", { try launchThreads(kt56233) })
}
}
@@ -0,0 +1,25 @@
@file:OptIn(kotlin.ExperimentalStdlibApi::class)
import kotlin.native.internal.GC
import kotlin.native.internal.gc.GCInfo
import kotlin.native.internal.isPermanent
import kotlin.test.*
private var _counter = 0
object Permanent {
var counter
get() = _counter
set(value) {
_counter = value
}
}
fun assertIsPermanent() {
assertTrue(Permanent.isPermanent())
}
fun stableRefsCount(): Long {
GC.collect()
return GC.lastGCInfo!!.rootSet.stableReferences
}
@@ -0,0 +1,24 @@
import PermanentObjects
func testPermanentObjects() throws {
PermanentObjects.KnlibraryKt.assertIsPermanent()
let stableRefsBefore = PermanentObjects.KnlibraryKt.stableRefsCount()
autoreleasepool {
for i in 0..<1000 {
PermanentObjects.Permanent().counter += 1
}
}
let stableRefsAfter = PermanentObjects.KnlibraryKt.stableRefsCount()
try assertEquals(actual: PermanentObjects.Permanent().counter, expected: 1000)
try assertEquals(actual: stableRefsAfter, expected: stableRefsBefore)
}
// -------- Execution of the test --------
class TestTests : SimpleTestProvider {
override init() {
super.init()
test("permanentObjects", testPermanentObjects)
}
}