JS: move more test to box tests

This commit is contained in:
Alexey Andreev
2016-08-30 14:59:26 +03:00
parent 7e2d5b04de
commit 3801052460
112 changed files with 760 additions and 676 deletions
@@ -0,0 +1,20 @@
package foo
open class A
class B : A() {
val a = 1
}
object O
interface I
enum class E {
X,
Y {
val a = 1
},
Z {}
}
@@ -0,0 +1,23 @@
package foo
fun <T> check(x: JsClass<T>, y: JsClass<T>, shouldBeEquals: Boolean = true) {
assertNotEquals(null, x)
assertNotEquals(null, y)
if (shouldBeEquals)
assertEquals(x, y)
else
assertNotEquals(x, y)
}
fun box(): String {
check(jsClass<A>(), A().jsClass)
check(jsClass<B>(), B().jsClass)
check(jsClass<O>(), O.jsClass)
assertNotEquals(null, jsClass<I>())
check(jsClass<E>(), E.X.jsClass)
check(jsClass<E>(), E.Y.jsClass, shouldBeEquals = false)
// TODO uncomment after KT-13338 is fixed
// check(jsClass<E>(), E.Z.jsClass)
return "OK"
}
@@ -0,0 +1,26 @@
package foo
fun testWithInstance() {
assertEquals("A", A().jsClass.name)
assertEquals("B", B().jsClass.name)
assertEquals("O", O.jsClass.name)
assertEquals("E", E.X.jsClass.name)
assertEquals("Y", E.Y.jsClass.name)
// TODO uncomment after KT-13338 is fixed
// assertEquals("E", E.Z.jsClass.name)
}
fun testWithClassReference() {
assertEquals("A", jsClass<A>().name)
assertEquals("B", jsClass<B>().name)
assertEquals("O", jsClass<O>().name)
assertEquals("I", jsClass<I>().name)
assertEquals("E", jsClass<E>().name)
}
fun box(): String {
testWithInstance()
testWithClassReference()
return "OK"
}
@@ -0,0 +1,15 @@
package foo
inline fun <reified T> foo(): JsClass<T> {
val T = 1
return jsClass<T>()
}
fun box(): String {
assertEquals(jsClass<A>(), foo<A>())
assertEquals(jsClass<B>(), foo<B>())
assertEquals(jsClass<O>(), foo<O>())
assertEquals(jsClass<E>(), foo<E>())
return "OK"
}
+22
View File
@@ -0,0 +1,22 @@
// KT-2470 another name mangling bug: kotlin.test.failsWith() gets generated to invalid JS
package foo
public fun <T : Throwable> failsWith(block: () -> Any): T {
try {
block()
}
catch (e: T) {
return e
}
throw Exception("Should have failed")
}
fun box(): String {
val a = failsWith<Exception> {
throw Exception("OK")
}
return a.message!!
}
@@ -0,0 +1,37 @@
package foo
val x: Int?
get() = null
// Note: `x ?: 2` expression used to force to create tempary variable
class A {
val a = x ?: 2
}
enum class E(val a: Int = 0) {
X(),
Y() {
val y = x ?: 4
override fun value() = y
};
val e = x ?: 3
open fun value() = e
}
open class B(val b: Int)
class C : B(x ?: 6)
fun box(): String {
assertEquals(2, A().a)
assertEquals(3, E.X.e)
assertEquals(4, E.Y.value())
assertEquals(6, C().b)
return "OK"
}
+4
View File
@@ -0,0 +1,4 @@
package foo
inline fun <reified T> isInstance(x: Any?): Boolean =
x is T
+18
View File
@@ -0,0 +1,18 @@
package foo
// CHECK_NOT_CALLED: test
// CHECK_NOT_CALLED: test1
class A
class B
inline fun <reified T> test(x: Any): Boolean = test1<T>(x)
inline fun <reified R> test1(x: Any): Boolean = x is R
fun box(): String {
assertEquals(true, test<A>(A()), "test<A>(A())")
assertEquals(false, test<A>(B()), "test<A>(B())")
return "OK"
}
+17
View File
@@ -0,0 +1,17 @@
package foo
// CHECK_NOT_CALLED: canBeCastedTo
open class A
class B
class C : A()
inline fun <reified T> Any.canBeCastedTo(): Boolean = this is T
fun box(): String {
assertEquals(true, A().canBeCastedTo<A>(), "A().canBeCastedTo<A>()")
assertEquals(false, A().canBeCastedTo<B>(), "A().canBeCastedTo<B>()")
assertEquals(true, C().canBeCastedTo<A>(), "C().canBeCastedTo<A>()")
return "OK"
}
@@ -0,0 +1,21 @@
package foo
class A
class B
fun <T, R> apply(x: T, fn: T.()->R): R = x.fn()
inline fun <reified T, reified R> test(x: Any, y: Any): Boolean =
x is T && apply(y) { this is R }
fun box(): String {
val a = A()
val b = B()
assertEquals(true, test<A, B>(a, b), "test<A, B>(a, b)")
assertEquals(false, test<A, B>(a, a), "test<A, B>(a, a)")
assertEquals(false, test<A, B>(b, b), "test<A, B>(b, b)")
assertEquals(false, test<A, B>(b, a), "test<A, B>(b, a)")
return "OK"
}
@@ -0,0 +1,27 @@
package foo
// CHECK_NOT_CALLED: test
// CHECK_NOT_CALLED: fn
class A(val x: Any? = null) {
inline fun <reified T, reified R> test(b: B) = b.fn<T, R>()
inline fun <reified T, reified R> B.fn() = x is T && y is R
}
class B(val y: Any? = null)
class X
class Y
fun box(): String {
val x = X()
val y = Y()
assertEquals(true, A(x).test<X, Y>(B(y)), "A(x).test<X, Y>(B(y))")
assertEquals(false, A(y).test<X, Y>(B(y)), "A(y).test<X, Y>(B(y))")
assertEquals(false, A(y).test<X, Y>(B(x)), "A(y).test<X, Y>(B(x))")
assertEquals(false, A(x).test<X, Y>(B(x)), "A(x).test<X, Y>(B(x))")
return "OK"
}
+31
View File
@@ -0,0 +1,31 @@
package foo
// CHECK_NOT_CALLED: typePredicate
open class A
class B
class C : A()
interface TypePredicate {
operator fun invoke(x: Any): Boolean
}
inline fun <reified T> typePredicate(): TypePredicate =
object : TypePredicate {
override fun invoke(x: Any): Boolean = x is T
}
fun box(): String {
val isA = typePredicate<A>()
val a: Any = A()
val b: Any = B()
val c: Any = C()
assertEquals(true, isA(a), "isA(a)")
assertEquals(false, isA(b), "isA(b)")
assertEquals(true, isA(c), "isA(c)")
return "OK"
}
+15
View File
@@ -0,0 +1,15 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<Boolean>(true))
assertEquals(true, isInstance<Boolean>(false))
assertEquals(false, isInstance<Boolean>("true"))
assertEquals(true, isInstance<Boolean?>(true), "isInstance<Boolean?>(true)")
assertEquals(true, isInstance<Boolean?>(null), "isInstance<Boolean?>(null)")
assertEquals(false, isInstance<Boolean?>("true"), "isInstance<Boolean?>(\"true\")")
return "OK"
}
+15
View File
@@ -0,0 +1,15 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<Char>('c'))
assertEquals(false, isInstance<Char>(""))
assertEquals(false, isInstance<Char>("cc"))
assertEquals(true, isInstance<Char?>('c'), "isInstance<Char?>('c')")
assertEquals(true, isInstance<Char?>(null), "isInstance<Char?>(null)")
assertEquals(false, isInstance<Char?>("cc"), "isInstance<Char?>(\"cc\")")
return "OK"
}
+25
View File
@@ -0,0 +1,25 @@
package foo
// CHECK_NOT_CALLED: isInstance
interface A
interface B
open class C : A, B
class D : C()
fun box(): String {
assertEquals(false, isInstance<A>(0))
assertEquals(false, isInstance<A>(""))
assertEquals(true, isInstance<A>(C()))
assertEquals(true, isInstance<A>(D()))
assertEquals(true, isInstance<D>(D()))
assertEquals(true, isInstance<C>(D()))
assertEquals(true, isInstance<D?>(D()), "isInstance<D?>(D())")
assertEquals(true, isInstance<D?>(null), "isInstance<D?>(null)")
assertEquals(false, isInstance<D?>(C()), "isInstance<D?>(C())")
return "OK"
}
+23
View File
@@ -0,0 +1,23 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<Short>(0.toShort()))
assertEquals(true, isInstance<Byte>(0.toByte()))
assertEquals(true, isInstance<Int>(0))
assertEquals(true, isInstance<Long>(0.toLong()))
assertEquals(true, isInstance<Double>(0.toDouble()))
assertEquals(true, isInstance<Float>(0.toFloat()))
assertEquals(true, isInstance<Number>(0))
assertEquals(true, isInstance<Number>(0.toLong()))
assertEquals(true, isInstance<Number>(0.0))
assertEquals(false, isInstance<Number>("0"))
assertEquals(true, isInstance<Int?>(0), "isInstance<Int?>(0)")
assertEquals(true, isInstance<Int?>(null), "isInstance<Int?>(null)")
assertEquals(false, isInstance<Int?>(true), "isInstance<Int?>(true)")
return "OK"
}
+14
View File
@@ -0,0 +1,14 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<String>(""))
assertEquals(true, isInstance<String>("a"))
assertEquals(true, isInstance<String?>(""), "isInstance<String?>(\"\")")
assertEquals(true, isInstance<String?>(null), "isInstance<String?>(null)")
assertEquals(false, isInstance<String?>(10), "isInstance<String?>(10)")
return "OK"
}
+21
View File
@@ -0,0 +1,21 @@
package foo
// CHECK_NOT_CALLED: isTypeOfOrNull
// CHECK_NULLS_COUNT: function=box count=10
inline
fun <reified T> Any?.isTypeOfOrNull() = this is T?
class A
class B
fun box(): String {
assertEquals(true, null.isTypeOfOrNull<A>(), "null.isTypeOfOrNull<A>()")
assertEquals(true, null.isTypeOfOrNull<A?>(), "null.isTypeOfOrNull<A?>()")
assertEquals(true, A().isTypeOfOrNull<A>(), "A().isTypeOfOrNull<A>()")
assertEquals(true, A().isTypeOfOrNull<A?>(), "A().isTypeOfOrNull<A?>()")
assertEquals(false, A().isTypeOfOrNull<B>(), "A().isTypeOfOrNull<B>()")
assertEquals(false, A().isTypeOfOrNull<B?>(), "A().isTypeOfOrNull<B?>()")
return "OK"
}
+39
View File
@@ -0,0 +1,39 @@
package foo
// CHECK_CALLED: doFilter
// CHECK_NOT_CALLED: filterIsInstance
data class A(val x: Int)
data class B(val x: Int)
// filter from stdlib is not used, because it's important,
// that filter function is not inline. When lambda is
// not inlined and captures some local variable,
// the test crashes on runtime (it's expected behaviour).
fun <T> Array<T>.doFilter(fn: (T)->Boolean): List<T> {
val filtered = arrayListOf<T>()
for (i in 0..lastIndex) {
val element = this[i]
if (fn(element)) {
filtered.add(element)
}
}
return filtered
}
inline fun <reified T> filterIsInstance(arrayOfAnys: Array<Any>): List<T> {
return arrayOfAnys.doFilter { it is T }.map { it as T }
}
fun box(): String {
val src: Array<Any> = arrayOf(A(1), B(2), A(3), B(4))
assertEquals(listOf(A(1), A(3)), filterIsInstance<A>(src))
assertEquals(listOf(B(2), B(4)), filterIsInstance<B>(src))
return "OK"
}
@@ -0,0 +1,32 @@
package foo
// CHECK_CALLED: doRun
// CHECK_NOT_CALLED: test
class X
class Y
fun <R> doRun(fn: ()->R): R = fn()
inline fun <reified A, reified B> test(x: Any, y: Any): Boolean =
doRun {
val isA = null
x is A
}
&& doRun {
val result = y is B
val isB = null
result
}
fun box(): String {
val x = X()
val y = Y()
assertEquals(true, test<X, Y>(x, y), "test<X, Y>(x, y)")
assertEquals(false, test<X, Y>(x, x), "test<X, Y>(x, x)")
assertEquals(false, test<X, Y>(y, x), "test<X, Y>(y, x)")
assertEquals(false, test<X, Y>(y, y), "test<X, Y>(y, y)")
return "OK"
}
+16
View File
@@ -0,0 +1,16 @@
package foo
// CHECK_NOT_CALLED: test
class A(val x: Any? = null) {
inline fun <reified T> test() = x is T
}
class B
fun box(): String {
assertEquals(true, A(A()).test<A>(), "A(A()).test<A>()")
assertEquals(false, A(B()).test<A>(), "A(B()).test<A>()")
return "OK"
}
@@ -0,0 +1,20 @@
package foo
class X
class Y
class Z
inline fun <reified A, B, reified C> test(x: Any): String =
when (x) {
is A -> "A"
is C -> "C"
else -> "Unknown"
}
fun box(): String {
assertEquals("A", test<X, Y, Z>(X()))
assertEquals("Unknown", test<X, Y, Z>(Y()))
assertEquals("C", test<X, Y, Z>(Z()))
return "OK"
}
@@ -0,0 +1,18 @@
package foo
// CHECK_NOT_CALLED: test
class A
inline fun <reified T> test(): String {
val a: Any = A()
return if (a is T) "A" else "Unknown"
}
fun box(): String {
assertEquals("A", test<A>())
assertEquals("Unknown", test<String>())
return "OK"
}
+22
View File
@@ -0,0 +1,22 @@
package foo
class A
class B
class C
inline fun <reified T, reified R> test(x: Any): String = test1<R, T>(x)
inline fun <reified R, reified T> test1(x: Any): String =
when (x) {
is R -> "R"
is T -> "T"
else -> "Unknown"
}
fun box(): String {
assertEquals("T", test<A, B>(A()), "test<T, R>(T())")
assertEquals("R", test<A, B>(B()), "test<T, R>(R())")
assertEquals("Unknown", test<A, B>(C()), "test<T, R>(L())")
return "OK"
}
+33
View File
@@ -0,0 +1,33 @@
package foo
// CHECK_NOT_CALLED: test
class A(val x: Int)
class B(val x: Int)
inline fun <reified T> test(vararg xs: Any): List<T> {
val ts = arrayListOf<T>()
for (x in xs) {
if (x is T) {
ts.add(x)
}
}
return ts
}
fun box(): String {
val a1 = A(1)
val b2 = B(2)
val a3 = A(3)
val b4 = B(4)
assertEquals(listOf(a1), test<A>(a1, b2), "test(a1, b2)")
assertEquals(listOf(b2, b4), test<B>(a1, b2, a3, b4), "test<B>(a1, b2, a3, b4)")
val objects = arrayOf(a1, b2)
assertEquals(listOf(b2, b4), test<B>(a1, a3, *objects, a3, b4), "test<B>(a1, a3, *objects, a3, b4)")
return "OK"
}
@@ -0,0 +1,21 @@
package foo
// NO_INLINE
// CHECK_CALLED_IN_SCOPE: scope=box function=isInstanceOf
class A
class B
fun box(): String {
val a = A()
val b = B()
assertEquals(true, isInstance<A>(a), "isInstance<A>(a)")
assertEquals(false, isInstance<A>(b), "isInstance<A>(b)")
assertEquals(true, isInstance<A?>(a), "isInstance<A?>(a)")
assertEquals(true, isInstance<A?>(null), "isInstance<A?>(null)")
assertEquals(false, isInstance<A?>(b), "isInstance<A?>(b)")
return "OK"
}
@@ -0,0 +1,96 @@
// KT-2468 ArrayList<String> is List<String> or HashSet<String> is Set<String> fails in generated JS code
package foo
class A
fun checkAbstractList(obj: Any) {
assertTrue(obj is AbstractMutableList<*>, "checkAbstractList: is AbstractMutableList")
assertTrue(obj is MutableList<*>, "checkAbstractList: is MutableList")
assertTrue(obj is List<*>, "checkAbstractList: is List")
assertTrue(obj is AbstractMutableCollection<*>, "checkAbstractList: is AbstractMutableCollection")
assertTrue(obj is MutableCollection<*>, "checkAbstractList: is MutableCollection")
assertTrue(obj is Collection<*>, "checkAbstractList: is Collection")
assertTrue(obj is MutableIterable<*>, "checkAbstractList: is MutableIterable")
assertTrue(obj is Iterable<*>, "checkAbstractList: is Iterable")
assertTrue((obj as Iterable<*>).iterator() is Iterator, "checkAbstractList: iterator() is Iterator")
}
fun checkArrayList(obj: Any) {
assertTrue(obj is ArrayList<*>, "checkArrayList: is ArrayList")
assertTrue((obj as Iterable<*>).iterator() is MutableIterator, "checkAbstractList: iterator() is MutableIterator")
checkAbstractList(obj)
}
fun checkHashSet(obj: Any) {
assertTrue(obj is HashSet<*>, "checkHashSet: is HashSet")
assertTrue(obj is AbstractMutableSet<*>, "checkHashSet: is AbstractMutableSet")
assertTrue(obj is AbstractMutableCollection<*>, "checkHashSet: is AbstractMutableCollection")
assertTrue(obj is MutableCollection<*>, "checkHashSet: is MutableCollection")
assertTrue(obj is Collection<*>, "checkHashSet: is Collection")
assertTrue(obj is MutableIterable<*>, "checkHashSet: is MutableIterable")
assertTrue(obj is Iterable<*>, "checkHashSet: is Iterable")
assertTrue(obj is MutableSet<*>, "checkHashSet: is MutableSet")
assertTrue(obj is Set<*>, "checkHashSet: is Set")
assertTrue((obj as Set<*>).iterator() is Iterator, "checkHashSet: iterator() is Iterator")
assertTrue((obj as Set<*>).iterator() is MutableIterator, "checkHashSet: iterator() is MutableIterator")
}
fun checkLinkedHashSet(obj: Any) {
assertTrue(obj is LinkedHashSet<*>, "checkLinkedHashSet: is LinkedHashSet")
checkHashSet(obj)
}
fun checkHashMap(obj: Any) {
assertTrue(obj is HashMap<*, *>, "checkHashMap: is HashMap")
assertTrue(obj is MutableMap<*, *>, "checkHashMap: is MutableMap")
assertTrue(obj is Map<*, *>, "checkHashMap: is Map")
assertTrue((obj as Map<*, *>).values is Collection, "checkHashMap: values is Collection")
assertTrue((obj as Map<*, *>).keys is Set, "checkHashMap: keys is Set")
}
fun checkLinkedHashMap(obj: Any) {
assertTrue(obj is LinkedHashMap<*, *>, "checkLinkedHashMap: is LinkedHashMap")
checkHashMap(obj)
}
fun box(): String {
checkArrayList(ArrayList<Int>())
checkArrayList(arrayListOf(1, 2, 3))
checkArrayList(ArrayList<String>())
checkArrayList(arrayListOf("first", "second"))
checkArrayList(ArrayList<A>())
checkArrayList(arrayListOf(A()))
checkHashSet(HashSet<Int>())
checkHashSet(hashSetOf(1))
checkLinkedHashSet(LinkedHashSet<Int>(0))
checkHashSet(HashSet<Double>())
checkHashSet(hashSetOf(1.0))
checkLinkedHashSet(LinkedHashSet<Double>(0))
checkHashSet(HashSet<String>())
checkHashSet(hashSetOf("test"))
checkLinkedHashSet(LinkedHashSet<String>(0))
checkHashSet(HashSet<A>())
checkHashSet(hashSetOf(A()))
checkLinkedHashSet(LinkedHashSet<A>(0))
checkHashMap(HashMap<Int, String>())
checkHashMap(hashMapOf(1 to "test"))
checkLinkedHashMap(LinkedHashMap<Int, String>())
checkHashMap(HashMap<Double, String>())
checkHashMap(hashMapOf(1.0 to "test"))
checkLinkedHashMap(LinkedHashMap<Double, String>())
checkHashMap(HashMap<String, String>())
checkHashMap(HashMap<A, String>())
checkLinkedHashMap(LinkedHashMap<A, String>())
return "OK"
}
@@ -0,0 +1,25 @@
package foo
class A : Comparable<A> {
override fun compareTo(other: A): Int = 0
}
class B
fun test(x: Any?): Boolean = x is Comparable<*>
fun box(): String {
assertEquals(true, test(A()), "A()")
assertEquals(true, test("abc"), "\"abc\"")
assertEquals(true, test('a'), "\'a\'")
assertEquals(true, test(0), "0")
assertEquals(true, test(0.toChar()), "0.toChar()")
assertEquals(true, test(0.toByte()), "0.toByte()")
assertEquals(true, test(0.toShort()), "0.toShort()")
assertEquals(true, test(0.toLong()), "0.toLong()")
assertEquals(true, test(0.toDouble()), "0.toDouble()")
assertEquals(true, test(0.toFloat()), "0.toFloat()")
assertEquals(false, test(B()), "B()")
return "OK"
}
@@ -0,0 +1,32 @@
package foo
enum class Type {
NUMBER,
STRING,
BOOLEAN,
OBJECT
}
fun test(a: Any, actualType: Type) {
assertEquals(actualType == Type.NUMBER, a is Int, "$a is Int")
assertEquals(actualType == Type.NUMBER, a is Number, "$a is Number")
assertEquals(actualType == Type.NUMBER, a is Double, "$a is Double")
assertEquals(actualType == Type.BOOLEAN, a is Boolean, "$a is Boolean")
assertEquals(actualType == Type.STRING, a is String, "$a is String")
}
fun box(): String {
test(1, Type.NUMBER)
test(12.3, Type.NUMBER)
test(12.3f, Type.NUMBER)
test("text", Type.STRING)
test(true, Type.BOOLEAN)
test(false, Type.BOOLEAN)
test(object {}, Type.OBJECT)
return "OK"
}
+11
View File
@@ -0,0 +1,11 @@
package foo
object Obj
fun box(): String {
val r: Any = Obj
if (r !is Obj) return "r !is Obj"
return "OK"
}
+10
View File
@@ -0,0 +1,10 @@
package foo
class A() {
}
fun box(): String {
assertEquals(true, A() is A)
return "OK"
}
@@ -0,0 +1,14 @@
package foo
open class A() {
}
class B() : A() {
}
fun box(): String {
assertEquals(true, A() !is B)
return "OK"
}
@@ -0,0 +1,32 @@
package foo
interface A
interface B
open class C
open class D
fun box(): String {
if ((object {} as Any) is A) return "object {} is A"
if ((object {} as Any) is C) return "object {} is C"
if ((object : A {} as Any) !is A) return "object : A {} !is A"
if ((object : A {} as Any) is B) return "object : A {} is B"
if ((object : A {} as Any) is C) return "object : A {} is C"
if ((object : C() {} as Any) is A) return "object : C() {} is A"
if ((object : C() {} as Any) !is C) return "object : C() {} !is C"
if ((object : C() {} as Any) is D) return "object : C() {} is D"
if ((object : B, D() {} as Any) is A) return "object : B, D() {} is A"
if ((object : B, D() {} as Any) !is B) return "object : B, D() {} !is B"
if ((object : B, D() {} as Any) is C) return "object : B, D() {} is C"
if ((object : B, D() {} as Any) !is D) return "object : B, D() {} !is D"
if ((object : D(), B {} as Any) is A) return "object : D(), B {} is A"
if ((object : D(), B {} as Any) !is B) return "object : D(), B {} !is B"
if ((object : D(), B {} as Any) is C) return "object : D(), B {} is C"
if ((object : D(), B {} as Any) !is D) return "object : D(), B {} !is D"
return "OK"
}
@@ -0,0 +1,18 @@
package foo
class C
interface I
fun box(): String {
val obj: Any = js("({})")
if (obj is C) return "obj is C"
if (obj is I) return "obj is I"
val obj2: Any = js("Object.create(null)")
if (obj2 is C) return "obj2 is C"
if (obj2 is I) return "obj2 is I"
return "OK"
}
@@ -0,0 +1,31 @@
package foo
class D
open class A
open class B : A()
open class C : B()
fun box(): String {
val a: Any = A()
val b: Any = B()
val c: Any = C()
if (a !is A) return "a !is A"
val t = a is A
if (!t) return "t = a is A; t != true"
if (b !is A) return "b !is A"
if (b !is B) return "b !is B"
if (c !is A) return "c !is A"
if (c !is B) return "c !is B"
if (c !is C) return "c !is C"
if (a is D) return "a is D"
if (b is D) return "b is D"
return "OK"
}
@@ -0,0 +1,21 @@
package foo
open class A
interface B
class C : A(), B
fun box(): String {
val a = A()
val b = object : B {
}
val c = C()
if (a is B) return "a is B"
if (b !is B) return "b !is B"
if (c !is B) return "c !is B"
return "OK"
}
@@ -0,0 +1,37 @@
package foo
interface A
interface B : A
interface C : A
interface D : B, C
interface E : C
open class CB : B
class CB2 : CB()
class CD : D
fun testPhrase(o: Any): String {
var s = ""
s += if (o is A) "Y" else "N"
s += if (o is B) "Y" else "N"
s += if (o is C) "Y" else "N"
s += if (o is D) "Y" else "N"
s += if (o is E) "Y" else "N"
return s
}
fun box(): String {
val b = CB()
val b2 = CB2()
val d = CD()
val e = object : E {
}
if (testPhrase(b) != "YYNNN") return "bad b, it: ${testPhrase(b)}"
if (testPhrase(b2) != "YYNNN") return "bad b2, it: ${testPhrase(b2)}"
if (testPhrase(d) != "YYYYN") return "bad d, it: ${testPhrase(d)}"
if (testPhrase(e) != "YNYNY") return "bad e, it: ${testPhrase(e)}"
return "OK"
}
@@ -0,0 +1,21 @@
package foo
var counter = 0
class A {
fun getCounter(): Any? =
when (counter++) {
0 -> "0"
1 -> null
else -> counter
}
}
fun box(): String {
assertEquals(false, A().getCounter() is Int?, "(1)")
assertEquals(true, A().getCounter() is Int?, "(2)")
assertEquals(true, A().getCounter() is Int?, "(3)")
assertEquals(3, counter)
return "OK"
}
@@ -0,0 +1,22 @@
package foo
var counter = 0
class A {
val x: Any?
get() =
when (counter++) {
0 -> "0"
1 -> null
else -> counter
}
}
fun box(): String {
assertEquals(false, A().x is Int?, "(1)")
assertEquals(true, A().x is Int?, "(2)")
assertEquals(true, A().x is Int?, "(3)")
assertEquals(3, counter)
return "OK"
}
@@ -0,0 +1,19 @@
// KT-5192 JS compiler fails to generate correct code for List implementation
package foo
import java.util.ArrayList
class stdlib_emptyListClass : List<Any> by ArrayList<Any>() {}
fun box(): String {
assertTrue(stdlib_emptyListClass() is List<*>, "stdlib_emptyListClass() is List<*> #1")
assertTrue((stdlib_emptyListClass() as Any) is List<*>, "stdlib_emptyListClass() is List<*> #2")
assertTrue((stdlib_emptyListClass() as Any) !is ArrayList<*>, "stdlib_emptyListClass() !is ArrayList<*>")
assertTrue(stdlib_emptyListClass().isEmpty(), "stdlib_emptyListClass().isEmpty()")
assertTrue(stdlib_emptyListClass().size == 0, "stdlib_emptyListClass().size == 0")
return "OK"
}
+22
View File
@@ -0,0 +1,22 @@
package foo
class A() {
var c = 3
}
fun box(): String {
var a1: A? = A()
var a2: A? = null
a1?.c = 4
a2?.c = 5
if (a1?.c != 4) {
return "fail1"
}
a2 = a1
a2?.c = 5
if (a2?.c != 5) return "fail2"
if (a1?.c != 5) return "fail3"
return "OK"
}
+12
View File
@@ -0,0 +1,12 @@
package foo
class A() {
fun doSomething() {
}
}
fun box(): String {
var a: A? = null;
a?.doSomething()
return "OK"
}
@@ -0,0 +1,28 @@
package foo
var c1 = 0;
fun getInt(): Int? {
c1++
return c1
}
fun getNullInt(): Int? = null
fun box(): String {
if (c1 != 0) {
return "Start value of counter not 0, it: $c1"
}
val nullRes = getNullInt()?.toString()
if (nullRes != null) {
return "Broken safeCall. nullRes: $nullRes"
}
val res = getInt()?.toString()
if (res != "1") {
return "res != 1, it: $res, and c1: $c1"
}
if (c1 != 1) {
return "Side effect. c1 != 1, it: $c1"
}
return "OK"
}
@@ -0,0 +1,79 @@
package foo
var c1 = 0
var c2 = 0
var c3 = 0
var c4 = 0
var c5 = 0
fun toStr(): String {
return "$c1$c2$c3$c4$c5"
}
fun getA(): A? {
c1++
return A()
}
fun getNullA(): A? {
c1++
return null
}
class A {
fun someFun(): Int {
c2++
return 1
}
val b: B
get() {
c3++
return B()
}
}
fun A.extFun(): Int {
c4++
return 3
}
class B {
operator fun invoke(): Int {
c5++
return 2
}
}
fun box(): String {
val n1 = getNullA()?.someFun()
if (n1 != null || toStr() != "10000") {
return "Bad call getNullA()?.someFun(). result: $n1, counters: ${toStr()}"
}
val n2 = getNullA()?.b?.invoke()
if (n2 != null || toStr() != "20000") {
return "Bad call getNullA()?.b(). result: $n2, counters: ${toStr()}"
}
val n3 = getNullA()?.extFun()
if (n3 != null || toStr() != "30000") {
return "Bad call getNullA()?.extFun(). result: $n3, counters: ${toStr()}"
}
val i1 = getA()?.someFun()
if (i1 != 1 || toStr() != "41000") {
return "Bad call getA()?.someFun(). result: $i1, counters: ${toStr()}"
}
val i2 = getA()?.b?.invoke()
if (i2 != 2 || toStr() != "51101") {
return "Bad call getA()?.b(). result: $i2, counters: ${toStr()}"
}
val i3 = getA()?.extFun()
if (i3 != 3 || toStr() != "61111") {
return "Bad call getA()?.extFun(). result: $i3, counters: ${toStr()}"
}
return "OK"
}
@@ -0,0 +1,10 @@
package foo
class A() {
val x = 4
}
fun box(): String {
var a: A? = null;
return if (a?.x == null) "OK" else "fail"
}
@@ -0,0 +1,36 @@
package foo
class A() {
var c = 3
}
fun A.i(): Int {
c = c + 1
return c
}
fun box(): String {
var a1: A? = A()
var a2: A? = null
if (a1?.i() != 4) {
return "1";
}
if (a1?.c != 4) {
return "2";
}
if (a2?.c != null) {
return "3";
}
a2?.i()
if (a1?.c != 4) {
return "4";
}
a2 = a1
if (a2?.i() != 5) {
return "5";
}
if ((a2?.i() != 6) || (a1?.c != 6)) {
return "6"
}
return "OK"
}
+9
View File
@@ -0,0 +1,9 @@
package foo
fun f(): Int {
var x: Int = 1
x = x + 1
return x
}
fun box() = if (f() == 2) "OK" else "fail"
+14
View File
@@ -0,0 +1,14 @@
package foo
fun box(): String {
var i = 0
do {
if (i == 3) {
break;
}
++i;
}
while (i < 100)
return if (i == 3) "OK" else "fail: $i"
}
+13
View File
@@ -0,0 +1,13 @@
package foo
fun box(): String {
var i = 0
while (i < 100) {
if (i == 3) {
break;
}
++i;
}
return if (i == 3) "OK" else "fail: $i"
}
@@ -0,0 +1,9 @@
package foo
class Test() {
}
fun box(): String {
var test = Test()
return "OK"
}
+9
View File
@@ -0,0 +1,9 @@
package foo
fun box(): String {
val a = 2;
val b = 3;
var c = 4;
return if (a < c) "OK" else "fail"
}
@@ -0,0 +1,14 @@
package foo
class Test(a: Int, b: Int) {
val c = a
val d = b
}
fun box(): String {
val test = Test(1 + 6 * 3, 10 % 2)
if (test.c != 19) return "fail1"
if (test.d != 0) return "fail2"
return "OK"
}
@@ -0,0 +1,10 @@
package foo
class Test(a: Int) {
val b = a
}
fun box(): String {
var test = Test(1)
return if (test.b == 1) "OK" else "fail"
}
@@ -0,0 +1,12 @@
package foo
class A(var b: Int, var a: String) {
}
fun box(): String {
val c = A(1, "1")
c.b = 2
c.a = "2"
return if (c.b == 2 && c.a == "2") "OK" else "fail"
}
+16
View File
@@ -0,0 +1,16 @@
package foo
fun box(): String {
var i = 0
var b = true
do {
++i;
if (i >= 1) {
continue;
}
b = false;
}
while (i < 100)
return if (b) "OK" else "fail"
}
+15
View File
@@ -0,0 +1,15 @@
package foo
fun box(): String {
var i = 0
var b = true
while (i < 100) {
++i;
if (i >= 1) {
continue;
}
b = false;
}
return if (b) "OK" else "fail"
}
+18
View File
@@ -0,0 +1,18 @@
package foo
fun box(): String {
val a = 50;
var b = 0;
var c = 0;
do {
b = b + 1;
c = c + 2;
}
while (b < a)
if (c == 100) {
return "OK"
}
return "fail: $c"
}
+14
View File
@@ -0,0 +1,14 @@
package foo
fun box(): String {
var x = 2;
do {
x = 1;
}
while (3 < 2)
if (x == 1) {
return "OK"
}
return "fail: $x"
}
+21
View File
@@ -0,0 +1,21 @@
package foo
fun bor(): Int {
val a = 2;
val b = 3;
var c = 4;
if (a < 2) {
return a;
}
else if (a > 2) {
return b;
}
else if (a == c) {
return c;
}
else {
return 5;
}
}
fun box() = if (bor() == 5) "OK" else "fail"
+22
View File
@@ -0,0 +1,22 @@
package foo
fun bol(): Int {
val a = 2;
val b = 3;
var c = 4;
if (a < 2) {
return a;
}
if (a > 2) {
return b;
}
if (a == c) {
return c;
}
else {
return 5;
}
}
fun box() = if (bol() == 5) "OK" else "fail"
@@ -0,0 +1,6 @@
package foo
fun box(): String {
val a = 2;
return if (a == 2) "OK" else "fail"
}
@@ -0,0 +1,12 @@
package foo
class Test() {
fun method(): String {
return "OK"
}
}
fun box(): String {
var test = Test()
return test.method()
}
@@ -0,0 +1,10 @@
package foo
var a = 3
fun box(): String {
a -= 10
return if (a == -7) "OK" else "fail"
}
+5
View File
@@ -0,0 +1,5 @@
package foo
fun box(): String {
return if (!false) "OK" else "fail"
}
+8
View File
@@ -0,0 +1,8 @@
package foo
fun box(): String {
var a = 3
a += 3
return if (a == 6) "OK" else "fail"
}
@@ -0,0 +1,7 @@
package foo
fun box(): String {
val b = -3
val c = +3
return if ((c - b) == 6) "OK" else "fail"
}
@@ -0,0 +1,10 @@
package foo
fun box(): String {
var a = 3;
val b = a++;
a--;
a--;
return if ((a++ == 2) && (b == 3)) "OK" else "fail"
}
@@ -0,0 +1,10 @@
package foo
fun box(): String {
var a = 3;
val b = ++a;
--a;
--a;
return if ((--a == 1) && (b == 4)) "OK" else "fail"
}
@@ -0,0 +1,10 @@
package foo
class A(var b: Int, var a: String) {
}
fun box(): String {
val c = A(2, "2")
return if (c.b == 2 && c.a == "2") "OK" else "fail"
}
@@ -0,0 +1,9 @@
package foo
class Test() {
val p = "OK"
}
fun box(): String {
return Test().p
}
@@ -0,0 +1,12 @@
package foo
class Test() {
var a: Int
init {
a = 3
}
}
fun box(): String {
return if (Test().a == 3) "OK" else "fail"
}
+17
View File
@@ -0,0 +1,17 @@
package foo
fun box(): String {
val a = 50;
var b = 0;
var c = 0;
while (b < a) {
b = b + 1;
c = c + 2;
}
if (c == 100) {
return "OK"
}
return "fail: $c"
}
+10
View File
@@ -0,0 +1,10 @@
package foo
fun box(): String {
while (2 < 1) {
return "fail"
}
return "OK"
}
@@ -0,0 +1,9 @@
package foo
fun box(): String {
val a = arrayOfNulls<Int>(2)
a.set(1, 2)
return if (a.get(1) == 2) "OK" else "fail"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
val a = arrayOfNulls<Int>(4)
a[1] = 2
a[2] = 3
return if ((a[1] == 2) && (a[2] == 3)) "OK" else "fail"
}
@@ -0,0 +1,25 @@
package foo
fun box(): String {
val s = Array<String>(3) { it.toString() }
if (s.size != 3) return "Fail Array size: ${s.size}"
if (s[1] != "1") return "Fail Array value: ${s[1]}"
val i = IntArray(3) { it }
if (i.size != 3) return "Fail IntArray size: ${i.size}"
if (i[1] != 1) return "Fail IntArray value: ${i[1]}"
val c = CharArray(3) { it.toChar() }
if (c.size != 3) return "Fail CharArray size: ${c.size}"
if (c[1] != 1.toChar()) return "Fail CharArray value: ${c[1]}"
val b = BooleanArray(3) { true }
if (b.size != 3) return "Fail BooleanArray size: ${b.size}"
if (b[1] != true) return "Fail BooleanArray value: ${b[1]}"
val l = LongArray(3) { it.toLong() }
if (l.size != 3) return "Fail LongArray size: ${l.size}"
if (l[1] != 1L) return "Fail LongArray value: ${l[1]}"
return "OK"
}
@@ -0,0 +1,38 @@
package foo
class Fail(message: String) : Exception(message)
fun test(testName: String, actual: Any, expectedAsString: String) {
val expected = eval("[$expectedAsString]") as Array<in Any>
val expectedJs: dynamic = expected
val actualJs: dynamic = actual
if (expectedJs.length != actualJs.length) {
fail("Lengths do not match: ${expectedJs} vs. ${actualJs}")
}
for (index in 0..(expectedJs.length)) {
val expectedElem = expectedJs[index] as Any?
val actualElem = actualJs[index] as Any?
if (expectedElem != actualElem) {
fail("Content do not match: ${expectedJs} vs. ${actualJs}")
}
}
}
fun box(): String {
try {
test("arrayOf", arrayOf(0, 1, 2, 3, 4), "0, 1, 2, 3, 4")
test("booleanArrayOf", booleanArrayOf(true, false, false, true, true), "true, false, false, true, true")
test("charArray'", charArrayOf('0', '1', '2', '3', '4'), "'0', '1', '2', '3', '4'")
test("byteArrayOf", byteArrayOf(0, 1, 2, 3, 4), "0, 1, 2, 3, 4")
test("shortArrayOf", shortArrayOf(0, 1, 2, 3, 4), "0, 1, 2, 3, 4")
test("intArray,", intArrayOf(0, 1, 2, 3, 4), "0, 1, 2, 3, 4")
test("longArrayOf", longArrayOf(0, 1, 2, 3, 4), "kotlin.Long.fromInt(0), kotlin.Long.fromInt(1), kotlin.Long.fromInt(2), kotlin.Long.fromInt(3), kotlin.Long.fromInt(4)")
test("floatArrayOf", floatArrayOf(0.0f, 1.0f, 2.0f, 3.0f, 4.0f), "0.0, 1.0, 2.0, 3.0, 4.0")
test("doubleArrayOf", doubleArrayOf(0.0, 1.1, 2.2, 3.3, 4.4), "0.0, 1.1, 2.2, 3.3, 4.4")
}
catch (e: Fail) {
return e.message!!
}
return "OK"
}
@@ -0,0 +1,7 @@
package foo
val f = { i: Int -> i + 1 }
val a = Array(3, f)
fun box() = if (a[0] == 1 && a[2] == 3 && a[1] == 2) "OK" else "fail"
@@ -0,0 +1,5 @@
package foo
val a = arrayOfNulls<Int>(3)
fun box() = if (a[0] == null && a[1] == null && a[2] == null) "OK" else "fail"
@@ -0,0 +1,9 @@
package foo
class A() {
}
val a1 = arrayOfNulls<Int>(3)
val a2 = arrayOfNulls<A>(2)
fun box() = if (a1.size == 3 && a2.size == 2) "OK" else "fail"
@@ -0,0 +1,16 @@
package foo
val a1 = Array<Int>(3, { i: Int -> i })
fun box(): String {
val i = a1.iterator()
if (i.hasNext() != true) return "fail1"
if (i.next() != 0) return "fail2"
if (i.hasNext() != true) return "fail3"
if (i.next() != 1) return "fail4"
if (i.hasNext() != true) return "fail5"
if (i.next() != 2) return "fail6"
if (i.hasNext() != false) return "fail7"
return "OK"
}
@@ -0,0 +1,86 @@
package foo
import java.util.HashMap
fun box(): String {
val x = true
val y = false
val mapWithIntKeys = HashMap<Int, Int>()
mapWithIntKeys[1] = 1
assertEquals("number", jsTypeOf (mapWithIntKeys.keys.iterator().next()), "mapWithIntKeys")
val mapWithShortKeys = HashMap<Short, Int>()
mapWithShortKeys[1.toShort()] = 1
assertEquals("number", jsTypeOf (mapWithShortKeys.keys.iterator().next()), "mapWithShortKeys")
val mapWithByteKeys = HashMap<Byte, Int>()
mapWithByteKeys[1.toByte()] = 1
assertEquals("number", jsTypeOf (mapWithByteKeys.keys.iterator().next()), "mapWithByteKeys")
val mapWithDoubleKeys = HashMap<Double, Int>()
mapWithDoubleKeys[1.0] = 1
assertEquals("number", jsTypeOf (mapWithDoubleKeys.keys.iterator().next()), "mapWithDoubleKeys")
// boxed NaNs are not equal, KT-13610
// mapWithDoubleKeys.clear()
// var dNaN = 0.0 / 0.0
// mapWithDoubleKeys[dNaN] = 100
// assertEquals(100, mapWithDoubleKeys[dNaN])
// assertEquals("number", jsTypeOf (mapWithDoubleKeys.keys.iterator().next()), "dNaN")
mapWithDoubleKeys.clear()
var dPositiveInfinity = +1.0 / 0.0
mapWithDoubleKeys[dPositiveInfinity] = 100
assertEquals(100, mapWithDoubleKeys[dPositiveInfinity])
assertEquals("number", jsTypeOf (mapWithDoubleKeys.keys.iterator().next()), "dPositiveInfinity")
mapWithDoubleKeys.clear()
var dNegativeInfinity = -1.0 / 0.0
mapWithDoubleKeys[dNegativeInfinity] = 100
assertEquals(100, mapWithDoubleKeys[dNegativeInfinity])
assertEquals("number", jsTypeOf (mapWithDoubleKeys.keys.iterator().next()), "dNegativeInfinity")
val mapWithFloatKeys = HashMap<Float, Int>()
mapWithFloatKeys[1.0f] = 1
assertEquals("number", jsTypeOf (mapWithFloatKeys.keys.iterator().next()), "mapWithFloatKeys")
// boxed NaNs are not equal, KT-13610
// mapWithFloatKeys.clear()
// var fNaN: Float = 0.0f / 0.0f
// mapWithFloatKeys[fNaN] = 100
// assertEquals(100, mapWithFloatKeys[fNaN])
// assertEquals("number", jsTypeOf (mapWithFloatKeys.keys.iterator().next()), "fNaN")
mapWithFloatKeys.clear()
var fPositiveInfinity = +1.0f / 0.0f
mapWithFloatKeys[fPositiveInfinity] = 100
assertEquals(100, mapWithFloatKeys[fPositiveInfinity])
assertEquals("number", jsTypeOf (mapWithFloatKeys.keys.iterator().next()), "fPositiveInfinity")
mapWithFloatKeys.clear()
var NegativeInfinity = -1.0f / 0.0f
mapWithFloatKeys[NegativeInfinity] = 100
assertEquals(100, mapWithFloatKeys[NegativeInfinity])
assertEquals("number", jsTypeOf (mapWithFloatKeys.keys.iterator().next()), "fNegativeInfinity")
val mapWithCharKeys = HashMap<Char, Int>()
mapWithCharKeys['A'] = 1
assertEquals("string", jsTypeOf (mapWithCharKeys.keys.iterator().next()), "mapWithCharKeys")
val mapWithLongKeys = HashMap<Long, Int>()
mapWithLongKeys[1L] = 1
assertEquals("object", jsTypeOf (mapWithLongKeys.keys.iterator().next()), "mapWithLongKeys")
val mapWithBooleanKeys = HashMap<Boolean, Int>()
mapWithBooleanKeys[true] = 1
assertEquals("boolean", jsTypeOf (mapWithBooleanKeys.keys.iterator().next()), "mapWithBooleanKeys")
val mapWithStringKeys = HashMap<String, Int>()
mapWithStringKeys["key"] = 1
assertEquals("string", jsTypeOf (mapWithStringKeys.keys.iterator().next()), "mapWithStringKeys")
return "OK"
}
@@ -0,0 +1,72 @@
package foo
import java.util.HashSet
fun box(): String {
val x = true
val y = false
val intSet = HashSet<Int>()
intSet.add(1)
assertEquals("number", jsTypeOf (intSet.iterator().next()), "intSet")
val shortSet = HashSet<Short>()
shortSet.add(1.toShort())
assertEquals("number", jsTypeOf (shortSet.iterator().next()), "shortSet")
val byteSet = HashSet<Byte>()
byteSet.add(1.toByte())
assertEquals("number", jsTypeOf (byteSet.iterator().next()), "byteSet")
val doubleSet = HashSet<Double>()
doubleSet.add(1.0)
assertEquals("number", jsTypeOf (doubleSet.iterator().next()), "doubleSet")
doubleSet.clear()
doubleSet.add(0.0 / 0.0)
assertEquals("number", jsTypeOf (doubleSet.iterator().next()), "dNaN")
doubleSet.clear()
doubleSet.add(1.0 / 0.0)
assertEquals("number", jsTypeOf (doubleSet.iterator().next()), "dPositiveInfinity")
doubleSet.clear()
doubleSet.add(-1.0 / 0.0)
assertEquals("number", jsTypeOf (doubleSet.iterator().next()), "dNegativeInfinity")
val floatSet = HashSet<Float>()
floatSet.add(1.0f)
assertEquals("number", jsTypeOf (floatSet.iterator().next()), "floatSet")
floatSet.clear()
floatSet.add(0.0f / 0.0f)
assertEquals("number", jsTypeOf (floatSet.iterator().next()), "fNaN")
floatSet.clear()
floatSet.add(+1.0f / 0.0f)
assertEquals("number", jsTypeOf (floatSet.iterator().next()), "fPositiveInfinity")
floatSet.clear()
floatSet.add(-1.0f / 0.0f)
assertEquals("number", jsTypeOf (floatSet.iterator().next()), "fNegativeInfinity")
val charSet = HashSet<Char>()
charSet.add('A')
assertEquals("string", jsTypeOf (charSet.iterator().next()), "charSet")
val longSet = HashSet<Long>()
longSet.add(1L)
assertEquals("object", jsTypeOf (longSet.iterator().next()), "longSet")
val booleanSet = HashSet<Boolean>()
booleanSet.add(true)
assertEquals("boolean", jsTypeOf (booleanSet.iterator().next()), "booleanSet")
val stringSet = HashSet<String>()
stringSet.add("text")
assertEquals("string", jsTypeOf (stringSet.iterator().next()), "stringSet")
return "OK"
}
@@ -0,0 +1,22 @@
package foo
class A {
override fun hashCode() = 23456
}
fun box(): String {
val x = A()
val y = A()
val map = mutableMapOf<A, Int>()
map[x] = 1
assertEquals(1, map.size)
map.remove(y)
assertEquals(1, map.size)
map.remove(x)
assertEquals(0, map.size)
return "OK"
}
@@ -0,0 +1,12 @@
package foo
fun box(): String {
val s = StringBuilder()
s.append("a")
s.append("b").append("c")
s.append('d').append("e")
if (s.toString() != "abcde") return s.toString()
return "OK"
}