Implement new assert semantics in back-end

Previously, assert was just a regular function and its argument used to
be computed on each call (even if assertions are disabled on JVM).
This change adds support for 3 new behaviours of assert:
* always-enable (independently from -ea on JVM)
* always-disable (independently from -ea JVM)
* runtime/jvm (compile the calls like javac generates assert-operator)
* legacy (leave current eager semantics) - this already existed

Default behaviour is legacy for now.

The behavior is changed based on -Xassertions flag.
 #KT-7540: Fixed
This commit is contained in:
Ilmir Usmanov
2018-04-28 22:15:29 +03:00
parent 3f5a2c6427
commit f568149863
44 changed files with 2685 additions and 33 deletions
+40
View File
@@ -0,0 +1,40 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=always-disable
// WITH_RUNTIME
fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l())
return hit
}
fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) { "BOOYA!" }
return hit
}
fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l())
return hit
}
fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) { "BOOYA!" }
return hit
}
fun box(): String {
if (checkTrue()) return "FAIL 0"
if (checkTrueWithMessage()) return "FAIL 1"
if (checkFalse()) return "FAIL 2"
if (checkFalseWithMessage()) return "FAIL 3"
return "OK"
}
+48
View File
@@ -0,0 +1,48 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=always-enable
// WITH_RUNTIME
fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l())
return hit
}
fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) { "BOOYA!" }
return hit
}
fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l())
return hit
}
fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) { "BOOYA!" }
return hit
}
fun box(): String {
if (!checkTrue()) return "FAIL 0"
if (!checkTrueWithMessage()) return "FAIL 1"
try {
checkFalse()
return "FAIL 3"
} catch (ignore: AssertionError) {
}
try {
checkFalseWithMessage()
return "FAIL 4"
} catch (ignore: AssertionError) {
}
return "OK"
}
@@ -0,0 +1,54 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l())
return hit
}
fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l())
return hit
}
fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) { "BOOYA" }
return hit
}
fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) { "BOOYA" }
return hit
}
}
class ShouldBeDisabled : Checker {}
class Dummy
fun disableAssertions(): Checker {
val loader = Dummy::class.java.classLoader
loader.setDefaultAssertionStatus(false)
val c = loader.loadClass("ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = disableAssertions()
if (c.checkTrue()) return "FAIL 0"
if (c.checkTrueWithMessage()) return "FAIL 1"
if (c.checkFalse()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 3"
return "OK"
}
@@ -0,0 +1,62 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l())
return hit
}
fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l())
return hit
}
fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) { "BOOYA" }
return hit
}
fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) { "BOOYA" }
return hit
}
}
class ShouldBeEnabled : Checker {}
class Dummy
fun enableAssertions(): Checker {
val loader = Dummy::class.java.classLoader
loader.setDefaultAssertionStatus(true)
val c = loader.loadClass("ShouldBeEnabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = enableAssertions()
if (!c.checkTrue()) return "FAIL 0"
if (!c.checkTrueWithMessage()) return "FAIL 1"
try {
c.checkFalse()
return "FAIL 2"
} catch (ignore: AssertionError) {
}
try {
c.checkFalseWithMessage()
return "FAIL 3"
} catch (ignore: AssertionError) {
}
return "OK"
}
@@ -0,0 +1,124 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean
fun checkFalse(): Boolean
fun checkTrueWithMessage(): Boolean
fun checkFalseWithMessage(): Boolean
}
class ShouldBeDisabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
val local = fun() {
assert(l())
}
local()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
val local = fun() {
assert(l())
}
local()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
val local = fun() {
assert(l()) { "BOOYA" }
}
local()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
val local = fun() {
assert(l()) { "BOOYA" }
}
local()
return hit
}
}
class ShouldBeEnabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
val local = fun() {
assert(l())
}
local()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
val local = fun() {
assert(l())
}
local()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
val local = fun() {
assert(l()) { "BOOYA" }
}
local()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
val local = fun() {
assert(l()) { "BOOYA" }
}
local()
return hit
}
}
fun setDesiredAssertionStatus(v: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setDefaultAssertionStatus(v)
val c = loader.loadClass(if (v) "ShouldBeEnabled" else "ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = setDesiredAssertionStatus(false)
if (c.checkTrue()) return "FAIL 0"
if (c.checkTrueWithMessage()) return "FAIL 1"
if (c.checkFalse()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 3"
c = setDesiredAssertionStatus(true)
if (!c.checkTrue()) return "FAIL 4"
if (!c.checkTrueWithMessage()) return "FAIL 5"
try {
c.checkFalse()
return "FAIL 6"
} catch (ignore: AssertionError) {
}
try {
c.checkFalseWithMessage()
return "FAIL 7"
} catch (ignore: AssertionError) {
}
return "OK"
}
+164
View File
@@ -0,0 +1,164 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean
fun checkFalse(): Boolean
fun checkTrueWithMessage(): Boolean
fun checkFalseWithMessage(): Boolean
}
class ShouldBeDisabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
class Local {
fun run() {
assert(l())
}
}
val local = Local()
local.run()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
class Local {
fun run() {
assert(l())
}
}
val local = Local()
local.run()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
class Local {
fun run() {
assert(l()) { "BOOYA" }
}
}
val local = Local()
local.run()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
class Local {
fun run() {
assert(l()) { "BOOYA" }
}
}
val local = Local()
local.run()
return hit
}
}
class ShouldBeEnabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
class Local {
fun run() {
assert(l())
}
}
val local = Local()
local.run()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
class Local {
fun run() {
assert(l())
}
}
val local = Local()
local.run()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
class Local {
fun run() {
assert(l()) { "BOOYA" }
}
}
val local = Local()
local.run()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
class Local {
fun run() {
assert(l()) { "BOOYA" }
}
}
val local = Local()
local.run()
return hit
}
}
fun setDesiredAssertionStatus(v: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setDefaultAssertionStatus(v)
val c = loader.loadClass(if (v) "ShouldBeEnabled" else "ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = setDesiredAssertionStatus(false)
if (c.checkTrue()) return "FAIL 0"
if (c.checkTrueWithMessage()) return "FAIL 1"
if (c.checkFalse()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 3"
c = setDesiredAssertionStatus(true)
if (!c.checkTrue()) return "FAIL 4"
if (!c.checkTrueWithMessage()) return "FAIL 5"
try {
c.checkFalse()
return "FAIL 6"
} catch (ignore: AssertionError) {
}
try {
c.checkFalseWithMessage()
return "FAIL 7"
} catch (ignore: AssertionError) {
}
return "OK"
}
@@ -0,0 +1,124 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean
fun checkFalse(): Boolean
fun checkTrueWithMessage(): Boolean
fun checkFalseWithMessage(): Boolean
}
class ShouldBeDisabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
fun local() {
assert(l())
}
local()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
fun local() {
assert(l())
}
local()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
fun local() {
assert(l()) { "BOOYA" }
}
local()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
fun local() {
assert(l()) { "BOOYA" }
}
local()
return hit
}
}
class ShouldBeEnabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
fun local() {
assert(l())
}
local()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
fun local() {
assert(l())
}
local()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
fun local() {
assert(l()) { "BOOYA" }
}
local()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
fun local() {
assert(l()) { "BOOYA" }
}
local()
return hit
}
}
fun setDesiredAssertionStatus(v: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setDefaultAssertionStatus(v)
val c = loader.loadClass(if (v) "ShouldBeEnabled" else "ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = setDesiredAssertionStatus(false)
if (c.checkTrue()) return "FAIL 0"
if (c.checkTrueWithMessage()) return "FAIL 1"
if (c.checkFalse()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 3"
c = setDesiredAssertionStatus(true)
if (!c.checkTrue()) return "FAIL 4"
if (!c.checkTrueWithMessage()) return "FAIL 5"
try {
c.checkFalse()
return "FAIL 6"
} catch (ignore: AssertionError) {
}
try {
c.checkFalseWithMessage()
return "FAIL 7"
} catch (ignore: AssertionError) {
}
return "OK"
}
+124
View File
@@ -0,0 +1,124 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean
fun checkFalse(): Boolean
fun checkTrueWithMessage(): Boolean
fun checkFalseWithMessage(): Boolean
}
class ShouldBeDisabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
val local = {
assert(l())
}
local()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
val local = {
assert(l())
}
local()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
val local = {
assert(l()) { "BOOYA" }
}
local()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
val local = {
assert(l()) { "BOOYA" }
}
local()
return hit
}
}
class ShouldBeEnabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
val local = {
assert(l())
}
local()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
val local = {
assert(l())
}
local()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
val local = {
assert(l()) { "BOOYA" }
}
local()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
val local = {
assert(l()) { "BOOYA" }
}
local()
return hit
}
}
fun setDesiredAssertionStatus(v: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setDefaultAssertionStatus(v)
val c = loader.loadClass(if (v) "ShouldBeEnabled" else "ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = setDesiredAssertionStatus(false)
if (c.checkTrue()) return "FAIL 0"
if (c.checkTrueWithMessage()) return "FAIL 1"
if (c.checkFalse()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 3"
c = setDesiredAssertionStatus(true)
if (!c.checkTrue()) return "FAIL 4"
if (!c.checkTrueWithMessage()) return "FAIL 5"
try {
c.checkFalse()
return "FAIL 6"
} catch (ignore: AssertionError) {
}
try {
c.checkFalseWithMessage()
return "FAIL 7"
} catch (ignore: AssertionError) {
}
return "OK"
}
+156
View File
@@ -0,0 +1,156 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean
fun checkFalse(): Boolean
fun checkTrueWithMessage(): Boolean
fun checkFalseWithMessage(): Boolean
}
class ShouldBeDisabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
val local = object {
fun run() {
assert(l())
}
}
local.run()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
val local = object {
fun run() {
assert(l())
}
}
local.run()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
val local = object {
fun run() {
assert(l()) { "BOOYA" }
}
}
local.run()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
val local = object {
fun run() {
assert(l()) { "BOOYA" }
}
}
local.run()
return hit
}
}
class ShouldBeEnabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
val local = object {
fun run() {
assert(l())
}
}
local.run()
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
val local = object {
fun run() {
assert(l())
}
}
local.run()
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
val local = object {
fun run() {
assert(l()) { "BOOYA" }
}
}
local.run()
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
val local = object {
fun run() {
assert(l()) { "BOOYA" }
}
}
local.run()
return hit
}
}
fun setDesiredAssertionStatus(v: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setDefaultAssertionStatus(v)
val c = loader.loadClass(if (v) "ShouldBeEnabled" else "ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = setDesiredAssertionStatus(false)
if (c.checkTrue()) return "FAIL 0"
if (c.checkTrueWithMessage()) return "FAIL 1"
if (c.checkFalse()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 3"
c = setDesiredAssertionStatus(true)
if (!c.checkTrue()) return "FAIL 4"
if (!c.checkTrueWithMessage()) return "FAIL 5"
try {
c.checkFalse()
return "FAIL 6"
} catch (ignore: AssertionError) {
}
try {
c.checkFalseWithMessage()
return "FAIL 7"
} catch (ignore: AssertionError) {
}
return "OK"
}
@@ -0,0 +1,75 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrueWithMessage(): Boolean
fun checkFalseWithMessage(): Boolean
}
class ShouldBeDisabled : Checker {
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) {
throw RuntimeException("FAIL 1")
}
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) {
throw RuntimeException("FAIL 3")
}
return hit
}
}
class ShouldBeEnabled : Checker {
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) {
throw RuntimeException("FAIL 5")
}
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) {
return hit
"BOOYA"
}
throw RuntimeException("FAIL 7")
}
}
fun setDesiredAssertionStatus(v: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setDefaultAssertionStatus(v)
val c = loader.loadClass(if (v) "ShouldBeEnabled" else "ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = setDesiredAssertionStatus(false)
if (c.checkTrueWithMessage()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 4"
c = setDesiredAssertionStatus(true)
if (!c.checkTrueWithMessage()) return "FAIL 6"
if (!c.checkFalseWithMessage()) return "FAIL 8"
return "OK"
}
+100
View File
@@ -0,0 +1,100 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean
fun checkFalse(): Boolean
fun checkTrueWithMessage(): Boolean
fun checkFalseWithMessage(): Boolean
}
class ShouldBeDisabled: Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l())
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l())
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) { "BOOYA" }
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) { "BOOYA" }
return hit
}
}
class ShouldBeEnabled: Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l())
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l())
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) { "BOOYA" }
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) { "BOOYA" }
return hit
}
}
fun setDesiredAssertionStatus(v: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setDefaultAssertionStatus(v)
val c = loader.loadClass(if (v) "ShouldBeEnabled" else "ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = setDesiredAssertionStatus(false)
if (c.checkTrue()) return "FAIL 0"
if (c.checkTrueWithMessage()) return "FAIL 1"
if (c.checkFalse()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 3"
c = setDesiredAssertionStatus(true)
if (!c.checkTrue()) return "FAIL 4"
if (!c.checkTrueWithMessage()) return "FAIL 5"
try {
c.checkFalse()
return "FAIL 6"
} catch (ignore: AssertionError) {
}
try {
c.checkFalseWithMessage()
return "FAIL 7"
} catch (ignore: AssertionError) {
}
return "OK"
}
@@ -0,0 +1,118 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
interface Checker {
fun checkTrue(): Boolean
fun checkFalse(): Boolean
fun checkTrueWithMessage(): Boolean
fun checkFalseWithMessage(): Boolean
}
open class IntHolder(i: Int)
class ShouldBeDisabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
val local = object : IntHolder(run { assert(l()); 0 }) {}
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
val local = object : IntHolder(run { assert(l()); 0 }) {}
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
val local = object : IntHolder(run { assert(l()) { "BOOYA!" }; 0 }) {}
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
val local = object : IntHolder(run { assert(l()) { "BOOYA!" }; 0 }) {}
return hit
}
}
class ShouldBeEnabled : Checker {
override fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
val local = object : IntHolder(run { assert(l()); 0 }) {}
return hit
}
override fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
val local = object : IntHolder(run { assert(l()); 0 }) {}
return hit
}
override fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
val local = object : IntHolder(run { assert(l()) { "BOOYA!" }; 0 }) {}
return hit
}
override fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
val local = object : IntHolder(run { assert(l()) { "BOOYA!" }; 0 }) {}
return hit
}
}
fun setDesiredAssertionStatus(v: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setDefaultAssertionStatus(v)
val c = loader.loadClass(if (v) "ShouldBeEnabled" else "ShouldBeDisabled")
return c.newInstance() as Checker
}
fun box(): String {
var c = setDesiredAssertionStatus(false)
if (c.checkTrue()) return "FAIL 0"
if (c.checkTrueWithMessage()) return "FAIL 1"
if (c.checkFalse()) return "FAIL 2"
if (c.checkFalseWithMessage()) return "FAIL 3"
c = setDesiredAssertionStatus(true)
if (!c.checkTrue()) return "FAIL 4"
if (!c.checkTrueWithMessage()) return "FAIL 5"
try {
c.checkFalse()
return "FAIL 6"
} catch (ignore: AssertionError) {
}
try {
c.checkFalseWithMessage()
return "FAIL 7"
} catch (ignore: AssertionError) {
}
return "OK"
}
@@ -0,0 +1,34 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// WITH_COROUTINES
// COMMON_COROUTINES_TEST
import helpers.*
import COROUTINES_PACKAGE.*
class Checker {
suspend fun check() {
assert(false)
}
}
class Dummy
fun disableAssertions(): Checker {
val loader = Dummy::class.java.classLoader
loader.setDefaultAssertionStatus(false)
val c = loader.loadClass("Checker")
return c.newInstance() as Checker
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var c = disableAssertions()
builder { c.check() }
return "OK"
}
@@ -0,0 +1,38 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// WITH_COROUTINES
// COMMON_COROUTINES_TEST
import helpers.*
import COROUTINES_PACKAGE.*
class Checker {
suspend fun check() {
assert(false)
}
}
class Dummy
fun enableAssertions(): Checker {
val loader = Dummy::class.java.classLoader
loader.setDefaultAssertionStatus(true)
val c = loader.loadClass("Checker")
return c.newInstance() as Checker
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var c = enableAssertions()
try {
builder { c.check() }
return "FAIL 6"
} catch (ignore: AssertionError) {
}
return "OK"
}
@@ -0,0 +1,34 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// WITH_COROUTINES
// COMMON_COROUTINES_TEST
import helpers.*
import COROUTINES_PACKAGE.*
class Checker {
fun check() {
builder { assert(false) }
}
}
class Dummy
fun disableAssertions(): Checker {
val loader = Dummy::class.java.classLoader
loader.setDefaultAssertionStatus(false)
val c = loader.loadClass("Checker")
return c.newInstance() as Checker
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var c = disableAssertions()
c.check()
return "OK"
}
@@ -0,0 +1,38 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// WITH_COROUTINES
// COMMON_COROUTINES_TEST
import helpers.*
import COROUTINES_PACKAGE.*
class Checker {
fun check() {
builder { assert(false) }
}
}
class Dummy
fun enableAssertions(): Checker {
val loader = Dummy::class.java.classLoader
loader.setDefaultAssertionStatus(true)
val c = loader.loadClass("Checker")
return c.newInstance() as Checker
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var c = enableAssertions()
try {
c.check()
return "FAIL 6"
} catch (ignore: AssertionError) {
}
return "OK"
}
+76
View File
@@ -0,0 +1,76 @@
// IGNORE_BACKEND: JS
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=legacy
// WITH_RUNTIME
// FULL_JDK
import java.lang.reflect.Field
import java.lang.reflect.Modifier
fun setDesiredAssertionStatus(v: Boolean) {
@Suppress("INVISIBLE_REFERENCE")
val field = kotlin._Assertions.javaClass.getField("ENABLED")
val modifiers = Field::class.java.getDeclaredField("modifiers");
modifiers.isAccessible = true
modifiers.setInt(field, field.modifiers and Modifier.FINAL.inv())
field.set(null, v)
}
fun checkTrue(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l())
return hit
}
fun checkTrueWithMessage(): Boolean {
var hit = false
val l = { hit = true; true }
assert(l()) { "BOOYA!" }
return hit
}
fun checkFalse(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l())
return hit
}
fun checkFalseWithMessage(): Boolean {
var hit = false
val l = { hit = true; false }
assert(l()) { "BOOYA!" }
return hit
}
fun box(): String {
setDesiredAssertionStatus(false)
if (!checkTrue()) return "FAIL 0"
setDesiredAssertionStatus(true)
if (!checkTrue()) return "FAIL 1"
setDesiredAssertionStatus(false)
if (!checkTrueWithMessage()) return "FAIL 2"
setDesiredAssertionStatus(true)
if (!checkTrueWithMessage()) return "FAIL 3"
setDesiredAssertionStatus(false)
if (!checkFalse()) return "FAIL 4"
setDesiredAssertionStatus(true)
try {
checkFalse()
return "FAIL 5"
} catch (ignore: AssertionError) {
}
setDesiredAssertionStatus(false)
if (!checkFalseWithMessage()) return "FAIL 6"
setDesiredAssertionStatus(true)
try {
checkFalseWithMessage()
return "FAIL 7"
} catch (ignore: AssertionError) {
}
return "OK"
}