backend/tests: Add blackbox tests from Kotlin JVM

Added tests from testData/codegen/box directory. There are blackbox tests
in other directories and they are to be added.
This commit is contained in:
Ilya Matveev
2017-01-12 19:43:20 +07:00
parent d5988297b1
commit 1b553ebfaf
2643 changed files with 66666 additions and 0 deletions
@@ -0,0 +1,9 @@
fun box(): String {
var bottles = 99
while (bottles > 0) {
// System.out.println("bottles of beer on the wall");
bottles -= 1
bottles--
}
return if (bottles == -1) "OK" else "Fail $bottles"
}
@@ -0,0 +1,15 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun box(): String {
OUTER@while (true) {
var x = ""
try {
do {
x = x + break@OUTER
} while (true)
} finally {
return "OK"
}
}
}
@@ -0,0 +1,10 @@
fun box(): String {
val ok: String? = "OK"
var res = ""
do {
res += ok ?: break
} while (false)
return res
}
@@ -0,0 +1,9 @@
fun test(str: String): String {
var s = ""
for (i in 1..3) {
s += if (i<2) str else break
}
return s
}
fun box(): String = test("OK")
@@ -0,0 +1,6 @@
fun box(): String {
var i = 0
do continue while (i++ < 3)
return "OK"
}
@@ -0,0 +1,7 @@
fun box(): String {
var s = "OK"
for (i in 1..3) {
s = s + if (i<2) "" else continue
}
return s
}
@@ -0,0 +1,17 @@
inline fun bar(block: () -> String) : String {
return block()
}
inline fun bar2() : String {
while (true) break
return bar { return "def" }
}
fun foobar(x: String, y: String, z: String) = x + y + z
fun box(): String {
val test = foobar("abc", bar2(), "ghi")
return if (test == "abcdefghi")
"OK"
else "Failed, test=$test"
}
@@ -0,0 +1,9 @@
fun box(): String {
var x = "OK"
do {
while (true) {
x = x + break
}
} while (false)
return x
}
@@ -0,0 +1,11 @@
fun foo(x: String): String {
var y: String
do {
y = x
} while (y != x.bar(x))
return y
}
inline fun String.bar(other: String) = this
fun box(): String = foo("OK")
@@ -0,0 +1,9 @@
fun box(): String {
var cycle = true;
while (true) {
if (true && break) {
return "fail"
}
}
return "OK"
}
@@ -0,0 +1,9 @@
fun box(): String {
var cycle = true;
while (true) {
if (true || break) {
return "OK"
}
}
return "fail"
}
@@ -0,0 +1,13 @@
fun foo(x: Long, y: Int, z: Double, s: String) {}
fun box(): String {
while (true) {
try {
foo(0, 0, 0.0, "" + continue)
}
finally {
foo(0, 0, 0.0, "" + break)
}
}
return "OK"
}
@@ -0,0 +1,12 @@
fun box(): String {
var x = "OK"
while (true) {
try {
x = x + continue
}
finally {
x = x + break
}
}
return x
}
@@ -0,0 +1,13 @@
fun box(): String {
var r = ""
for (i in 1..1) {
try {
r += "O"
break
} finally {
r += "K"
continue
}
}
return r
}
@@ -0,0 +1,4 @@
fun box(): String {
while (true) break
return "OK"
}
@@ -0,0 +1,11 @@
fun box(): String {
while (true) {
try {
continue;
}
finally {
break;
}
}
return "OK"
}
@@ -0,0 +1,8 @@
fun box(): String {
val x: Int? = 0
if (x != 0) return "Fail $x"
if (0 != x) return "Fail $x"
if (!(x == 0)) return "Fail $x"
if (!(0 == x)) return "Fail $x"
return "OK"
}
@@ -0,0 +1,13 @@
var result = "Fail"
fun setOK(): Boolean {
result = "OK"
return true
}
fun box(): String {
if (setOK()) {
} else {
}
return result
}
@@ -0,0 +1,16 @@
// WITH_RUNTIME
fun concatNonNulls(strings: List<String?>): String {
var result = ""
for (str in strings) {
result += str?:continue
}
return result
}
fun box(): String {
val test = concatNonNulls(listOf("abc", null, null, "", null, "def"))
if (test != "abcdef") return "Failed: test=$test"
return "OK"
}
@@ -0,0 +1,123 @@
fun for_int_range(): Int {
var c = 0
for (i in 1..10) {
if (c >= 5) continue
c++
}
return c
}
fun for_byte_range(): Int {
var c = 0
val from: Byte = 1
val to: Byte = 10
for (i in from..to) {
if (c >= 5) continue
c++
}
return c
}
fun for_long_range(): Int {
var c = 0
val from: Long = 1
val to: Long = 10
for (i in from..to) {
if (c >= 5) continue
c++
}
return c
}
fun for_int_list(): Int {
val a = ArrayList<Int>()
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
var c = 0
for (i in a) {
if (c >= 5) continue
c++
}
return c
}
fun for_byte_list(): Int {
val a = ArrayList<Byte>()
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
var c = 0
for (i in a) {
if (c >= 5) continue
c++
}
return c
}
fun for_long_list(): Int {
val a = ArrayList<Long>()
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
var c = 0
for (i in a) {
if (c >= 5) continue
c++
}
return c
}
fun for_double_list(): Int {
val a = ArrayList<Double>()
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
var c = 0
for (i in a) {
if (c >= 5) continue
c++
}
return c
}
fun for_object_list(): Int {
val a = ArrayList<Any>()
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
var c = 0
for (i in a) {
if (c >= 5) continue
c++
}
return c
}
fun for_str_array(): Int {
val a = arrayOfNulls<String>(10)
var c = 0
for (i in a) {
if (c >= 5) continue
c++
}
return c
}
fun for_intarray(): Int {
val a = IntArray(10)
var c = 0
for (i in a) {
if (c >= 5) continue
c++
}
return c
}
fun box(): String {
if (for_int_range() != 5) return "fail 1"
if (for_byte_range() != 5) return "fail 2"
if (for_long_range() != 5) return "fail 3"
if (for_intarray() != 5) return "fail 4"
if (for_str_array() != 5) return "fail 5"
if (for_int_list() != 5) return "fail 6"
if (for_byte_list() != 5) return "fail 7"
if (for_long_list() != 5) return "fail 8"
if (for_double_list() != 5) return "fail 9"
return "OK"
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun foo(): List<String>? = listOf("abcde")
fun box(): String {
for (i in 1..3) {
for (value in foo() ?: continue) {
if (value != "abcde") return "Fail"
}
}
return "OK"
}
@@ -0,0 +1,16 @@
fun foo(i: Int): Int {
var count = i;
var result = 0;
while(count > 0) {
count = count - 1;
if (count <= 2) continue;
result = result + count;
}
return result;
}
fun box(): String {
if (foo(4) != 3) return "Fail 1"
if (foo(5) != 7) return "Fail 2"
return "OK"
}
@@ -0,0 +1,123 @@
fun for_int_range(): Int {
var c = 0
loop@ for (i in 1..10) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_byte_range(): Int {
var c = 0
val from: Byte = 1
val to: Byte = 10
loop@ for (i in from..to) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_long_range(): Int {
var c = 0
val from: Long = 1
val to: Long = 10
loop@ for (i in from..to) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_int_list(): Int {
val a = ArrayList<Int>()
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
var c = 0
loop@ for (i in a) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_byte_list(): Int {
val a = ArrayList<Byte>()
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
var c = 0
loop@ for (i in a) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_long_list(): Int {
val a = ArrayList<Long>()
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
var c = 0
loop@ for (i in a) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_double_list(): Int {
val a = ArrayList<Double>()
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
var c = 0
loop@ for (i in a) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_object_list(): Int {
val a = ArrayList<Any>()
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
var c = 0
loop@ for (i in a) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_str_array(): Int {
val a = arrayOfNulls<String>(10)
var c = 0
loop@ for (i in a) {
if (c >= 5) continue@loop
c++
}
return c
}
fun for_intarray(): Int {
val a = IntArray(10)
var c = 0
loop@ for (i in a) {
if (c >= 5) continue@loop
c++
}
return c
}
fun box(): String {
if (for_int_range() != 5) return "fail 1"
if (for_byte_range() != 5) return "fail 2"
if (for_long_range() != 5) return "fail 3"
if (for_intarray() != 5) return "fail 4"
if (for_str_array() != 5) return "fail 5"
if (for_int_list() != 5) return "fail 6"
if (for_byte_list() != 5) return "fail 7"
if (for_long_list() != 5) return "fail 8"
if (for_double_list() != 5) return "fail 9"
return "OK"
}
@@ -0,0 +1,15 @@
fun box(): String {
var x = 0
do x++ while (x < 5)
if (x != 5) return "Fail 1 $x"
var y = 0
do { y++ } while (y < 5)
if (y != 5) return "Fail 2 $y"
var z = ""
do { z += z.length } while (z.length < 5)
if (z != "01234") return "Fail 3 $z"
return "OK"
}
@@ -0,0 +1,12 @@
fun box(): String {
var fx = 1
var fy = 1
do {
var tmp = fy
fy = fx + fy
fx = tmp
} while (fy < 100)
return if (fy == 144) "OK" else "Fail $fx $fy"
}
@@ -0,0 +1,21 @@
fun box(): String {
var i = 0
do {
if (i++ > 100) break;
continue;
} while(false)
if (i != 1) return "Fail 1, expected 1, but $i"
i = 0
do {
if (i++ > 100) break;
continue;
} while(i<10)
if (i != 10) return "Fail 2, expected 10, but $i"
i = 0
do continue while(i++<10)
if (i != 11) return "Fail 3, expected 11, but $i"
return "OK"
}
@@ -0,0 +1,9 @@
fun box(): String {
do while (false);
var x = 0
do while (x++<5);
if (x != 6) return "Fail: $x"
return "OK"
}
@@ -0,0 +1,19 @@
var index = 0
interface IterableIterator : Iterator<Int> {
operator fun iterator(): Iterator<Int> = this
}
val iterator = object : IterableIterator {
override fun hasNext() = index < 5
override fun next() = index++
}
fun box(): String {
for (x in 1..5);
for (x in iterator);
if (index != 5) return "Fail: $index"
return "OK"
}
@@ -0,0 +1,9 @@
fun box(): String {
while (false);
var x = 0
while (x++<5);
if (x != 6) return "Fail: $x"
return "OK"
}
@@ -0,0 +1,44 @@
// WITH_RUNTIME
import kotlin.test.assertEquals
fun facWhile(i: Int): Int {
var count = 1;
var result = 1;
while(count < i) {
count = count + 1;
result = result * count;
}
return result;
}
fun facBreak(i: Int): Int {
var count = 1;
var result = 1;
while(true) {
count = count + 1;
result = result * count;
if (count == i) break;
}
return result;
}
fun facDoWhile(i: Int): Int {
var count = 1;
var result = 1;
do {
count = count + 1;
result = result * count;
} while(count != i);
return result;
}
fun box(): String {
assertEquals(6, facWhile(3))
assertEquals(6, facBreak(3))
assertEquals(6, facDoWhile(3))
assertEquals(120, facWhile(5))
assertEquals(120, facBreak(5))
assertEquals(120, facDoWhile(5))
return "OK"
}
@@ -0,0 +1,14 @@
var result = "Fail"
fun foo() {
try {
return
} finally {
result = "OK"
}
}
fun box(): String {
foo()
return result
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
val alist = arrayListOf(1, 2, 3) // : j.u.ArrayList<k.Int>
fun box(): String {
var result = 0
for (i: Int in alist) {
result += i
}
return if (result == 6) "OK" else "fail: $result"
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
val alist = arrayListOf(1 to 2, 2 to 3, 3 to 4)
fun box(): String {
var result = 0
for ((i: Int, z: Int) in alist) {
result += i + z
}
return if (result == 15) "OK" else "fail: $result"
}
@@ -0,0 +1,14 @@
fun f(x: Any?): Any? {
if (x is Array<*>) {
for (i in x) {
return i
}
}
return "FAIL"
}
fun box(): String {
val a = arrayOfNulls<String>(1) as Array<String>
a[0] = "OK"
return f(a) as String
}
@@ -0,0 +1,14 @@
fun box() : String {
val a = arrayOfNulls<Int>(5)
var i = 0
var sum = 0
for(el in 0..4) {
a[i] = i++
}
for (el in (a as Array<Int>)) {
sum = sum + el
}
if(sum != 10) return "a failed"
return "OK"
}
@@ -0,0 +1,26 @@
class It {
}
class C {
}
class X {
var hasNext = true
operator fun It.hasNext() = if (hasNext) {hasNext = false; true} else false
operator fun It.next() = 5
operator fun C.iterator(): It = It()
fun test() {
for (i in C()) {
foo(i)
}
}
}
fun foo(x: Int) {}
fun box(): String {
X().test()
return "OK"
}
@@ -0,0 +1,26 @@
class It {
operator fun next() = 5
}
class C {
operator fun iterator(): It = It()
}
class X {
var hasNext = true
operator fun It.hasNext() = if (hasNext) {hasNext = false; true} else false
fun test() {
for (i in C()) {
foo(i)
}
}
}
fun foo(x: Int) {}
fun box(): String {
X().test()
return "OK"
}
@@ -0,0 +1,29 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
class It {
var hasNext = true
operator fun hasNext() = if (hasNext) {hasNext = false; true} else false
}
class C {
operator fun iterator(): It = It()
}
class X {
operator fun It.next() = 5
fun test() {
for (i in C()) {
foo(i)
}
}
}
fun foo(x: Int) {}
fun box(): String {
X().test()
return "OK"
}
@@ -0,0 +1,15 @@
fun box() : String {
val b : Array<Int?> = arrayOfNulls<Int> (5)
var i = 0
var sum = 0
while(i < 5) {
b[i] = i++
}
sum = 0
for (el in b) {
sum = sum + (el ?: 0)
}
if(sum != 10) return "b failed"
return "OK"
}
@@ -0,0 +1,14 @@
fun box() : String {
val a = IntArray (5)
var i = 0
var sum = 0
for(el in 0..4) {
a[i] = i++
}
for (el in a) {
sum = sum + el
}
if(sum != 10) return "a failed"
return "OK"
}
@@ -0,0 +1,117 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun box() : String {
var sum : Int = 0
var i = 0
val c6 = MyCollection4()
sum = 0
for (el in c6) {
sum = sum + el
}
if(sum != 15) return "c6 failed"
val c5 = MyCollection3()
sum = 0
for (el in c5) {
sum = sum + (el ?: 0)
}
if(sum != 15) return "c5 failed"
val c1: Iterable<Int> = MyCollection1()
sum = 0
for (el in c1) {
sum = sum + el!!
}
if(sum != 15) return "c1 failed"
val c2 = MyCollection1()
sum = 0
for (el in c2) {
sum = sum + el!!
}
if(sum != 15) return "c2 failed"
val c3: Iterable<Int> = MyCollection2()
sum = 0
for (el in c3) {
sum = sum + el!!
}
if(sum != 15) return "c3 failed"
val c4 = MyCollection2()
sum = 0
for (el in c4) {
sum = sum + el!!
}
if(sum != 15) return "c4 failed"
val a : Array<Int> = arrayOfNulls<Int>(5) as Array<Int>
for(el in 0..4) {
a[i] = i++
}
sum = 0
for (el in a) {
sum = sum + el!!
}
if(sum != 10) return "a failed"
val b : Array<Int?> = arrayOfNulls<Int> (5)
i = 0
while(i < 5) {
b[i] = i++
}
sum = 0
for (el in b) {
sum = sum + (el ?: 0)
}
System.out?.println(sum)
if(sum != 10) return "b failed"
return "OK"
}
class MyCollection1(): Iterable<Int> {
override fun iterator(): Iterator<Int> = MyIterator()
class MyIterator(): Iterator<Int> {
var k : Int = 5
override fun next() : Int = k--
override fun hasNext() = k > 0
}
}
class MyCollection2(): Iterable<Int> {
override fun iterator(): Iterator<Int> = MyIterator()
class MyIterator(): Iterator<Int> {
var k : Int = 5
override fun next() : Int = k--
override fun hasNext() : Boolean = k > 0
}
}
class MyCollection3() {
operator fun iterator() = MyIterator()
class MyIterator() {
var k : Int = 5
operator fun next() : Int? = k--
operator fun hasNext() : Boolean = k > 0
}
}
class MyCollection4() {
operator fun iterator() = MyIterator()
class MyIterator() {
var k : Int = 5
operator fun next() : Int = k--
operator fun hasNext() = k > 0
}
}
@@ -0,0 +1,8 @@
operator fun Int.contains(i : Int) = true
fun box(): String {
when (1) {
in 2 -> return "OK"
else -> return "fail"
}
}
@@ -0,0 +1,20 @@
var field: Int = 0
fun next(): Int {
return ++field
}
fun box(): String {
val task: String
do {
if (next() % 2 == 0) {
task = "OK"
break
}
}
while (true)
return task
}
@@ -0,0 +1,20 @@
var field: Int = 0
fun next(): Int {
return ++field
}
fun box(): String {
val task: String
do {
if (next() % 2 == 0) {
task = "OK"
break
}
}
while (!false)
return task
}
@@ -0,0 +1,16 @@
class Foo {
var rnd = 10
public override fun equals(that : Any?) : Boolean = that is Foo && (that.rnd == rnd)
}
fun box() : String {
val a = Foo()
val b = Foo()
if (a !== a) return "fail 1"
if (b !== b) return "fail 2"
if (b === a) return "fail 3"
if (a === b) return "fail 4"
if( a !=b ) return "fail5"
return "OK"
}
@@ -0,0 +1,10 @@
fun box(): String {
try {
} catch (e: Exception) {
inlineFunctionWithDefaultArguments(e)
}
return "OK"
}
inline fun inlineFunctionWithDefaultArguments(t: Throwable? = null, bug: Boolean = true) =
Unit
@@ -0,0 +1,10 @@
fun box(): String {
var s = ""
try {
throw RuntimeException()
} catch (e : RuntimeException) {
} finally {
s += "OK"
}
return s
}
@@ -0,0 +1,7 @@
fun box(): String {
val x = 2
return when(x) {
in (1..3) -> "OK"
else -> "fail"
}
}
@@ -0,0 +1,5 @@
fun box(): String {
if (1 != 0) {
}
return "OK"
}
@@ -0,0 +1,9 @@
class Foo {
fun isOk() = true
}
fun box(): String {
val foo: Foo? = Foo()
if (foo?.isOk()!!) return "OK"
return "fail"
}
@@ -0,0 +1,10 @@
fun main(args: Array<String>) {
try {
} finally {
try {
} catch (e: Throwable) {
}
}
}
fun box() = "OK"
@@ -0,0 +1,6 @@
fun box(): String {
1 in 1.rangeTo(10)
1..10
'h' in 'A'.rangeTo('Z')
return "OK"
}
@@ -0,0 +1,44 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun main(args: Array<String>?) {
val y: Unit = Unit //do not compile
A<Unit>() //do not compile
C<Unit>(Unit) //do not compile
//do not compile
System.out?.println(fff<Unit>(Unit)) //do not compile
System.out?.println(id<Unit>(y)) //do not compile
System.out?.println(fff<Unit>(id<Unit>(y)) == id<Unit>(foreach(arrayOfNulls<Int>(0) as Array<Int>,{ e : Int -> }))) //do not compile
}
class A<T>()
class C<T>(val value: T) {
fun foo(): T = value
}
fun <T> fff(x: T) : T { return x }
fun <T> id(value: T): T = value
fun foreach(array: Array<Int>, action: (Int)-> Unit) {
for (el in array) {
action(el) //exception through compilation (see below)
}
}
fun almostFilter(array: Array<Int>, action: (Int)-> Int) {
for (el in array) {
action(el)
}
}
fun box() : String {
val a = arrayOfNulls<Int>(3) as Array<Int>
a[0] = 0
a[1] = 1
a[2] = 2
foreach(a, { el : Int -> System.out?.println(el) })
almostFilter(a, { el : Int -> el })
main(null)
return "OK"
}
@@ -0,0 +1,16 @@
fun box(): String {
9 in 0..9
val intRange = 0..9
9 in intRange
val charRange = '0'..'9'
'9' in charRange
val byteRange = 0.toByte()..9.toByte()
// seems no stdlib available here, thus no contains as extension
9 in byteRange
val longRange = 0.toLong()..9.toLong()
9.toLong() in longRange
val shortRange = 0.toShort()..9.toShort()
9 in shortRange
return "OK"
}
@@ -0,0 +1,53 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FULL_JDK
import java.util.LinkedList
fun ok1(): Boolean {
val queue = LinkedList(listOf(1, 2, 3))
while (!queue.isEmpty()) {
queue.poll()
for (y in 1..3) {
if (queue.contains(y)) {
return true
}
}
}
return false
}
fun ok2(): Boolean {
val queue = LinkedList(listOf(1, 2, 3))
val array = arrayOf(1, 2, 3)
while (!queue.isEmpty()) {
queue.poll()
for (y in array) {
if (queue.contains(y)) {
return true
}
}
}
return false
}
fun ok3(): Boolean {
val queue = LinkedList(listOf(1, 2, 3))
while (!queue.isEmpty()) {
queue.poll()
var x = 0
do {
x++
if (x == 2) return true
} while (x < 2)
}
return false
}
fun box(): String {
if (!ok1()) return "Fail #1"
if (!ok2()) return "Fail #2"
if (!ok3()) return "Fail #3"
return "OK"
}
@@ -0,0 +1,12 @@
fun foo(): Int {
try {
} finally {
try {
return 1
} catch (e: Throwable) {
return 2
}
}
}
fun box() = if (foo() == 1) "OK" else "Fail"
@@ -0,0 +1,10 @@
fun box(): String {
var i = 0
{
if (1 == 1) {
i++
} else {
}
}()
return "OK"
}
@@ -0,0 +1,21 @@
class MyRange1() : ClosedRange<Int> {
override val start: Int
get() = 0
override val endInclusive: Int
get() = 0
override fun contains(item: Int) = true
}
class MyRange2() {
operator fun contains(item: Int) = true
}
fun box(): String {
if (1 in MyRange1()) {
if (1 in MyRange2()) {
return "OK"
}
return "fail 2"
}
return "fail 1"
}
@@ -0,0 +1,26 @@
fun putNumberCompareAsUnit() {
if (1 == 1) {
}
else if (1 == 1) {
}
}
fun putNumberCompareAsVoid() {
if (1 == 1) {
1 == 1
} else {
}
}
fun putInvertAsUnit(b: Boolean) {
if (1 == 1) {
} else if (!b) {
}
}
fun box(): String {
putNumberCompareAsUnit()
putNumberCompareAsVoid()
putInvertAsUnit(true)
return "OK"
}
@@ -0,0 +1,19 @@
fun testIf() {
val condition = true
val result = if (condition) {
val hello: String? = "hello"
if (hello == null) {
false
}
else {
true
}
}
else true
if (!result) throw AssertionError("result is false")
}
fun box(): String {
testIf()
return "OK"
}
@@ -0,0 +1,20 @@
fun check1() {
val result = if (true) {
if (true) 1 else 2
}
else 3
if (result != 1) throw AssertionError("result: $result")
}
fun check2() {
val result = if (true)
if (true) 1 else 2
else 3
if (result != 1) throw AssertionError("result: $result")
}
fun box(): String {
check1()
check2()
return "OK"
}
@@ -0,0 +1,21 @@
fun printlnMock(a: Any) {}
public fun testCoalesce() {
val value: String = when {
true -> {
if (true) {
"foo"
} else {
"bar"
}
}
else -> "Hello world"
}
printlnMock(value.length)
}
fun box(): String {
testCoalesce()
return "OK"
}
@@ -0,0 +1,24 @@
fun foo() {
var x = 0
do {
x++
var y = x + 5
} while (y < 10)
if (x != 5) throw AssertionError("$x")
}
fun bar() {
var b = false
do {
var x = "X"
var y = "Y"
b = true
} while (x + y != "XY")
if (!b) throw AssertionError()
}
fun box(): String {
foo()
bar()
return "OK"
}
@@ -0,0 +1,12 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun nil() = null
fun list() = java.util.Arrays.asList("1")
fun box(): String {
for (x in nil()?:list()) {
}
return "OK"
}
@@ -0,0 +1,4 @@
fun box() : String {
var a = 10
return if(a?.plus(10) == 20) "OK" else "fail"
}
@@ -0,0 +1,24 @@
// WITH_RUNTIME
class A() {
infix fun <T> ArrayList<T>.add3(el: T) = add(el)
fun test(list: ArrayList<Int>) {
for (i in 1..10) {
list add3 i
}
}
}
infix fun <T> ArrayList<T>.add2(el: T) = add(el)
fun box() : String{
var list = ArrayList<Int>()
for (i in 1..10) {
list.add(i)
list add2 i
}
A().test(list)
println(list)
return "OK"
}
@@ -0,0 +1,48 @@
class A() {
fun action() = "OK"
infix fun infix(a: String) = "O" + a
val property = "OK"
val a : A
get() = A()
}
fun test1() = A()!!.property
fun test2() = (A() as A?)!!.property
fun test3() = A()!!.action()
fun test4() = (A() as A?)!!.action()
fun test5() = (null as A?)!!.action()
fun test6() = A().a.a!!.action()
fun test7() = 10!!.plus(11)
fun test8() = (10 as Int?)!!.plus(11)
fun test9() = A()!! infix "K"
fun test10() = (A() as A?) !! infix "K"
fun test11() = (A() as A?) !! infix("K")
fun test12() = A()!! infix ("K")
fun box() : String {
if(test1() != "OK") return "fail"
if(test2() != "OK") return "fail"
if(test3() != "OK") return "fail"
if(test4() != "OK") return "fail"
try {
test5()
return "fail"
}
catch(e: NullPointerException) { //
}
if(test6() != "OK") return "fail"
if(test7() != 21) return "fail"
if(test8() != 21) return "fail"
if(test9() != "OK") return "fail"
if(test10() != "OK") return "fail"
if(test11() != "OK") return "fail"
if(test12() != "OK") return "fail"
return "OK"
}
@@ -0,0 +1,14 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
package w_range
fun box() : String {
var i = 0
when (i) {
1 -> i--
else -> { i = 2 }
}
System.out?.println(i)
return "OK"
}
@@ -0,0 +1,25 @@
package demo2
fun print(o : Any?) {}
fun test(i : Int) {
var monthString : String? = "<empty>"
when (i) {
1 -> {
print(1)
print(2)
print(3)
print(4)
print(5)
}
else -> {
monthString = "Invalid month"
}
}
print(monthString)
}
fun box() : String {
for (i in 1..12) test(i)
return "OK"
}
@@ -0,0 +1,25 @@
package demo2
fun print(o : Any?) {}
fun test(i : Int) {
var monthString : String? = "<empty>"
when (i) {
1 -> {
print(1)
print(2)
print(3)
print(4)
print(5)
}
else -> {
monthString = "Invalid month"
}
}
print(monthString)
}
fun box() : String {
for (i in 1..12) test(i)
return "OK"
}
@@ -0,0 +1,32 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
class A(var value: String)
fun box(): String {
val a = A("start")
try {
test(a)
} catch(e: java.lang.RuntimeException) {
}
if (a.value != "start, try, finally1, finally2") return "fail: ${a.value}"
return "OK"
}
fun test(a: A) : String {
try {
try {
a.value += ", try"
return a.value
} finally {
a.value += ", finally1"
}
} finally {
a.value += ", finally2"
throw RuntimeException("fail")
}
}
@@ -0,0 +1,36 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
class A(var value: String)
fun box(): String {
val a = A("start")
try {
test(a)
} catch(e: java.lang.RuntimeException) {
}
if (a.value != "start, try, finally1, finally2") return "fail: ${a.value}"
return "OK"
}
fun test(a: A) : String {
while (true) {
try {
try {
a.value += ", try"
break
}
finally {
a.value += ", finally1"
}
}
finally {
a.value += ", finally2"
throw RuntimeException("fail")
}
}
}
@@ -0,0 +1,37 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
class A(var value: String)
fun box(): String {
val a = A("start")
try {
test(a)
} catch(e: java.lang.RuntimeException) {
}
if (a.value != "start, try, finally1, finally2") return "fail: ${a.value}"
return "OK"
}
fun test(a: A) : String {
while (a.value == "start") {
try {
try {
a.value += ", try"
continue
}
finally {
a.value += ", finally1"
}
}
finally {
a.value += ", finally2"
throw RuntimeException("fail")
}
}
return "fail"
}
@@ -0,0 +1,5 @@
fun box() = when {
1 > 2 -> "false"
1 >= 1 -> "OK"
else -> "else"
}
@@ -0,0 +1,29 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun testOr(b: Boolean): Boolean {
return b || return !b;
}
fun testOr(): Boolean {
return true || return false;
}
fun testAnd(b: Boolean): Boolean {
return b && return !b;
}
fun testAnd(): Boolean {
return true && return false;
}
fun box(): String {
if (testOr(false) != true) return "fail 1"
if (testOr(true) != true) return "fail 2"
if (testAnd(false) != false) return "fail 3"
if (testAnd(true) != false) return "fail 4"
if (testOr() != true) return "fail 5"
if (testAnd() != false) return "fail 6"
return "OK"
}
@@ -0,0 +1,12 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun box(): String {
var cycle = true;
while (true) {
if (true || throw java.lang.RuntimeException()) {
return "OK"
}
}
return "fail"
}
@@ -0,0 +1,21 @@
fun foo() : Int =
try {
2
}
finally {
"s"
}
fun bar(set : MutableSet<Int>) : Set<Int> =
try {
set
}
finally {
set.add(42)
}
fun box() : String {
if (foo() != 2) return "fail 1"
val s = bar(HashSet<Int>())
return if (s.contains(42)) "OK" else "fail 2"
}
@@ -0,0 +1,3 @@
fun test() = 239
fun box() = if(test() in 239..240) "OK" else "fail"
@@ -0,0 +1,8 @@
fun box(): String {
val r = 1.toLong()..2
var s = ""
for (l in r) {
s += l
}
return if (s == "12") "OK" else "fail: $s"
}
@@ -0,0 +1,41 @@
fun IntArray.swap(i:Int, j:Int) {
val temp = this[i]
this[i] = this[j]
this[j] = temp
}
fun IntArray.quicksort() = quicksort(0, size-1)
fun IntArray.quicksort(L: Int, R:Int) {
val m = this[(L + R) / 2]
var i = L
var j = R
while (i <= j) {
while (this[i] < m)
i++
while (this[j] > m)
j--
if (i <= j) {
swap(i++, j--)
}
else {
}
}
if (L < j)
quicksort(L, j)
if (R > i)
quicksort(i, R)
}
fun box() : String {
val a = IntArray(10)
for(i in 0..4) {
a[2*i] = 2*i
a[2*i+1] = -2*i-1
}
a.quicksort()
for(i in 0..a.size-2) {
if (a[i] > a[i+1]) return "Fail $i: ${a[i]} > ${a[i+1]}"
}
return "OK"
}
@@ -0,0 +1,14 @@
var flag = true
fun exit(): Nothing = null!!
fun box(): String {
val a: String
if (flag) {
a = "OK"
}
else {
exit()
}
return a
}
@@ -0,0 +1,12 @@
inline fun exit(): Nothing = null!!
fun box(): String {
val a: String
try {
a = "OK"
}
catch (e: Exception) {
exit()
}
return a
}
@@ -0,0 +1,16 @@
var flag = true
object Test {
val magic: Nothing get() = null!!
}
fun box(): String {
val a: String
if (flag) {
a = "OK"
}
else {
Test.magic
}
return a
}
@@ -0,0 +1,12 @@
fun exit(): Nothing = null!!
fun box(): String {
val a: String
try {
a = "OK"
}
catch (e: Exception) {
exit()
}
return a
}
@@ -0,0 +1,14 @@
fun exit(): Nothing = null!!
var x = 0
fun box(): String {
val a: String
when (x) {
0 -> a = "OK"
1 -> a = "???"
2 -> exit()
else -> exit()
}
return a
}
@@ -0,0 +1,19 @@
fun box() : String {
try {
} finally {
try {
try {
} finally {
try {
} finally {
}
}
} catch (e: Exception) {
try {
} catch (f: Exception) {
} finally {
}
}
return "OK"
}
}
@@ -0,0 +1,2 @@
fun box(): String =
"O" + try { throw Exception("oops!") } catch (e: Exception) { "K" }
@@ -0,0 +1,52 @@
fun cleanup() {}
inline fun concat(x: String, y: String): String = x + y
inline fun throws() {
try {
throw Exception()
}
finally {
cleanup()
}
}
inline fun first(x: String, y: String): String = x
fun box(): String =
"" + concat(
try { "" } finally { "0" },
"" + concat(
first(
try {
try {
"O"
}
finally {
"1"
}
}
catch (e: Exception) {
throw e
}
finally {
cleanup()
},
"2"
),
first(
try {
throws()
throw Exception()
"3"
}
catch (e: Exception) {
"K"
}
finally {
cleanup()
},
"4"
)
)
)
@@ -0,0 +1,26 @@
inline fun catchAll(x: String, block: () -> Unit): String {
try {
block()
} catch (e: Throwable) {
}
return x
}
inline fun tryTwice(block: () -> Unit) {
try {
block()
try {
block()
} catch (e: Exception) {
}
} catch (e: Exception) {
}
}
fun box(): String {
return catchAll("OK") {
tryTwice {
throw Exception()
}
}
}
@@ -0,0 +1,11 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun foo(b: Byte, s: String, i: Int, d: Double, li: Long): String = "$b $s $i $d $li"
fun box(): String {
val test = foo(1, "abc", 1, 1.0, try { 1L } catch (e: Exception) { 10L })
if (test != "1 abc 1 1.0 1") return "Failed, test==$test"
return "OK"
}
@@ -0,0 +1,35 @@
public inline fun fails(block: () -> Unit): Throwable? {
var thrown: Throwable? = null
try {
block()
} catch (e: Throwable) {
thrown = e
}
if (thrown == null)
throw Exception("Expected an exception to be thrown")
return thrown
}
public inline fun throwIt(msg: String) {
throw Exception(msg)
}
fun box(): String {
fails {
throwIt("oops!")
}
var x = 0
try {
fails {
x = 1
}
}
catch (e: Exception) {
x = 2
}
if (x != 2) return "Failed: x==$x"
return "OK"
}
@@ -0,0 +1,2 @@
fun box(): String =
"O" + try { "K" } finally { "hmmm" }
@@ -0,0 +1,17 @@
inline fun <T> tryOrElse(f1: () -> T, f2: () -> T): T {
try {
return f1()
}
catch (e: Exception) {
return f2()
}
}
fun testIt() = "abc" + tryOrElse({ "def" }, { "oops" }) + "ghi"
fun box(): String {
val test = testIt()
if (test != "abcdefghi") return "Failed, test==$test"
return "OK"
}
@@ -0,0 +1,14 @@
inline fun <T> tryOrElse(f1: () -> T, f2: () -> T): T =
try { f1() } catch (e: Exception) { f2() }
fun testIt() =
"abc" +
tryOrElse({ try { "def" } catch(e: Exception) { "oops!" } }, { "hmmm..." }) +
"ghi"
fun box(): String {
val test = testIt()
if (test != "abcdefghi") return "Failed, test==$test"
return "OK"
}
@@ -0,0 +1,22 @@
inline fun <T> tryAndThen(f1: () -> Unit, f2: () -> Unit, f3: () -> T): T {
try {
f1()
}
catch (e: Exception) {
f2()
}
finally {
return f3()
}
}
fun testIt() = "abc" +
tryAndThen({}, {}, { "def" }) +
"ghi"
fun box(): String {
val test = testIt()
if (test != "abcdefghi") return "Failed, test==$test"
return "OK"
}
@@ -0,0 +1,30 @@
interface Callable {
fun call(b: Boolean)
}
inline fun run(f: () -> Unit) { f() }
class A {
fun foo(): String {
run {
val x = object : Callable {
override fun call(b: Boolean) {
if (b) {
x()
} else {
try {
x()
} catch(t: Throwable) {
}
}
}
}
}
return "OK"
}
private fun x() {}
}
fun box(): String =
A().foo()
@@ -0,0 +1,21 @@
inline fun doCall(f: () -> Any) = f()
fun test1() {
val localResult = doCall {
try { "1" } catch (e: Exception) { "2" }
return
}
}
fun test2(): String {
val localResult = doCall {
try { "1" } catch (e: Exception) { "2" }
return@test2 "OK"
}
return "Hmmm..."
}
fun box(): String {
test1()
return test2()
}
@@ -0,0 +1,20 @@
class Exception1(msg: String): Exception(msg)
class Exception2(msg: String): Exception(msg)
class Exception3(msg: String): Exception(msg)
fun box(): String =
"O" + try {
throw Exception3("K")
}
catch (e1: Exception1) {
"e1"
}
catch (e2: Exception2) {
"e2"
}
catch (e3: Exception3) {
e3.message
}
catch (e: Exception) {
"e"
}
@@ -0,0 +1,25 @@
inline fun test(s: () -> Int): Int =
try {
val i = s()
i + 10
}
finally {
0
}
fun box() : String {
test {
try {
val p = 1
return "OK"
}
catch(e: Exception) {
-2
}
finally {
-3
}
}
return "Failed"
}
@@ -0,0 +1,11 @@
fun shouldReturnFalse() : Boolean {
try {
return true
} finally {
if (true)
return false
}
}
fun box(): String =
if (shouldReturnFalse()) "Failed" else "OK"

Some files were not shown because too many files have changed in this diff Show More