JS: move expressions test to box tests
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package kotlin
|
||||
|
||||
// CHECK_NOT_REFERENCED: Kotlin.isInstanceOf
|
||||
// CHECK_NOT_REFERENCED: Kotlin.isTypeOf
|
||||
// CHECK_NOT_REFERENCED: Kotlin.orNull
|
||||
|
||||
fun success(message: String, fn: ()->Unit) {
|
||||
try {
|
||||
fn()
|
||||
}
|
||||
catch (e: Exception) {
|
||||
throw Exception("Exception was thrown: message=$message, exception=$e")
|
||||
}
|
||||
}
|
||||
|
||||
fun failsClassCast(message: String, fn: ()->Unit) {
|
||||
try {
|
||||
fn()
|
||||
}
|
||||
catch (e: ClassCastException) {
|
||||
return
|
||||
}
|
||||
|
||||
throw Exception("Expected ClassCastException to be thrown: message=$message")
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.KMutableProperty1
|
||||
|
||||
class A
|
||||
|
||||
var A.x: Int
|
||||
get() = 23
|
||||
set(value) { }
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(true, (A::x as Any) is KProperty1<*, *>)
|
||||
assertEquals(true, (A::x as Any) is KMutableProperty1<*, *>)
|
||||
assertEquals(23, ((A::x as Any) as KProperty1<A, Any>)(A()))
|
||||
assertEquals(23, ((A::x as Any) as KMutableProperty1<A, Any>)(A()))
|
||||
assertEquals(false, (23 as Any) is KMutableProperty1<*, *>)
|
||||
assertEquals(false, ({ A().x } as Any) is KMutableProperty1<*, *>)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty1
|
||||
|
||||
class A
|
||||
|
||||
val A.x: Int
|
||||
get() = 23
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(true, (A::x as Any) is KProperty1<*, *>)
|
||||
assertEquals(23, ((A::x as Any) as KProperty1<A, Any>)(A()))
|
||||
assertEquals(false, (23 as Any) is KProperty1<*, *>)
|
||||
assertEquals(false, ({ A().x } as Any) is KProperty1<*, *>)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package foo
|
||||
|
||||
class A
|
||||
|
||||
fun box(): String {
|
||||
val nil: A? = null
|
||||
val a = A()
|
||||
|
||||
success("10 as Any") { assertEquals<Any>(10, 10 as Any) }
|
||||
success("\"abc\" as Any") { assertEquals<Any>("abc", "abc" as Any) }
|
||||
success("\"abc\" as Any") { assertEquals<Any>(true, true as Any) }
|
||||
val array = arrayOf(1, 2)
|
||||
success("arrayOf(1, 2) as Any") { assertEquals<Any>(array, array as Any) }
|
||||
success("{ 0 } as Any") { { 0 } as Any }
|
||||
success("a as Any") { assertEquals<Any>(a, a as Any) }
|
||||
success("object{} as Any") { object{} as Any }
|
||||
failsClassCast("nil as Any") { nil as Any }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val ints: Any? = arrayOf(1, 2)
|
||||
val strings: Any? = arrayOf("a", "b")
|
||||
val nil: Any? = null
|
||||
val obj: Any? = object{}
|
||||
|
||||
success("ints") { ints as Array<*> }
|
||||
success("strings") { strings as Array<*> }
|
||||
failsClassCast("null") { nil as Array<*> }
|
||||
failsClassCast("obj") { obj as Array<*> }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun test(fn: Any?): Function0<Int> =
|
||||
fn as Function0<Int>
|
||||
|
||||
fun box(): String {
|
||||
val get11: Any? = { 11 }
|
||||
assertEquals(11, test(get11)(), "get11")
|
||||
failsClassCast("null") { test(null)() }
|
||||
failsClassCast("object {}") { test(object {})() }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package foo
|
||||
|
||||
class A(val s: String)
|
||||
|
||||
fun <T> castsNotNullToNullableT(a: Any) {
|
||||
a as T
|
||||
a as T?
|
||||
a as? T
|
||||
a as? T?
|
||||
}
|
||||
|
||||
fun <T> castsNullableToNullableT(a: Any?) {
|
||||
a as T
|
||||
a as T?
|
||||
a as? T
|
||||
a as? T?
|
||||
}
|
||||
|
||||
|
||||
fun <T : Any> castsNotNullToNotNullT(a: Any) {
|
||||
a as T
|
||||
a as T?
|
||||
a as? T
|
||||
a as? T?
|
||||
}
|
||||
|
||||
fun <T : Any> castNullableToNotNullT(a: Any?) {
|
||||
a as T
|
||||
}
|
||||
|
||||
fun <T : Any> castsNullableToNotNullT(a: Any?) {
|
||||
a as T?
|
||||
a as? T
|
||||
a as? T?
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A("OK")
|
||||
|
||||
success("castsNotNullToNullableT<A>(a)") { castsNotNullToNullableT<A>(a) }
|
||||
success("castsNullableToNullableT<A>(a)") { castsNullableToNullableT<A>(a) }
|
||||
success("castsNullableToNullableT<A>(null)") { castsNullableToNullableT<A>(null) }
|
||||
success("castsNotNullToNotNullT<A>(a)") { castsNotNullToNotNullT<A>(a) }
|
||||
success("castsNullableToNotNullT<A>(a)") { castsNullableToNotNullT<A>(a) }
|
||||
success("castsNullableToNotNullT<A>(null)") { castsNullableToNotNullT<A>(null) }
|
||||
success("castNullableToNotNullT<A>(a)") { castNullableToNotNullT<A>(a) }
|
||||
failsClassCast("castNullableToNotNullT<A>(null)") { castNullableToNotNullT<A>(null) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package foo
|
||||
|
||||
open class A()
|
||||
|
||||
interface X
|
||||
|
||||
interface Y
|
||||
|
||||
class B() : A(), X, Y {
|
||||
override fun toString() = "B"
|
||||
}
|
||||
|
||||
class C() : A(), X, Y {
|
||||
override fun toString() = "C"
|
||||
}
|
||||
|
||||
class D() : A() {
|
||||
override fun toString() = "D"
|
||||
}
|
||||
|
||||
class E() : X {
|
||||
override fun toString() = "E"
|
||||
}
|
||||
|
||||
class F() : A(), Y {
|
||||
override fun toString() = "E"
|
||||
}
|
||||
|
||||
fun <T> test(a: Any): String where T : A, T : X, T : Y {
|
||||
return (try {
|
||||
a as T
|
||||
}
|
||||
catch (e: Exception) {
|
||||
"error"
|
||||
}).toString()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val b = B()
|
||||
val c = C()
|
||||
val d = D()
|
||||
val e = E()
|
||||
val f = F()
|
||||
|
||||
assertEquals("B", test<B>(b))
|
||||
assertEquals("B", test<C>(b))
|
||||
|
||||
assertEquals("C", test<B>(c))
|
||||
assertEquals("C", test<C>(c))
|
||||
|
||||
assertEquals("error", test<B>(d))
|
||||
assertEquals("error", test<C>(d))
|
||||
assertEquals("error", test<B>(e))
|
||||
assertEquals("error", test<C>(e))
|
||||
assertEquals("error", test<B>(f))
|
||||
assertEquals("error", test<C>(f))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package foo
|
||||
|
||||
open class A
|
||||
class B : A()
|
||||
class C
|
||||
|
||||
fun <T : A> notNullToNotNullT(a: Any): T = a as T
|
||||
|
||||
fun <T : A> nullableToNotNullT(a: Any?): T = a as T
|
||||
|
||||
fun <T : A> notNullToNullableT(a: Any): T? = a as T?
|
||||
|
||||
fun <T : A> nullableToNullableT(a: Any?): T? = a as T?
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
val b = B()
|
||||
val c = C()
|
||||
|
||||
success("notNullToNotNullT<A>(a)") { assertEquals(a, notNullToNotNullT<A>(a)) }
|
||||
success("notNullToNotNullT<A>(b)") { assertEquals(b, notNullToNotNullT<A>(b)) }
|
||||
failsClassCast("notNullToNotNullT<A>(c)") { notNullToNotNullT<A>(c) }
|
||||
|
||||
success("nullableToNotNullT<A>(a)") {assertEquals(a, nullableToNotNullT<A>(a)) }
|
||||
success("nullableToNotNullT<A>(b)") {assertEquals(b, nullableToNotNullT<A>(b)) }
|
||||
failsClassCast("nullableToNotNullT<A>(c)") { nullableToNotNullT<A>(c) }
|
||||
failsClassCast("nullableToNotNullT<A>(null)") { nullableToNotNullT<A>(null) }
|
||||
|
||||
success("notNullToNullableT<A>(a)") { assertEquals(a, notNullToNullableT<A>(a))}
|
||||
success("notNullToNullableT<A>(b)") { assertEquals(b, notNullToNullableT<A>(b))}
|
||||
failsClassCast("notNullToNullableT<A>(c)") { notNullToNullableT<A>(c) }
|
||||
|
||||
success("nullableToNullableT<A>(a)") { assertEquals(a, nullableToNullableT<A>(a)) }
|
||||
success("nullableToNullableT<A>(b)") { assertEquals(b, nullableToNullableT<A>(b)) }
|
||||
success("nullableToNullableT<A>(null)") { assertEquals(null, nullableToNullableT<A>(null)) }
|
||||
failsClassCast("nullableToNullableT<A>(c)") { nullableToNullableT<A>(c) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty0
|
||||
import kotlin.reflect.KMutableProperty0
|
||||
|
||||
var x = 23
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(true, (::x as Any) is KProperty0<*>)
|
||||
assertEquals(true, (::x as Any) is KMutableProperty0<*>)
|
||||
assertEquals(23, ((::x as Any) as KProperty0<Any>)())
|
||||
assertEquals(23, ((::x as Any) as KMutableProperty0<Any>)())
|
||||
assertEquals(false, (23 as Any) is KMutableProperty0<*>)
|
||||
assertEquals(false, ({ x } as Any) is KMutableProperty0<*>)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.KMutableProperty1
|
||||
|
||||
class A {
|
||||
var x = 23
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(true, (A::x as Any) is KProperty1<*, *>)
|
||||
assertEquals(true, (A::x as Any) is KMutableProperty1<*, *>)
|
||||
assertEquals(23, ((A::x as Any) as KProperty1<A, Any>)(A()))
|
||||
assertEquals(23, ((A::x as Any) as KMutableProperty1<A, Any>)(A()))
|
||||
assertEquals(false, (23 as Any) is KProperty1<*, *>)
|
||||
assertEquals(false, (23 as Any) is KMutableProperty1<*, *>)
|
||||
assertEquals(false, ({ A().x } as Any) is KMutableProperty1<*, *>)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty0
|
||||
|
||||
val x = 23
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(true, (::x as Any) is KProperty0<*>)
|
||||
assertEquals(23, ((::x as Any) as KProperty0<Any>)())
|
||||
assertEquals(false, (23 as Any) is KProperty0<*>)
|
||||
assertEquals(false, ({ x } as Any) is KProperty0<*>)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty1
|
||||
|
||||
class A {
|
||||
val x = 23
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(true, (A::x as Any) is KProperty1<*, *>)
|
||||
assertEquals(23, ((A::x as Any) as KProperty1<A, Any>)(A()))
|
||||
assertEquals(false, (23 as Any) is KProperty1<*, *>)
|
||||
assertEquals(false, ({ A().x } as Any) is KProperty1<*, *>)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
interface A
|
||||
|
||||
class AImpl: A
|
||||
|
||||
fun test(x: Any?): A = x as A
|
||||
|
||||
fun box(): String {
|
||||
var a: A = AImpl()
|
||||
assertEquals(a, test(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test(a), "a = object : A{}")
|
||||
failsClassCast("test(null)") { test(null) }
|
||||
failsClassCast("test(object{})") { test(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
interface A
|
||||
|
||||
class AImpl : A {}
|
||||
|
||||
fun test(x: Any?): A? = x as A?
|
||||
|
||||
fun box(): String {
|
||||
var a: A? = AImpl()
|
||||
assertEquals(a, test(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test(a), "a = object : A{}")
|
||||
assertEquals(null, test(null), "test(null)")
|
||||
failsClassCast("test(object{})") { test(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var a: Long = 23
|
||||
var b: Long = 200 + 30
|
||||
var c: Long = id((2000).plus(300))
|
||||
|
||||
assertEquals(24L, a + 1)
|
||||
assertEquals(231L, b + 1)
|
||||
assertEquals(2301L, c + 1)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun id(x: Long) = x
|
||||
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED_IN_SCOPE: scope=box function=isType
|
||||
|
||||
open class A()
|
||||
|
||||
class B() : A()
|
||||
|
||||
fun box(): String {
|
||||
assertTrue(B() is A)
|
||||
assertTrue(B() is A?)
|
||||
assertTrue(B() is B)
|
||||
assertTrue(B() is B?)
|
||||
assertTrue((B() as B?) is A?)
|
||||
assertTrue((null as A?) is A?)
|
||||
|
||||
assertNotEquals(null, B() as? A)
|
||||
assertNotEquals(null, B() as? A?)
|
||||
assertNotEquals(null, B() as? B)
|
||||
assertNotEquals(null, B() as? B?)
|
||||
|
||||
assertNotEquals(null, B() as A)
|
||||
assertNotEquals(null, B() as A?)
|
||||
assertNotEquals(null, B() as B)
|
||||
assertNotEquals(null, B() as B?)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
interface A
|
||||
|
||||
class B
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(false, (23 as Any) is A)
|
||||
assertEquals(false, (23 as Any) is B)
|
||||
assertEquals(false, (23L as Any) is A)
|
||||
assertEquals(false, (23L as Any) is B)
|
||||
assertEquals(false, ("qwe" as Any) is A)
|
||||
assertEquals(false, ("qwe" as Any) is B)
|
||||
assertEquals(false, ({ 23 } as Any) is A)
|
||||
assertEquals(false, ({ 23 } as Any) is B)
|
||||
assertEquals(false, (true as Any) is A)
|
||||
assertEquals(false, (true as Any) is B)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: test
|
||||
|
||||
interface A
|
||||
|
||||
class AImpl: A
|
||||
|
||||
inline
|
||||
fun <reified T> test(x: Any?): T = x as T
|
||||
|
||||
fun box(): String {
|
||||
var a: A = AImpl()
|
||||
assertEquals(a, test<A>(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test<A>(a), "a = object : A{}")
|
||||
failsClassCast("test(null)") { test<A>(null) }
|
||||
failsClassCast("test(object{})") { test<A>(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
interface A
|
||||
|
||||
class AImpl : A {}
|
||||
|
||||
inline
|
||||
fun <reified T> test(x: Any?): T = x as T
|
||||
|
||||
fun box(): String {
|
||||
var a: A? = AImpl()
|
||||
assertEquals(a, test<A?>(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test<A?>(a), "a = object : A{}")
|
||||
assertEquals(null, test<A?>(null), "test(null)")
|
||||
failsClassCast("test(object{})") { test<A?>(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
interface A
|
||||
|
||||
class AImpl : A {}
|
||||
|
||||
inline
|
||||
fun <reified T> test(x: Any?): T? = x as T?
|
||||
|
||||
fun box(): String {
|
||||
var a: A? = AImpl()
|
||||
assertEquals(a, test<A>(a), "a = AImpl()")
|
||||
a = object : A {}
|
||||
assertEquals(a, test<A>(a), "a = object : A{}")
|
||||
assertEquals(null, test<A>(null), "test(null)")
|
||||
failsClassCast("test(object{})") { test<A>(object{}) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package foo
|
||||
|
||||
open class A
|
||||
class B : A()
|
||||
class C
|
||||
|
||||
fun <T : A> notNullToNullableT(a: Any): T? = a as? T?
|
||||
|
||||
fun <T : A> nullableToNullableT(a: Any?): T? = a as? T?
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
val b = B()
|
||||
val c = C()
|
||||
|
||||
success("notNullToNullableT<A>(a)") { assertEquals(a, notNullToNullableT<A>(a)) }
|
||||
success("notNullToNullableT<A>(b)") { assertEquals(b, notNullToNullableT<A>(b)) }
|
||||
success("notNullToNullableT<A>(c)") { assertEquals(null, notNullToNullableT<A>(c)) }
|
||||
|
||||
success("nullableToNullableT<A>(a)") { assertEquals(a, nullableToNullableT<A>(a)) }
|
||||
success("nullableToNullableT<A>(b)") { assertEquals(b, nullableToNullableT<A>(b)) }
|
||||
success("nullableToNullableT<A>(null)") { assertEquals(null, nullableToNullableT<A>(null)) }
|
||||
success("nullableToNullableT<A>(c)") { assertEquals(null, nullableToNullableT<A>(c)) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
class A
|
||||
class B
|
||||
|
||||
fun box(): String {
|
||||
val a: Any? = A()
|
||||
val nil: Any? = null
|
||||
val b: Any? = B()
|
||||
|
||||
assertEquals(a, a as? A, "a")
|
||||
assertEquals(null, nil as? A, "nil")
|
||||
assertEquals(null, b as? A, "b")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
class A
|
||||
class B
|
||||
|
||||
fun box(): String {
|
||||
val a: Any? = A()
|
||||
val nil: Any? = null
|
||||
val b: Any? = B()
|
||||
|
||||
assertEquals(a, a as? A?, "a")
|
||||
assertEquals(null, nil as? A?, "nil")
|
||||
assertEquals(null, b as? A?, "b")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: castTo
|
||||
|
||||
class A
|
||||
class B
|
||||
|
||||
inline
|
||||
fun <reified T> Any?.castTo(): T? = this as? T
|
||||
|
||||
fun box(): String {
|
||||
val a: Any? = A()
|
||||
val nil: Any? = null
|
||||
val b: Any? = B()
|
||||
|
||||
assertEquals(a, a.castTo<A>(), "a")
|
||||
assertEquals(null, nil.castTo<A>(), "nil")
|
||||
assertEquals(null, b.castTo<A>(), "b")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: castTo
|
||||
|
||||
class A
|
||||
class B
|
||||
|
||||
inline
|
||||
fun <reified T> Any?.castTo(): T? = this as? T?
|
||||
|
||||
fun box(): String {
|
||||
val a: Any? = A()
|
||||
val nil: Any? = null
|
||||
val b: Any? = B()
|
||||
|
||||
assertEquals(a, a.castTo<A>(), "a")
|
||||
assertEquals(null, nil.castTo<A>(), "nil")
|
||||
assertEquals(null, b.castTo<A>(), "b")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
fun foo(a: Int) = "A.foo($a)"
|
||||
}
|
||||
|
||||
fun Any.bar() = "Any.bar()"
|
||||
fun A.bar() = "A.bar()"
|
||||
|
||||
fun boo(a: Any) = "boo(Any)"
|
||||
fun boo(a: A) = "boo(A)"
|
||||
|
||||
fun Any.testInTopLevel() {
|
||||
assertEquals(bar(), "Any.bar()", "bar()")
|
||||
assertEquals(this.bar(), "Any.bar()", "this.bar()")
|
||||
assertEquals(boo(this), "boo(Any)", "boo(this)")
|
||||
|
||||
if (this is A) {
|
||||
assertEquals(foo(47), "A.foo(47)", "foo(47)")
|
||||
assertEquals(bar(), "A.bar()", "bar()")
|
||||
|
||||
assertEquals(this.foo(47), "A.foo(47)", "this.foo(47)")
|
||||
assertEquals(this.bar(), "A.bar()", "this.bar()")
|
||||
|
||||
assertEquals(boo(this), "boo(A)", "boo(this: A)")
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
fun Any.test() {
|
||||
assertEquals(bar(), "Any.bar()", "bar()")
|
||||
assertEquals(this.bar(), "Any.bar()", "this.bar()")
|
||||
assertEquals(boo(this), "boo(Any)", "boo(this)")
|
||||
|
||||
if (this is A) {
|
||||
assertEquals(foo(47), "A.foo(47)", "foo(47)")
|
||||
assertEquals(bar(), "A.bar()", "bar()")
|
||||
|
||||
assertEquals(this.foo(47), "A.foo(47)", "this.foo(47)")
|
||||
assertEquals(this.bar(), "A.bar()", "this.bar()")
|
||||
|
||||
assertEquals(boo(this), "boo(A)", "boo(this: A)")
|
||||
}
|
||||
}
|
||||
|
||||
fun testInClass() {
|
||||
A().test()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
A().testInTopLevel()
|
||||
B().testInClass()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
fun foo(a: Int) = "A.foo($a)"
|
||||
}
|
||||
|
||||
fun Any.bar() = "Any.bar()"
|
||||
fun A.bar() = "A.bar()"
|
||||
|
||||
fun boo(a: Any) = "boo(Any)"
|
||||
fun boo(a: A) = "boo(A)"
|
||||
|
||||
fun testInTopLevel(a: Any) {
|
||||
assertEquals(a.bar(), "Any.bar()", "bar()")
|
||||
assertEquals(a.bar(), "Any.bar()", "this.bar()")
|
||||
assertEquals(boo(a), "boo(Any)", "boo(this)")
|
||||
|
||||
if (a is A) {
|
||||
assertEquals(a.foo(47), "A.foo(47)", "a.foo(47)")
|
||||
assertEquals(a.bar(), "A.bar()", "a.bar()")
|
||||
assertEquals(boo(a), "boo(A)", "boo(a: A)")
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
fun testInClass(a: Any) {
|
||||
assertEquals(a.bar(), "Any.bar()", "bar()")
|
||||
assertEquals(a.bar(), "Any.bar()", "this.bar()")
|
||||
assertEquals(boo(a), "boo(Any)", "boo(this)")
|
||||
|
||||
if (a is A) {
|
||||
assertEquals(a.foo(47), "A.foo(47)", "a.foo(47)")
|
||||
assertEquals(a.bar(), "A.bar()", "a.bar()")
|
||||
assertEquals(boo(a), "boo(A)", "boo(a: A)")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testInTopLevel(A())
|
||||
B().testInClass(A())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package foo
|
||||
|
||||
class A(val value: Int) : Comparable<A> {
|
||||
override public fun compareTo(other: A): Int = other.value.compareTo(value)
|
||||
}
|
||||
|
||||
class B(val value: Int)
|
||||
|
||||
fun testExtensionFunctionAsCompareTo() {
|
||||
val compareTo: B.( B ) -> Int = { other -> other.value.compareTo(this.value) }
|
||||
|
||||
val x: B = B(100)
|
||||
val y: B = B(200)
|
||||
|
||||
assertEquals(1, x.compareTo(y), "ext fun: x compareTo y")
|
||||
}
|
||||
|
||||
fun testMethodAsCompareTo() {
|
||||
val x: A = A(100)
|
||||
val y: A = A(200)
|
||||
|
||||
assertEquals(false, x < y, "meth: x < y")
|
||||
assertEquals(true, x > y, "meth: x > y")
|
||||
assertEquals(1, x.compareTo(y), "meth: x compareTo y")
|
||||
|
||||
val comparable: Comparable<A> = x
|
||||
assertEquals(false, comparable < y, "meth: (x: Comparable<A>) < y")
|
||||
assertEquals(true, comparable > y, "meth: (x: Comparable<A>) > y")
|
||||
assertEquals(1, comparable.compareTo(y), "meth: (x: Comparable<A>) compareTo y")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
testExtensionFunctionAsCompareTo()
|
||||
|
||||
testMethodAsCompareTo()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package foo
|
||||
|
||||
fun MyController(`$scope`: String): String {
|
||||
return "Hello " + `$scope` + "!"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("Hello world!", MyController("world"))
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val a = arrayOf(1, 2, 3)
|
||||
val b = arrayOf(1, 2, 3)
|
||||
val c = a
|
||||
|
||||
if (a == b) return "fail1"
|
||||
if (a != c) return "fail2"
|
||||
if (c == b) return "fail3"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// KT-3518 Null pointer during null comparison in JS Backend
|
||||
package foo
|
||||
|
||||
class MyClazz(val nullableL : List<String>?)
|
||||
|
||||
fun box(): String {
|
||||
val a = MyClazz(null)
|
||||
if(a.nullableL != null) return "a.nullableL != null"
|
||||
|
||||
val b = MyClazz(listOf("somthing"))
|
||||
if(b.nullableL == null) return "b.nullableL == null"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
override fun equals(other: Any?) = this === other
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a: A? = null
|
||||
val b: A? = null
|
||||
val c: A? = A()
|
||||
val d: A? = A()
|
||||
val e: A = A()
|
||||
|
||||
// compare nullable vals with null
|
||||
testTrue { a == b }
|
||||
testTrue { a == a }
|
||||
testFalse { a != b }
|
||||
testFalse { a != a }
|
||||
|
||||
// compare null and non-null inside nullable vals
|
||||
testFalse { a == c }
|
||||
testTrue { a != c }
|
||||
testFalse { c == a }
|
||||
testTrue { c != a }
|
||||
|
||||
// compare nullables vals with non-null
|
||||
testFalse { c == d }
|
||||
testTrue { c == c }
|
||||
testTrue { c != d }
|
||||
testFalse { d == c }
|
||||
testTrue { d != c }
|
||||
testFalse { d != d }
|
||||
|
||||
// compare nullable val with null with non-nullable
|
||||
testFalse { a == e }
|
||||
testTrue { a != e }
|
||||
testFalse { e == a }
|
||||
testTrue { e != a }
|
||||
|
||||
// compare nullable val with non-null with non-nullable
|
||||
testFalse { c == e }
|
||||
testTrue { c != e }
|
||||
testFalse { e == c }
|
||||
testTrue { e != c }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
override fun equals(other: Any?) = super.equals(other)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a: A? = null
|
||||
|
||||
testTrue { a == null }
|
||||
testFalse { a != null }
|
||||
testTrue { null == a }
|
||||
testFalse { null != a }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package foo
|
||||
|
||||
class Foo(val name: String) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is Foo) {
|
||||
return false
|
||||
}
|
||||
return this.name == other.name
|
||||
}
|
||||
}
|
||||
|
||||
fun callEqualsMethod(v1: Foo?, v2: Foo?): Boolean {
|
||||
return v1 == v2
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = Foo("abc")
|
||||
val b = Foo("abc")
|
||||
val c = Foo("def")
|
||||
|
||||
if (!callEqualsMethod(a, b)) return "fail1"
|
||||
if (callEqualsMethod(a, c)) return "fail2"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package foo
|
||||
|
||||
class Foo(val name: String) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is Foo) {
|
||||
return false
|
||||
}
|
||||
return this.name == other.name
|
||||
}
|
||||
}
|
||||
|
||||
fun callEqualsMethod(v1: Any?, v2: Any?): Boolean {
|
||||
return v1 == v2
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = Foo("abc")
|
||||
val b = Foo("abc")
|
||||
val c = Foo("def")
|
||||
|
||||
if (!callEqualsMethod(a, b)) return "fail1"
|
||||
if (callEqualsMethod(a, c)) return "fail2"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package foo
|
||||
|
||||
var log = ""
|
||||
|
||||
class A {
|
||||
override fun equals(o: Any?): Boolean {
|
||||
log += "$o;"
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
|
||||
assertTrue(a.equals("aaa"))
|
||||
assertEquals("aaa;", log)
|
||||
assertFalse(a == null)
|
||||
assertEquals("aaa;", log)
|
||||
assertTrue(a.equals(null))
|
||||
assertEquals("aaa;null;", log)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val a: Int? = null
|
||||
val r = a == null
|
||||
if (!r || a != null)
|
||||
return "wrong result on simple nullable check"
|
||||
|
||||
//force using Kotlin.equals
|
||||
val t = null
|
||||
if (t != undefined)
|
||||
return "wrong result when compare null and undefined using Kotlin.equals"
|
||||
|
||||
var i = 0;
|
||||
fun foo(): Int? = ++i;
|
||||
if (foo() == null)
|
||||
return "wrong result on nullable check with side effects"
|
||||
|
||||
if (i != 1)
|
||||
return "wrong affects when using nullable check with side effects"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
class Foo(val name: String) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is Foo) {
|
||||
return false
|
||||
}
|
||||
return this.name == other.name
|
||||
}
|
||||
}
|
||||
|
||||
class Bar() {
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = Foo("abc")
|
||||
val b = Foo("abc")
|
||||
val c = Foo("def")
|
||||
|
||||
if (!(a.equals(b))) return "fail1"
|
||||
if (a.equals(c)) return "fail2"
|
||||
if (Bar().equals(Bar())) return "fail3"
|
||||
val g = Bar()
|
||||
if (!(g.equals(g))) return "fail4"
|
||||
if (g.equals(Bar())) return "fail5"
|
||||
return "OK"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val a = 2
|
||||
if (!(a.equals(a))) return "fail1"
|
||||
if (!(a.equals(2))) return "fail2"
|
||||
if (!(a.equals(2.0))) return "fail3"
|
||||
val c = "a"
|
||||
if (!("a".equals(c))) return "fail4"
|
||||
if (!((null as Any?)?.equals(null) ?: true)) return "fail5"
|
||||
val d = 5.6
|
||||
if (!(d.toShort().equals(5.toShort()))) return "fail6"
|
||||
if (!(d.toByte().equals(5.toByte()))) return "fail7"
|
||||
if (!(d.toFloat().equals(5.6.toFloat()))) return "fail8"
|
||||
if (!(d.toInt().equals(5))) return "fail9"
|
||||
if (true.equals(false)) return "fail10"
|
||||
|
||||
val n: Number = 3
|
||||
if (!(n.equals(3.3.toInt()))) return "fail11"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
import java.util.*
|
||||
|
||||
fun box(): String {
|
||||
val data = ArrayList<String>()
|
||||
data.add("foo")
|
||||
data.add("bar")
|
||||
data.add("whatnot")
|
||||
val data2 = ArrayList<String>()
|
||||
data2.addAll(data)
|
||||
return if (data.equals(data2)) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val a = "abc"
|
||||
val b = "abc"
|
||||
val c = "def"
|
||||
|
||||
if (a != b) return "fail1"
|
||||
if (a == c) return "fail2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package foo
|
||||
|
||||
open class A {
|
||||
var log = ""
|
||||
|
||||
var called = false
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (called) fail("recursion detected")
|
||||
|
||||
log += "A.equals;"
|
||||
|
||||
called = true
|
||||
val result = super.equals(other)
|
||||
called = false
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
log += "B.equals;"
|
||||
if (other == null) return false
|
||||
return super.equals(other)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
testFalse { a == A() }
|
||||
assertEquals("A.equals;", a.log)
|
||||
|
||||
val b1 = B()
|
||||
testTrue { b1 == b1 }
|
||||
assertEquals("B.equals;A.equals;", b1.log)
|
||||
|
||||
val b2 = B()
|
||||
testFalse { b2 == B() }
|
||||
assertEquals("B.equals;A.equals;", b2.log)
|
||||
|
||||
val b3 = B()
|
||||
testFalse { b3 == null }
|
||||
assertEquals("", b3.log)
|
||||
|
||||
val b4 = B()
|
||||
testFalse { b4.equals(null) }
|
||||
assertEquals("B.equals;", b4.log)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
if (f(0) != -3) {
|
||||
return "fail1"
|
||||
}
|
||||
if (f(102) != 201) {
|
||||
return "fail2"
|
||||
}
|
||||
if (f(103) != 100) {
|
||||
return "fail3"
|
||||
}
|
||||
if (f(-100) != -100) {
|
||||
return "fail4"
|
||||
}
|
||||
if (f(-99) != -201) {
|
||||
return "fail5"
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun f(i: Int): Int {
|
||||
var j = i
|
||||
return --j + (if (j < -100) return -100 else --j) + (if (j > 100) return 100 else 0)
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun id(s: String, b: Boolean): Boolean {
|
||||
global += s
|
||||
return b
|
||||
}
|
||||
|
||||
fun bar(b: Boolean): String {
|
||||
if (id("A", b) && return "A")
|
||||
return "B"
|
||||
else
|
||||
return "C"
|
||||
}
|
||||
|
||||
fun testBreak(b: Boolean, expected: Int) {
|
||||
global = ""
|
||||
var i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) id("A", b) && break
|
||||
}
|
||||
assertEquals(expected, i, "break 1")
|
||||
assertEquals("A", global, "break 1")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) {
|
||||
var x = id("A", b) && break
|
||||
}
|
||||
}
|
||||
assertEquals(expected, i, "break 2")
|
||||
assertEquals("A", global, "break 2")
|
||||
}
|
||||
|
||||
fun testContinue(b: Boolean, expected: Int) {
|
||||
global = ""
|
||||
var i = 0
|
||||
var n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) id("A", b) && continue
|
||||
n++
|
||||
}
|
||||
assertEquals(expected, n, "continue 1")
|
||||
assertEquals("A", global, "continue 1")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) {
|
||||
var x = id("A", b) && continue
|
||||
assertEquals(x, b)
|
||||
}
|
||||
n++
|
||||
}
|
||||
assertEquals(expected, n, "continue 2")
|
||||
assertEquals("A", global, "continue 2")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var b: Boolean
|
||||
|
||||
testBreak(true, 2)
|
||||
testBreak(false, 6)
|
||||
|
||||
global = ""
|
||||
var i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) break && id("A", false)
|
||||
}
|
||||
assertEquals(2, i, "break && false")
|
||||
assertEquals("", global, "break && false")
|
||||
|
||||
testContinue(true, 4)
|
||||
testContinue(false, 5)
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
var n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) continue && id("A", false)
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue && false")
|
||||
assertEquals("", global, "continue && false")
|
||||
|
||||
assertEquals("A", bar(true), "bar")
|
||||
assertEquals("A", global, "bar")
|
||||
|
||||
global = ""
|
||||
assertEquals("C", bar(false))
|
||||
assertEquals("A", global, "bar")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun bar(s: String, value: Boolean): Boolean {
|
||||
global += s
|
||||
return value
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
// Simple && Simple
|
||||
global = ""
|
||||
assertEquals(true, bar("A", true) && bar("B", true))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A", true) && bar("B", false))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A", false) && bar("B", true))
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A", false) && bar("B", false))
|
||||
assertEquals("A", global)
|
||||
|
||||
// Simple && Complex
|
||||
global = ""
|
||||
assertEquals(true, bar("A", true) && try { bar("B", true) } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A", true) && try { bar("B", false) } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A", false) && try { bar("B", true) } finally {})
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A", false) && try { bar("B", false) } finally {})
|
||||
assertEquals("A", global)
|
||||
|
||||
// Complex && Simple
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A", true) } finally {} && bar("B", true))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A", true) } finally {} && bar("B", false))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A", false) } finally {} && bar("B", true))
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A", false) } finally {} && bar("B", false))
|
||||
assertEquals("A", global)
|
||||
|
||||
// Complex && Complex
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A", true) } finally {} && try { bar("B", true) } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A", true) } finally {} && try { bar("B", false) } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A", false) } finally {} && try { bar("B", true) } finally {})
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A", false) } finally {} && try { bar("B", false) } finally {})
|
||||
assertEquals("A", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+79
@@ -0,0 +1,79 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
class A {
|
||||
var prop: Int = 0
|
||||
}
|
||||
|
||||
fun bar(s: String, index: Int): Int {
|
||||
global += s
|
||||
return index
|
||||
}
|
||||
|
||||
val baz: Int
|
||||
get() {
|
||||
global += ":baz:"; return 1
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = arrayOf(0,1,2,3)
|
||||
|
||||
|
||||
global = ""
|
||||
a[bar("A", 1)] = try { global += "B"; 10 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(10, a[1])
|
||||
|
||||
global = ""
|
||||
a[bar("A", 1)] += try { global += "B"; 10 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(20, a[1])
|
||||
|
||||
global = ""
|
||||
a[bar("A", 1)] -= try { global += "B"; 10 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(10, a[1])
|
||||
|
||||
global = ""
|
||||
a[bar("A", 1)] *= try { global += "B"; 2 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(20, a[1])
|
||||
|
||||
global = ""
|
||||
a[bar("A", 1)] /= try { global += "B"; 5 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(4, a[1])
|
||||
|
||||
global = ""
|
||||
a[bar("A", 1)] %= try { global += "B"; 3 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(1, a[1])
|
||||
|
||||
global = ""
|
||||
a[bar("A", 1)]++
|
||||
assertEquals("A", global)
|
||||
assertEquals(2, a[1])
|
||||
|
||||
global = ""
|
||||
a[try { bar("A", 1)} finally {}]++
|
||||
assertEquals("A", global)
|
||||
assertEquals(3, a[1])
|
||||
|
||||
global = ""
|
||||
++a[bar("A", 1)]
|
||||
assertEquals("A", global)
|
||||
assertEquals(4, a[1])
|
||||
|
||||
global = ""
|
||||
++a[try { bar("A", 1)} finally {}]
|
||||
assertEquals("A", global)
|
||||
assertEquals(5, a[1])
|
||||
|
||||
global = ""
|
||||
a[baz] = try { global += "right"; 100 } finally {}
|
||||
assertEquals(":baz:right", global)
|
||||
assertEquals(100, a[1])
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+59
@@ -0,0 +1,59 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
class A {
|
||||
var prop: Int = 0
|
||||
}
|
||||
|
||||
fun bar(s: String, a: A): A {
|
||||
global += s
|
||||
return a
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
|
||||
global = ""
|
||||
bar("A", a).prop = try { global += "B"; 10 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(10, a.prop)
|
||||
|
||||
global = ""
|
||||
bar("A", a).prop += try { global += "B"; 20 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(30, a.prop)
|
||||
|
||||
global = ""
|
||||
bar("A", a).prop -= try { global += "B"; 20 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(10, a.prop)
|
||||
|
||||
global = ""
|
||||
bar("A", a).prop *= try { global += "B"; 2 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(20, a.prop)
|
||||
|
||||
global = ""
|
||||
bar("A", a).prop /= try { global += "B"; 5 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(4, a.prop)
|
||||
|
||||
global = ""
|
||||
bar("A", a).prop %= try { global += "B"; 3 } finally {}
|
||||
assertEquals("AB", global)
|
||||
assertEquals(1, a.prop)
|
||||
|
||||
global = ""
|
||||
(try { bar("A", a) } finally {}).prop++
|
||||
assertEquals("A", global)
|
||||
assertEquals(2, a.prop)
|
||||
|
||||
global = ""
|
||||
++(try { bar("A", a) } finally {}).prop
|
||||
assertEquals("A", global)
|
||||
assertEquals(3, a.prop)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package foo
|
||||
|
||||
var global = ""
|
||||
|
||||
fun addToGlobal(s: String): String {
|
||||
global += s
|
||||
return ""
|
||||
}
|
||||
|
||||
fun bar(s: String, u: Int) { global += ":bar:${s}" }
|
||||
|
||||
class A() {
|
||||
fun memFun(s: String) { global += ":memFun:${s}" }
|
||||
}
|
||||
|
||||
class B() {
|
||||
fun A.bExt(s: String) { global += ":bExt:${s}" }
|
||||
|
||||
fun baz(s: String, a: A) = a.bExt(s)
|
||||
}
|
||||
|
||||
fun A.extFun(s: String) { global += ":extFun:${s}" }
|
||||
|
||||
fun box(): String {
|
||||
|
||||
bar(addToGlobal("A"), try { global += "B"; 10 } finally {})
|
||||
assertEquals("AB:bar:", global)
|
||||
|
||||
global = ""
|
||||
val a = A()
|
||||
a.memFun(try { "A" } finally {})
|
||||
assertEquals(":memFun:A", global)
|
||||
|
||||
global = ""
|
||||
(try { global += "A"; a } finally {}).memFun("B")
|
||||
assertEquals("A:memFun:B", global)
|
||||
|
||||
global = ""
|
||||
(try { global += "A"; a } finally {}).memFun(try { global += "B"; "C" } finally {})
|
||||
assertEquals("AB:memFun:C", global)
|
||||
|
||||
global = ""
|
||||
val b = B()
|
||||
b.baz("S", a)
|
||||
assertEquals(":bExt:S", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun <T> bar(s: String, value: T): T {
|
||||
global += s
|
||||
return value
|
||||
}
|
||||
|
||||
fun <T> baz(vararg args: T): String {
|
||||
return "baz: ${args.size}"
|
||||
}
|
||||
|
||||
fun <T> idVarArg(vararg a: T) = a
|
||||
|
||||
fun box(): String {
|
||||
baz(bar("A", 10), try { global += "B"; 20} finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
baz(bar("A", 10), 30, if (true) { while(false){}; global+= "B"; 20 } else { 50 })
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals("baz: 4", baz(bar("A", 1), *try {bar("B", arrayOf(2, 3))} catch(e: Exception) { bar("C", arrayOf(4, 5))}, bar("D", 6)))
|
||||
assertEquals("ABD", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun f(arg1: String, arg2: String, arg3: String): String {
|
||||
global += ":f:"
|
||||
return arg1 + arg2 + arg3
|
||||
}
|
||||
|
||||
fun id(s: String): String {
|
||||
global += s
|
||||
return s
|
||||
}
|
||||
|
||||
fun bar(s: String): String {
|
||||
return f(id(s), if (true) return ":bar:" else return "b", id(s))
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var b: Boolean
|
||||
|
||||
var i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) f(id("A"), break, id("B"))
|
||||
}
|
||||
assertEquals(2, i, "break 1")
|
||||
assertEquals("A", global, "break 1")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) {
|
||||
var x = f(id("A"), break, id("B"))
|
||||
}
|
||||
}
|
||||
assertEquals(2, i, "break 2")
|
||||
assertEquals("A", global, "break 2")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) {
|
||||
var x = "B" + f(id("A"), break, id("B"))
|
||||
}
|
||||
}
|
||||
assertEquals(2, i, "break 3")
|
||||
assertEquals("A", global, "break 3")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
var n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) f(id("A"), continue, id("B"))
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue 1")
|
||||
assertEquals("A", global, "continue 1")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) {
|
||||
var x = f(id("A"), continue, id("B"))
|
||||
}
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue 2")
|
||||
assertEquals("A", global, "continue 2")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) {
|
||||
var x = "B" + f(id("A"), continue, id("B"))
|
||||
}
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue 3")
|
||||
assertEquals("A", global, "continue 3")
|
||||
|
||||
global = ""
|
||||
assertEquals(":bar:", bar("A"))
|
||||
assertEquals("A", global, "bar")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package foo
|
||||
|
||||
fun castWithReturn(): Int {
|
||||
return (return 23) as Int
|
||||
}
|
||||
|
||||
fun safeCastWithReturn(): Int? {
|
||||
return (return 23) as Int?
|
||||
}
|
||||
|
||||
fun isWithReturn(): Int {
|
||||
return if ((return 23) is Int) 2 else 3
|
||||
}
|
||||
|
||||
fun castWithBreak(): Int {
|
||||
var result = 23
|
||||
while (result < 40) {
|
||||
if ((break as Int) == 0) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun safeCastWithBreak(): Int {
|
||||
var result = 23
|
||||
while (result < 40) {
|
||||
if ((break as Int) == 0) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun isWithBreak(): Int {
|
||||
var result = 23
|
||||
while (result < 40) {
|
||||
if (break is Int) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun castWithContinue(): Int {
|
||||
var result = 25
|
||||
while (result in 24..30) {
|
||||
--result
|
||||
if ((continue as Int) == 0) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun castNullableWithContinue(): Int {
|
||||
var result = 25
|
||||
while (result in 24..30) {
|
||||
--result
|
||||
if ((continue as Int?) == 0) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun safeCastWithContinue(): Int {
|
||||
var result = 25
|
||||
while (result in 24..30) {
|
||||
--result
|
||||
if ((continue as? Int) == 0) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun safeCastNullableWithContinue(): Int {
|
||||
var result = 25
|
||||
while (result in 24..30) {
|
||||
--result
|
||||
if ((continue as? Int?) == 0) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun isWithContinue(): Int {
|
||||
var result = 25
|
||||
while (result in 24..30) {
|
||||
--result
|
||||
if (continue is Int) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun isNullableWithContinue(): Int {
|
||||
var result = 23
|
||||
while (result in 24..30) {
|
||||
--result
|
||||
if (continue is Int?) result += 2 else result += 3
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun whenWithReturn(): Int {
|
||||
return when ((return 23) as Int) {
|
||||
is Int -> 24
|
||||
else -> 25
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(23, castWithReturn())
|
||||
assertEquals(23, safeCastWithReturn())
|
||||
assertEquals(23, isWithReturn())
|
||||
|
||||
assertEquals(23, castWithBreak())
|
||||
assertEquals(23, safeCastWithBreak())
|
||||
assertEquals(23, isWithBreak())
|
||||
|
||||
assertEquals(23, castWithContinue())
|
||||
assertEquals(23, safeCastWithContinue())
|
||||
assertEquals(23, isWithContinue())
|
||||
|
||||
assertEquals(23, castNullableWithContinue())
|
||||
assertEquals(23, safeCastNullableWithContinue())
|
||||
assertEquals(23, isNullableWithContinue())
|
||||
|
||||
assertEquals(23, whenWithReturn())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+61
@@ -0,0 +1,61 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun bar(s: String): String {
|
||||
global += s
|
||||
return s
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A") < bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A") <= bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A") > bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A") >= bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A") < try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A") <= try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A") } finally {} > bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A") } finally {} >= bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A") } finally {} < try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A") } finally {} <= try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A") } finally {} > try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A") } finally {} >= try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
var i = 0
|
||||
|
||||
inline fun f() = i * 2
|
||||
|
||||
fun box(): String {
|
||||
return if ((++i + f()) == 3) "OK" else "fail"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
if (f(0) != 201) {
|
||||
return "fail1"
|
||||
}
|
||||
if (f(1) != 104) {
|
||||
return "fail2"
|
||||
}
|
||||
if (f(-2) != -100) {
|
||||
return "fail3"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun f(i: Int): Int {
|
||||
var j = i
|
||||
return ++j + if (j != 1) {
|
||||
(if (j > 0) 100 else return -100) + 2
|
||||
}
|
||||
else 200
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
var sideEffect: Int = 0;
|
||||
|
||||
class A(val x: Int)
|
||||
|
||||
val global_a = A(1)
|
||||
val global_b = A(2)
|
||||
|
||||
fun nullFun(value: Boolean): Any? = if (value) null else global_a
|
||||
|
||||
fun box(): String {
|
||||
|
||||
nullFun(false) ?: global_b
|
||||
assertEquals(global_a, nullFun(false) ?: try { ++sideEffect; global_b } finally {}, "false, global_b")
|
||||
assertEquals(0, sideEffect, "false, global_b side effect")
|
||||
|
||||
assertEquals(global_b, nullFun(true) ?: try { ++sideEffect; global_b } finally {}, "true, global_b")
|
||||
assertEquals(1, sideEffect, "true, global_b side effect")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package foo
|
||||
|
||||
class A
|
||||
|
||||
fun bar(a: A?): String {
|
||||
if ((a ?: return "A") != null)
|
||||
return "B"
|
||||
else
|
||||
return "C"
|
||||
}
|
||||
|
||||
fun testBreak(a: A?, expected: Int) {
|
||||
var i = 0
|
||||
while(i++<5) {
|
||||
if (i==2) a ?: break
|
||||
}
|
||||
assertEquals(expected, i, "break 1")
|
||||
|
||||
i = 0
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
var x = a ?: break
|
||||
}
|
||||
}
|
||||
assertEquals(expected, i, "break 2")
|
||||
}
|
||||
|
||||
fun testContinue(a: A?, expected: Int) {
|
||||
var i = 0
|
||||
var n = 0
|
||||
while(i++<5) {
|
||||
if (i==2) a ?: continue
|
||||
n++
|
||||
}
|
||||
assertEquals(expected, n)
|
||||
|
||||
i = 0
|
||||
n = 0
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
var x = a ?: continue
|
||||
}
|
||||
n++
|
||||
}
|
||||
assertEquals(expected, n)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
testBreak(null, 2)
|
||||
testBreak(A(), 6)
|
||||
|
||||
var i = 0
|
||||
while(i++<5) {
|
||||
if (i==2) break ?: null
|
||||
}
|
||||
assertEquals(2, i, "break ?: null")
|
||||
|
||||
testContinue(null, 4)
|
||||
testContinue(A(), 5)
|
||||
|
||||
i = 0
|
||||
var n = 0
|
||||
while(i++<5) {
|
||||
if (i==2) continue ?: null
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue ?: null")
|
||||
|
||||
assertEquals("A", bar(null))
|
||||
assertEquals("B", bar(A()))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun bar(s: String): String {
|
||||
global += s
|
||||
return s
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A") == bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A") != bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A") == try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A") != try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A") } finally {} == bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A") } finally {} != bar("B"))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A") } finally {} == try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A") } finally {} != try { bar("B") } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package foo
|
||||
|
||||
var d = 0
|
||||
|
||||
fun f(): Int {
|
||||
d = if (d < 0) -100 else 100
|
||||
return d
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
d = d-- + f() + when(d) {
|
||||
-100 -> return "OK"
|
||||
1 -> 1
|
||||
else -> return "fail1"
|
||||
}
|
||||
return "fail2"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Order of evaluation differs for JVM and Javascript backend
|
||||
// http://youtrack.jetbrains.com/issue/KT-5254
|
||||
|
||||
package foo
|
||||
|
||||
var s = ""
|
||||
|
||||
fun a():String {
|
||||
s += "A"
|
||||
return ""
|
||||
}
|
||||
|
||||
fun b():String {
|
||||
s += "B"
|
||||
return ""
|
||||
}
|
||||
|
||||
fun c():String {
|
||||
s += "C"
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var res = (if(true) {a()} else "") + b() + (if (true) {c()} else "")
|
||||
assertEquals("ABC", s)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var i = 0
|
||||
val c = sum(++i, if (i == 0) return "fail1" else i + 2)
|
||||
if (c != 4) {
|
||||
return "fail2: $c"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
fun sum(a1: Int, a2: Int) = a1 + a2
|
||||
@@ -0,0 +1,10 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var i = 0
|
||||
var t = ++i + if (i == 0) 0 else 2
|
||||
if (t != 3) {
|
||||
return "fail: $t"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
var sideEffect: Int = 0;
|
||||
|
||||
fun id(value: Boolean): Boolean = value
|
||||
|
||||
fun box(): String {
|
||||
|
||||
assertEquals(if (id(true)) try { ++sideEffect; 10 } finally {} else 20, 10)
|
||||
assertEquals(1, sideEffect)
|
||||
|
||||
assertEquals(if (id(false)) try { ++sideEffect; 10 } finally {} else 20, 20)
|
||||
assertEquals(1, sideEffect)
|
||||
|
||||
assertEquals(if (id(true)) 100 else try { ++sideEffect; 200 } finally {}, 100)
|
||||
assertEquals(1, sideEffect)
|
||||
|
||||
assertEquals(if (id(false)) 100 else try { ++sideEffect; 200 } finally {}, 200)
|
||||
assertEquals(2, sideEffect)
|
||||
|
||||
assertEquals(if (id(true)) try { ++sideEffect; 1000 } finally {} else try { ++sideEffect; 2000 } finally {}, 1000)
|
||||
assertEquals(3, sideEffect)
|
||||
|
||||
assertEquals(if (id(false)) try { ++sideEffect; 1000 } finally {} else try { ++sideEffect; 2000 } finally {}, 2000)
|
||||
assertEquals(4, sideEffect)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var x = 0
|
||||
assertEquals(false, ++x > x)
|
||||
assertEquals(false, ++x > try {x} finally {})
|
||||
|
||||
assertEquals(false, x++ > x)
|
||||
assertEquals(false, x++ > try {x} finally {})
|
||||
|
||||
assertEquals(true, ++x == x)
|
||||
assertEquals(true, ++x == try {x} finally {})
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+133
@@ -0,0 +1,133 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
class A {
|
||||
var prop: Boolean = false
|
||||
}
|
||||
|
||||
fun getA(): A = try { global += "getA"; A() } finally {}
|
||||
|
||||
fun barEq(): String {
|
||||
10 == return "eq"
|
||||
}
|
||||
|
||||
fun barLt(): String {
|
||||
10 < (return "lt") as Int
|
||||
}
|
||||
|
||||
fun setGlobal(i: Int): Int {
|
||||
global = "setGlobal"
|
||||
return i
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var b: Boolean
|
||||
|
||||
var i = 0
|
||||
while(i++<5) {
|
||||
if (i==2) 10 == break
|
||||
}
|
||||
assertEquals(2, i, "break 1")
|
||||
|
||||
i = 0
|
||||
while(i++<5) {
|
||||
if (i==2) 10 < (break as Int)
|
||||
}
|
||||
assertEquals(2, i, "break 2")
|
||||
|
||||
i = 0
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
var x = 10 == break;
|
||||
}
|
||||
}
|
||||
assertEquals(2, i, "break 3")
|
||||
|
||||
i = 0
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
var x = 10 < (break as Int)
|
||||
}
|
||||
}
|
||||
assertEquals(2, i, "break 4")
|
||||
|
||||
i = 0
|
||||
var bVar: Boolean
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
bVar = 10 < (break as Int)
|
||||
}
|
||||
}
|
||||
assertEquals(2, i, "break 5")
|
||||
|
||||
i = 0
|
||||
var bVarArray = arrayOf(true, false)
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
bVarArray[try { global += "A"; 0} finally {}] = 10 < (break as Int)
|
||||
}
|
||||
}
|
||||
assertEquals(2, i, "break 6")
|
||||
assertEquals("A", global, "break 6")
|
||||
|
||||
i = 0
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
bVarArray[setGlobal(0)] = 10 < (break as Int)
|
||||
}
|
||||
}
|
||||
assertEquals(2, i, "break 6a")
|
||||
assertEquals("setGlobal", global, "break 6a")
|
||||
|
||||
i = 0
|
||||
global = ""
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
getA().prop = 10 < (break as Int)
|
||||
}
|
||||
}
|
||||
assertEquals(2, i, "break 7")
|
||||
assertEquals("getA", global, "break 7")
|
||||
|
||||
i = 0
|
||||
var n = 0
|
||||
while(i++<5) {
|
||||
if (i==2) 10 == continue
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue 1")
|
||||
|
||||
i = 0
|
||||
n = 0
|
||||
while(i++<5) {
|
||||
if (i==2) 10 < (continue as Int)
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue 2")
|
||||
|
||||
i = 0
|
||||
n = 0
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
var x = 10 == continue
|
||||
}
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue 3")
|
||||
|
||||
i = 0
|
||||
n = 0
|
||||
while(i++<5) {
|
||||
if (i==2) {
|
||||
var x = 10 < (continue as Int)
|
||||
}
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue 4")
|
||||
|
||||
assertEquals("eq", barEq())
|
||||
assertEquals("lt", barLt())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+35
@@ -0,0 +1,35 @@
|
||||
// http://youtrack.jetbrains.com/issue/KT-4225
|
||||
// Compiler to JavaScript produces semantically wrong code
|
||||
|
||||
package foo
|
||||
|
||||
var i = 0
|
||||
var j = 0
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun incI(){
|
||||
i++
|
||||
}
|
||||
|
||||
fun incJ(a: Any){
|
||||
j++
|
||||
}
|
||||
|
||||
fun foo(f: () -> Unit) = f
|
||||
|
||||
fun box(): String {
|
||||
val bar = 1
|
||||
|
||||
val f = foo {
|
||||
incI()
|
||||
incJ(if (bar == 2) "A" else "B")
|
||||
}
|
||||
|
||||
global +="i = $i, j = $j"
|
||||
f()
|
||||
global +=" : i = $i, j = $j"
|
||||
assertEquals("i = 0, j = 0 : i = 1, j = 1", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+87
@@ -0,0 +1,87 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
|
||||
fun whileReturn(): String {
|
||||
var i = 0
|
||||
while(if (i<2) true else { global += ":return:"; return ":whileReturn:"}) {
|
||||
i++
|
||||
global += "A"
|
||||
}
|
||||
return ":whileReturn:after:"
|
||||
}
|
||||
|
||||
fun whileImmediateReturn(): String {
|
||||
var i = 0
|
||||
while(return ":whileImmediateReturn:") {
|
||||
global += "A"
|
||||
}
|
||||
return ":whileImmediateReturn:after"
|
||||
}
|
||||
|
||||
fun doWhileReturn(): String {
|
||||
var i = 0
|
||||
do {
|
||||
i++
|
||||
global += "A"
|
||||
}
|
||||
while(if (i<2) true else { global += ":return:"; return ":doWhileReturn:"})
|
||||
return ":doWhileReturn:after:"
|
||||
}
|
||||
|
||||
fun doWhileReturnFromCondition(): String {
|
||||
var i = 0
|
||||
do {
|
||||
global += "A"
|
||||
} while(return ":doWhileReturnFromCondition:")
|
||||
return ":doWhileReturnFromCondition:after"
|
||||
}
|
||||
|
||||
fun doWhileImmediateReturn(): String {
|
||||
var i = 0
|
||||
do {
|
||||
return ":doWhileImmediateReturn:"
|
||||
global += "A"
|
||||
} while(false)
|
||||
return ":doWhileImmediateReturn:after"
|
||||
}
|
||||
|
||||
fun forReturn(b: Boolean): String {
|
||||
var i = 0
|
||||
for(i in (if (b) 1..2 else { global += ":return:"; return ":forReturn:"})) {
|
||||
global += "A"
|
||||
}
|
||||
return ":forReturn:after:"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(":whileReturn:", whileReturn())
|
||||
assertEquals("AA:return:", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(":whileImmediateReturn:", whileImmediateReturn())
|
||||
assertEquals("", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(":doWhileReturn:", doWhileReturn())
|
||||
assertEquals("AA:return:", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(":doWhileReturnFromCondition:", doWhileReturnFromCondition())
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(":doWhileImmediateReturn:", doWhileImmediateReturn())
|
||||
assertEquals("", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(":forReturn:after:", forReturn(true))
|
||||
assertEquals("AA", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(":forReturn:", forReturn(false))
|
||||
assertEquals(":return:", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun id(s: String, b: Boolean): Boolean {
|
||||
global += s
|
||||
return b
|
||||
}
|
||||
|
||||
fun bar(b: Boolean): String {
|
||||
if (id("A",b) || return "A")
|
||||
return "B"
|
||||
else
|
||||
return "C"
|
||||
}
|
||||
|
||||
fun testBreak(b: Boolean, expected: Int) {
|
||||
global = ""
|
||||
var i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) id("A", b) || break
|
||||
}
|
||||
assertEquals(expected, i, "break 1")
|
||||
assertEquals("A", global, "break 1")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) {
|
||||
var x = id("A", b) || break
|
||||
}
|
||||
}
|
||||
assertEquals(expected, i, "break 2")
|
||||
assertEquals("A", global, "break 2")
|
||||
}
|
||||
|
||||
fun testContinue(b: Boolean, expected: Int) {
|
||||
global = ""
|
||||
var i = 0
|
||||
var n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) id("A", b) || continue
|
||||
n++
|
||||
}
|
||||
assertEquals(expected, n, "continue 1")
|
||||
assertEquals("A", global, "continue 1")
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) {
|
||||
var x = id("A", b) || continue
|
||||
}
|
||||
n++
|
||||
}
|
||||
assertEquals(expected, n, "continue 2")
|
||||
assertEquals("A", global, "continue 2")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
testBreak(true, 6)
|
||||
testBreak(false, 2)
|
||||
|
||||
global = ""
|
||||
var i = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) break || id("A", true)
|
||||
}
|
||||
assertEquals(2, i, "break || true")
|
||||
assertEquals("", global, "break || false")
|
||||
|
||||
testContinue(true, 5)
|
||||
testContinue(false, 4)
|
||||
|
||||
global = ""
|
||||
i = 0
|
||||
var n = 0
|
||||
while (i++ < 5) {
|
||||
if (i == 2) continue || id("A", true)
|
||||
n++
|
||||
}
|
||||
assertEquals(4, n, "continue || true")
|
||||
assertEquals("", global, "continue || true")
|
||||
|
||||
assertEquals("B", bar(true))
|
||||
assertEquals("A", global, "bar")
|
||||
global = ""
|
||||
assertEquals("A", bar(false))
|
||||
assertEquals("A", global, "bar")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun bar(s: String, value: Boolean): Boolean {
|
||||
global += s
|
||||
return value
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = if (true || if(bar("A", false)) {2} else {3} == 0) true else false
|
||||
assertEquals("", global)
|
||||
|
||||
true || if(bar("A", false)) {2} else {3} == 0
|
||||
assertEquals("", global)
|
||||
|
||||
// Simple || Simple
|
||||
global = ""
|
||||
assertEquals(true, bar("A", true) || bar("B", true))
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A", true) || bar("B", false))
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A", false) || bar("B", true))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A", false) || bar("B", false))
|
||||
assertEquals("AB", global)
|
||||
|
||||
// Simple || Complex
|
||||
global = ""
|
||||
assertEquals(true, bar("A", true) || try { bar("B", true) } finally {})
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A", true) || try { bar("B", false) } finally {})
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, bar("A", false) || try { bar("B", true) } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, bar("A", false) || try { bar("B", false) } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
// Complex || Simple
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A", true) } finally {} || bar("B", true))
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A", true) } finally {} || bar("B", false))
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A", false) } finally {} || bar("B", true))
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A", false) } finally {} || bar("B", false))
|
||||
assertEquals("AB", global)
|
||||
|
||||
// Complex || Complex
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A", true) } finally {} || try { bar("B", true) } finally {})
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A", true) } finally {} || try { bar("B", false) } finally {})
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(true, try { bar("A", false) } finally {} || try { bar("B", true) } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
assertEquals(false, try { bar("A", false) } finally {} || try { bar("B", false) } finally {})
|
||||
assertEquals("AB", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package foo
|
||||
|
||||
var log = ""
|
||||
|
||||
class A(val value: Int) {
|
||||
operator fun plus(other: A): A {
|
||||
log += "A.plus(${other.value});"
|
||||
return A(value + other.value)
|
||||
}
|
||||
}
|
||||
|
||||
val _array = arrayOf(A(2))
|
||||
|
||||
fun getArray(): Array<A> {
|
||||
log += "getArray();"
|
||||
return _array
|
||||
}
|
||||
|
||||
fun getArrayIndex(): Int {
|
||||
log += "getArrayIndex();"
|
||||
return 0
|
||||
}
|
||||
|
||||
class B(value: Int) {
|
||||
var a = A(value)
|
||||
}
|
||||
|
||||
val _property = B(10)
|
||||
val _functionResult = B(100)
|
||||
|
||||
val foo: B
|
||||
get() {
|
||||
log += "foo;"
|
||||
return _property
|
||||
}
|
||||
|
||||
fun bar(): B {
|
||||
log += "bar();"
|
||||
return _functionResult
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
getArray()[getArrayIndex()] += A(3)
|
||||
assertEquals(5, _array[0].value)
|
||||
|
||||
foo.a += A(20)
|
||||
assertEquals(30, _property.a.value)
|
||||
|
||||
bar().a += A(200)
|
||||
assertEquals(300, _functionResult.a.value)
|
||||
|
||||
assertEquals("getArray();getArrayIndex();A.plus(3);foo;A.plus(20);bar();A.plus(200);", log)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var i = 0
|
||||
var t = ++i + when(i) {
|
||||
3 -> 4
|
||||
1 -> 2
|
||||
0 -> 1
|
||||
else -> 100
|
||||
}
|
||||
if (t != 3) {
|
||||
return "fail: $t"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun bar(s: String): Int {
|
||||
global += s
|
||||
return 1
|
||||
}
|
||||
fun testWhen() {
|
||||
global = ""
|
||||
when(arrayOf(bar("A"),2,3)) {
|
||||
arrayOf(1) -> println("1")
|
||||
arrayOf(2) -> println("2")
|
||||
else -> println("else")
|
||||
}
|
||||
assertEquals("A", global)
|
||||
|
||||
}
|
||||
|
||||
fun testIntrinsic() {
|
||||
global = ""
|
||||
val x = arrayOf(bar("A")) == try { arrayOf(bar("B")) } finally {}
|
||||
assertEquals("AB", global)
|
||||
}
|
||||
|
||||
fun testElvis() {
|
||||
global = ""
|
||||
var x = arrayOf(bar("A")) ?: 10
|
||||
assertEquals("A", global)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testWhen()
|
||||
testIntrinsic()
|
||||
testElvis()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun id(s: String, value: Boolean): Boolean {
|
||||
global += s
|
||||
return value
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
when {
|
||||
id("A", true) || id("B", true) -> 10
|
||||
}
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
when {
|
||||
id("A", false) || id("B", true) || id("C", true) -> 10
|
||||
}
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
var b = true
|
||||
when {
|
||||
try { global += "A"; b } finally {} -> 10
|
||||
try { global += "B"; !b } finally {} -> 20
|
||||
}
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
b = false
|
||||
when {
|
||||
try { global += "A"; b } finally {} -> 10
|
||||
try { global += "B"; !b } finally {} -> 20
|
||||
}
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
b = true
|
||||
when {
|
||||
b || try { global += "A"; !b } finally {} -> 10
|
||||
}
|
||||
assertEquals("", global)
|
||||
|
||||
global = ""
|
||||
b = false
|
||||
when {
|
||||
b || try { global += "A"; !b } finally {} -> 10
|
||||
}
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
when {
|
||||
false -> {
|
||||
global += "A"
|
||||
}
|
||||
try { global += "B"; false } finally {} -> {
|
||||
global += "D"
|
||||
}
|
||||
else -> {
|
||||
global += "C"
|
||||
}
|
||||
}
|
||||
assertEquals("BC", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package foo
|
||||
|
||||
val a1 = arrayOfNulls<Int>(10)
|
||||
|
||||
fun box(): String {
|
||||
var c = 0
|
||||
var d = 0
|
||||
a1[3] = 3
|
||||
a1[5] = 5
|
||||
|
||||
for (a: Int? in a1) {
|
||||
if (a != null) {
|
||||
c += 1;
|
||||
}
|
||||
else {
|
||||
d += 1
|
||||
}
|
||||
}
|
||||
assertEquals(2, c)
|
||||
assertEquals(8, d)
|
||||
|
||||
var s: String = ""
|
||||
for(i in arrayOf(0,1,2))
|
||||
try { s += "A${i}:"} finally {}
|
||||
assertEquals("A0:A1:A2:", s)
|
||||
|
||||
var sLong = 0L
|
||||
var aLong = longArrayOf(1,2,3,4,5,6,7,8,9,10)
|
||||
for(i in aLong)
|
||||
sLong += i
|
||||
assertEquals(55L, sLong)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun up(s: String, value: Int): Int {
|
||||
global += s
|
||||
return value
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
// http://youtrack.jetbrains.com/issue/KT-5262
|
||||
// JS: wrong code for `for` over range when start greater end
|
||||
var n: Int = 0
|
||||
for(i in 3..0)
|
||||
n++
|
||||
assertEquals(0, n)
|
||||
|
||||
for(i in 0..3)
|
||||
n++
|
||||
assertEquals(4, n)
|
||||
|
||||
// Evaluation order
|
||||
for (i in up("A", 0)..up("B", 5)) {
|
||||
}
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
for (i in try { up("A", 0) } finally {} ..up("B", 5)) {
|
||||
}
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
for (i in up("A", 0).. try { up("B", 5)} finally {}) {
|
||||
}
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
for (i in try { up("A", 0) } finally {}.. try { up("B", 5)} finally {}) {
|
||||
}
|
||||
assertEquals("AB", global)
|
||||
|
||||
var sLong = 0L
|
||||
for(i in 0L..10L)
|
||||
sLong += i
|
||||
assertEquals(55L, sLong)
|
||||
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var n = 0
|
||||
var r = 3..0
|
||||
for(i in r)
|
||||
n++
|
||||
assertEquals(0, n)
|
||||
|
||||
r = 0..3
|
||||
for(i in r)
|
||||
n++
|
||||
assertEquals(4, n)
|
||||
|
||||
var sLong = 0L
|
||||
var rLong = 0L..10L
|
||||
for(i in rLong)
|
||||
sLong += i
|
||||
assertEquals(55L, sLong)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
class Iter(val upper: Int) {
|
||||
var count: Int = 0
|
||||
operator fun hasNext(): Boolean = count < upper
|
||||
operator fun next(): Int = count++
|
||||
}
|
||||
|
||||
class A(val upper: Int) {
|
||||
operator fun iterator(): Iter = Iter(upper)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var n = 0
|
||||
for(i in A(10)) {
|
||||
n++
|
||||
}
|
||||
assertEquals(10, n)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package foo
|
||||
|
||||
val a1 = arrayOfNulls<Int>(0)
|
||||
|
||||
fun box(): String {
|
||||
for (a in a1) {
|
||||
return "fail"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var s: String = ""
|
||||
|
||||
s = ""
|
||||
for(i in 0..2)
|
||||
try { s += "A"} finally {}
|
||||
assertEquals("AAA", s)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
for (i in 0..5);
|
||||
|
||||
val r = 0..5
|
||||
for (i in r);
|
||||
|
||||
for (i in arrayOf(1, 2, 3));
|
||||
|
||||
for (i in arrayOf(1, 2, 3).asList());
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+45
@@ -0,0 +1,45 @@
|
||||
package foo
|
||||
|
||||
var log = ""
|
||||
|
||||
class T(val id: Int) {
|
||||
operator fun component1(): Int {
|
||||
log += "($id).component1();"
|
||||
return 1
|
||||
}
|
||||
operator fun component2(): String {
|
||||
log += "($id).component2();"
|
||||
return "1"
|
||||
}
|
||||
}
|
||||
|
||||
class C {
|
||||
operator fun iterator(): Iterator<T> = object: Iterator<T> {
|
||||
var i = 0
|
||||
var data = arrayOf(T(3), T(1), T(2))
|
||||
override fun hasNext(): Boolean {
|
||||
log += "C.hasNext();"
|
||||
return i < data.size
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
log += "C.next();"
|
||||
return data[i++]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
for ((a, b) in arrayOf(T(3), T(1), T(2)));
|
||||
assertEquals("(3).component1();(3).component2();(1).component1();(1).component2();(2).component1();(2).component2();", log)
|
||||
|
||||
|
||||
log = ""
|
||||
for ((a, b) in C());
|
||||
assertEquals("C.hasNext();C.next();(3).component1();(3).component2();C.hasNext();C.next();" +
|
||||
"(1).component1();(1).component2();C.hasNext();C.next();" +
|
||||
"(2).component1();(2).component2();C.hasNext();", log)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
val a1 = arrayOfNulls<Int>(0)
|
||||
|
||||
fun box(): String {
|
||||
var bar = 33
|
||||
outer@ for (a in arrayOf(1, 2)) {
|
||||
for (b in arrayOf(1, 4)) {
|
||||
break@outer
|
||||
}
|
||||
bar = 42
|
||||
}
|
||||
if (bar != 33) return "fail: $bar"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// http://youtrack.jetbrains.com/issue/KT-5257
|
||||
// JS: for with continue with label fails on runtime
|
||||
|
||||
package foo
|
||||
|
||||
var global: String = ""
|
||||
|
||||
fun up(s: String, value: Int): Int {
|
||||
global += s
|
||||
return value
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
list@
|
||||
for (i in 0..up("A", 5)) {
|
||||
continue@list
|
||||
}
|
||||
assertEquals("A", global)
|
||||
|
||||
global = ""
|
||||
list1@
|
||||
for (i in up("A", 0)..try { global += "B"; 5} finally {}) {
|
||||
continue@list1
|
||||
}
|
||||
assertEquals("AB", global)
|
||||
|
||||
global = ""
|
||||
list2@
|
||||
for (i in try { up("A", 0) } finally {}..try { global += "B"; 5} finally {}) {
|
||||
continue@list2
|
||||
}
|
||||
assertEquals("AB", global)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var n = 0
|
||||
forLabel0@ for(i in 0..10) {
|
||||
var j = 0
|
||||
whileLabel0@ while(j++<i) {
|
||||
n++;
|
||||
if (j==2)
|
||||
continue@forLabel0
|
||||
}
|
||||
}
|
||||
assertEquals(19, n)
|
||||
|
||||
n = 0
|
||||
forLabel1@ for(i in 0..10) {
|
||||
var j = 0
|
||||
whileLabel1@ while(try {j++} finally {} <i) {
|
||||
n++;
|
||||
if (j==2)
|
||||
continue@forLabel1
|
||||
}
|
||||
}
|
||||
assertEquals(19, n)
|
||||
|
||||
n = 0
|
||||
forLabel2@ for(i in 0..10) {
|
||||
var j = 0
|
||||
whileLabel2@ while(j++<i) {
|
||||
n++;
|
||||
if (j==2)
|
||||
break@forLabel2
|
||||
}
|
||||
}
|
||||
assertEquals(3, n)
|
||||
|
||||
n = 0
|
||||
forLabel3@ for(i in 0..10) {
|
||||
var j = 0
|
||||
whileLabel3@ while(j++<i) {
|
||||
n++;
|
||||
if (j==2)
|
||||
break@whileLabel3
|
||||
}
|
||||
}
|
||||
assertEquals(19, n)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import java.util.*
|
||||
|
||||
var log = ""
|
||||
|
||||
private fun printLog(message: String) {
|
||||
log += message
|
||||
}
|
||||
|
||||
private fun printlnLog(message: String) = printLog("$message\n")
|
||||
|
||||
class Lifetime() {
|
||||
val attached = ArrayList<Function0<Unit>>()
|
||||
|
||||
public fun attach(action: () -> Unit) {
|
||||
attached.add(action)
|
||||
}
|
||||
|
||||
fun close() {
|
||||
for (x in attached) x()
|
||||
attached.clear()
|
||||
}
|
||||
}
|
||||
|
||||
public class Viewable<T>() {
|
||||
val items = ArrayList<T>()
|
||||
|
||||
fun add(item: T) {
|
||||
items.add(item)
|
||||
}
|
||||
|
||||
fun remove(item: T) {
|
||||
items.remove(item)
|
||||
}
|
||||
|
||||
fun view(lifetime: Lifetime, viewer: (itemLifetime: Lifetime, item: T) -> Unit) {
|
||||
for (item in items) {
|
||||
viewer(lifetime, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun lifetime(body: (Lifetime) -> Unit) {
|
||||
val l = Lifetime()
|
||||
body(l)
|
||||
l.close()
|
||||
}
|
||||
|
||||
fun<T> Dump(items: ArrayList<T>) {
|
||||
for (item in items) {
|
||||
printLog(item.toString() + ", ")
|
||||
}
|
||||
printlnLog("end")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val v = Viewable<Int>()
|
||||
val x = ArrayList<Int>()
|
||||
v.add(1)
|
||||
v.add(2)
|
||||
v.add(3)
|
||||
lifetime() {
|
||||
v.view(it) { itemLifetime, item ->
|
||||
x.add(item)
|
||||
Dump(x)
|
||||
itemLifetime.attach() {
|
||||
x.remove(item as Any);
|
||||
Dump(x);
|
||||
printlnLog("!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val expected =
|
||||
"1, end\n" +
|
||||
"1, 2, end\n" +
|
||||
"1, 2, 3, end\n" +
|
||||
"2, 3, end\n" +
|
||||
"!\n" +
|
||||
"3, end\n" +
|
||||
"!\n" +
|
||||
"end\n" +
|
||||
"!\n"
|
||||
|
||||
if (log != expected) return "fail: $log"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
@native
|
||||
fun String.replace(regexp: RegExp, replacement: String): String = noImpl
|
||||
|
||||
fun String.replaceAll(regexp: String, replacement: String): String = replace(RegExp(regexp, "g"), replacement)
|
||||
|
||||
@native
|
||||
fun String.search(regexp: RegExp): Int = noImpl
|
||||
|
||||
@native
|
||||
class RegExp(regexp: String, flags: String = "") {
|
||||
fun exec(s: String): Array<String>? = noImpl
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var sum = 0
|
||||
val adder = { a: Int -> sum += a }
|
||||
adder(3)
|
||||
adder(2)
|
||||
|
||||
if (sum != 5) return "fail: $sum"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
class A()
|
||||
|
||||
private val doInit = {
|
||||
A()
|
||||
}()
|
||||
|
||||
fun box(): String = if (doInit is A) "OK" else "fail"
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
return apply("OK", { arg: String -> arg })
|
||||
}
|
||||
|
||||
fun apply(arg: String, f: (p: String) -> String): String {
|
||||
return f(arg)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
return if (apply(5, { arg: Int -> arg + 13 }) == 18) "OK" else "fail"
|
||||
}
|
||||
|
||||
fun apply(arg: Int, f: (p: Int) -> Int): Int {
|
||||
return f(arg)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun f(a: Int = 2, b: Int = 3) = a + b
|
||||
|
||||
fun box(): String {
|
||||
if (f(1, 2) != 3) return "fail1"
|
||||
if (f(1, 3) != 4) return "fail2"
|
||||
if (f(3) != 6) return "fail3"
|
||||
if (f() != 5) return "fail4"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
class Point(val x: Int, val y: Int) {
|
||||
fun mul(): (scalar: Int) -> Point {
|
||||
return { scalar: Int -> Point(x * scalar, y * scalar) }
|
||||
}
|
||||
}
|
||||
|
||||
val m = Point(2, 3).mul()
|
||||
|
||||
fun box(): String {
|
||||
val answer = m(5)
|
||||
return if (answer.x == 10 && answer.y == 15) "OK" else "FAIL"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val a = 23.(fun Int.(a: Int): Int = a * a + this)(3)
|
||||
if (a != 32) return "a != 32, a = $a";
|
||||
|
||||
return "OK";
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package foo
|
||||
|
||||
import java.util.*;
|
||||
|
||||
val d = { a: Int -> a + 1 }
|
||||
val p = { a: Int -> a * 3 }
|
||||
|
||||
val list = ArrayList<Function1<Int, Int>>();
|
||||
|
||||
fun chain(start: Int): Int {
|
||||
var res = start;
|
||||
for (func in list) {
|
||||
res = (func)(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (chain(0) != 0) {
|
||||
return "fail1"
|
||||
}
|
||||
list.add(d);
|
||||
if (list.get(0)(0) != 1) {
|
||||
return "fail2"
|
||||
}
|
||||
list.add(p);
|
||||
if (list.get(1)(10) != 30) {
|
||||
return "fail3"
|
||||
}
|
||||
if (chain(0) != 3) {
|
||||
return "fail4"
|
||||
}
|
||||
list.add({ it * it });
|
||||
list.add({ it - 100 });
|
||||
if (chain(2) != -19) {
|
||||
return "fail5"
|
||||
}
|
||||
if (({ a: Int -> a * a }(3)) != 9) {
|
||||
return "fail7"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
fun Any.foo1(): () -> String {
|
||||
return { "239" + this }
|
||||
}
|
||||
|
||||
fun Int.foo2(): (i: Int) -> Int {
|
||||
return { x -> x + this }
|
||||
}
|
||||
|
||||
fun <T> fooT1(t: T) = { t.toString() }
|
||||
|
||||
fun <T> fooT2(t: T) = { x: T -> t.toString() + x.toString() }
|
||||
|
||||
fun box(): Any? {
|
||||
if ( (10.foo1())() != "23910") return "foo1 fail"
|
||||
if ( (10.foo2())(1) != 11 ) return "foo2 fail"
|
||||
|
||||
if (1.(fun Int.(): Int = this + 1)() != 2) return "test 3 failed";
|
||||
if ( { 1 }() != 1) return "test 4 failed";
|
||||
if ( { x: Int -> x }(1) != 1) return "test 5 failed";
|
||||
if ( 1.(fun Int.(x: Int): Int = x + this)(1) != 2) return "test 6 failed";
|
||||
val tmp = 1.(fun Int.(): Int = this)()
|
||||
if (+tmp != 1) return "test 7 failed, res: $tmp ${tmp is Int}";
|
||||
if ( (fooT1<String>("mama"))() != "mama") return "test 8 failed";
|
||||
if ( (fooT2<String>("mama"))("papa") != "mamapapa") return "test 9 failed";
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
fun f() = 3
|
||||
|
||||
if ((f() + f()) != 6) return "fail1"
|
||||
if (b() != 24) return "fail2"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
fun b(): Int {
|
||||
|
||||
fun a(): Int {
|
||||
fun c() = 4
|
||||
return c() * 3
|
||||
}
|
||||
val a = 2
|
||||
return a() * a
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var sum = 0
|
||||
val addFive = { a: Int -> a + 5 }
|
||||
sum = addFive(sum)
|
||||
if (sum != 5) return "fail: $sum"
|
||||
return "OK"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
fun f(a: (Int) -> Int) = a(1)
|
||||
|
||||
fun box(): String {
|
||||
|
||||
if (f() {
|
||||
it + 2
|
||||
} != 3) return "fail1"
|
||||
|
||||
if (f() { a: Int -> a * 300 } != 300) return "fail2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package foo
|
||||
|
||||
fun apply(f: (Int) -> Int, t: Int): Int {
|
||||
return f(t)
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return if (apply({ a: Int -> a + 5 }, 3) == 8) return "OK" else "fail"
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return f()
|
||||
}
|
||||
|
||||
fun f(): String {
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user