JS: added reified is-check test cases

This commit is contained in:
Alexey Tsvetkov
2015-03-17 17:25:49 +03:00
parent 0a3ed7f12e
commit 8cdff2439b
18 changed files with 444 additions and 0 deletions
@@ -0,0 +1,4 @@
package foo
inline fun isInstance<reified T>(x: Any): Boolean =
x is T
@@ -0,0 +1,18 @@
package foo
// CHECK_NOT_CALLED: test
// CHECK_NOT_CALLED: test1
class A
class B
inline fun test<reified T>(x: Any): Boolean = test1<T>(x)
inline fun test1<reified R>(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"
}
@@ -0,0 +1,29 @@
package foo
// CHECK_NOT_CALLED: test
// CHECK_NOT_CALLED: fn
class A(val x: Any? = null) {
inline
fun test<reified T, reified R>(b: B) = b.fn<T, R>()
inline
fun B.fn<reified T, reified R>() = 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"
}
@@ -0,0 +1,31 @@
package foo
// CHECK_NOT_CALLED: typePredicate
open class A
class B
class C : A()
trait TypePredicate {
fun invoke(x: Any): Boolean
}
inline fun typePredicate<reified T>(): 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"
}
@@ -0,0 +1,11 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<Boolean>(true))
assertEquals(true, isInstance<Boolean>(false))
assertEquals(false, isInstance<Boolean>("true"))
return "OK"
}
@@ -0,0 +1,11 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<Char>('c'))
assertEquals(false, isInstance<Char>(""))
assertEquals(false, isInstance<Char>("cc"))
return "OK"
}
@@ -0,0 +1,21 @@
package foo
// CHECK_NOT_CALLED: isInstance
trait A
trait 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()))
return "OK"
}
@@ -0,0 +1,19 @@
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"))
return "OK"
}
@@ -0,0 +1,10 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<String>(""))
assertEquals(true, isInstance<String>("a"))
return "OK"
}
@@ -0,0 +1,33 @@
package foo
// CHECK_CALLED: doRun
// CHECK_NOT_CALLED: test
class X
class Y
noinline
fun doRun<R>(fn: ()->R): R = fn()
inline fun test<reified A, reified B>(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"
}
@@ -0,0 +1,17 @@
package foo
// CHECK_NOT_CALLED: test
class A(val x: Any? = null) {
inline
fun test<reified T>() = 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 test<reified A, B, reified C>(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 test<reified T>(): 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"
}
@@ -0,0 +1,22 @@
package foo
class A
class B
class C
inline fun test<reified T, reified R>(x: Any): String = test1<R, T>(x)
inline fun test1<reified R, reified T>(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"
}
@@ -0,0 +1,34 @@
package foo
// CHECK_NOT_CALLED: test
class A(val x: Int)
class B(val x: Int)
inline
fun test<reified T>(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"
}