[Tests] Migrate backend-independent tests from native to compiler/testData.
^KT-65979
This commit is contained in:
committed by
Space Team
parent
dd9332d9e1
commit
febac0dd5f
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
foo().hashCode()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun foo(): Any {
|
||||
return Array<Any?>(0, { i -> null })
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
MODULE main
|
||||
CLASS Canonical_nameKt.class
|
||||
PACKAGE METADATA
|
||||
PROPERTY getSb()Ljava/lang/StringBuilder;
|
||||
Property: class.metadata.property.returnType
|
||||
K1
|
||||
java/lang/StringBuilder /* = kotlin/text/StringBuilder^ */
|
||||
K2
|
||||
java/lang/StringBuilder
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// JVM_ABI_K1_K2_DIFF: KT-63864
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
val sb = StringBuilder()
|
||||
|
||||
interface I<U, T> {
|
||||
fun foo(a: U): T
|
||||
fun qux(a: T): U
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
class A1
|
||||
class A2
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
class A : I<A1, A2> {
|
||||
override fun foo(a: A1): A2 { sb.appendLine("A:foo"); return A2() }
|
||||
override fun qux(a: A2): A1 { sb.appendLine("A:qux"); return A1() }
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
fun <U, V> baz(i: I<U, V>, u: U, v:V) {
|
||||
i.foo(u)
|
||||
i.qux(v)
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
fun box(): String {
|
||||
baz<A1, A2>(A(), A1(), A2())
|
||||
|
||||
assertEquals("A:foo\nA:qux\n", sb.toString())
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
testCast(null, false)
|
||||
testCastToNullable(null, true)
|
||||
testCastToNullable(TestKlass(), true)
|
||||
testCastToNullable("", false)
|
||||
testCastNotNullableToNullable(TestKlass(), true)
|
||||
testCastNotNullableToNullable("", false)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
class TestKlass
|
||||
|
||||
fun ensure(b: Boolean) {
|
||||
if (!b) {
|
||||
throw Error("Error")
|
||||
}
|
||||
}
|
||||
|
||||
fun testCast(x: Any?, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as TestKlass
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
|
||||
fun testCastToNullable(x: Any?, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as TestKlass?
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
|
||||
fun testCastNotNullableToNullable(x: Any, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as TestKlass?
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
open class A() {}
|
||||
class B(): A() {}
|
||||
|
||||
fun castSimple(o: Any) : A = o as A
|
||||
|
||||
fun castTest(): Boolean {
|
||||
val b = B()
|
||||
castSimple(b)
|
||||
return true
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (!castTest()) throw Error()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
interface I
|
||||
class A() : I {}
|
||||
class B() {}
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
fun isTypeOf(a: Any?) : Boolean {
|
||||
return a is A
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
fun isTypeNullableOf(a: Any?) : Boolean {
|
||||
return a is A?
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
fun isNotTypeOf(a: Any) : Boolean {
|
||||
return a !is A
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
fun isTypeOfInterface(a: Any) : Boolean {
|
||||
return a is I
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------//
|
||||
|
||||
fun box(): String {
|
||||
if (!isTypeOf(A())) return "FAIL !isTypeOf(A())"
|
||||
if (isTypeOf(null)) return "FAIL isTypeOf(null)"
|
||||
if (!isTypeNullableOf(A())) return "FAIL !isTypeNullableOf(A())"
|
||||
if (!isTypeNullableOf(null)) return "FAIL !isTypeNullableOf(null)"
|
||||
if (!isNotTypeOf(B())) return "FAIL !isNotTypeOf(B())"
|
||||
if (!isTypeOfInterface(A())) return "FAIL !isTypeOfInterface(A())"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
class A {
|
||||
companion object {
|
||||
fun foo() = "OK"
|
||||
}
|
||||
}
|
||||
|
||||
fun box() = A.foo()
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
val s = "world"
|
||||
val i = 1
|
||||
val res1 = "Hello $s $i ${2 * i}"
|
||||
if (res1 != "Hello world 1 2") return "FAIL 1: $res1"
|
||||
|
||||
val a = "a"
|
||||
val res2 = "Hello, $a"
|
||||
if (res2 != "Hello, a") return "FAIL 2: $res2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
//Original issue here https://youtrack.jetbrains.com/issue/KT-37212
|
||||
@Suppress("DIVISION_BY_ZERO")
|
||||
const val fpInfConst = 1.0F / 0.0F
|
||||
@Suppress("DIVISION_BY_ZERO")
|
||||
val fpInfVal = 1.0F / 0.0F
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(fpInfConst, Float.POSITIVE_INFINITY)
|
||||
assertEquals(fpInfVal, Float.POSITIVE_INFINITY)
|
||||
assertEquals(fpInfConst, fpInfVal)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
Any() as String
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
foo()
|
||||
} catch (e: Throwable) {
|
||||
return "OK"
|
||||
}
|
||||
|
||||
return "Fail"
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
assertTrue(Reproducer().repro() > 0)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
// Based on https://youtrack.jetbrains.com/issue/KT-42000#focus=Comments-27-4404934.0-0
|
||||
|
||||
val Int.isEven get() = this % 2 == 0
|
||||
|
||||
inline operator fun <reified T : Number> T.plus(other: T): T = when (T::class) {
|
||||
Double::class -> (this as Double) + (other as Double)
|
||||
Int::class -> (this as Int) + (other as Int)
|
||||
Long::class -> (this as Long) + (other as Long)
|
||||
else -> TODO()
|
||||
} as T
|
||||
|
||||
inline fun <reified T : Number> Collection<T>.median(): Double {
|
||||
val sorted = this.sortedBy {
|
||||
it.toDouble()
|
||||
}
|
||||
|
||||
return if (size.isEven || size == 1) {
|
||||
sorted[size / 2]
|
||||
} else {
|
||||
sorted[size / 2] + sorted[size / 2 + 1]
|
||||
}.toDouble()
|
||||
}
|
||||
|
||||
class Reproducer {
|
||||
private var someListOfLongs = mutableListOf<Long>(1L)
|
||||
|
||||
fun repro() = someListOfLongs.median()
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
// https://youtrack.jetbrains.com/issue/KT-42000
|
||||
|
||||
fun box(): String {
|
||||
assertFailsWith<Error> {
|
||||
when (1) {
|
||||
else -> throw Error()
|
||||
} as String
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun local_variable(a: Int) : Int {
|
||||
var b = 0
|
||||
b = a + 11
|
||||
return b
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (local_variable(3) != 14) throw Error()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
//--- Test "eqeq" -------------------------------------------------------------//
|
||||
|
||||
fun check_eqeq(a: Any?) = a == null
|
||||
|
||||
fun null_check_eqeq1() : Boolean {
|
||||
return check_eqeq(Any())
|
||||
}
|
||||
|
||||
fun null_check_eqeq2() : Boolean {
|
||||
return check_eqeq(null)
|
||||
}
|
||||
|
||||
//--- Test "eqeqeq" -----------------------------------------------------------//
|
||||
|
||||
fun check_eqeqeq(a: Any?) = a === null
|
||||
|
||||
fun null_check_eqeqeq1() : Boolean {
|
||||
return check_eqeqeq(Any())
|
||||
}
|
||||
|
||||
fun null_check_eqeqeq2() : Boolean {
|
||||
return check_eqeqeq(null)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (null_check_eqeq1()) return "FAIL null_check_eqeq1()"
|
||||
if (!null_check_eqeq2()) return "FAIL !null_check_eqeq2()"
|
||||
if (null_check_eqeqeq1()) return "FAIL null_check_eqeqeq1()"
|
||||
if (!null_check_eqeqeq2()) return "FAIL !null_check_eqeqeq2()"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
open class A
|
||||
class B : A()
|
||||
class C
|
||||
|
||||
fun foo(a: Any) : A? = a as? A
|
||||
|
||||
fun safe_cast_positive(): Boolean {
|
||||
val b = B()
|
||||
return foo(b) === b
|
||||
}
|
||||
|
||||
fun safe_cast_negative(): Boolean {
|
||||
val c = C()
|
||||
return foo(c) == null
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val safeCastPositive = safe_cast_positive().toString()
|
||||
val safeCastNegative = safe_cast_negative().toString()
|
||||
if (safeCastPositive != "true") return "FAIL safeCastPositive"
|
||||
if (safeCastNegative != "true") return "FAIL safeCastNegative"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
val list0 = _arrayOf("K", "o", "t", "l", "i", "n")
|
||||
val list1 = _arrayOf("l", "a","n", "g", "u", "a", "g", "e")
|
||||
val list = foo(list0, list1)
|
||||
|
||||
val expected = listOf("K", "o", "t", "l", "i", "n", " ", "i", "s", " ", "c", "o", "o", "l", " ", "l", "a", "n", "g", "u", "a", "g", "e")
|
||||
if (list != expected)
|
||||
return "FAIL: $list"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
fun foo(a:Array<out String>, b:Array<out String>) = listOf(*a," ", "i", "s", " ", "c", "o", "o", "l", " ", *b)
|
||||
|
||||
|
||||
fun _arrayOf(vararg arg:String) = arg
|
||||
@@ -0,0 +1,9 @@
|
||||
MODULE main
|
||||
CLASS Statements0Kt.class
|
||||
PACKAGE METADATA
|
||||
PROPERTY getSb()Ljava/lang/StringBuilder;
|
||||
Property: class.metadata.property.returnType
|
||||
K1
|
||||
java/lang/StringBuilder /* = kotlin/text/StringBuilder^ */
|
||||
K2
|
||||
java/lang/StringBuilder
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// JVM_ABI_K1_K2_DIFF: KT-63864
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
val sb = StringBuilder()
|
||||
|
||||
fun simple() {
|
||||
var a = 238
|
||||
a++
|
||||
sb.appendLine(a)
|
||||
--a
|
||||
sb.appendLine(a)
|
||||
}
|
||||
|
||||
class Foo() {
|
||||
val j = 2
|
||||
var i = 29
|
||||
|
||||
fun more() {
|
||||
i++
|
||||
}
|
||||
|
||||
fun less() {
|
||||
--i
|
||||
}
|
||||
}
|
||||
|
||||
fun fields() {
|
||||
val foo = Foo()
|
||||
foo.more()
|
||||
sb.appendLine(foo.i)
|
||||
foo.less()
|
||||
sb.appendLine(foo.i)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
simple()
|
||||
fields()
|
||||
|
||||
assertEquals("239\n238\n30\n29\n", sb.toString())
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
open class C {
|
||||
open fun f() = "<fun:C>"
|
||||
}
|
||||
|
||||
class C1: C() {
|
||||
override fun f() = super<C>.f() + "<fun:C1>"
|
||||
}
|
||||
|
||||
open class C2: C() {
|
||||
}
|
||||
|
||||
class C3: C2() {
|
||||
override fun f() = super<C2>.f() + "<fun:C3>"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c1f = C1().f()
|
||||
if (c1f != "<fun:C><fun:C1>") return "FAIL 1: $c1f"
|
||||
val c3f = C3().f()
|
||||
if (c3f != "<fun:C><fun:C3>") return "FAIL 2: $c3f"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
open class C {
|
||||
open val p1 = "<prop:C>"
|
||||
}
|
||||
|
||||
class C1: C() {
|
||||
override val p1 = super<C>.p1 + "<prop:C1>"
|
||||
}
|
||||
|
||||
open class C2: C() {
|
||||
}
|
||||
|
||||
class C3: C2() {
|
||||
override val p1 = super<C2>.p1 + "<prop:C3>"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c1p1 = C1().p1
|
||||
if (c1p1 != "<prop:C><prop:C1>") return "FAIL 1: $c1p1"
|
||||
val c3p1 = C3().p1
|
||||
if (c3p1 != "<prop:C><prop:C3>") return "FAIl 2: $c3p1"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
open class C {
|
||||
open var p2 = "<prop:C>"
|
||||
set(value) { field = "<prop:C>" + value }
|
||||
}
|
||||
|
||||
class C1: C() {
|
||||
override var p2 = super<C>.p2 + "<prop:C1>"
|
||||
set(value) {
|
||||
super<C>.p2 = value
|
||||
field = "<prop:C1>" + super<C>.p2
|
||||
}
|
||||
}
|
||||
|
||||
open class C2: C() {
|
||||
}
|
||||
|
||||
class C3: C2() {
|
||||
override var p2 = super<C2>.p2 + "<prop:C3>"
|
||||
set(value) {
|
||||
super<C2>.p2 = value
|
||||
field = "<prop:C3>" + super<C2>.p2
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c1 = C1()
|
||||
val c3 = C3()
|
||||
c1.p2 = "zzz"
|
||||
c3.p2 = "zzz"
|
||||
val c1p2 = c1.p2
|
||||
if (c1p2 != "<prop:C1><prop:C>zzz") return "FAIL 1: "
|
||||
val c3p2 = c3.p2
|
||||
if (c3p2 != "<prop:C3><prop:C>zzz") return "FAIL 2: "
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
val x = Bar(42).x
|
||||
if (x != 42 ) return "FAIL: $x"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
class Foo(val x: Int)
|
||||
typealias Bar = Foo
|
||||
@@ -0,0 +1,9 @@
|
||||
MODULE main
|
||||
CLASS Unchecked_cast1Kt.class
|
||||
PACKAGE METADATA
|
||||
PROPERTY getSb()Ljava/lang/StringBuilder;
|
||||
Property: class.metadata.property.returnType
|
||||
K1
|
||||
java/lang/StringBuilder /* = kotlin/text/StringBuilder^ */
|
||||
K2
|
||||
java/lang/StringBuilder
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// JVM_ABI_K1_K2_DIFF: KT-63864
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
val sb = StringBuilder()
|
||||
|
||||
fun box(): String {
|
||||
foo<String>("17")
|
||||
bar<String>("17")
|
||||
foo<String>(42)
|
||||
bar<String>(42)
|
||||
|
||||
assertEquals("17\n17\n42\n42\n", sb.toString())
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun <T> foo(x: Any?) {
|
||||
val y = x as T
|
||||
sb.appendLine(y.toString())
|
||||
}
|
||||
|
||||
fun <T> bar(x: Any?) {
|
||||
val y = x as? T
|
||||
sb.appendLine(y.toString())
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// KT-66086
|
||||
// IGNORE_BACKEND: NATIVE
|
||||
// KT-66084
|
||||
// IGNORE_BACKEND: JS_IR, JS_IR_ES6
|
||||
// KT-66085
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
val x = cast<String>(Any())
|
||||
return "FAIL: ${x.length}"
|
||||
} catch (e: Throwable) {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> cast(x: Any?) = x as T
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
testCast<TestKlass>(TestKlass(), true)
|
||||
testCast<TestKlass>(null, false)
|
||||
testCastToNullable<TestKlass>(null, true)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
class TestKlass
|
||||
|
||||
fun ensure(b: Boolean) {
|
||||
if (!b) {
|
||||
println("Error")
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : Any> testCast(x: Any?, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as T
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
|
||||
fun <T : Any> testCastToNullable(x: Any?, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as T?
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
// WITH_STDLIB
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
CI1I2().uncheckedCast<CI1I2>()
|
||||
CI1I2().uncheckedCast<OtherCI1I2>()
|
||||
|
||||
assertFailsWith<ClassCastException> {
|
||||
Any().uncheckedCast<CI1I2>()
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun <R : C> Any?.uncheckedCast() where R : I1, R : I2 {
|
||||
this as R
|
||||
}
|
||||
|
||||
interface I1
|
||||
interface I2
|
||||
open class C
|
||||
|
||||
class CI1I2 : C(), I1, I2
|
||||
class OtherCI1I2 : C(), I1, I2
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
fun myPrintln(a: Any): Unit {}
|
||||
|
||||
fun box(): String {
|
||||
val unit = myPrintln("First")
|
||||
if (unit.toString() != "kotlin.Unit") return "FAIL 1: $unit"
|
||||
return "OK"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
fun box(): String {
|
||||
val x = foo()
|
||||
if (x.toString() != "kotlin.Unit") return "FAIL: $x"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
return Unit
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
fun box(): String {
|
||||
val actual = foo(Unit)
|
||||
if (actual != "kotlin.Unit") return "FAIL: $actual"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun foo(x: Any): String {
|
||||
return x.toString()
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
fun box(): String {
|
||||
for (x in 0 .. 8) {
|
||||
foo(x, Unit)
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
|
||||
var global = 42
|
||||
|
||||
fun foo(x: Int, unit: Unit) {
|
||||
var local = 5
|
||||
val y: Unit = when (x) {
|
||||
0 -> {}
|
||||
1 -> local = 6
|
||||
2 -> global = 43
|
||||
3 -> unit
|
||||
4 -> Unit
|
||||
5 -> bar()
|
||||
6 -> return
|
||||
7 -> {
|
||||
5
|
||||
bar()
|
||||
}
|
||||
8 -> {
|
||||
val z: Any = Unit
|
||||
z as Unit
|
||||
}
|
||||
else -> throw Error()
|
||||
}
|
||||
|
||||
if (y !== Unit) {
|
||||
throw Error("Fail at x = $x")
|
||||
}
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
}
|
||||
Reference in New Issue
Block a user