Move blackBoxFile() testData to box/ directory
Delete all test methods (and empty test classes), since they'll be auto-generated
This commit is contained in:
committed by
Alexander Udalov
parent
ecbb2f10ef
commit
41a416da60
@@ -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,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 = java.util.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 = java.util.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 = java.util.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 = java.util.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 = java.util.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 = Array<String>(10) {i -> "$i"}
|
||||
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,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 = java.util.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 = java.util.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 = java.util.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 = java.util.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 = java.util.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 = Array<String>(10) {i -> "$i"}
|
||||
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,14 @@
|
||||
var result = "Fail"
|
||||
|
||||
fun foo() {
|
||||
try {
|
||||
return
|
||||
} finally {
|
||||
result = "OK"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
foo()
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fun f(x: Any?): String {
|
||||
if (x is Array<String>) {
|
||||
for (i in x) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return "FAIL"
|
||||
}
|
||||
|
||||
fun box(): String = f(Array<String>(1, {"OK"}))
|
||||
@@ -0,0 +1,14 @@
|
||||
fun box() : String {
|
||||
val a = Array<Int> (5, {0})
|
||||
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,13 @@
|
||||
fun box() : String {
|
||||
val a = arrayOfNulls<String>(3)
|
||||
a[0] = "a"
|
||||
a[1] = "b"
|
||||
a[2] = "c"
|
||||
|
||||
var result = 0
|
||||
for(i in a.indices) {
|
||||
result += i
|
||||
}
|
||||
if (result != 3) return "FAIL"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
class It {
|
||||
}
|
||||
|
||||
class C {
|
||||
}
|
||||
|
||||
class X {
|
||||
var hasNext = true
|
||||
fun It.hasNext() = if (hasNext) {hasNext = false; true} else false
|
||||
fun It.next() = 5
|
||||
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 {
|
||||
fun next() = 5
|
||||
}
|
||||
|
||||
class C {
|
||||
fun iterator(): It = It()
|
||||
}
|
||||
|
||||
class X {
|
||||
var hasNext = true
|
||||
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,26 @@
|
||||
class It {
|
||||
var hasNext = true
|
||||
fun hasNext() = if (hasNext) {hasNext = false; true} else false
|
||||
}
|
||||
|
||||
class C {
|
||||
fun iterator(): It = It()
|
||||
}
|
||||
|
||||
class X {
|
||||
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,114 @@
|
||||
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> = Array<Int> (5, {0})
|
||||
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() {
|
||||
fun iterator() = MyIterator()
|
||||
|
||||
class MyIterator() {
|
||||
var k : Int = 5
|
||||
|
||||
fun next() : Int? = k--
|
||||
fun hasNext() : Boolean = k > 0
|
||||
}
|
||||
}
|
||||
|
||||
class MyCollection4() {
|
||||
fun iterator() = MyIterator()
|
||||
|
||||
class MyIterator() {
|
||||
var k : Int = 5
|
||||
|
||||
fun next() : Int = k--
|
||||
fun hasNext() = k > 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun Int.contains(i : Int) = true
|
||||
|
||||
fun box(): String {
|
||||
when (1) {
|
||||
in 2 -> return "OK"
|
||||
else -> return "fail"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
class Foo {
|
||||
var rnd = 10
|
||||
|
||||
public fun equals(that : Any) : Boolean = that is Foo && (that.rnd == rnd)
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
val a = Foo()
|
||||
val b = Foo()
|
||||
if (!a.identityEquals(a)) return "fail 1"
|
||||
if (!b.identityEquals(b)) return "fail 2"
|
||||
if (b.identityEquals(a)) return "fail 3"
|
||||
if (a.identityEquals(b)) return "fail 4"
|
||||
if( a !=b ) return "fail5"
|
||||
return "OK"
|
||||
}
|
||||
@@ -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,5 @@
|
||||
fun box(): String {
|
||||
val a = if(true) {
|
||||
}
|
||||
return if (a.toString() == "Unit.VALUE") "OK" else "fail"
|
||||
}
|
||||
@@ -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,41 @@
|
||||
fun main(args: Array<String>?) {
|
||||
val y: Unit = Unit.VALUE //do not compile
|
||||
A<Unit>() //do not compile
|
||||
C<Unit>(Unit.VALUE) //do not compile
|
||||
//do not compile
|
||||
System.out?.println(fff<Unit>(Unit.VALUE)) //do not compile
|
||||
System.out?.println(id<Unit>(y)) //do not compile
|
||||
System.out?.println(fff<Unit>(id<Unit>(y)) == id<Unit>(foreach(Array<Int>(0,{0}),{(e : Int) : Unit -> }))) //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 = Array<Int> (3,{-1})
|
||||
a[0] = 0
|
||||
a[1] = 1
|
||||
a[2] = 2
|
||||
foreach(a, { (el : Int) : Unit -> System.out?.println(el) })
|
||||
almostFilter(a, { (el : Int) : Int -> el })
|
||||
main(null)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
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()
|
||||
9.toByte() in byteRange
|
||||
val longRange = 0.toLong()..9.toLong()
|
||||
9.toLong() in longRange
|
||||
val shortRange = 0.toShort()..9.toShort()
|
||||
9.toShort() in shortRange
|
||||
|
||||
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,9 @@
|
||||
fun foo(condition: Boolean): String {
|
||||
val u = if (condition) {
|
||||
"OK"
|
||||
} else {
|
||||
}
|
||||
return u.toString()
|
||||
}
|
||||
|
||||
fun box() = foo(true)
|
||||
@@ -0,0 +1,17 @@
|
||||
class MyRange1() : Range<Int> {
|
||||
override fun contains(item: Int) = true
|
||||
}
|
||||
|
||||
class MyRange2() {
|
||||
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,4 @@
|
||||
fun box() : String {
|
||||
var a = 10
|
||||
return if(a?.plus(10) == 20) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import java.util.*
|
||||
|
||||
class A() {
|
||||
fun <T> ArrayList<T>.add3(el: T) = add(el)
|
||||
|
||||
fun test(list: ArrayList<Int>) {
|
||||
for (i in 1..10) {
|
||||
list add3 i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
System.out?.println(list)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
class A() {
|
||||
fun action() = "OK"
|
||||
|
||||
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,11 @@
|
||||
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,5 @@
|
||||
fun box() = when {
|
||||
1 > 2 -> "false"
|
||||
1 >= 1 -> "OK"
|
||||
else -> "else"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import java.util.HashSet
|
||||
|
||||
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,34 @@
|
||||
fun findPairless(a : IntArray) : Int {
|
||||
@loop for (i in a.indices) {
|
||||
for (j in a.indices) {
|
||||
if (i != j && a[i] == a[j]) continue@loop
|
||||
}
|
||||
return a[i]
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
fun hasDuplicates(a : IntArray) : Boolean {
|
||||
var duplicate = false
|
||||
@loop for (i in a.indices) {
|
||||
for (j in a.indices) {
|
||||
if (i != j && a[i] == a[j]) {
|
||||
duplicate = true
|
||||
break@loop
|
||||
}
|
||||
}
|
||||
}
|
||||
return duplicate
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
val a = IntArray(5)
|
||||
a[0] = 0
|
||||
a[1] = 0
|
||||
a[2] = 1
|
||||
a[3] = 1
|
||||
a[4] = 5
|
||||
if(findPairless(a) != 5) return "fail"
|
||||
return if(hasDuplicates(a)) "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,19 @@
|
||||
fun box() : String {
|
||||
try {
|
||||
} finally {
|
||||
try {
|
||||
try {
|
||||
} finally {
|
||||
try {
|
||||
} finally {
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
try {
|
||||
} catch (f: Exception) {
|
||||
} finally {
|
||||
}
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user