JS: move inline test to box tests

This commit is contained in:
Alexey Andreev
2016-08-29 11:55:22 +03:00
parent b159049be8
commit efb82a044f
150 changed files with 1417 additions and 1430 deletions
@@ -0,0 +1,26 @@
package foo
// CHECK_NOT_CALLED: buzz
private var LOG = ""
fun log(string: String) {
LOG += "$string;"
}
fun pullLog(): String {
val string = LOG
LOG = ""
return string
}
fun <T> fizz(x: T): T {
log("fizz($x)")
return x
}
inline
fun <T> buzz(x: T): T {
log("buzz($x)")
return x
}
@@ -0,0 +1,13 @@
package foo
fun sum(x: Int, y: Int): Int {
log("sum($x, $y)")
return x + y
}
fun box(): String {
assertEquals(3, sum(fizz(1), buzz(2)))
assertEquals("fizz(1);buzz(2);sum(1, 2);", pullLog())
return "OK"
}
@@ -0,0 +1,13 @@
package foo
fun sum(a: Int, b: Int, c: Int, d: Int): Int {
log("sum($a, $b, $c, $d)")
return a + b + c + d
}
fun box(): String {
assertEquals(10, sum(fizz(1), buzz(2), fizz(3), buzz(4)))
assertEquals("fizz(1);buzz(2);fizz(3);buzz(4);sum(1, 2, 3, 4);", pullLog())
return "OK"
}
@@ -0,0 +1,19 @@
package foo
// CHECK_NOT_CALLED: max
inline fun max(a: Int, b: Int): Int {
log("max($a, $b)")
if (a > b) return a
return b
}
fun box(): String {
val test = max(fizz(1), max(fizz(2), buzz(3)))
assertEquals(3, test)
assertEquals("fizz(1);fizz(2);buzz(3);max(2, 3);max(1, 3);", pullLog())
return "OK"
}
@@ -0,0 +1,16 @@
package foo
class Sum(x: Int, y: Int) {
init {
log("new Sum($x, $y)")
}
val value = x + y
}
fun box(): String {
assertEquals(3, Sum(fizz(1), buzz(2)).value)
assertEquals("fizz(1);buzz(2);new Sum(1, 2);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertEquals(2, fizz(arrayOf(1, 2))[buzz(1)])
assertEquals("fizz(1,2);buzz(1);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertEquals(2, arrayOf(1, 2)[fizz(0) + buzz(1)])
assertEquals("fizz(0);buzz(1);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertEquals(2, arrayOf(1, fizz(2))[fizz(0) + buzz(1)])
assertEquals("fizz(2);fizz(0);buzz(1);", pullLog())
return "OK"
}
@@ -0,0 +1,16 @@
package foo
var global = ""
class A(val data: Array<Int>)
fun box(): String {
val a = A(arrayOf(1, 2, 3))
a.data[0] = listOf(1, 2, 3).fold(0) { a, b ->
global += "$b;"
a + b
}
assertEquals(6, a.data[0])
assertEquals("1;2;3;", global)
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertArrayEquals(arrayOf(1, 2), arrayOf(fizz(1), buzz(2)))
assertEquals("fizz(1);buzz(2);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertArrayEquals(arrayOf(1, 2, 3, 4), arrayOf(fizz(1), buzz(2), fizz(3), buzz(4)))
assertEquals("fizz(1);buzz(2);fizz(3);buzz(4);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertArrayEquals(arrayOf(arrayOf(1, 2), arrayOf(3, 4)), arrayOf(arrayOf(fizz(1), buzz(2)), arrayOf(fizz(3), buzz(4))))
assertEquals("fizz(1);buzz(2);fizz(3);buzz(4);", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
class A(var x: Int) {
override fun toString(): String = "A($x)"
}
fun box(): String {
val a = A(10)
fizz(a).x = buzz(20)
assertEquals(20, a.x)
assertEquals("fizz(A(10));buzz(20);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertEquals(3, fizz(1) + buzz(2))
assertEquals("fizz(1);buzz(2);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertEquals(10, (fizz(1) + buzz(2)) + (fizz(3) + buzz(4)))
assertEquals("fizz(1);buzz(2);fizz(3);buzz(4);", pullLog())
return "OK"
}
@@ -0,0 +1,13 @@
package foo
fun multiplyFun(): (Int, Int)->Int {
log("multiplyFun()")
return { x, y -> x * y }
}
fun box(): String {
assertEquals(6, multiplyFun()(fizz(2), buzz(3)))
assertEquals("multiplyFun();fizz(2);buzz(3);", pullLog())
return "OK"
}
@@ -0,0 +1,21 @@
package foo
// CHECK_NOT_CALLED: multiplyFunInline
fun multiplyFun(): (Int, Int)->Int {
log("multiplyFun()")
return { x, y -> x * y }
}
inline
fun multiplyFunInline(): (Int, Int)->Int {
log("multiplyFunInline()")
return { x, y -> x * y }
}
fun box(): String {
assertEquals(6, arrayOf(multiplyFun(), multiplyFunInline())[0](fizz(2), fizz(3)))
assertEquals("multiplyFun();multiplyFunInline();fizz(2);fizz(3);", pullLog())
return "OK"
}
@@ -0,0 +1,13 @@
package foo
fun test(x: Boolean): Boolean =
if (fizz(x)) buzz(true) else buzz(false)
fun box(): String {
assertEquals(true, test(true))
assertEquals("fizz(true);buzz(true);", pullLog())
assertEquals(false, test(false))
assertEquals("fizz(false);buzz(false);", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
fun test(x: Boolean?): Boolean = fizz(x) ?: buzz(true)
fun box(): String {
assertEquals(true, test(null))
assertEquals("fizz(null);buzz(true);", pullLog())
assertEquals(false, test(false))
assertEquals("fizz(false);", pullLog())
assertEquals(true, test(true))
assertEquals("fizz(true);", pullLog())
return "OK"
}
@@ -0,0 +1,23 @@
package foo
fun test(x: Boolean, y: Boolean): Int =
if (fizz(x))
if (fizz(y)) buzz(1) else buzz(2)
else
if (fizz(y)) buzz(3) else buzz(4)
fun box(): String {
assertEquals(1, test(true, true))
assertEquals("fizz(true);fizz(true);buzz(1);", pullLog())
assertEquals(2, test(true, false))
assertEquals("fizz(true);fizz(false);buzz(2);", pullLog())
assertEquals(3, test(false, true))
assertEquals("fizz(false);fizz(true);buzz(3);", pullLog())
assertEquals(4, test(false, false))
assertEquals("fizz(false);fizz(false);buzz(4);", pullLog())
return "OK"
}
@@ -0,0 +1,13 @@
package foo
fun test(x: Boolean): Boolean =
if (buzz(x)) buzz(true) else fizz(false)
fun box(): String {
assertEquals(true, test(true))
assertEquals("buzz(true);buzz(true);", pullLog())
assertEquals(false, test(false))
assertEquals("buzz(false);fizz(false);", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
fun test(x: Boolean?): Boolean = buzz(x) ?: fizz(true)
fun box(): String {
assertEquals(true, test(null))
assertEquals("buzz(null);fizz(true);", pullLog())
assertEquals(false, test(false))
assertEquals("buzz(false);", pullLog())
assertEquals(true, test(true))
assertEquals("buzz(true);", pullLog())
return "OK"
}
@@ -0,0 +1,35 @@
package foo
private inline fun bar(predicate: (Char) -> Boolean): Int {
var i = -1
val str = "abc "
do {
i++
if (i == 1) continue
log(i.toString())
} while (predicate(str[i]) && i < 3)
return i
}
private fun test(c: Char): Int {
return bar {
log(it.toString())
it != c
}
}
fun box(): String {
assertEquals(0, test('a'))
assertEquals("0;a;", pullLog())
assertEquals(1, test('b'))
assertEquals("0;a;b;", pullLog())
assertEquals(2, test('c'))
assertEquals("0;a;b;2;c;", pullLog())
assertEquals(3, test('*'))
assertEquals("0;a;b;2;c;3; ;", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
fun box(): String {
var c = 2
do {
if (c > 4) throw Exception("Timeout!")
c++
} while (buzz(c) < 4)
assertEquals("buzz(3);buzz(4);", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
fun box(): String {
var c = 1
do {
if (c > 4) throw Exception("Timeout!")
c++
} while (fizz(c) % 2 == 0 || buzz(c) % 3 == 0)
assertEquals("fizz(2);fizz(3);buzz(3);fizz(4);fizz(5);buzz(5);", pullLog())
return "OK"
}
@@ -0,0 +1,11 @@
package foo
fun box(): String {
for (i in fizz(1)..buzz(3)) {
fizz(i)
}
assertEquals("fizz(1);buzz(3);fizz(1);fizz(2);fizz(3);", pullLog())
return "OK"
}
@@ -0,0 +1,23 @@
package foo
fun test(x: Boolean, y: Boolean): Boolean {
if (fizz(x) && buzz(y)) {
return true
}
return false
}
fun box(): String {
assertEquals(false, test(false, true))
assertEquals("fizz(false);", pullLog())
assertEquals(false, test(true, false))
assertEquals("fizz(true);buzz(false);", pullLog())
assertEquals(true, test(true, true))
assertEquals("fizz(true);buzz(true);", pullLog())
return "OK"
}
@@ -0,0 +1,101 @@
// See KT-11711
package foo
interface A {
val b: B
}
interface B {
fun c(a: Any?)
}
val a: A
get() {
log("a.get")
return object : A {
override val b: B
get() {
log("b.get")
return object : B {
override fun c(a: Any?) {
log("c()")
}
}
}
}
}
val g: Any?
get() {
log("g.get")
return "c"
}
inline fun foo(): Any? {
log("foo()")
return g;
}
inline fun bar(): Any? {
return g;
}
inline fun baz(): Any? {
return log("baz()");
}
inline fun boo(a: Any?): Any? {
return log("boo()");
}
fun box(): String {
log("--1--")
a.b.c(g)
log("--2--")
a.b.c(foo())
log("--3--")
a.b.c(bar())
log("--4--")
a.b.c(baz())
log("--5--")
a.b.c(boo(g))
assertEquals("""--1--
a.get
b.get
g.get
c()
--2--
a.get
b.get
foo()
g.get
c()
--3--
a.get
b.get
g.get
c()
--4--
a.get
b.get
baz()
c()
--5--
a.get
b.get
g.get
boo()
c()
""", pullLog().replace(';', '\n'))
return "OK"
}
@@ -0,0 +1,24 @@
// See KT-7674
package foo
class A(val a: Int) {
val plus: (Int)->Int
get() {
log("get plus fun")
return {
log("do plus")
a + it
}
}
}
inline fun <T : Any> id(x: T): T {
log(x.toString())
return x
}
fun box(): String {
assertEquals(3, A(id(1)).plus(id(2)))
assertEquals("1;get plus fun;2;do plus;", pullLog())
return "OK"
}
@@ -0,0 +1,49 @@
package foo
/* This tests checks, that lambda fabric invocation is not extracted.
An example:
fun foo() {
val status = "OK"
run { println(status) }
}
It's compiled to something like:
function foo() {
var status = "OK";
run(foo$(status));
}
function foo$(status) {
return function() {
console.log(status);
};
}
Thus, we need to be sure, that foo$() is not extracted to some temporary var.
*/
// CHECK_NOT_CALLED: max
// CHECK_NOT_CALLED: box$f
// CHECK_NOT_CALLED: box$f_0
inline fun max(getA: ()->Int, b: Int): Int {
val a = getA()
log("max($a, $b)")
if (a > b) return a
return b
}
fun box(): String {
val one = 1
val two = 2
val three = 3
val test = max({ fizz(one) }, max({ fizz(two) }, buzz(three)))
assertEquals(3, test)
assertEquals("buzz(3);fizz(2);max(2, 3);fizz(1);max(1, 3);", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
fun box(): String {
assertEquals(false, fizz(false) && buzz(false))
assertEquals("fizz(false);", pullLog())
assertEquals(false, fizz(true) && buzz(false))
assertEquals("fizz(true);buzz(false);", pullLog())
assertEquals(true, fizz(true) && buzz(true))
assertEquals("fizz(true);buzz(true);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertEquals(true, fizz(true) || buzz(true) && (fizz(false) || buzz(true)))
assertEquals("fizz(true);", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
fun box(): String {
assertEquals(true, fizz(true) || buzz(false))
assertEquals("fizz(true);", pullLog())
assertEquals(true, fizz(false) || buzz(true))
assertEquals("fizz(false);buzz(true);", pullLog())
assertEquals(false, fizz(false) || buzz(false))
assertEquals("fizz(false);buzz(false);", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
// Test for KT-7502
class A(val value: Int) {
fun plus(num: Int): Int = this.value + num
}
fun box(): String {
assertEquals(15, A(fizz(5)).plus(buzz(10)))
assertEquals("fizz(5);buzz(10);", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
val v = mapOf(1 to "1", 2 to "2").mapValues { it.value.map { it.toString() } }
assertEquals(2, v.size)
return "OK"
}
@@ -0,0 +1,15 @@
package foo
// Test for KT-7502
// CHECK_NOT_CALLED: plus
class A(val value: Int) {
inline fun plus(num: Int): Int = this.value + num
}
fun box(): String {
assertEquals(15, A(fizz(5)).plus(buzz(10)))
assertEquals("fizz(5);buzz(10);", pullLog())
return "OK"
}
@@ -0,0 +1,18 @@
package foo
// CHECK_NOT_CALLED: component2
class A(val x: Int, val y: Int)
operator fun A.component1(): Int = fizz(x)
inline operator fun A.component2(): Int = buzz(y)
fun box(): String {
val (a, b) = A(1, 2)
assertEquals(a, 1)
assertEquals(b, 2)
assertEquals("fizz(1);buzz(2);", pullLog())
return "OK"
}
@@ -0,0 +1,30 @@
package foo
// CHECK_NOT_CALLED: component2
// CHECK_NOT_CALLED: component3
// CHECK_NOT_CALLED: component5
class A(val a: Int, val b: Int, val c: Int, val d: Int, val e: Int)
operator fun A.component1(): Int = fizz(a)
inline operator fun A.component2(): Int = buzz(b)
inline operator fun A.component3(): Int = buzz(c)
operator fun A.component4(): Int = fizz(d)
inline operator fun A.component5(): Int = buzz(e)
fun box(): String {
val (a, b, c, d, e) = A(1, 2, 3, 4, 5)
assertEquals(1, a)
assertEquals(2, b)
assertEquals(3, c)
assertEquals(4, d)
assertEquals(5, e)
assertEquals("fizz(1);buzz(2);buzz(3);fizz(4);buzz(5);", pullLog())
return "OK"
}
@@ -0,0 +1,28 @@
package foo
private inline fun bar(predicate: (Int) -> Boolean) {
var i = -1
outer@do {
i++
if (i == 1) continue
var j = -1
do {
++j
if (j == 1) {
if (i == 3) continue@outer else continue
}
log("i$j")
} while (j < 3)
log("o$i")
} while (predicate(i))
}
fun box(): String {
bar {
log("p$it")
it < 5
}
assertEquals("i0;i2;i3;o0;p0;p1;i0;i2;i3;o2;p2;i0;p3;i0;i2;i3;o4;p4;i0;i2;i3;o5;p5;", pullLog())
return "OK"
}
@@ -0,0 +1,8 @@
package foo
fun box(): String {
assertEquals(3, buzz(fizz(1) + buzz(2)))
assertEquals("fizz(1);buzz(2);buzz(3);", pullLog())
return "OK"
}
@@ -0,0 +1,20 @@
package foo
class A(val x: Int = fizz(1) + 1) {
val y = buzz(x) + 1
val z: Int
init {
z = fizz(x) + buzz(y)
}
}
fun box(): String {
val a = A()
assertEquals(2, a.x)
assertEquals(3, a.y)
assertEquals(5, a.z)
assertEquals("fizz(1);buzz(2);fizz(2);buzz(3);", pullLog())
return "OK"
}
@@ -0,0 +1,17 @@
package foo
class A {
val x: Int
init {
x = fizz(1) + buzz(2)
}
}
fun box(): String {
val a = A()
assertEquals(3, a.x)
assertEquals("fizz(1);buzz(2);", pullLog())
return "OK"
}
@@ -0,0 +1,16 @@
package foo
class A {
var x = 23
}
inline fun bar(value: Int, a: A): Int {
a.x = 42
return value
}
fun box(): String {
val a = A()
assertEquals(23, bar(a.x, a))
return "OK"
}
@@ -0,0 +1,21 @@
package foo
object A {
init {
log("A.init")
}
val x = 23
}
inline fun bar(value: Int) {
log("bar->begin")
log("value=$value")
log("bar->end")
}
fun box(): String {
bar(A.x)
assertEquals("A.init;bar->begin;value=23;bar->end;", pullLog())
return "OK"
}
@@ -0,0 +1,39 @@
package foo
var g: Any?
get() {
log("g.get")
return null
}
set(v) {
log("g.set")
}
public inline fun Array<String>.boo() {
var a = g
for (element in this);
}
public inline fun Iterable<String>.boo(i: Any?) {
var a = i
for (element in this);
}
fun test1(f: () -> Array<String>) {
f().boo()
}
fun test2(f: () -> Iterable<String>) {
f().boo(g)
}
fun box(): String {
test1 { log("lambda1"); arrayOf() }
assertEquals("lambda1;g.get;", pullLog())
test2 { log("lambda2"); listOf() }
assertEquals("lambda2;g.get;", pullLog())
return "OK"
}
@@ -0,0 +1,20 @@
// Looks similar to KT-7674
package foo
inline fun bar(): Int {
log("bar")
return 10
}
val x: Int
get() {
log("x")
return 1
}
fun box(): String {
assertEquals(12, x + bar() + x)
assertEquals("x;bar;x;", pullLog())
return "OK"
}
@@ -0,0 +1,27 @@
// See KT-7043, KT-11711
package foo
inline fun foo(b: Any) {
val t = aa[0]
val a = b
}
val a: Array<String>
get() {
log("a.get")
return arrayOf("a")
}
val aa: Array<String>
get() {
log("aa.get")
return arrayOf("aa")
}
fun box(): String {
foo(a[0])
assertEquals("a.get;aa.get;", pullLog())
return "OK"
}
@@ -0,0 +1,20 @@
package foo
// CHECK_NOT_CALLED: bar_vux9f0$
inline fun bar(n: Int, x: Int) = if (n <= 5) x else 10
fun test(n: Int): Int = bar(n, fizz(n))
fun box(): String {
var result = test(4)
if (result != 4) return "fail1: $result"
result = test(8)
if (result != 10) return "fail2: $result"
var log = pullLog()
if (log != "fizz(4);fizz(8);") return "fail_log: $log"
return "OK"
}
@@ -0,0 +1,14 @@
package foo
fun box(): String {
var c = 2
while (buzz(c) <= 4) {
if (c > 4) throw Exception("Timeout!")
c++
}
assertEquals("buzz(2);buzz(3);buzz(4);buzz(5);", pullLog())
return "OK"
}
@@ -0,0 +1,14 @@
package foo
fun box(): String {
var c = 2
while (fizz(c) % 2 == 0 || buzz(c) % 3 == 0) {
if (c > 4) throw Exception("Timeout!")
c++
}
assertEquals("fizz(2);fizz(3);buzz(3);fizz(4);fizz(5);buzz(5);", pullLog())
return "OK"
}
@@ -0,0 +1,24 @@
// See KT-8005
package foo
private inline fun bar(predicate: (Char) -> Boolean): Int {
var i = 0
val str = "abc "
while (predicate(str[i]) && i < 3) {
i++
}
return i
}
private fun test(c: Char): Int {
return bar { it != c }
}
fun box(): String {
assertEquals(0, test('a'))
assertEquals(1, test('b'))
assertEquals(2, test('c'))
assertEquals(3, test('*'))
return "OK"
}
@@ -0,0 +1 @@
@native public fun parseInt(s: String, radix: Int = 10): Int = noImpl
@@ -0,0 +1,51 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/anonymousObject/anonymousObjectOnCallSite.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun box() : String {
val o = "O"
val p = "GOOD"
val result = doWork {
val k = "K"
val s = object : A<String>() {
val param = p;
override fun getO(): String {
return o;
}
override fun getK(): String {
return k;
}
}
s.getO() + s.getK() + s.param
}
if (result != "OKGOOD") return "fail $result"
return "OK"
}
// FILE: test.kt
package test
abstract class A<R> {
abstract fun getO() : R
abstract fun getK() : R
}
inline fun <R> doWork(job: ()-> R) : R {
return job()
}
@@ -0,0 +1,46 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/anonymousObject/anonymousObjectOnCallSiteSuperParams.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun box() : String {
val o = "O"
val result = doWork {
val k = "K"
val s = object : A<String>("11") {
override fun getO(): String {
return o;
}
override fun getK(): String {
return k;
}
}
s.getO() + s.getK() + s.param
}
if (result != "OK11") return "fail $result"
return "OK"
}
// FILE: test.kt
package test
abstract class A<R>(val param: R) {
abstract fun getO() : R
abstract fun getK() : R
}
inline fun <R> doWork(job: ()-> R) : R {
return job()
}
@@ -0,0 +1,90 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/anonymousObject/anonymousObjectOnDeclarationSite.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun test1(): String {
val o = "O"
val result = doWork ({o}, {"K"}, "GOOD")
return result.getO() + result.getK() + result.getParam()
}
fun test2() : String {
//same names as in object
val o1 = "O"
val k1 = "K"
val result = doWorkInConstructor ({o1}, {k1}, "GOOD")
return result.getO() + result.getK() + result.getParam()
}
fun box() : String {
val result1 = test1();
if (result1 != "OKGOOD") return "fail1 $result1"
val result2 = test2();
if (result2 != "OKGOOD") return "fail2 $result2"
return "OK"
}
// FILE: test.kt
package test
abstract class A<R> {
abstract fun getO() : R
abstract fun getK() : R
abstract fun getParam() : R
}
inline fun <R> doWork(crossinline jobO: ()-> R, crossinline jobK: ()-> R, param: R) : A<R> {
val s = object : A<R>() {
override fun getO(): R {
return jobO()
}
override fun getK(): R {
return jobK()
}
override fun getParam(): R {
return param
}
}
return s;
}
inline fun <R> doWorkInConstructor(crossinline jobO: ()-> R, crossinline jobK: ()-> R, param: R) : A<R> {
val s = object : A<R>() {
val p = param;
val o1 = jobO()
val k1 = jobK()
override fun getO(): R {
return o1
}
override fun getK(): R {
return k1
}
override fun getParam(): R {
return p
}
}
return s;
}
@@ -0,0 +1,77 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/anonymousObject/anonymousObjectOnDeclarationSiteSuperParams.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun test1(): String {
val o = "O"
val result = doWork ({o}, {"K"}, "11")
return result.getO() + result.getK() + result.param
}
fun test2() : String {
//same names as in object
val o1 = "O"
val k1 = "K"
val param = "11"
val result = doWorkInConstructor ({o1}, {k1}, {param})
return result.getO() + result.getK() + result.param
}
fun box() : String {
val result1 = test1();
if (result1 != "OK11") return "fail1 $result1"
val result2 = test2();
if (result2 != "OK11") return "fail2 $result2"
return "OK"
}
// FILE: test.kt
package test
abstract class A<R>(val param : R) {
abstract fun getO() : R
abstract fun getK() : R
}
inline fun <R> doWork(crossinline jobO: ()-> R, crossinline jobK: ()-> R, param: R) : A<R> {
val s = object : A<R>(param) {
override fun getO(): R {
return jobO()
}
override fun getK(): R {
return jobK()
}
}
return s;
}
inline fun <R> doWorkInConstructor(crossinline jobO: ()-> R, crossinline jobK: ()-> R, crossinline param: () -> R) : A<R> {
val s = object : A<R>(param()) {
val o1 = jobO()
val k1 = jobK()
override fun getO(): R {
return o1
}
override fun getK(): R {
return k1
}
}
return s;
}
@@ -0,0 +1,291 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/builders/builders.1.kt
*/
// FILE: foo.kt
package foo
fun testAllInline() : String {
val args = arrayOf("1", "2", "3")
val result =
html {
val htmlVal = 0
head {
title { +"XML encoding with Kotlin" }
}
body {
var bodyVar = 1
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project"
}
p { +"some text" }
// content generated from command-line arguments
p {
+"Command line arguments were:"
ul {
for (arg in args)
li { +arg; +"$htmlVal"; +"$bodyVar" }
}
}
}
}
return result.toString()!!
}
fun testHtmlNoInline() : String {
val args = arrayOf("1", "2", "3")
val result =
htmlNoInline() {
val htmlVal = 0
head {
title { +"XML encoding with Kotlin" }
}
body {
var bodyVar = 1
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project"
}
p { +"some text" }
// content generated from command-line arguments
p {
+"Command line arguments were:"
ul {
for (arg in args)
li { +arg; +"$htmlVal"; +"$bodyVar" }
}
}
}
}
return result.toString()!!
}
fun testBodyNoInline() : String {
val args = arrayOf("1", "2", "3")
val result =
html {
val htmlVal = 0
head {
title { +"XML encoding with Kotlin" }
}
bodyNoInline {
var bodyVar = 1
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project"
}
p { +"some text" }
// content generated from command-line arguments
p {
+"Command line arguments were:"
ul {
for (arg in args)
li { +arg; +"$htmlVal"; +"$bodyVar" }
}
}
}
}
return result.toString()!!
}
fun testBodyHtmlNoInline() : String {
val args = arrayOf("1", "2", "3")
val result =
htmlNoInline {
val htmlVal = 0
head {
title { +"XML encoding with Kotlin" }
}
bodyNoInline {
var bodyVar = 1
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project"
}
p { +"some text" }
// content generated from command-line arguments
p {
+"Command line arguments were:"
ul {
for (arg in args)
li { +arg; +"$htmlVal"; +"$bodyVar" }
}
}
}
}
return result.toString()!!
}
fun box(): String {
var expected = testAllInline();
if (expected != testHtmlNoInline()) return "fail 1: ${testHtmlNoInline()}\nbut expected\n${expected} "
if (expected != testBodyNoInline()) return "fail 2: ${testBodyNoInline()}\nbut expected\n${expected} "
if (expected != testBodyHtmlNoInline()) return "fail 3: ${testBodyHtmlNoInline()}\nbut expected\n${expected} "
return "OK"
}
// FILE: bar.kt
package foo
import java.util.ArrayList
import java.util.HashMap
abstract class Element {
abstract fun render(builder: StringBuilder, indent: String)
override fun toString(): String {
val builder = StringBuilder()
render(builder, "")
return builder.toString()
}
}
class TextElement(val text: String) : Element() {
override fun render(builder: StringBuilder, indent: String) {
builder.append("$indent$text\n")
}
}
abstract class Tag(val name: String) : Element() {
val children = ArrayList<Element>()
val attributes = HashMap<String, String>()
inline protected fun <T : Element> initTag(tag: T, init: T.() -> Unit): T {
tag.init()
children.add(tag)
return tag
}
override fun render(builder: StringBuilder, indent: String) {
builder.append("$indent<$name${renderAttributes()}>\n")
for (c in children) {
c.render(builder, indent + " ")
}
builder.append("$indent</$name>\n")
}
private fun renderAttributes(): String? {
val builder = StringBuilder()
for (a in attributes.keys) {
builder.append(" $a=\"${attributes[a]}\"")
}
return builder.toString()
}
}
abstract class TagWithText(name: String) : Tag(name) {
operator fun String.unaryPlus() {
children.add(TextElement(this))
}
}
class HTML() : TagWithText("html") {
inline fun head(init: Head.() -> Unit) = initTag(Head(), init)
inline fun body(init: Body.() -> Unit) = initTag(Body(), init)
fun bodyNoInline(init: Body.() -> Unit) = initTag(Body(), init)
}
class Head() : TagWithText("head") {
inline fun title(init: Title.() -> Unit) = initTag(Title(), init)
}
class Title() : TagWithText("title")
abstract class BodyTag(name: String) : TagWithText(name) {
inline fun b(init: B.() -> Unit) = initTag(B(), init)
inline fun p(init: P.() -> Unit) = initTag(P(), init)
inline fun pNoInline(init: P.() -> Unit) = initTag(P(), init)
inline fun h1(init: H1.() -> Unit) = initTag(H1(), init)
inline fun ul(init: UL.() -> Unit) = initTag(UL(), init)
inline fun a(href: String, init: A.() -> Unit) {
val a = initTag(A(), init)
a.href = href
}
}
class Body() : BodyTag("body")
class UL() : BodyTag("ul") {
inline fun li(init: LI.() -> Unit) = initTag(LI(), init)
}
class B() : BodyTag("b")
class LI() : BodyTag("li")
class P() : BodyTag("p")
class H1() : BodyTag("h1")
class A() : BodyTag("a") {
public var href: String
get() = attributes["href"]!!
set(value) {
attributes["href"] = value
}
}
inline fun html(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}
fun htmlNoInline(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}
@@ -0,0 +1,295 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/builders/buildersAndLambdaCapturing.1.kt
*/
// FILE: foo.kt
package foo
inline fun testAllInline(f: () -> String) : String {
val args = arrayOf("1", "2", "3")
val result =
html {
val htmlVal = 0
head {
title { +"XML encoding with Kotlin" }
}
body {
var bodyVar = 1
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project"
}
p { +"some text" }
// content generated from command-line arguments
p {
+"Command line arguments were:"
ul {
for (arg in args)
li { +arg; +"$htmlVal"; +"$bodyVar"; +"${f()}" }
}
}
}
}
return result.toString()!!
}
inline fun testHtmlNoInline(crossinline f: () -> String) : String {
val args = arrayOf("1", "2", "3")
val result =
htmlNoInline() {
val htmlVal = 0
head {
title { +"XML encoding with Kotlin" }
}
body {
var bodyVar = 1
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project"
}
p { +"some text" }
// content generated from command-line arguments
p {
+"Command line arguments were:"
ul {
for (arg in args)
li { +arg; +"$htmlVal"; +"$bodyVar"; +"${f()}" }
}
}
}
}
return result.toString()!!
}
inline fun testBodyNoInline(crossinline f: () -> String) : String {
val args = arrayOf("1", "2", "3")
val result =
html {
val htmlVal = 0
head {
title { +"XML encoding with Kotlin" }
}
bodyNoInline {
var bodyVar = 1
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project"
}
p { +"some text" }
// content generated from command-line arguments
p {
+"Command line arguments were:"
ul {
for (arg in args)
li { +arg; +"$htmlVal"; +"$bodyVar"; +"${f()}" }
}
}
}
}
return result.toString()!!
}
inline fun testBodyHtmlNoInline(crossinline f: () -> String) : String {
val args = arrayOf("1", "2", "3")
val result =
htmlNoInline {
val htmlVal = 0
head {
title { +"XML encoding with Kotlin" }
}
bodyNoInline {
var bodyVar = 1
h1 { +"XML encoding with Kotlin" }
p { +"this format can be used as an alternative markup to XML" }
// an element with attributes and text content
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
// mixed content
p {
+"This is some"
b { +"mixed" }
+"text. For more see the"
a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
+"project"
}
p { +"some text" }
// content generated from command-line arguments
p {
+"Command line arguments were:"
ul {
for (arg in args)
li { +arg; +"$htmlVal"; +"$bodyVar"; +"${f()}" }
}
}
}
}
return result.toString()!!
}
fun box(): String {
var expected = testAllInline({"x"});
print(expected + " " + testHtmlNoInline({"x"}))
if (expected != testHtmlNoInline({"x"})) return "fail 1: ${testHtmlNoInline({"x"})}\nbut expected\n${expected} "
if (expected != testBodyNoInline({"x"})) return "fail 2: ${testBodyNoInline({"x"})}\nbut expected\n${expected} "
if (expected != testBodyHtmlNoInline({"x"})) return "fail 3: ${testBodyHtmlNoInline({"x"})}\nbut expected\n${expected} "
var captured = "x"
if (expected != testHtmlNoInline({captured})) return "fail 4: ${testHtmlNoInline({captured})}\nbut expected\n${expected} "
if (expected != testBodyNoInline({captured})) return "fail 5: ${testBodyNoInline({captured})}\nbut expected\n${expected} "
if (expected != testBodyHtmlNoInline({captured})) return "fail 6: ${testBodyHtmlNoInline({captured})}\nbut expected\n${expected} "
return "OK"
}
// FILE: bar.kt
package foo
import java.util.ArrayList
import java.util.HashMap
abstract class Element {
abstract fun render(builder: StringBuilder, indent: String)
override fun toString(): String {
val builder = StringBuilder()
render(builder, "")
return builder.toString()
}
}
class TextElement(val text: String) : Element() {
override fun render(builder: StringBuilder, indent: String) {
builder.append("$indent$text\n")
}
}
abstract class Tag(val name: String) : Element() {
val children = ArrayList<Element>()
val attributes = HashMap<String, String>()
inline protected fun <T : Element> initTag(tag: T, init: T.() -> Unit): T {
tag.init()
children.add(tag)
return tag
}
override fun render(builder: StringBuilder, indent: String) {
builder.append("$indent<$name${renderAttributes()}>\n")
for (c in children) {
c.render(builder, indent + " ")
}
builder.append("$indent</$name>\n")
}
private fun renderAttributes(): String? {
val builder = StringBuilder()
for (a in attributes.keys) {
builder.append(" $a=\"${attributes[a]}\"")
}
return builder.toString()
}
}
abstract class TagWithText(name: String) : Tag(name) {
operator fun String.unaryPlus() {
children.add(TextElement(this))
}
}
class HTML() : TagWithText("html") {
inline fun head(init: Head.() -> Unit) = initTag(Head(), init)
inline fun body(init: Body.() -> Unit) = initTag(Body(), init)
fun bodyNoInline(init: Body.() -> Unit) = initTag(Body(), init)
}
class Head() : TagWithText("head") {
inline fun title(init: Title.() -> Unit) = initTag(Title(), init)
}
class Title() : TagWithText("title")
abstract class BodyTag(name: String) : TagWithText(name) {
inline fun b(init: B.() -> Unit) = initTag(B(), init)
inline fun p(init: P.() -> Unit) = initTag(P(), init)
fun pNoInline(init: P.() -> Unit) = initTag(P(), init)
inline fun h1(init: H1.() -> Unit) = initTag(H1(), init)
inline fun ul(init: UL.() -> Unit) = initTag(UL(), init)
inline fun a(href: String, init: A.() -> Unit) {
val a = initTag(A(), init)
a.href = href
}
}
class Body() : BodyTag("body")
class UL() : BodyTag("ul") {
inline fun li(init: LI.() -> Unit) = initTag(LI(), init)
}
class B() : BodyTag("b")
class LI() : BodyTag("li")
class P() : BodyTag("p")
class H1() : BodyTag("h1")
class A() : BodyTag("a") {
public var href: String
get() = attributes["href"]!!
set(value) {
attributes["href"] = value
}
}
inline fun html(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}
fun htmlNoInline(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}
@@ -0,0 +1,33 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/capture/captureInlinable.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun box(): String {
val result = doWork({11})
if (result != 11) return "test1: ${result}"
val result2 = doWork({12; result+1})
if (result2 != 12) return "test2: ${result2}"
return "OK"
}
// FILE: test.kt
package test
inline fun <R> doWork(crossinline job: ()-> R) : R {
return notInline({job()})
}
fun <R> notInline(job: ()-> R) : R {
return job()
}
@@ -0,0 +1,33 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/capture/captureInlinableAndOther.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun box(): String {
val result = doWork({11})
if (result != 11) return "test1: ${result}"
val result2 = doWork({12; result+1})
if (result2 != 12) return "test2: ${result2}"
return "OK"
}
// FILE: test.kt
package test
inline fun <R> doWork(crossinline job: ()-> R) : R {
val k = 10;
return notInline({k; job()})
}
fun <R> notInline(job: ()-> R) : R {
return job()
}
@@ -0,0 +1,47 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/capture/captureThisAndReceiver.1.kt
*/
// FILE: foo.kt
package foo
fun test1() : Int {
val inlineX = My(111)
return inlineX.perform<My, Int>{
val outX = My(1111111)
outX.perform<My, Int>(
{inlineX.value}
)
}
}
inline fun My.execute(): Int {
return perform { this.value }
}
fun test2(): Int {
val inlineX = My(11)
return inlineX.execute()
}
fun box(): String {
if (test1() != 111) return "test1: ${test1()}"
if (test2() != 11) return "test2: ${test2()}"
return "OK"
}
// FILE: bar.kt
package foo
class My(val value: Int)
inline fun <T, R> T.perform(job: (T)-> R) : R {
return job(this)
}
@@ -0,0 +1,29 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/complex/closureChain.1.kt
*/
// FILE: foo.kt
package foo
fun test1(): Int {
val inlineX = Inline()
return inlineX.foo({ z: Int -> "" + z}, 25, { -> this.length })
}
fun box(): String {
if (test1() != 2) return "test1: ${test1()}"
return "OK"
}
// FILE: bar.kt
package foo
class Inline() {
inline fun foo(closure1 : (l: Int) -> String, param: Int, closure2: String.() -> Int) : Int {
return closure1(param).closure2()
}
}
@@ -0,0 +1,42 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/defaultValues/defaultMethod.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun simple(): String {
val k = "K"
return simpleFun(lambda = {it + "O"}) + simpleFun("K", {k + it})
}
fun simpleR(): String {
val k = "K"
return simpleFunR({it + "O"}) + simpleFunR({k + it}, "K")
}
fun box(): String {
var result = simple()
if (result != "OOKK") return "fail1: ${result}"
result = simpleR()
if (result != "OOKK") return "fail2: ${result}"
return "OK"
}
// FILE: test.kt
package test
inline fun <T> simpleFun(arg: String = "O", lambda: (String) -> T): T {
return lambda(arg)
}
inline fun <T> simpleFunR(lambda: (String) -> T, arg: String = "O"): T {
return lambda(arg)
}
@@ -0,0 +1,37 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/capture/generics.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun test1(s: Int): String {
var result = "OK"
result = mfun(s) { a ->
result + doSmth(s) + doSmth(a)
}
return result
}
fun box(): String {
val result = test1(11)
if (result != "OK1111") return "fail1: ${result}"
return "OK"
}
// FILE: test.kt
package test
inline fun <T, R> mfun(arg: T, f: (T) -> R) : R {
return f(arg)
}
inline fun <T> doSmth(a: T): String {
return a.toString()
}
@@ -0,0 +1,42 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/defaultValues/inlineInDefaultParameter.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun testCompilation(arg: String = getStringInline()): String {
return arg
}
inline fun testCompilationInline(arg: String = getStringInline()): String {
return arg
}
fun box(): String {
var result = testCompilation()
if (result != "OK") return "fail1: ${result}"
result = testCompilation("OKOK")
if (result != "OKOK") return "fail2: ${result}"
result = testCompilationInline()
if (result != "OK") return "fail3: ${result}"
result = testCompilationInline("OKOK")
if (result != "OKOK") return "fail4: ${result}"
return "OK"
}
// FILE: test.kt
package test
inline fun getStringInline(): String {
return "OK"
}
@@ -0,0 +1,19 @@
// FILE: a.kt
package foo
inline fun sum(a: Int, b: Int): Int {
return a + b
}
// FILE: b.kt
package foo
// CHECK_NOT_CALLED: sum
fun box(): String {
val sum3 = sum(1, 2)
assertEquals(3, sum3)
return "OK"
}
@@ -0,0 +1,39 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/lambdaTransformation/lambdaCloning.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun test1(s: Long): String {
return doSmth(s)
}
fun test2(s: Int): String {
return doSmth2(s)
}
fun box(): String {
var result = test1(11)
if (result != "11") return "fail1: ${result}"
result = test2(11)
if (result != "11") return "fail2: ${result}"
return "OK"
}
// FILE: test.kt
package test
inline fun <T> doSmth(a: T) : String {
return {a.toString()}()
}
inline fun <T> doSmth2(a: T) : String {
return {{a.toString()}()}()
}
@@ -0,0 +1,41 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/lambdaTransformation/lambdaInLambda2.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun test1(prefix: String): String {
var result = "fail"
mfun {
concat("start") {
if (it.startsWith(prefix)) {
result = "OK"
}
}
}
return result
}
fun box(): String {
if (test1("start") != "OK") return "fail1"
if (test1("nostart") != "fail") return "fail2"
return "OK"
}
// FILE: test.kt
package test
inline fun <R> mfun(f: () -> R) {
f()
}
fun concat(suffix: String, l: (s: String) -> Unit) {
l(suffix)
}
@@ -0,0 +1,77 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/lambdaTransformation/lambdaInLambdaNoInline.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun test1(param: String): String {
var result = "fail1"
noInlineFun(param) { a ->
concat("start") {
result = doSmth(a).toString()
}
}
return result
}
fun test11(param: String): String {
var result = "fail1"
noInlineFun("stub") { a ->
concat("start") {
result = doSmth(param).toString()
}
}
return result
}
inline fun test2(crossinline param: () -> String): String {
var result = "fail1"
noInlineFun("stub") { a ->
concat(param()) {
result = doSmth(param()).toString()
}
}
return result
}
inline fun test22(crossinline param: () -> String): String {
var result = "fail1"
{{result = param()}()}()
return result
}
fun box(): String {
if (test1("start") != "start") return "fail1: ${test1("start")}"
if (test1("nostart") != "nostart") return "fail2: ${test1("nostart")}"
if (test11("start") != "start") return "fail3: ${test11("start")}"
if (test2({"start"}) != "start") return "fail4: ${test2({"start"})}"
if (test22({"start"}) != "start") return "fail5: ${test22({"start"})}"
return "OK"
}
// FILE: test.kt
package test
fun concat(suffix: String, l: (s: String) -> Unit) {
l(suffix)
}
fun <T> noInlineFun(arg: T, f: (T) -> Unit) {
f(arg)
}
inline fun doSmth(a: String): String {
return a.toString()
}
@@ -0,0 +1,40 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/lambdaTransformation/regeneratedLambdaName.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun sameName(s: Long): Long {
return call {
s
}
}
fun sameName(s: Int): Int {
return call {
s
}
}
fun box(): String {
val result = sameName(1)
if (result != 1) return "fail1: ${result}"
val result2 = sameName(2)
if (result2 != 2) return "fail2: ${result2}"
return "OK"
}
// FILE: test.kt
package test
inline fun <R> call(crossinline f: () -> R) : R {
return {f()} ()
}
@@ -0,0 +1,49 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/lambdaTransformation/sameCaptured.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun testSameCaptured() : String {
var result = 0;
result = doWork({result+=1; result}, {result += 11; result})
return if (result == 12) "OK" else "fail ${result}"
}
inline fun testSameCaptured(crossinline lambdaWithResultCaptured: () -> Unit) : String {
var result = 1;
result = doWork({result+=11; lambdaWithResultCaptured(); result})
return if (result == 12) "OK" else "fail ${result}"
}
fun box(): String {
if (testSameCaptured() != "OK") return "test1 : ${testSameCaptured()}"
var result = 0;
if (testSameCaptured{result += 1111} != "OK") return "test2 : ${testSameCaptured{result = 1111}}"
return "OK"
}
// FILE: test.kt
package test
inline fun <R> doWork(crossinline job: ()-> R) : R {
val k = 10;
return notInline({k; job()})
}
inline fun <R> doWork(crossinline job: ()-> R, crossinline job2: () -> R) : R {
val k = 10;
return notInline({k; job(); job2()})
}
fun <R> notInline(job: ()-> R) : R {
return job()
}
@@ -0,0 +1,90 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/capture/simpleCapturingInClass.1.kt
*/
// FILE: a.kt
package foo
fun testAll(): String {
val inlineX = InlineAll()
return inlineX.inline({ a1: Int, a2: Double, a3: Double, a4: String, a5: Long ->
"" + a1 + a2 + a3 + a4 + a5},
1, 12.5, 13.5, "14", 15)
}
fun testAllWithCapturedVal(): String {
val inlineX = InlineAll()
val c1 = 21
val c2 = 22.5
val c3 = 23.5
val c4 = "24"
val c5 = 25
val c6 = 'H'
val c7 = 26
val c8 = 27
val c9 = 28.5
return inlineX.inline({ a1: Int, a2: Double, a3: Double, a4: String, a5: Long ->
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
1, 12.5, 13.5, "14", 15)
}
fun testAllWithCapturedVar(): String {
val inlineX = InlineAll()
var c1 = 21
var c2 = 22.5
var c3 = 23.5
var c4 = "24"
var c5 = 25
var c6 = 'H'
var c7 = 26
var c8 = 27
val c9 = 28.5
return inlineX.inline({ a1: Int, a2: Double, a3: Double, a4: String, a5: Long ->
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
1, 12.5, 13.5, "14", 15)
}
fun testAllWithCapturedValAndVar(): String {
val inlineX = InlineAll()
var c1 = 21
var c2 = 22.5
val c3 = 23.5
val c4 = "24"
var c5 = 25
val c6 = 'H'
var c7 = 26
var c8 = 27
val c9 = 28.5
return inlineX.inline({ a1: Int, a2: Double, a3: Double, a4: String, a5: Long ->
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
1, 12.5, 13.5, "14", 15)
}
fun box(): String {
if (testAll() != "112.513.51415") return "testAll: ${testAll()}"
if (testAllWithCapturedVal() != "112.513.514152122.523.52425H262728.5") return "testAllWithCapturedVal: ${testAllWithCapturedVal()}"
if (testAllWithCapturedVar() != "112.513.514152122.523.52425H262728.5") return "testAllWithCapturedVar: ${testAllWithCapturedVar()}"
if (testAllWithCapturedValAndVar() != "112.513.514152122.523.52425H262728.5") return "testAllWithCapturedVal: ${testAllWithCapturedValAndVar()}"
return "OK"
}
// FILE: b.kt
package foo
class InlineAll {
inline fun inline(s: (Int, Double, Double, String, Long) -> String,
a1: Int, a2: Double, a3: Double, a4: String, a5: Long): String {
return s(a1, a2, a3, a4, a5)
}
}
@@ -0,0 +1,79 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/capture/simpleCapturingInPackage.1.kt
*/
// FILE: a.kt
package foo
fun testAll(): String {
return inline({ a1: Int, a2: Double, a3: Double, a4: String, a5: Long ->
"" + a1 + a2 + a3 + a4 + a5},
1, 12.5, 13.5, "14", 15)
}
fun testAllWithCapturedVal(): String {
val c1 = 21
val c2 = 22.5
val c3 = 23.5
val c4 = "24"
val c5 = 25
val c6 = 'H'
val c7 = 26
val c8 = 27
val c9 = 28.5
return inline({ a1: Int, a2: Double, a3: Double, a4: String, a5: Long ->
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
1, 12.5, 13.5, "14", 15)
}
fun testAllWithCapturedVar(): String {
var c1 = 21
var c2 = 22.5
var c3 = 23.5
var c4 = "24"
var c5 = 25
var c6 = 'H'
var c7 = 26
var c8 = 27
val c9 = 28.5
return inline({ a1: Int, a2: Double, a3: Double, a4: String, a5: Long ->
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
1, 12.5, 13.5, "14", 15)
}
fun testAllWithCapturedValAndVar(): String {
var c1 = 21
var c2 = 22.5
val c3 = 23.5
val c4 = "24"
var c5 = 25
val c6 = 'H'
var c7 = 26
var c8 = 27
val c9 = 28.5
return inline({ a1: Int, a2: Double, a3: Double, a4: String, a5: Long ->
"" + a1 + a2 + a3 + a4 + a5 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9},
1, 12.5, 13.5, "14", 15)
}
fun box(): String {
if (testAll() != "112.513.51415") return "testAll: ${testAll()}"
if (testAllWithCapturedVal() != "112.513.514152122.523.52425H262728.5") return "testAllWithCapturedVal: ${testAllWithCapturedVal()}"
if (testAllWithCapturedVar() != "112.513.514152122.523.52425H262728.5") return "testAllWithCapturedVar: ${testAllWithCapturedVar()}"
if (testAllWithCapturedValAndVar() != "112.513.514152122.523.52425H262728.5") return "testAllWithCapturedVal: ${testAllWithCapturedValAndVar()}"
return "OK"
}
// FILE: b.kt
package foo
inline fun inline(s: (Int, Double, Double, String, Long) -> String,
a1: Int, a2: Double, a3: Double, a4: String, a5: Long): String {
return s(a1, a2, a3, a4, a5)
}
@@ -0,0 +1,43 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/defaultValues/simpleDefaultMethod.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun testCompilation(): String {
emptyFun()
emptyFun("K")
return "OK"
}
fun simple(): String {
return simpleFun() + simpleFun("K")
}
fun box(): String {
var result = testCompilation()
if (result != "OK") return "fail1: ${result}"
result = simple()
if (result != "OK") return "fail2: ${result}"
return "OK"
}
// FILE: test.kt
package test
inline fun emptyFun(arg: String = "O") {
}
inline fun simpleFun(arg: String = "O"): String {
val r = arg;
return r;
}
+52
View File
@@ -0,0 +1,52 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/trait/trait.1.kt
*/
// FILE: foo.kt
package foo
import test.*
// CHECK_CONTAINS_NO_CALLS: testClassObject
internal fun testFinalInline(): String {
return Z().finalInline({"final"})
}
internal fun testFinalInline2(instance: InlineTrait): String {
return instance.finalInline({"final2"})
}
internal fun testClassObject(): String {
return InlineTrait.finalInline({"classobject"})
}
fun box(): String {
if (testFinalInline() != "final") return "test1: ${testFinalInline()}"
if (testFinalInline2(Z()) != "final2") return "test2: ${testFinalInline2(Z())}"
if (testClassObject() != "classobject") return "test3: ${testClassObject()}"
return "OK"
}
// FILE: test.kt
package test
internal interface InlineTrait {
public fun finalInline(s: () -> String): String {
return s()
}
companion object {
public inline final fun finalInline(s: () -> String): String {
return s()
}
}
}
class Z: InlineTrait {
}
@@ -0,0 +1,74 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/tryCatchFinally/tryCatch.1.kt
*/
// FILE: a.kt
package foo
fun test1() : Int {
val inlineX = My(111)
var result = 0
val res = inlineX.perform<My, Int>{
try {
throw RuntimeException()
} catch (e: RuntimeException) {
result = -1
}
result
}
return result
}
fun test11() : Int {
val inlineX = My(111)
val res = inlineX.perform<My, Int>{
try {
throw RuntimeException()
} catch (e: RuntimeException) {
-1
}
}
return res
}
fun test2() : Int {
try {
val inlineX = My(111)
var result = 0
val res = inlineX.perform<My, Int>{
try {
throw RuntimeExceptionWithValue("-1")
} catch (e: RuntimeException) {
throw RuntimeExceptionWithValue("-2")
}
}
return result
} catch (e: RuntimeExceptionWithValue) {
return e.value.toInt2()!!
}
}
fun box(): String {
if (test1() != -1) return "test1: ${test1()}"
if (test11() != -1) return "test11: ${test11()}"
if (test2() != -2) return "test2: ${test2()}"
return "OK"
}
// FILE: b.kt
package foo
class My(val value: Int)
inline fun <T, R> T.perform(job: (T)-> R) : R {
return job(this)
}
inline fun String.toInt2() : Int = parseInt(this)
class RuntimeExceptionWithValue(val value: String) : RuntimeException()
@@ -0,0 +1,145 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/tryCatchFinally/tryCatch2.1.kt
*/
// FILE: a.kt
package foo
fun test1(): Int {
val res = My(111).performWithFail<My, Int>(
{
throw RuntimeExceptionWithValue()
}, {
it.value
})
return res
}
fun test11(): Int {
val res = My(111).performWithFail2<My, Int>(
{
try {
throw RuntimeExceptionWithValue("1")
} catch (e: RuntimeExceptionWithValue) {
throw RuntimeExceptionWithValue("2")
}
},
{ ex, thizz ->
if (ex.value == "2") {
thizz.value
} else {
-11111
}
})
return res
}
fun test2(): Int {
val res = My(111).performWithFail<My, Int>(
{
it.value
},
{
it.value + 1
})
return res
}
fun test22(): Int {
val res = My(111).performWithFail2<My, Int>(
{
try {
throw RuntimeExceptionWithValue("1")
} catch (e: RuntimeExceptionWithValue) {
it.value
111
}
},
{ ex, thizz ->
-11111
})
return res
}
fun test3(): Int {
try {
val res = My(111).performWithFail<My, Int>(
{
throw RuntimeExceptionWithValue("-1")
}, {
throw RuntimeExceptionWithValue("-2")
})
return res
} catch (e: RuntimeExceptionWithValue) {
return e.value.toInt2()!!
}
}
fun test33(): Int {
try {
val res = My(111).performWithFail2<My, Int>(
{
try {
throw RuntimeExceptionWithValue("-1")
} catch (e: RuntimeExceptionWithValue) {
throw RuntimeExceptionWithValue("-2")
}
},
{ ex, thizz ->
if (ex.value == "-2") {
throw RuntimeExceptionWithValue("-3")
} else {
-11111
}
})
return res
} catch (e: RuntimeExceptionWithValue) {
return e.value.toInt2()!!
}
}
fun box(): String {
if (test1() != 111) return "test1: ${test1()}"
if (test11() != 111) return "test11: ${test11()}"
if (test2() != 111) return "test2: ${test2()}"
if (test22() != 111) return "test22: ${test22()}"
if (test3() != -2) return "test3: ${test3()}"
if (test33() != -3) return "test33: ${test33()}"
return "OK"
}
// FILE: b.kt
package foo
class My(val value: Int)
inline fun <T, R> T.performWithFail(job: (T)-> R, failJob: (T) -> R): R {
try {
return job(this)
} catch (e: RuntimeExceptionWithValue) {
return failJob(this)
}
}
inline fun <T, R> T.performWithFail2(job: (T)-> R, failJob: (e: RuntimeExceptionWithValue, T) -> R): R {
try {
return job(this)
} catch (e: RuntimeExceptionWithValue) {
return failJob(e, this)
}
}
@native object Number {
fun parseInt(str: String): Int = noImpl
}
inline fun String.toInt2(): Int = parseInt(this)
class RuntimeExceptionWithValue(val value: String = "") : RuntimeException()
@@ -0,0 +1,107 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/tryCatchFinally/tryCatchFinally.1.kt
*/
// FILE: a.kt
package foo
fun test1(): Int {
var res = My(111).performWithFinally<My, Int>(
{
1
}, {
it.value
})
return res
}
fun test11(): Int {
var result = -1;
val res = My(111).performWithFinally<My, Int>(
{
try {
result = it.value
throw RuntimeException("1")
} catch (e: RuntimeException) {
++result
throw RuntimeException("2")
}
},
{
++result
})
return res
}
fun test2(): Int {
var res = My(111).performWithFinally<My, Int>(
{
throw RuntimeException("1")
},
{
it.value
})
return res
}
fun test3(): Int {
try {
var result = -1;
val res = My(111).performWithFailFinally<My, Int>(
{
result = it.value;
throw RuntimeException("-1")
},
{ e, z ->
++result
throw RuntimeException("-2")
},
{
++result
})
return res
} catch (e: RuntimeException) {
return e.message?.toInt2()!!
}
}
fun box(): String {
if (test1() != 111) return "test1: ${test1()}"
if (test11() != 113) return "test11: ${test11()}"
if (test2() != 111) return "test2: ${test2()}"
if (test3() != 113) return "test3: ${test3()}"
return "OK"
}
// FILE: b.kt
package foo
class My(val value: Int)
inline fun <T, R> T.performWithFinally(job: (T)-> R, finallyFun: (T) -> R) : R {
try {
job(this)
} finally {
return finallyFun(this)
}
}
inline fun <T, R> T.performWithFailFinally(job: (T)-> R, failJob : (e: RuntimeException, T) -> R, finallyFun: (T) -> R) : R {
try {
job(this)
} catch (e: RuntimeException) {
failJob(e, this)
} finally {
return finallyFun(this)
}
}
inline fun String.toInt2() : Int = parseInt(this)
+70
View File
@@ -0,0 +1,70 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/complex/use.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun Data.test1(d: Data) : Int {
val input2 = Input(this)
val input = Input(this)
return input.use<Input, Int>{
val output = Output(d)
output.use<Output,Int>{
input.copyTo(output, 10)
}
}
}
fun box(): String {
val result = Data().test1(Data())
if (result != 100) return "test1: ${result}"
return "OK"
}
// FILE: test.kt
package test
public class Data()
public data class Input(val d: Data) : Closeable {
public fun data() : Int = 100
}
public class Output(val d: Data) : Closeable {
public fun doOutput(data: Int): Int = data
}
public interface Closeable {
open public fun close() {}
}
public fun Input.copyTo(output: Output, size: Int): Int {
return output.doOutput(this.data())
}
public inline fun <T: Closeable, R> T.use(block: (T)-> R) : R {
var closed = false
try {
return block(this)
} catch (e: Exception) {
closed = true
try {
this.close()
} catch (closeException: Exception) {
}
throw e
} finally {
if (!closed) {
this.close()
}
}
}
+83
View File
@@ -0,0 +1,83 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/complex/with.1.kt
*/
// FILE: foo.kt
package foo
import test.*
fun Data.test1(d: Data) : Int {
val input = Input(this)
var result = 10
with(input) {
result = use<Int>{
val output = Output(d)
use<Int>{
data()
copyTo(output, 10)
}
}
}
return result
}
fun Data.test2(d: Data) : Int {
val input = Input(this)
var result = 10
with2(input) {
result = use<Int>{
val output = Output(d)
useNoInline<Int>{
data()
copyTo(output, 10)
}
}
}
return result
}
fun box(): String {
val result = Data().test1(Data())
if (result != 100) return "test1: ${result}"
val result2 = Data().test2(Data())
if (result2 != 100) return "test2: ${result2}"
return "OK"
}
// FILE: test.kt
package test
public class Data()
public class Input(val d: Data) : Closeable {
public fun data() : Int = 100
}
public class Output(val d: Data) : Closeable {
public fun doOutput(data: Int): Int = data
}
public interface Closeable {
open public fun close() {}
}
public inline fun <R> use(block: ()-> R) : R {
return block()
}
public fun <R> useNoInline(block: ()-> R) : R {
return block()
}
public fun Input.copyTo(output: Output, size: Int): Int {
return output.doOutput(this.data())
}
public inline fun <T> with2(receiver : T, crossinline body : T.() -> Unit) : Unit = {receiver.body()}()
@@ -0,0 +1,29 @@
// MODULE: lib
// FILE: lib.kt
package utils
public var LOG: String = ""
inline
public fun log(s: String): String {
LOG += s
return LOG
}
// MODULE: main(lib)
// FILE: main.kt
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
internal fun test(s: String): String = log(s + ";")
fun box(): String {
assertEquals("a;", test("a"))
assertEquals("a;b;", test("b"))
return "OK"
}
@@ -0,0 +1,25 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline
public fun <T, R> apply(x: T, fn: (T)->R): R =
fn(x)
// MODULE: main(lib)
// FILE: main.kt
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
internal fun multiplyBy2(x: Int): Int = x * 2
internal fun test(x: Int): Int = apply(x, ::multiplyBy2)
fun box(): String {
assertEquals(6, test(3))
return "OK"
}
@@ -0,0 +1,21 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline
public fun sum(x: Int, y: Int): Int =
x + y
// MODULE: main(lib)
// FILE: main.kt
// CHECK_CONTAINS_NO_CALLS: test
internal fun test(x: Int, y: Int): Int = utils.sum(x, y)
fun box(): String {
assertEquals(3, test(1, 2))
assertEquals(5, test(2, 3))
return "OK"
}
@@ -0,0 +1,26 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline
public fun <T, R> apply(x: T, fn: T.()->R): R =
x.fn()
// MODULE: main(lib)
// FILE: main.kt
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
internal class A(val n: Int)
internal fun test(a: A, m: Int): Int = apply(a) { n * m }
fun box(): String {
assertEquals(6, test(A(2), 3))
return "OK"
}
@@ -0,0 +1,37 @@
// MODULE: lib
// FILE: lib.kt
package lib
var global = ""
inline fun baz(x: () -> Int) = A(1).bar(x())
class A(val y: Int) {
fun bar(x: Int) = x + y
}
// MODULE: main(lib)
// FILE: main.kt
package foo
import lib.*
fun qqq(): Int {
global += "qqq;"
return 23
}
fun box(): String {
assertEquals(24, baz {
global += "before;"
val result = qqq()
global += "after;"
result
})
assertEquals("before;qqq;after;", global)
return "OK"
}
@@ -0,0 +1,38 @@
// MODULE: lib
// FILE: lib.kt
package lib
var global = ""
inline fun baz(x: () -> Int) = ((A(1).B(x()) as Any) as A.B).bar()
class A(val y: Int) {
inner class B(val x: Int) {
fun bar() = x + y
}
}
// MODULE: main(lib)
// FILE: main.kt
package foo
import lib.*
fun qqq(): Int {
global += "qqq;"
return 23
}
fun box(): String {
assertEquals(24, baz {
global += "before;"
val result = qqq()
global += "after;"
result
})
assertEquals("before;qqq;after;", global)
return "OK"
}
@@ -0,0 +1,24 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline
public fun <T, R> apply(x: T, fn: (T)->R): R =
fn(x)
// MODULE: main(lib)
// FILE: main.kt
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
internal fun test(x: Int): Int = apply(x) { it * 2 }
fun box(): String {
assertEquals(6, test(3))
return "OK"
}
@@ -0,0 +1,27 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline
public fun <T, R> apply(x: T, crossinline fn: (T)->R): R {
val result = object {
val x = fn(x)
}
return result.x
}
// MODULE: main(lib)
// FILE: main.kt
import utils.*
internal fun test(x: Int): Int = apply(x) { it * 2 }
fun box(): String {
assertEquals(6, test(3))
return "OK"
}
@@ -0,0 +1,24 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline
public fun <T, R> apply(x: T, fn: (T)->R): R =
fn(x)
// MODULE: main(lib)
// FILE: main.kt
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
internal fun test(x: Int, y: Int): Int = apply(x) { it + y }
fun box(): String {
assertEquals(3, test(1, 2))
return "OK"
}
@@ -0,0 +1,26 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline
public fun <T, R> apply(x: T, fn: (T)->R): R {
val y = fn(x)
return y
}
// MODULE: main(lib)
// FILE: main.kt
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
internal fun test(x: Int, y: Int): Int = apply(x) { it + 1 } * y
fun box(): String {
assertEquals(6, test(1, 3))
return "OK"
}
@@ -0,0 +1,25 @@
// MODULE: lib
// FILE: lib.kt
package utils
public class A(public val x: Int) {
inline
public fun plus(y: Int): Int = x + y
}
// MODULE: main(lib)
// FILE: main.kt
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
internal fun test(a: A, y: Int): Int = a.plus(y)
fun box(): String {
assertEquals(5, test(A(2), 3))
return "OK"
}
@@ -0,0 +1,21 @@
// MODULE: lib
// FILE: lib.kt
package foo
inline fun bar(x: Int = 0) = x + 10
// MODULE: main(lib)
// FILE: main.kt
package foo
// CHECK_NOT_CALLED: bar
fun box(): String {
assertEquals(10, bar())
assertEquals(11, bar(1))
return "OK"
}
@@ -0,0 +1,25 @@
// MODULE: lib
// FILE: lib.kt
package utils
inline
public fun sum(x: Int, y: Int): Int =
x + y
// MODULE: main(lib)
// FILE: main.kt
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
internal fun test(x: Int, y: Int): Int = sum(x, y)
fun box(): String {
assertEquals(3, test(1, 2))
assertEquals(5, test(2, 3))
return "OK"
}
@@ -0,0 +1,73 @@
package foo
// CHECK_VARS_COUNT: function=test1 count=0
// CHECK_VARS_COUNT: function=test2 count=0
// CHECK_VARS_COUNT: function=test3 count=0
// CHECK_VARS_COUNT: function=test4 count=0
// CHECK_VARS_COUNT: function=test5 count=0
var global = ""
var globalNum = 1
var returnValue = 0
fun pure(n: Int) = n
fun x(n: Int) = globalNum++ * n
inline fun a(n: Int) = x(n)
fun b(n: Int) {
returnValue = n
}
fun c(first: Int, second: Int, third: Int) = first + second + third
inline fun d(first: Int, second: Int, third: Int) {
returnValue = first + second + third
}
fun test1(): Int {
globalNum = 1
return x(a(1) + a(10) + a(100))
}
fun test2() {
globalNum = 1
b(a(1) + a(10) + a(100))
}
fun test3(): Int {
globalNum = 1
return c(a(1), a(10), a(100))
}
fun test4() {
globalNum = 1
d(a(1), a(10), a(100))
}
fun test5(): Int {
globalNum = 1
return globalNum++ + a(10) + (globalNum++ * 100)
}
fun box(): String {
var result = test1()
if (result != 1284) return "fail1: $result"
test2()
result = returnValue
if (result != 321) return "fail2: $result"
result = test3()
if (result != 321) return "fail3: $result"
test4()
result = returnValue
if (result != 321) return "fail4: $result"
result = test5()
if (result != 321) return "fail5: $result"
return "OK"
}
@@ -0,0 +1,37 @@
package foo
// CHECK_NOT_CALLED: f1
// CHECK_NOT_CALLED: f2
// CHECK_BREAKS_COUNT: function=test count=3
internal var even = arrayListOf<Int>()
internal var odd = arrayListOf<Int>()
internal inline fun f2(x: Int): Unit {
if (x % 2 == 0) {
even.add(x)
return
}
odd.add(x)
return
}
internal inline fun f1(x: Boolean, y: Int, z: Int): Unit {
if (x) {
return f2(y)
}
return f2(z)
}
internal fun test(x: Boolean, y: Int, z: Int): Unit = f1(x, y, z)
fun box(): String {
test(true, 2, 1)
test(false, 2, 1)
assertEquals(listOf(2), even)
assertEquals(listOf(1), odd)
return "OK"
}
@@ -0,0 +1,28 @@
package foo
// CHECK_VARS_COUNT: function=test_za3lpa$ count=2
inline fun if1(f: (Int) -> Int, a: Int, b: Int, c: Int): Int {
val result = f(a)
if (result == b) {
return f(a)
}
return f(c)
}
fun test(x: Int): Int {
val test1 = if1({ it }, x, 2, 3)
return test1
}
fun box(): String {
var result = test(2)
if (result != 2) return "fail1: $result"
result = test(100)
if (result != 3) return "fail2: $result"
return "OK"
}
@@ -0,0 +1,39 @@
package foo
// CHECK_CONTAINS_NO_CALLS: test1
// CHECK_CONTAINS_NO_CALLS: test2
// CHECK_VARS_COUNT: function=test1 count=0
// CHECK_VARS_COUNT: function=test2 count=1
var log = ""
internal inline fun run1(fn: ()->Int): Int {
log += "1;"
return 1 + fn()
}
internal inline fun run2(fn: ()->Int): Int {
log += "2;"
return 2 + run1(fn)
}
internal inline fun run3(fn: ()->Int): Int {
log += "3;"
return 3 + run2(fn)
}
internal fun test1(x: Int): Int = run3 { x }
internal fun test2(x: Int): Int {
val result = 1 + run3 { x }
return result
}
fun box(): String {
assertEquals(7, test1(1))
assertEquals("3;2;1;", log)
assertEquals(8, test2(1))
return "OK"
}
@@ -0,0 +1,25 @@
package foo
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_VARS_COUNT: function=test count=0
object SumHolder {
var sum = 0
}
internal inline fun sum(x: Int, y: Int): Int {
if (x == 0 || y == 0) return 0
return x + y
}
internal fun test(x: Int, y: Int) {
SumHolder.sum = sum(x, y)
}
fun box(): String {
test(1, 2)
assertEquals(3, SumHolder.sum)
return "OK"
}
@@ -0,0 +1,29 @@
// CHECK_VARS_COUNT: function=box count=1
package foo
var log = ""
class A() {
var x = 23
}
fun sideEffect(): Int {
log += "sideEffect();"
return 42
}
fun bar(a: Int, b: Int) {
log += "bar($a, $b);"
}
inline fun test(a: Int, b: Int) {
bar(b, a)
}
fun box(): String {
val a = A()
test(sideEffect(), a.x)
assertEquals("sideEffect();bar(23, 42);", log)
return "OK"
}
@@ -0,0 +1,30 @@
package foo
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_VARS_COUNT: function=test count=0
internal inline fun sign(x: Int): Int {
if (x < 0) return -1
if (x == 0) return 0
return 1
}
internal fun test(x: Int, y: Int): Int {
if (x != 0) {
return sign(x)
}
return sign(y)
}
fun box(): String {
assertEquals(-1, test(-2, 2))
assertEquals(1, test(2, -2))
assertEquals(-1, test(0, -2))
assertEquals(1, test(0, 2))
assertEquals(0, test(0, 0))
return "OK"
}

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