[K/N][Tests] Move codegen test sources cycles..innerClass

^KT-61259
This commit is contained in:
Vladimir Sukharev
2023-12-13 23:53:55 +01:00
committed by Space Team
parent 35d2be25b2
commit 71a834b778
282 changed files with 2376 additions and 756 deletions
+21
View File
@@ -0,0 +1,21 @@
/*
* 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.
*/
package codegen.cycles.cycle
import kotlin.test.*
fun cycle(cnt: Int): Int {
var sum = 1
while (sum == cnt) {
sum = sum + 1
}
return sum
}
@Test fun runTest() {
if (cycle(1) != 2) throw Error()
if (cycle(0) != 1) throw Error()
}
+21
View File
@@ -0,0 +1,21 @@
/*
* 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.
*/
package codegen.cycles.cycle_do
import kotlin.test.*
fun cycle_do(cnt: Int): Int {
var sum = 1
do {
sum = sum + 2
} while (sum == cnt)
return sum
}
@Test fun runTest() {
if (cycle_do(3) != 5) throw Error()
if (cycle_do(0) != 3) throw Error()
}
@@ -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.
*/
package codegen.cycles.cycle_for
import kotlin.test.*
fun cycle_for(arr: Array<Int>) : Int {
var sum = 0
for (i in arr) {
sum += i
}
return sum
}
@Test fun runTest() {
if (cycle_for(Array(4, init = { it })) != 6) throw Error()
}
+18
View File
@@ -0,0 +1,18 @@
/*
* 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.
*/
package codegen.dataflow.scope1
import kotlin.test.*
var b = true
@Test fun runTest() {
var x = 1
if (b) {
var x = 2
}
println(x)
}
@@ -0,0 +1 @@
1
@@ -0,0 +1,26 @@
/*
* 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.
*/
package codegen.dataflow.uninitialized_val
import kotlin.test.*
fun foo(b: Boolean): Int {
val x: Int
if (b) {
x = 1
} else {
x = 2
}
return x
}
@Test fun runTest() {
val uninitializedUnused: Int
println(foo(true))
println(foo(false))
}
@@ -0,0 +1,2 @@
1
2
@@ -0,0 +1,18 @@
/*
* 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.
*/
package codegen.delegatedProperty.lazy
import kotlin.test.*
val lazyValue: String by lazy {
println("computed!")
"Hello"
}
@Test fun runTest() {
println(lazyValue)
println(lazyValue)
}
@@ -0,0 +1,3 @@
computed!
Hello
Hello
@@ -0,0 +1,27 @@
/*
* 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.
*/
package codegen.delegatedProperty.local
import kotlin.test.*
import kotlin.reflect.KProperty
fun foo(): Int {
class Delegate {
operator fun getValue(receiver: Any?, p: KProperty<*>): Int {
println(p.name)
return 42
}
}
val x: Int by Delegate()
return x
}
@Test fun runTest() {
println(foo())
}
@@ -0,0 +1,2 @@
x
42
@@ -0,0 +1,22 @@
/*
* 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.
*/
package codegen.delegatedProperty.map
import kotlin.test.*
class User(val map: Map<String, Any?>) {
val name: String by map
val age: Int by map
}
@Test fun runTest() {
val user = User(mapOf(
"name" to "John Doe",
"age" to 25
))
println(user.name) // Prints "John Doe"
println(user.age) // Prints 25
}
@@ -0,0 +1,2 @@
John Doe
25
@@ -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.
*/
package codegen.delegatedProperty.observable
import kotlin.test.*
import kotlin.properties.Delegates
class User {
var name: String by Delegates.observable("<no name>") {
prop, old, new ->
println("$old -> $new")
}
}
@Test fun runTest() {
val user = User()
user.name = "first"
user.name = "second"
}
@@ -0,0 +1,2 @@
<no name> -> first
first -> second
@@ -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.
*/
package codegen.delegatedProperty.packageLevel
import kotlin.test.*
import kotlin.reflect.KProperty
class Delegate {
operator fun getValue(receiver: Any?, p: KProperty<*>): Int {
println(p.name)
return 42
}
}
val x: Int by Delegate()
@Test fun runTest() {
println(x)
}
@@ -0,0 +1,2 @@
x
42
@@ -0,0 +1,25 @@
/*
* 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.
*/
package codegen.delegatedProperty.simpleVal
import kotlin.test.*
import kotlin.reflect.KProperty
class Delegate {
operator fun getValue(receiver: Any?, p: KProperty<*>): Int {
println(p.name)
return 42
}
}
class C {
val x: Int by Delegate()
}
@Test fun runTest() {
println(C().x)
}
@@ -0,0 +1,2 @@
x
42
@@ -0,0 +1,35 @@
/*
* 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.
*/
package codegen.delegatedProperty.simpleVar
import kotlin.test.*
import kotlin.reflect.KProperty
class Delegate {
var f: Int = 42
operator fun getValue(receiver: Any?, p: KProperty<*>): Int {
println("get ${p.name}")
return f
}
operator fun setValue(receiver: Any?, p: KProperty<*>, value: Int) {
println("set ${p.name}")
f = value
}
}
class C {
var x: Int by Delegate()
}
@Test fun runTest() {
val c = C()
println(c.x)
c.x = 117
println(c.x)
}
@@ -0,0 +1,5 @@
get x
42
set x
get x
117
@@ -0,0 +1,22 @@
/*
* 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.
*/
interface I {
fun foo()
}
fun test() {
val impl = object : I {
override fun foo() { println("zzz") }
}
val delegating = object: I by impl { }
delegating.foo()
}
fun main() {
test()
}
@@ -0,0 +1 @@
zzz
@@ -0,0 +1,5 @@
class Foo(val box: String = "box")
fun main() {
println(Foo().box)
}
@@ -0,0 +1,13 @@
interface Base { val id: Int }
inline class Child(override val id: Int = 1) : Base
interface Base2 { val prop: Base }
class Child2(override val prop: Child) : Base2
fun main() {
val x : Base = Child(5)
println(x.id)
val y : Base2 = Child2(Child(5))
println(y.prop)
}
@@ -0,0 +1,2 @@
5
Child(id=5)
@@ -0,0 +1,14 @@
interface I {
fun foo(): Int
}
class A : I {
override fun foo() = 42
}
fun main(args: Array<String>) {
lateinit var a: I
if (args.size == 0)
a = A()
println(a.foo())
}
@@ -0,0 +1 @@
42
@@ -0,0 +1,33 @@
/*
* 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.
*/
package codegen.enum.companionObject
import kotlin.test.*
enum class Game {
ROCK,
PAPER,
SCISSORS;
companion object {
fun foo() = ROCK
val bar = PAPER
val values2 = values()
val scissors = valueOf("SCISSORS")
}
}
fun box(): String {
if (Game.foo() != Game.ROCK) return "Fail 1"
if (Game.bar != Game.PAPER) return "Fail 2: ${Game.bar}"
if (Game.values().size != 3) return "Fail 3"
if (Game.valueOf("SCISSORS") != Game.SCISSORS) return "Fail 4"
if (Game.values2.size != 3) return "Fail 5"
if (Game.scissors != Game.SCISSORS) return "Fail 6"
return "OK"
}
@Test fun runTest() = println(box())
@@ -0,0 +1 @@
OK
@@ -0,0 +1,28 @@
/*
* 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.
*/
package codegen.enum.interfaceCallNoEntryClass
import kotlin.test.*
interface A {
fun foo(): String
}
enum class Zzz(val zzz: String, val x: Int) : A {
Z1("z1", 1),
Z2("z2", 2),
Z3("z3", 3);
override fun foo(): String{
return "('$zzz', $x)"
}
}
@Test fun runTest() {
println(Zzz.Z3.foo())
val a: A = Zzz.Z3
println(a.foo())
}
@@ -0,0 +1,2 @@
('z3', 3)
('z3', 3)
@@ -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.
*/
package codegen.enum.interfaceCallWithEntryClass
import kotlin.test.*
interface A {
fun f(): String
}
enum class Zzz: A {
Z1 {
override fun f() = "z1"
},
Z2 {
override fun f() = "z2"
};
override fun f() = ""
}
@Test fun runTest() {
println(Zzz.Z1.f() + Zzz.Z2.f())
val a1: A = Zzz.Z1
val a2: A = Zzz.Z2
println(a1.f() + a2.f())
}
@@ -0,0 +1,2 @@
z1z2
z1z2
+28
View File
@@ -0,0 +1,28 @@
/*
* 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.
*/
@file:OptIn(FreezingIsDeprecated::class, kotlin.experimental.ExperimentalNativeApi::class)
package codegen.enum.isFrozen
import kotlin.test.*
import kotlin.native.concurrent.*
enum class Zzz(val zzz: String, var value: Int = 0) {
Z1("z1"),
Z2("z2")
}
@Test fun runTest() {
if (Platform.memoryModel == MemoryModel.STRICT) {
assertTrue(Zzz.Z1.isFrozen)
assertFailsWith<InvalidMutabilityException> {
Zzz.Z1.value = 42
}
assertEquals(0, Zzz.Z1.value)
} else {
assertFalse(Zzz.Z1.isFrozen)
Zzz.Z1.value = 42
assertEquals(42, Zzz.Z1.value)
}
}
+107
View File
@@ -0,0 +1,107 @@
/*
* Copyright 2010-2021 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.
*/
package codegen.enum.kt38540
import kotlin.test.*
public enum class Node(
public val external: Boolean,
public val dependsOn: Set<Node>,
public val required: Boolean
) {
A(
external = false,
dependsOn = emptySet(),
required = true
),
B(
external = false,
dependsOn = emptySet(),
required = true
),
C(
external = true,
dependsOn = emptySet(),
required = true
),
D(
external = true,
dependsOn = emptySet(),
required = true
),
E(
external = true,
dependsOn = emptySet(),
required = true
),
F(
external = true,
dependsOn = emptySet(),
required = true
),
G(
external = true,
dependsOn = emptySet(),
required = false
),
H(
external = true,
dependsOn = emptySet(),
required = true
),
I(
external = true,
dependsOn = emptySet(),
required = true
),
J(
external = true,
dependsOn = emptySet(),
required = true
),
K(
external = true,
dependsOn = setOf(I),
required = true
),
L(
external = true,
dependsOn = setOf(I),
required = true
),
M(
external = true,
dependsOn = setOf(I),
required = true
),
N(
external = true,
dependsOn = emptySet(),
required = true
),
O(
external = true,
dependsOn = emptySet(),
required = true
),
AG(
external = true,
dependsOn = emptySet(),
required = true
),
FIELD_REPORT(
external = true,
dependsOn = setOf(AG, O, J),
required = true
)
}
@Test
fun runTest() {
assertTrue(Node.FIELD_REPORT.dependsOn.contains(Node.AG))
assertTrue(Node.FIELD_REPORT.dependsOn.contains(Node.O))
assertTrue(Node.FIELD_REPORT.dependsOn.contains(Node.J))
}
@@ -0,0 +1,18 @@
/*
* 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.
*/
package codegen.enum.lambdaInDefault
import kotlin.test.*
enum class Zzz(val value: String.() -> Int = {
length
}) {
Q()
}
@Test fun runTest() {
println(Zzz.Q)
}
@@ -0,0 +1 @@
Q
+20
View File
@@ -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.
*/
package codegen.enum.loop
import kotlin.test.*
enum class Zzz {
Z {
init {
println(Z.name)
}
}
}
@Test fun runTest() {
println(Zzz.Z)
}
+2
View File
@@ -0,0 +1,2 @@
Z
Z
+18
View File
@@ -0,0 +1,18 @@
/*
* 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.
*/
package codegen.enum.nested
import kotlin.test.*
enum class Foo {
A;
enum class Bar { C }
}
@Test fun runTest() {
println(Foo.A)
println(Foo.Bar.C)
}
+2
View File
@@ -0,0 +1,2 @@
A
C
@@ -0,0 +1,73 @@
/*
* 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.
*/
package codegen.enum.reorderedArguments
import kotlin.test.*
// Regression test for https://github.com/JetBrains/kotlin-native/issues/1779
enum class Foo(val a: Int, val b: Int, val c: Int = 0) {
A(a = 1, b = 0),
B(b = 1, a = 0),
C(c = 1, b = 0, a = 0),
D(0, 0),
E(1, 1, 1)
}
interface Base<T> {
val value: T
}
enum class Bar(override val value: Foo) : Base<Foo> {
A(Foo.A),
B(Foo.B),
C(Foo.C),
D(Foo.D),
E(Foo.E)
}
@Test fun runTest() {
assertEquals(Foo.A.a, 1)
assertEquals(Foo.A.b, 0)
assertEquals(Foo.A.c, 0)
assertEquals(Foo.B.a, 0)
assertEquals(Foo.B.b, 1)
assertEquals(Foo.B.c, 0)
assertEquals(Foo.C.a, 0)
assertEquals(Foo.C.b, 0)
assertEquals(Foo.C.c, 1)
assertEquals(Foo.D.a, 0)
assertEquals(Foo.D.b, 0)
assertEquals(Foo.D.c, 0)
assertEquals(Foo.E.a, 1)
assertEquals(Foo.E.b, 1)
assertEquals(Foo.E.c, 1)
assertEquals(Bar.A.value.a, 1)
assertEquals(Bar.A.value.b, 0)
assertEquals(Bar.A.value.c, 0)
assertEquals(Bar.B.value.a, 0)
assertEquals(Bar.B.value.b, 1)
assertEquals(Bar.B.value.c, 0)
assertEquals(Bar.C.value.a, 0)
assertEquals(Bar.C.value.b, 0)
assertEquals(Bar.C.value.c, 1)
assertEquals(Bar.D.value.a, 0)
assertEquals(Bar.D.value.b, 0)
assertEquals(Bar.D.value.c, 0)
assertEquals(Bar.E.value.a, 1)
assertEquals(Bar.E.value.b, 1)
assertEquals(Bar.E.value.c, 1)
}
@@ -0,0 +1,91 @@
/*
* 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.
*/
package codegen.enum.switchLowering
import kotlin.test.*
enum class EnumA {
A, B, C
}
enum class EnumB {
A, B
}
enum class E {
ONE, TWO, THREE
}
fun produceEntry() = EnumA.A
// Check that we fail on comparison of different enum types.
fun differentEnums() {
println(when (produceEntry()) {
EnumB.A -> "EnumB.A"
EnumA.A -> "EnumA.A"
EnumA.B -> "EnumA.B"
else -> "nah"
})
}
// Nullable subject shouldn't be lowered.
fun nullable() {
val x: EnumA? = null
when(x) {
EnumA.A -> println("fail")
else -> println("ok")
}
}
// Operator overloading won't trick us!
fun operatorOverloading() {
operator fun E.contains(other: E): Boolean = false
val y = E.ONE
when(y) {
in E.ONE -> println("Should not reach here")
else -> println("ok")
}
}
fun smoke1() {
when (produceEntry()) {
EnumA.B -> println("error")
EnumA.A -> println("ok")
EnumA.C -> println("error")
}
}
fun smoke2() {
when (produceEntry()) {
EnumA.B -> println("error")
else -> println("ok")
}
}
fun eA() = EnumA.A
fun eB() = EnumA.B
fun nestedWhen() {
println(when (eA()) {
EnumA.A, EnumA.C -> when (eB()) {
EnumA.B -> "ok"
else -> "nope"
}
else -> "nope"
})
}
fun main() {
differentEnums()
nullable()
operatorOverloading()
smoke1()
smoke2()
nestedWhen()
}
@@ -0,0 +1,6 @@
EnumA.A
ok
ok
ok
ok
ok
+18
View File
@@ -0,0 +1,18 @@
/*
* 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.
*/
package codegen.enum.test0
import kotlin.test.*
val TOP_LEVEL = 5
enum class MyEnum(value: Int) {
VALUE(TOP_LEVEL)
}
@Test fun runTest() {
println(MyEnum.VALUE.toString())
}
+1
View File
@@ -0,0 +1 @@
VALUE
+17
View File
@@ -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.
*/
package codegen.enum.test1
import kotlin.test.*
enum class Zzz(val zzz: String, val x: Int) {
Z1("z1", 1),
Z2("z2", 2)
}
@Test fun runTest() {
println(Zzz.Z1.zzz + Zzz.Z2.x)
}
+1
View File
@@ -0,0 +1 @@
z12
@@ -0,0 +1,22 @@
/*
* 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.
*/
package codegen.enum.vCallNoEntryClass
import kotlin.test.*
enum class Zzz(val zzz: String, val x: Int) {
Z1("z1", 1),
Z2("z2", 2),
Z3("z3", 3);
override fun toString(): String{
return "('$zzz', $x)"
}
}
@Test fun runTest() {
println(Zzz.Z3.toString())
}
@@ -0,0 +1 @@
('z3', 3)
@@ -0,0 +1,24 @@
/*
* 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.
*/
package codegen.enum.vCallWithEntryClass
import kotlin.test.*
enum class Zzz {
Z1 {
override fun f() = "z1"
},
Z2 {
override fun f() = "z2"
};
open fun f() = ""
}
@Test fun runTest() {
println(Zzz.Z1.f() + Zzz.Z2.f())
}
@@ -0,0 +1 @@
z1z2
+23
View File
@@ -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.
*/
package codegen.enum.valueOf
import kotlin.test.*
enum class E {
E3,
E1,
E2
}
@Test fun runTest() {
println(E.valueOf("E1").toString())
println(E.valueOf("E2").toString())
println(E.valueOf("E3").toString())
println(enumValueOf<E>("E1").toString())
println(enumValueOf<E>("E2").toString())
println(enumValueOf<E>("E3").toString())
}
+6
View File
@@ -0,0 +1,6 @@
E1
E2
E3
E1
E2
E3
+23
View File
@@ -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.
*/
package codegen.enum.values
import kotlin.test.*
enum class E {
E3,
E1,
E2
}
@Test fun runTest() {
println(E.values()[0].toString())
println(E.values()[1].toString())
println(E.values()[2].toString())
println(enumValues<E>()[0].toString())
println(enumValues<E>()[1].toString())
println(enumValues<E>()[2].toString())
}
+6
View File
@@ -0,0 +1,6 @@
E3
E1
E2
E3
E1
E2
@@ -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.
*/
package codegen.enum.varargParam
import kotlin.test.*
enum class Piece(vararg val states: Int) {
I(3, 4, 5)
}
@Test fun runTest() {
println(Piece.I.states[0])
}
@@ -0,0 +1 @@
3
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2021 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.
*/
package codegen.escapeAnalysis.negativeArraySize
import kotlin.test.*
const val size = -42
fun foo() {
val arr = IntArray(size) { it }
for (x in arr) println(x)
}
@Test fun runTest() {
assertFailsWith<IllegalArgumentException> { foo() }
}
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.recursion
import kotlin.test.*
class A {
var f: A? = null
}
fun foo(k: Int, a1: A, a2: A): A {
val a3 = A()
if (k == 0)
return a1
a3.f = a1
return foo(k - 1, a2, a3)
}
@Test fun runTest() {
foo(3, A(), A()).toString()
}
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2021 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.
*/
package codegen.escapeAnalysis.stackAllocated
import kotlin.test.*
import kotlin.native.internal.*
class A {
fun f(x: Int) = x + 13
}
fun f(x: Int): Int {
val a = A()
assertTrue(a.isLocal())
return a.f(x)
}
@Test fun runTest() {
assertEquals(f(42), 55)
}
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package codegen.escapeAnalysis.stack_array
import kotlin.test.*
@Test fun runTest() {
val array = IntArray(2)
array[0] = 1
array[1] = 2
val check = array is IntArray
println(check)
println(array[0] + array[1])
}
@@ -0,0 +1,2 @@
true
3
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2021 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.
*/
package codegen.escapeAnalysis.string
import kotlin.test.*
import kotlin.native.internal.*
@Test fun runTest() {
val s = String()
assertTrue(s.isLocal())
}
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test1
class A(val s: String)
// ----- Agressive -----
// PointsTo:
// RET.v@lue -> P0
// Escapes:
// ----- Passive -----
// PointsTo:
// RET.v@lue -> P0
// Escapes:
fun foo(a: A) = a
fun main() = println(foo(A("zzz")))
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test10
class G(val x: Int)
class F(val s: String) {
var g = G(0)
}
class A {
var f = F("")
}
// ----- Agressive -----
// PointsTo:
// P0.f -> D0
// RET.v@lue -> D0
// Escapes:
// ----- Passive -----
// PointsTo:
// P0.f -> D0
// RET.v@lue -> D0
// Escapes: D0
fun foo(a: A): F {
val v = F("zzz")
a.f = v
return v
}
fun bar(): F {
val w = A()
val u = foo(w)
w.f.g = G(42)
return u
}
fun main() = println(bar().g.x)
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test11
class F(val x: Int)
class A(val s: String) {
var f = F(0)
}
var f: F? = null
// ----- Agressive -----
// PointsTo:
// RET.v@lue -> P0.f
// D0 -> P0.f
// Escapes: D0
// ----- Passive -----
// PointsTo:
// RET.v@lue -> P0.f
// D0 -> P0.f
// Escapes: D0
fun foo(a: A): F {
f = a.f
return a.f
}
fun main() = println(foo(A("zzz")))
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test12
class A(val s: String)
// ----- Agressive -----
// PointsTo:
// RET.v@lue -> P0.inte$tines
// Escapes:
// ----- Passive -----
// PointsTo:
// RET.v@lue -> P0.inte$tines
// Escapes:
fun foo(arr: Array<A>) = arr[0]
fun main() = println(foo(arrayOf(A("zzz"))).s)
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test13
class A {
var f: A? = null
}
fun foo(a: A, k: Int): A {
return if (k == 0) a else foo(a.f!!, k - 1)
}
fun main() {
val a = A()
a.f = A()
println(foo(a, 1))
}
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test2
class A(val s: String)
// ----- Agressive -----
// PointsTo:
// RET.v@lue -> P0.s
// Escapes:
// ----- Passive -----
// PointsTo:
// RET.v@lue -> P0.s
// Escapes:
fun foo(a: A) = a.s
fun main() = println(foo(A("zzz")))
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test3
class A(val s: String)
class B {
var s: String? = null
}
// ----- Agressive -----
// PointsTo:
// P1.s -> P0.s
// RET.v@lue -> P0.s
// Escapes:
// ----- Passive -----
// PointsTo:
// P1.s -> P0.s
// RET.v@lue -> P0.s
// Escapes:
fun foo(a: A, b: B): String {
b.s = a.s
return a.s
}
fun main() = println(foo(A("zzz"), B()))
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test4
class A(val s: String)
class B {
var f: A = A("qzz")
}
class C {
var g: B = B()
}
// ----- Agressive -----
// PointsTo:
// P0.g.f -> P1.g.f
// RET.v@lue -> D0
// Escapes: D0
// ----- Passive -----
// PointsTo:
// P0.g.f -> P1.g.f
// RET.v@lue -> D0
// Escapes: D0
fun foo(c1: C, c2: C) {
c1.g.f = c2.g.f
}
fun main() = println(foo(C(), C()))
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test5
class A(val s: String)
class B {
var f: A = A("qzz")
}
class C {
var g: B = B()
}
// ----- Agressive -----
// PointsTo:
// RET.v@lue -> D0
// D0.f -> P0.g.f
// Escapes:
// ----- Passive -----
// PointsTo:
// P0.g.f -> D2
// RET.v@lue -> D0
// D0.f -> P0.g.f
// D0.f -> D1
// D1 -> D2
// Escapes: D0 D1
fun foo(c1: C, c2: C): B {
val b = B()
b.f = c1.g.f
return b
}
fun main() = println(foo(C(), C()))
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test6
class A(val s: String)
class B {
var f: A = A("qzz")
}
class C {
var g: B = B()
}
// ----- Agressive -----
// PointsTo:
// P1.g -> D0
// P2.g -> D0
// RET.v@lue -> P1.g
// RET.v@lue -> P2.g
// RET.v@lue -> D0
// D0.f -> P3
// Escapes:
// ----- Passive -----
// PointsTo:
// P1.g -> D0
// P2.g -> D0
// RET.v@lue -> P1.g
// RET.v@lue -> P2.g
// RET.v@lue -> D0
// D0.f -> P3
// Escapes:
fun foo(z: Boolean, c1: C, c2: C, a: A): B {
val v = if(z) c1.g else c2.g
v.f = a
return v
}
fun main() = println(foo(true, C(), C(), A("zzz")))
@@ -0,0 +1,52 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test7
class A(val s: String) {
var h: String = ""
var p: C = C()
}
class B {
var f: A = A("qzz")
}
class C {
var g: A = A("")
}
class D {
var o: A = A("")
}
// ----- Agressive -----
// PointsTo:
// P1.g -> D0
// P2.f -> D0
// P4.o -> P1.g
// P4.o -> P2.f
// P4.o -> D0
// RET.v@lue -> D1
// D0.p -> P1
// D0.h -> P3
// Escapes: D1
// ----- Passive -----
// PointsTo:
// P1.g -> D0
// P2.f -> D0
// P4.o -> P1.g
// P4.o -> P2.f
// P4.o -> D0
// RET.v@lue -> D1
// D0.p -> P1
// D0.h -> P3
// Escapes: D1
fun foo(z: Boolean, c: C, b: B, s: String, d: D) {
val v = if(z) c.g else b.f
v.h = s
d.o = v
val u = v
u.p = c
}
fun main() = println(foo(true, C(), B(), "zzz", D()))
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test8
class F(val s: String) {
var g = F("")
}
class A {
var f = F("qzz")
}
// ----- Agressive -----
// PointsTo:
// P0.f -> D0
// RET.v@lue -> P0.f
// RET.v@lue -> D0
// RET.v@lue -> D0.g
// D0.g -> P0.f
// D0.g -> D0
// Escapes:
// ----- Passive -----
// PointsTo:
// P0.f -> D0
// RET.v@lue -> P0.f
// RET.v@lue -> D0
// RET.v@lue -> D0.g
// D0.g -> P0.f
// D0.g -> D0
// D0.g -> D2
// D1 -> D0
// D2 -> D0
// Escapes: D1 D2
fun foo(a: A): F {
a.f = F("zzz")
a.f.g = a.f
return a.f.g.g
}
fun main() = println(foo(A()).s)
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.test9
class H(val x: Int)
class F(val s: String) {
var g = F("")
var h = H(0)
}
class A {
var f = F("qzz")
}
// ----- Agressive -----
// PointsTo:
// P0.f -> P1.g
// RET.v@lue -> P1.g.h
// Escapes:
// ----- Passive -----
// PointsTo:
// P0.f -> P1.g
// P1.g.h -> D0
// RET.v@lue -> P1.g.h
// Escapes: D0
fun foo(a: A, f: F): H {
a.f = f.g
a.f.h = H(42)
return f.g.h
}
fun main() = println(foo(A(), F("zzz")).x)
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2020 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.
*/
package codegen.escapeAnalysis.zeroOutObjectOnAlloc
import kotlin.test.*
class A {
var x = 0
}
@Test fun runTest() {
var sum1 = 0
var sum2 = 0
for (i in 0 until 10) {
val a = A()
sum1 += a.x
a.x = i
sum2 += a.x
}
assertEquals(0, sum1)
assertEquals(45, sum2)
}
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2021 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.
*/
package codegen.funInterface.implIsNotFunction
import kotlin.test.*
fun interface Foo {
fun invoke(): String
}
fun foo(f: Foo) = f is Function<*>
@Test
fun test() {
assertFalse(foo { "zzz" })
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2023 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.
*/
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
package codegen.funInterface.kt43887
import kotlin.test.*
import kotlinx.cinterop.*
typealias heap_t = IntVar
fun heap_create(size: Long): CPointer<heap_t>? = null
fun heap_alloc(heap: CPointer<heap_t>?, size: ULong): CPointer<*>? = null
fun heap_free(heap: CPointer<heap_t>?, ptr: CPointer<*>?): CPointer<BooleanVar>? = null
inline fun Heap(size: Long): CPointer<heap_t>? {
return heap_create(size)
}
inline fun <reified T : CVariable> CPointer<heap_t>?.use(f: (CPointer<T>) -> Unit) {
alloc<T>()
?.also(f)
?.also(::free)
}
inline fun <reified T : CVariable> CPointer<heap_t>?.alloc(): CPointer<T>? {
return heap_alloc(this, sizeOf<T>().toULong())?.reinterpret()
}
inline fun CPointer<heap_t>?.free(ptr: CPointer<*>?): Boolean {
return heap_free(this, ptr)?.pointed?.value ?: false
}
@Test
fun runTest(): Unit = memScoped {
val heap = Heap(1024)
heap.use<IntVar> { ptr ->
ptr.pointed.value = 40
//println("PTR ${ptr.pointed}")
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2021 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.
*/
package codegen.funInterface.kt49384
import kotlin.test.*
interface A<T>
// https://youtrack.jetbrains.com/issue/KT-49384
class B<T> {
init {
mutableListOf<A<out T>>()
.sortWith { _, _ -> 1 }
}
}
@Test
fun test1() {
val b = B<Any>()
assertEquals(b, b) // Just to ensure B is not deleted by DCE
}
fun interface Foo<T> {
fun same(obj: T): T
}
fun getSame(obj: A<out Any>, foo: Foo<A<out Any>>) = foo.same(obj)
@Test
fun test2() {
val obj = object : A<Any> {}
assertSame(obj, getSame(obj) { it })
}
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2021 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.
*/
package codegen.funInterface.nonTrivialProjectionInSuperType
import kotlin.test.*
fun <T> foo(comparator: kotlin.Comparator<in T>, a: T, b: T) = comparator.compare(a, b)
fun bar(x: Int, y: Int) = foo<Int> ({ a, b -> a - b}, x, y)
@Test
fun test() {
assertTrue(bar(42, 117) < 0)
}
@@ -0,0 +1,22 @@
/*
* 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.
*/
package codegen.function.arithmetic
import kotlin.test.*
fun square(a:Int):Int = a * a
fun sumOfSquares(a:Int, b:Int):Int = square(a) + square(b)
fun diffOfSquares(a:Int, b:Int):Int = square(a) - square(b)
fun mod(a:Int,b:Int):Int = a / b
fun remainder(a:Int, b:Int):Int = a % b
@Test fun runTest() {
if (square(2) != 4) throw Error()
if (sumOfSquares(2, 4) != 20) throw Error()
if (diffOfSquares(2, 4) != -12) throw Error()
if (mod(5, 2) != 2) throw Error()
if (remainder(5, 2) != 1) throw Error()
}
@@ -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.
*/
package codegen.function.boolean
import kotlin.test.*
fun bool_yes(): Boolean = true
@Test fun runTest() {
if (!bool_yes()) throw Error()
}
@@ -0,0 +1,102 @@
/*
* 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.
*/
package codegen.function.defaults
import kotlin.test.*
/**
* Created by minamoto on 12/26/16.
*/
//package defaults
open class A(val a:Int) {
override fun equals(other: Any?): Boolean {
if (other == null || other as? A == null) return false
return (other as A).a == a // Where is smart casting?
}
companion object {
val zero = A(0)
val one = A(1)
val magic = A(42)
}
}
// FUN public fun foo(a: defaults.A = ...): kotlin.Int
// a: EXPRESSION_BODY
// CALL '<get-magic>(): A' type=defaults.A origin=GET_PROPERTY
// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion
// BLOCK_BODY
// RETURN type=kotlin.Nothing from='foo(A = ...): Int'
// CALL '<get-a>(): Int' type=kotlin.Int origin=GET_PROPERTY
// $this: GET_VAR 'value-parameter a: A = ...' type=defaults.A origin=null
fun foo(a: A = A.magic, b:Int = 0xdeadbeef.toInt()) = a.a
// FUN public fun bar(a: defaults.A, inc: kotlin.Int = ...): defaults.A
// inc: EXPRESSION_BODY
// CONST Int type=kotlin.Int value='0'
// BLOCK_BODY
// RETURN type=kotlin.Nothing from='bar(A, Int = ...): A'
// CALL 'constructor A(Int)' type=defaults.A origin=null
// a: CALL 'plus(Int): Int' type=kotlin.Int origin=PLUS
// $this: CALL '<get-a>(): Int' type=kotlin.Int origin=GET_PROPERTY
// $this: GET_VAR 'value-parameter a: A' type=defaults.A origin=null
// other: GET_VAR 'value-parameter inc: Int = ...' type=kotlin.Int origin=null
fun bar(a:A, inc:Int = 0) = A(a.a + inc)
@Test fun runTest() {
// if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ
// arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ
// arg0: CALL 'foo(A = ...): Int' type=kotlin.Int origin=null
// arg1: CALL '<get-a>(): Int' type=kotlin.Int origin=GET_PROPERTY
// $this: CALL '<get-magic>(): A' type=defaults.A origin=GET_PROPERTY
// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion
if (foo() != A.magic.a) {
println("magic failed")
throw Error()
}
// if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ
// arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ
// arg0: CALL 'foo(A = ...): Int' type=kotlin.Int origin=null
// a: CALL 'constructor A(Int)' type=defaults.A origin=null
// a: CONST Int type=kotlin.Int value='1'
// arg1: CONST Int type=kotlin.Int value='1'
if (foo(A(1)) != 1) {
println("one failed: foo(A(1))")
throw Error()
}
// if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ
// arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ
// arg0: CALL 'bar(A, Int = ...): A' type=defaults.A origin=null
// a: CALL '<get-one>(): A' type=defaults.A origin=GET_PROPERTY <---
// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion
// arg1: CALL '<get-one>(): A' type=defaults.A origin=GET_PROPERTY
// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion
if (bar(A.one) != A.one) {
println("A one failed")
throw Error()
}
// if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ
// arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ
// arg0: CALL '<get-a>(): Int' type=kotlin.Int origin=GET_PROPERTY
// $this: CALL 'bar(A, Int = ...): A' type=defaults.A origin=null
// a: CALL '<get-one>(): A' type=defaults.A origin=GET_PROPERTY
// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion
// inc: CONST Int type=kotlin.Int value='1'
// arg1: CONST Int type=kotlin.Int value='2'
if (bar(A.one, 1).a != 2) {
println("A one + 1 failed")
throw Error()
}
println("all tests passed")
}
@@ -0,0 +1,18 @@
/*
* 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.
*/
package codegen.function.defaults1
import kotlin.test.*
fun foo(x:Int = 0, y:Int = x + 1, z:Int = x + y + 1) = x + y + z
@Test fun runTest() {
val v = foo()
if (v != 3) {
println("test failed $v expected 3")
throw Error()
}
}
@@ -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.
*/
package codegen.function.defaults10
import kotlin.test.*
enum class A(one: Int, val two: Int = one) {
FOO(42)
}
@Test fun runTest() {
println(A.FOO.two)
}
@@ -0,0 +1 @@
42
@@ -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.
*/
package codegen.function.defaults2
import kotlin.test.*
fun Int.foo(inc0:Int, inc:Int = 0) = this + inc0 + inc
@Test fun runTest() {
val v = 42.foo(0)
if (v != 42) {
println("test failed v:$v expected:42")
throw Error()
}
}
@@ -0,0 +1,21 @@
/*
* 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.
*/
package codegen.function.defaults3
import kotlin.test.*
fun foo(a:Int = 2, b:String = "Hello", c:Int = 4):String = "$b-$c$a"
fun foo(a:Int = 3, b:Int = a + 1, c:Int = a + b) = a + b + c
@Test fun runTest() {
val a = foo(b="Universe")
if (a != "Universe-42")
throw Error()
val b = foo(b = 5)
if (b != (/* a = */ 3 + /* b = */ 5 + /* c = */ (3 + 5)))
throw Error()
}
@@ -0,0 +1,22 @@
/*
* 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.
*/
package codegen.function.defaults4
import kotlin.test.*
open class A {
open fun foo(x: Int = 42) = println(x)
}
open class B : A()
class C : B() {
override fun foo(x: Int) = println(x + 1)
}
@Test fun runTest() {
C().foo()
}
@@ -0,0 +1 @@
43
@@ -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.
*/
package codegen.function.defaults5
import kotlin.test.*
class TestClass(val x: Int) {
fun foo(y: Int = x) {
println(y)
}
}
fun TestClass.bar(y: Int = x) {
println(y)
}
@Test fun runTest() {
TestClass(5).foo()
TestClass(6).bar()
}
@@ -0,0 +1,2 @@
5
6
@@ -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.
*/
package codegen.function.defaults6
import kotlin.test.*
open class Foo(val x: Int = 42)
class Bar : Foo()
@Test fun runTest() {
println(Bar().x)
}
@@ -0,0 +1 @@
42
@@ -0,0 +1,25 @@
/*
* 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.
*/
package codegen.function.defaults7
import kotlin.test.*
/**
* Test fails with code generation:
* Call parameter type does not match function signature!
* %5 = invoke %struct.ObjHeader* @"kfun:kotlin.native.internal.boxInt(kotlin.Int)"(i32 1, %struct.ObjHeader** %4)
* to label %label_ unwind label %cleanup_landingpad
* i32 invoke void @"kfun:foo$default(kotlin.Int;kotlin.Int;kotlin.Int)"(%struct.ObjHeader* %5, i32 0, i32 2)
* to label %label_1 unwind label %cleanup_landingpad
*/
fun <T> foo(a : T, b : Int = 42){
println(b)
}
@Test fun runTest() {
foo(1)
}
@@ -0,0 +1 @@
42

Some files were not shown because too many files have changed in this diff Show More