Merge boxWithStdlib testData into box, delete BoxWithStdlib test
This commit is contained in:
committed by
Alexander Udalov
parent
22bfc9786a
commit
06a67e6602
@@ -0,0 +1,25 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
fun box(): String {
|
||||
var obj0 = "0" as java.lang.Object
|
||||
var obj1 = "1" as java.lang.Object
|
||||
|
||||
var v = obj0
|
||||
synchronized (v) {
|
||||
v = obj1
|
||||
}
|
||||
assertThatThreadDoesNotOwnMonitor(obj0)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun assertThatThreadDoesNotOwnMonitor(obj: java.lang.Object) {
|
||||
try {
|
||||
obj.wait(1)
|
||||
throw IllegalStateException("Not owning a monitor!")
|
||||
}
|
||||
catch (e: IllegalMonitorStateException) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun box(): String {
|
||||
val obj = "" as java.lang.Object
|
||||
val e = IllegalArgumentException()
|
||||
fun m(): Nothing = throw e
|
||||
try {
|
||||
synchronized (m()) {
|
||||
throw AssertionError("Should not have reached this point")
|
||||
}
|
||||
}
|
||||
catch (caught: Throwable) {
|
||||
if (caught !== e) return "Fail: $caught"
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
fun box(): String {
|
||||
val obj = "" as java.lang.Object
|
||||
|
||||
val e = IllegalArgumentException()
|
||||
try {
|
||||
synchronized (obj) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
catch (caught: Throwable) {
|
||||
if (caught !== e) return "Fail: $caught"
|
||||
// If monitorexit didn't happen (a finally block failed), this assertion would fail
|
||||
assertThatThreadDoesNotOwnMonitor(obj)
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun assertThatThreadDoesNotOwnMonitor(obj: java.lang.Object) {
|
||||
try {
|
||||
obj.wait(1)
|
||||
throw IllegalStateException("Not owning a monitor!")
|
||||
}
|
||||
catch (e: IllegalMonitorStateException) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun box(): String {
|
||||
var obj = "0" as java.lang.Object
|
||||
val result = synchronized (obj) {
|
||||
239L
|
||||
}
|
||||
|
||||
if (result != 239L) return "Fail: $result"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun box(): String {
|
||||
val obj = "" as java.lang.Object
|
||||
val obj2 = "1" as java.lang.Object
|
||||
|
||||
synchronized (obj) {
|
||||
synchronized (obj2) {
|
||||
obj.wait(1)
|
||||
obj2.wait(1)
|
||||
}
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun box(): String {
|
||||
val obj = "" as java.lang.Object
|
||||
|
||||
synchronized (obj) {
|
||||
synchronized (obj) {
|
||||
obj.wait(1)
|
||||
}
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.Callable
|
||||
import java.util.concurrent.Future
|
||||
|
||||
val count: Int = 10;
|
||||
var index: Int = 0;
|
||||
val doneSignal = CountDownLatch(count)
|
||||
val startSignal = CountDownLatch(1);
|
||||
val mutex: Any = Object()
|
||||
val results = arrayListOf<Int>()
|
||||
val executorService = Executors.newFixedThreadPool(count)
|
||||
|
||||
class MyException(message: String): Exception(message)
|
||||
|
||||
enum class ExecutionType {
|
||||
LOCAL,
|
||||
NON_LOCAL_SIMPLE,
|
||||
NON_LOCAL_EXCEPTION,
|
||||
NON_LOCAL_FINALLY,
|
||||
NON_LOCAL_EXCEPTION_AND_FINALLY,
|
||||
NON_LOCAL_EXCEPTION_AND_FINALLY_WITH_RETURN,
|
||||
NON_LOCAL_NESTED
|
||||
}
|
||||
|
||||
class TestLocal(val name: String, val executionType: ExecutionType) : Callable<String> {
|
||||
|
||||
override fun call(): String {
|
||||
startSignal.await()
|
||||
return when (executionType) {
|
||||
ExecutionType.LOCAL -> local()
|
||||
ExecutionType.NON_LOCAL_SIMPLE -> nonLocalSimple()
|
||||
ExecutionType.NON_LOCAL_EXCEPTION -> nonLocalWithException()
|
||||
ExecutionType.NON_LOCAL_FINALLY -> nonLocalWithFinally()
|
||||
ExecutionType.NON_LOCAL_EXCEPTION_AND_FINALLY -> nonLocalWithExceptionAndFinally()
|
||||
ExecutionType.NON_LOCAL_EXCEPTION_AND_FINALLY_WITH_RETURN -> nonLocalWithExceptionAndFinallyWithReturn()
|
||||
ExecutionType.NON_LOCAL_NESTED -> nonLocalNested()
|
||||
else -> "fail"
|
||||
}
|
||||
}
|
||||
|
||||
private fun underMutexFun() {
|
||||
results.add(++index);
|
||||
doneSignal.countDown()
|
||||
}
|
||||
|
||||
fun local(): String {
|
||||
synchronized(mutex) {
|
||||
underMutexFun()
|
||||
}
|
||||
return executionType.toString()
|
||||
}
|
||||
|
||||
|
||||
fun nonLocalSimple(): String {
|
||||
synchronized(mutex) {
|
||||
underMutexFun()
|
||||
return executionType.name
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
|
||||
fun nonLocalWithException(): String {
|
||||
synchronized(mutex) {
|
||||
try {
|
||||
underMutexFun()
|
||||
throw MyException(executionType.name)
|
||||
} catch (e: MyException) {
|
||||
return e.message!!
|
||||
}
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
|
||||
fun nonLocalWithFinally(): String {
|
||||
synchronized(mutex) {
|
||||
try {
|
||||
underMutexFun()
|
||||
return "fail"
|
||||
} finally {
|
||||
return executionType.name
|
||||
}
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
|
||||
fun nonLocalWithExceptionAndFinally(): String {
|
||||
synchronized(mutex) {
|
||||
try {
|
||||
underMutexFun()
|
||||
throw MyException(executionType.name)
|
||||
} catch (e: MyException) {
|
||||
return e.message!!
|
||||
} finally {
|
||||
"123"
|
||||
}
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
|
||||
fun nonLocalWithExceptionAndFinallyWithReturn(): String {
|
||||
synchronized(mutex) {
|
||||
try {
|
||||
underMutexFun()
|
||||
throw MyException(executionType.name)
|
||||
} catch (e: MyException) {
|
||||
return "fail1"
|
||||
} finally {
|
||||
return executionType.name
|
||||
}
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
|
||||
fun nonLocalNested(): String {
|
||||
synchronized(mutex) {
|
||||
try {
|
||||
try {
|
||||
underMutexFun()
|
||||
throw MyException(executionType.name)
|
||||
} catch (e: MyException) {
|
||||
return "fail1"
|
||||
} finally {
|
||||
return executionType.name
|
||||
}
|
||||
} finally {
|
||||
val p = 1 + 1
|
||||
}
|
||||
}
|
||||
return "fail"
|
||||
}
|
||||
}
|
||||
|
||||
fun testTemplate(type: ExecutionType, producer: (Int) -> Callable<String>): String {
|
||||
|
||||
try {
|
||||
val futures = arrayListOf<Future<String>>()
|
||||
for (i in 1..count) {
|
||||
futures.add(executorService.submit (producer(i)))
|
||||
}
|
||||
|
||||
startSignal.countDown()
|
||||
val b = doneSignal.await(10, TimeUnit.SECONDS)
|
||||
if (!b) return "fail: processes not finished"
|
||||
|
||||
for (i in 1..count) {
|
||||
if (results[i - 1] != i)
|
||||
return "fail $i != ${results[i]}: synchronization not works : " + results.joinToString()
|
||||
}
|
||||
|
||||
for (f in futures) {
|
||||
if (f.get() != type.name) return "failed result ${f.get()} != ${type.name}"
|
||||
}
|
||||
} finally {
|
||||
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun runTest(type: ExecutionType): String {
|
||||
return testTemplate (type) { TestLocal(it.toString(), type) }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
for (type in ExecutionType.values()) {
|
||||
val result = runTest(type)
|
||||
if (result != "OK") return "fail on $type execution: $result"
|
||||
}
|
||||
} finally {
|
||||
executorService.shutdown()
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun box(): String {
|
||||
var obj = "0" as java.lang.Object
|
||||
val result = synchronized (obj) {
|
||||
"239"
|
||||
}
|
||||
|
||||
if (result != "239") return "Fail: $result"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
import java.util.concurrent.*
|
||||
import java.util.concurrent.atomic.*
|
||||
|
||||
fun thread(block: ()->Unit ) {
|
||||
val thread = object: Thread() {
|
||||
override fun run() {
|
||||
block()
|
||||
}
|
||||
}
|
||||
thread.start()
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
val mtref = AtomicInteger()
|
||||
val cdl = CountDownLatch(11)
|
||||
for(i in 0..10) {
|
||||
thread {
|
||||
var current = 0
|
||||
do {
|
||||
current = synchronized(mtref) {
|
||||
val v = mtref.get() + 1
|
||||
if(v < 100)
|
||||
mtref.set(v+1)
|
||||
v
|
||||
}
|
||||
}
|
||||
while(current < 100)
|
||||
cdl.countDown()
|
||||
}
|
||||
}
|
||||
cdl.await()
|
||||
return if(mtref.get() == 100) "OK" else mtref.get().toString()
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun box(): String {
|
||||
var obj = "0" as java.lang.Object
|
||||
val result = synchronized (obj) {
|
||||
239
|
||||
}
|
||||
|
||||
if (result != 239) return "Fail: $result"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// WITH_RUNTIME
|
||||
// FULL_JDK
|
||||
|
||||
fun box(): String {
|
||||
val obj = "" as java.lang.Object
|
||||
try {
|
||||
obj.wait(1)
|
||||
return "Fail: exception should have been thrown"
|
||||
}
|
||||
catch (e: IllegalMonitorStateException) {
|
||||
// OK
|
||||
}
|
||||
|
||||
synchronized (obj) {
|
||||
obj.wait(1)
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user