Add more tests for -Xassertions=jvm corner cases

This commit is contained in:
Steven Schäfer
2019-07-05 15:24:54 +02:00
committed by max-kammerer
parent 3e25166832
commit d11344ce2e
15 changed files with 491 additions and 1 deletions
@@ -0,0 +1,54 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// Assertions which run before the class initializer are always checked
package initializerAssertionsEnabled
class Checker {
fun test() = Baz.testAsserts()
}
open class Bar {
companion object {
val barAssertionThrown = try {
Baz().assertFalse()
false
} catch(error: java.lang.AssertionError) {
true
}
}
}
class Baz : Bar() {
fun assertFalse() = assert(false)
companion object {
val bazAssertionThrown = try {
Baz().assertFalse()
false
} catch(error: java.lang.AssertionError) {
true
}
fun testAsserts(): String {
if (!barAssertionThrown) return "Fail 1"
if (bazAssertionThrown) return "Fail 2"
return "OK"
}
}
}
class Dummy
fun disableAssertions(): Checker {
val loader = Dummy::class.java.classLoader
loader.setPackageAssertionStatus("initializerAssertionsEnabled", false)
return loader.loadClass("initializerAssertionsEnabled.Checker").newInstance() as Checker
}
fun box(): String {
return disableAssertions().test()
}
@@ -0,0 +1,34 @@
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue(): Boolean {
var hit = false
assert({ hit = true; true }())
return hit
}
}
class ShouldBeDisabled {
fun checkFalse(): Boolean {
var hit = false
assert({ hit = true; true }())
return hit
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,40 @@
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue() = ShouldBeEnabled.hit
companion object {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
class ShouldBeDisabled {
fun checkFalse() = ShouldBeDisabled.hit
companion object {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue() = Inner().hit
inner class Inner {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
class ShouldBeDisabled {
fun checkFalse() = Inner().hit
inner class Inner {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,43 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue(): Boolean {
class Local {
var hit = false
init {
assert({ hit = true; true}())
}
}
return Local().hit
}
}
class ShouldBeDisabled {
fun checkFalse(): Boolean {
class Local {
var hit = false
init {
assert({ hit = true; true}())
}
}
return Local().hit
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,47 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
package classAssertions
class ShouldBeEnabled {
fun checkTrue() = A.B().hit
class A {
class B {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
}
class ShouldBeDisabled {
fun checkFalse() = A.B().hit
class A {
class B {
var hit = false
init {
assert({ hit = true; true }())
}
}
}
}
class Dummy
fun box(): String {
val loader = Dummy::class.java.classLoader
loader.setClassAssertionStatus("classAssertions.ShouldBeEnabled", true)
loader.setClassAssertionStatus("classAssertions.ShouldBeDisabled", false)
val c1 = loader.loadClass("classAssertions.ShouldBeEnabled").newInstance() as ShouldBeEnabled
val c2 = loader.loadClass("classAssertions.ShouldBeDisabled").newInstance() as ShouldBeDisabled
if (!c1.checkTrue()) return "FAIL 0"
if (c2.checkFalse()) return "FAIL 1"
return "OK"
}
@@ -0,0 +1,51 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// If assertions are disabled, neither argument to assert should be evaluated.
// If assertions are enabled, both arguments should be evaluate to values before
// checking the assertion.
package assertions
interface Checker {
fun check(): Boolean
}
class Checker1 : Checker {
override fun check(): Boolean {
var result = true
assert(true, {
result = false
{ "Assertion failure" }
}())
return result
}
}
class Checker2 : Checker {
override fun check(): Boolean {
var result = true
assert(true, {
result = false
{ "Assertion failure" }
}())
return result
}
}
fun checkerWithAssertions(enabled: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setPackageAssertionStatus("assertions", enabled)
val c = loader.loadClass(if (enabled) "assertions.Checker1" else "assertions.Checker2")
return c.newInstance() as Checker
}
fun box(): String {
var c = checkerWithAssertions(true)
if (c.check()) return "Fail 1"
c = checkerWithAssertions(false)
if (!c.check()) return "Fail 2"
return "OK"
}
@@ -0,0 +1,26 @@
// TARGET_BACKEND: JVM
// KOTLIN_CONFIGURATION_FLAGS: ASSERTIONS_MODE=jvm
// WITH_RUNTIME
// Reusing the $assertionsDisabled field in the Outer class might seem like a good idea,
// but it would result in an error in this case.
class Outer {
companion object {
init { error("") }
}
init { assert(true) }
class Inner {
init { assert(true) }
}
}
fun box(): String {
try {
Outer.Inner()
} catch (e: Throwable) {
return "Fail"
}
return "OK"
}