JS: move more test to box tests
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var c: Int = 0
|
||||
|
||||
js("""
|
||||
for (var i = 0; i < 10; i++) {
|
||||
c = i;
|
||||
|
||||
if (i === 3) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
assertEquals(3, c)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package foo
|
||||
|
||||
fun test(action: ()->Unit): String = js("""
|
||||
var e = { message: "ok" };
|
||||
|
||||
try {
|
||||
action();
|
||||
} catch (e) {
|
||||
return e.message;
|
||||
}
|
||||
|
||||
return e.message;
|
||||
""")
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("ok", test {})
|
||||
assertEquals("not ok", test { throw Exception("not ok") })
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var c: Int = 0
|
||||
|
||||
val code = "c = 3"
|
||||
js(code)
|
||||
|
||||
assertEquals(3, c)
|
||||
js(("c = 5"))
|
||||
|
||||
assertEquals(5, c)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var c: Int = 0
|
||||
|
||||
js("""
|
||||
for (var i = 1; i < 6; i++) {
|
||||
if (i % 2 === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
c++;
|
||||
}
|
||||
""")
|
||||
|
||||
assertEquals(3, c)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
fun factorial(n: Int): Int = js("""
|
||||
var result = 1;
|
||||
var i = 0;
|
||||
|
||||
do {
|
||||
result *= ++i;
|
||||
} while(i < n);
|
||||
|
||||
return result;
|
||||
""")
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(24, factorial(4))
|
||||
assertEquals(120, factorial(5))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
fun factorial(n: Int): Int = js("""
|
||||
var result = 1;
|
||||
|
||||
for (var i = 1; i <= n; i++) {
|
||||
result *= i;
|
||||
}
|
||||
|
||||
return result;
|
||||
""")
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(24, factorial(4))
|
||||
assertEquals(120, factorial(5))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
fun countKeys(a: Array<Int>): Int = js("""
|
||||
var result = 0;
|
||||
|
||||
for (var key in a) {
|
||||
result += 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
""")
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(3, countKeys(arrayOf(1,2,3)))
|
||||
assertEquals(4, countKeys(arrayOf(1,2,3,4)))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun callWithArgs(sumFunc: (Int, Int) -> Int, a: Int, b: Int): Int {
|
||||
return sumFunc(a, b)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val kotlinSum: (Int, Int) -> Int = { a, b -> a + b}
|
||||
val jsSum: (Int, Int) -> Int = js("function (a, b) { return a + b; }")
|
||||
assertEquals(callWithArgs(kotlinSum, 1, 2), callWithArgs(jsSum, 1, 2))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
fun testIf(flag: Boolean): Int = js("""
|
||||
if (flag)
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
""")
|
||||
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(1, testIf(true))
|
||||
assertEquals(-1, testIf(false))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun <A, B, C> run(a: A, b: B, func: (A, B) -> C): C = js("func(a, b)")
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(3, run(1, 2) { a, b -> a + b})
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package foo
|
||||
|
||||
fun testLabelledBlock() {
|
||||
var c: Int = 0
|
||||
|
||||
js("""
|
||||
block: {
|
||||
c = 1;
|
||||
break block;
|
||||
c = 2;
|
||||
}
|
||||
""")
|
||||
|
||||
assertEquals(1, c, "testLabelledBlock")
|
||||
}
|
||||
|
||||
fun testBreakInFor() {
|
||||
var c: Int = 0
|
||||
|
||||
js("""
|
||||
outer: for (var i = 0; i < 10; i++) {
|
||||
for (var j = 0; j < 10; j++) {
|
||||
if (i === 1) {
|
||||
break outer;
|
||||
}
|
||||
|
||||
c += 1;
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
assertEquals(10, c, "testBreakInFor")
|
||||
}
|
||||
|
||||
fun testContinueInFor() {
|
||||
var c: Int = 0
|
||||
|
||||
js("""
|
||||
outer: for (var i = 0; i < 10; i++) {
|
||||
for (var j = 0; j < 10; j++) {
|
||||
if (i >= 1) {
|
||||
continue outer;
|
||||
}
|
||||
|
||||
c += 1;
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
assertEquals(10, c, "testContinueInFor")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testLabelledBlock()
|
||||
testBreakInFor()
|
||||
testContinueInFor()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package foo
|
||||
|
||||
// CHECK_LABELS_COUNT: function=box name=block count=1
|
||||
// CHECK_LABELS_COUNT: function=box name=block_0 count=1
|
||||
|
||||
fun box(): String {
|
||||
var i = 0
|
||||
var j = 0
|
||||
|
||||
js("""
|
||||
block: {
|
||||
i++;
|
||||
|
||||
block: {
|
||||
j++;
|
||||
break block;
|
||||
j++;
|
||||
}
|
||||
|
||||
break block;
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
""")
|
||||
|
||||
assertEquals(1, i, "i")
|
||||
assertEquals(1, j, "j")
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var sum = 0
|
||||
var sumInner = 0
|
||||
var sumOuter = 0
|
||||
|
||||
val range = 0..10
|
||||
val skipInner = 5
|
||||
val skipOuter = 8
|
||||
|
||||
|
||||
block@ for (i in range) {
|
||||
sum += i
|
||||
|
||||
if (i == skipOuter) break@block
|
||||
|
||||
js("""
|
||||
block: {
|
||||
if (i === skipInner) break block;
|
||||
|
||||
sumInner += i
|
||||
}
|
||||
""")
|
||||
|
||||
sumOuter += i
|
||||
}
|
||||
|
||||
assertEquals(sum - skipOuter, sumOuter, "sumOuter")
|
||||
assertEquals(sum - skipOuter - skipInner, sumInner, "sumInner")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package foo
|
||||
|
||||
// CHECK_LABELS_COUNT: function=box name=block count=2
|
||||
|
||||
fun box(): String {
|
||||
var i = 0
|
||||
var j = 0
|
||||
|
||||
js("""
|
||||
block: {
|
||||
i++;
|
||||
break block;
|
||||
i++;
|
||||
}
|
||||
|
||||
block: {
|
||||
j++;
|
||||
break block;
|
||||
j++;
|
||||
}
|
||||
""")
|
||||
|
||||
assertEquals(1, i, "i")
|
||||
assertEquals(1, j, "j")
|
||||
return "OK"
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package foo
|
||||
|
||||
@native interface HasName {
|
||||
val name: String
|
||||
}
|
||||
|
||||
fun <T> assertArrayEquals(expected: Array<T>, actual: Array<T>) {
|
||||
val expectedSize = expected.size
|
||||
val actualSize = actual.size
|
||||
|
||||
if (expectedSize != actualSize) {
|
||||
throw Exception("expected size -- $expectedSize, actual size -- $actualSize")
|
||||
}
|
||||
|
||||
for (i in 0..expectedSize) {
|
||||
val expectedIth = expected[i]
|
||||
val actualIth = actual[i]
|
||||
|
||||
if (expected[i] != actual[i]) {
|
||||
throw Exception("expected[$i] -- $expectedIth, actual[$i] -- $actualIth")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(10, js("10"), "Int")
|
||||
assertEquals(10.5, js("10.5"), "Float")
|
||||
assertEquals("10", js("'10'"), "String")
|
||||
assertEquals(true, js("true"), "True")
|
||||
assertEquals(false, js("false"), "False")
|
||||
|
||||
val obj: HasName = js("({name: 'OBJ'})")
|
||||
assertEquals("OBJ", obj.name, "Object")
|
||||
|
||||
assertArrayEquals(arrayOf(1, 2, 3), js("[1, 2, 3]"))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package foo
|
||||
|
||||
@native interface Summizer {
|
||||
fun sum(a: Int, b: Int): Int
|
||||
}
|
||||
|
||||
fun getSummizer(): Summizer = js("""
|
||||
var summizer = {
|
||||
sum: function(a, b) { return a + b;}
|
||||
};
|
||||
|
||||
return summizer;
|
||||
""");
|
||||
|
||||
fun box(): String {
|
||||
val summizer = getSummizer()
|
||||
assertEquals(3, summizer.sum(1, 2))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
@native interface Summizer {
|
||||
fun sum(a: Int, b: Int): Int
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val summizer1: Summizer = js("({ sum: function(a, b) { return a + b; }})")
|
||||
assertEquals(3, summizer1.sum(1, 2), "summizer1")
|
||||
|
||||
val summizer2: Summizer = js("({ sum: function(a, b) { return a + b; }})")
|
||||
assertEquals(3, summizer2.sum(1, 2), "summizer2")
|
||||
|
||||
return "OK";
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package foo
|
||||
|
||||
data class A(val value: Int)
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(-1, js("-1"), "- (unary)")
|
||||
assertEquals(1, js("+'1'"), "+ (unary)")
|
||||
|
||||
assertEquals(1, js("2 - 1"), "- (binary)")
|
||||
assertEquals(3, js("2 + 1"), "+ (binary)")
|
||||
assertEquals(10, js("2 * 5"), "*")
|
||||
assertEquals(5, js("10 / 2"), "/")
|
||||
assertEquals(1, js("11 % 2"), "%")
|
||||
|
||||
assertEquals(36, js("9 << 2"), "<<")
|
||||
assertEquals(2, js("9 >> 2"), ">>")
|
||||
assertEquals(1073741821, js("-9 >>> 2"), ">>>")
|
||||
assertEquals(0, js("0 & 1"), "&")
|
||||
assertEquals(1, js("0 | 1"), "|")
|
||||
assertEquals(1, js("0 ^ 1"), "^")
|
||||
assertEquals(-2, js("~1"), "~")
|
||||
|
||||
var i = 2
|
||||
assertEquals(1, js("--i"), "-- prefix")
|
||||
assertEquals(1, js("i--"), "-- postfix (1)")
|
||||
assertEquals(0, js("i"), "-- postfix (0)")
|
||||
assertEquals(1, js("++i"), "++ prefix")
|
||||
assertEquals(1, js("i++"), "++ postfix (1)")
|
||||
assertEquals(2, js("i"), "++ postfix (0)")
|
||||
|
||||
assertEquals(true , js("true || false"), "||")
|
||||
assertEquals(false , js("true && false"), "&&")
|
||||
assertEquals(false , js("!true"), "!")
|
||||
|
||||
assertEquals(true , js("1 < 2"), "<")
|
||||
assertEquals(false, js("1 > 2"), ">")
|
||||
assertEquals(true, js("1 <= 2"), "<=")
|
||||
assertEquals(false, js("1 >= 2"), ">=")
|
||||
|
||||
assertEquals(false, js("2 === '2'"), "===")
|
||||
assertEquals(true, js("2 !== '2'"), "!==")
|
||||
assertEquals(true, js("2 == '2'"), "==")
|
||||
assertEquals(false, js("2 != '2'"), "!=")
|
||||
|
||||
assertEquals("odd", js("(1 % 2 === 0)?'even':'odd'"), "?:")
|
||||
assertEquals("even", js("(4 % 2 === 0)?'even':'odd'"), "?:")
|
||||
assertEquals(3, js("1,2,3"), ", (comma)")
|
||||
|
||||
var j = 0
|
||||
assertEquals(1, js("j = 1"), "=")
|
||||
assertEquals(3, js("j += 2"), "+=")
|
||||
assertEquals(2, js("j -= 1"), "-=")
|
||||
assertEquals(14, js("j *= 7"), "*=")
|
||||
assertEquals(7, js("j /= 2"), "/=")
|
||||
assertEquals(1, js("j %= 2"), "%=")
|
||||
|
||||
assertEquals(undefined, js("(void 0)"), "void")
|
||||
assertEquals(true, js("'key' in {'key': 10}"), "in")
|
||||
assertEquals("string", js("typeof 'str'"), "typeof")
|
||||
assertEquals(A(2), js("new _.foo.A(2)"), "new")
|
||||
assertEquals(true, js("new String('str') instanceof String"), "instanceof")
|
||||
|
||||
var s: Any = js("({key: 10})")
|
||||
assertEquals(undefined, js("delete s.key, s.key"), "delete")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
fun singleQuoted(i: Int): Int = js("return i")
|
||||
fun tripleQuoted(i: Int): Int = js("""return i""")
|
||||
fun tripleQuotedInnerQuotes(i: Int): String = js("""return "i"""")
|
||||
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(0, singleQuoted(0))
|
||||
assertEquals(0, tripleQuoted(0))
|
||||
assertEquals("i", tripleQuotedInnerQuotes(0))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package foo
|
||||
|
||||
fun testSwitch(number: Int): String = js("""
|
||||
var result;
|
||||
|
||||
switch(number) {
|
||||
case 1:
|
||||
result = "one";
|
||||
break;
|
||||
case 2:
|
||||
result = "two";
|
||||
break;
|
||||
default:
|
||||
result = "don't know";
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
""")
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("one", testSwitch(1))
|
||||
assertEquals("two", testSwitch(2))
|
||||
assertEquals("don't know", testSwitch(3))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package foo
|
||||
|
||||
class Counter {
|
||||
var count: Int = 0
|
||||
|
||||
public fun inc() {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
fun test(c: Counter, ex: Exception): Unit = js("""
|
||||
try {
|
||||
throw ex;
|
||||
} catch (e) {
|
||||
c.inc()
|
||||
} finally {
|
||||
c.inc()
|
||||
}
|
||||
""")
|
||||
|
||||
fun box(): String {
|
||||
val c = Counter()
|
||||
test(c, NullPointerException())
|
||||
assertEquals(2, c.count)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
fun factorial(n: Int): Int = js("""
|
||||
var result = 1;
|
||||
var i = 1;
|
||||
|
||||
while(i <= n) {
|
||||
result *= i++;
|
||||
}
|
||||
|
||||
return result;
|
||||
""")
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(24, factorial(4))
|
||||
assertEquals(120, factorial(5))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// CHECK_LABELS_COUNT: function=test0 count=0
|
||||
// CHECK_LABELS_COUNT: function=test1 count=0
|
||||
// CHECK_LABELS_COUNT: function=test2 count=0
|
||||
// CHECK_LABELS_COUNT: function=test3 count=0
|
||||
|
||||
package foo
|
||||
|
||||
fun <R> myRun(f: () -> R) = f()
|
||||
|
||||
fun test0() {
|
||||
val a = aa@ 1
|
||||
|
||||
assertEquals(1, a)
|
||||
assertEquals(3, l1@ a + l2@ 2)
|
||||
|
||||
val b = bb@ if (true) t@ "then block" else e@ "else block"
|
||||
|
||||
assertEquals("then block", b)
|
||||
}
|
||||
|
||||
fun test1() {
|
||||
run label@ {
|
||||
return@label false
|
||||
}
|
||||
}
|
||||
|
||||
fun test2() {
|
||||
myRun label@ {
|
||||
return@label false
|
||||
}
|
||||
}
|
||||
|
||||
// KT-7487
|
||||
public fun test3() {
|
||||
val f = Foo()
|
||||
f.iter label@ {
|
||||
return@label false
|
||||
}
|
||||
}
|
||||
|
||||
class Foo {
|
||||
inline fun iter(body: ()->Boolean) {
|
||||
for (i in 0 .. 10) {
|
||||
if (!body()) break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
test0()
|
||||
test1()
|
||||
test2()
|
||||
test3()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package foo
|
||||
|
||||
// CHECK_LABELS_COUNT: function=test1 name=loop count=1
|
||||
// CHECK_LABELS_COUNT: function=test2 name=loop count=1
|
||||
|
||||
fun test1() {
|
||||
var `loop$` = 0
|
||||
|
||||
loop@ for (i in 1..10) {
|
||||
`loop$` = i
|
||||
if (i == 5) break@loop
|
||||
}
|
||||
|
||||
assertEquals(5, `loop$`, "test1")
|
||||
}
|
||||
|
||||
fun test2() {
|
||||
var loop = 0
|
||||
|
||||
loop@ for (i in 1..10) {
|
||||
loop = i
|
||||
if (i == 5) break@loop
|
||||
}
|
||||
|
||||
assertEquals(5, loop, "test2")
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
test1()
|
||||
test2()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package foo
|
||||
|
||||
var state = false
|
||||
|
||||
inline fun blockImpl(p: () -> Unit) {
|
||||
if (state) return
|
||||
|
||||
p()
|
||||
}
|
||||
|
||||
inline fun block(p: () -> Unit) {
|
||||
if (state) return
|
||||
|
||||
blockImpl(p)
|
||||
}
|
||||
|
||||
fun test(x: Int): Int {
|
||||
block outer@ {
|
||||
block inner@ {
|
||||
if (x < 10) return@inner
|
||||
if (x == 10) return@outer
|
||||
|
||||
block innermost@ {
|
||||
if (x > 500) {
|
||||
return 500500
|
||||
}
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
if (x < 5) return@outer
|
||||
return x + 10
|
||||
}
|
||||
return x + 100
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(16, test(6))
|
||||
assertEquals(104, test(4))
|
||||
assertEquals(110, test(110))
|
||||
assertEquals(11, test(11))
|
||||
assertEquals(500500, test(502))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
// CHECK_LABELS_COUNT: function=test name=loop count=1
|
||||
// CHECK_LABELS_COUNT: function=test name=loop_0 count=1
|
||||
|
||||
fun test() {
|
||||
var i = 0
|
||||
var j = 0
|
||||
|
||||
loop@ for (k in 1..10) {
|
||||
loop@ for (m in 1..10) {
|
||||
if (m == 4) break@loop
|
||||
j = m
|
||||
}
|
||||
|
||||
if (k == 8) break@loop
|
||||
i = k
|
||||
}
|
||||
|
||||
assertEquals(3, j)
|
||||
assertEquals(7, i)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
test()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package foo
|
||||
|
||||
// CHECK_CONTAINS_NO_CALLS: test
|
||||
// CHECK_LABELS_COUNT: function=test name=loop count=1
|
||||
// CHECK_LABELS_COUNT: function=test name=loop_0 count=1
|
||||
// CHECK_LABELS_COUNT: function=test name=loop_1 count=1
|
||||
|
||||
class State() {
|
||||
public var value: Int = 0
|
||||
}
|
||||
|
||||
internal inline fun test1(state: State) {
|
||||
loop@ for (i in 1..10) {
|
||||
state.value++
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
}
|
||||
|
||||
internal inline fun test2(state: State) {
|
||||
loop@ for (i in 1..10) {
|
||||
test1(state)
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
}
|
||||
|
||||
internal inline fun test3(state: State) {
|
||||
loop@ for (i in 1..10) {
|
||||
test2(state)
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
}
|
||||
|
||||
internal fun test(state: State) {
|
||||
test3(state)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val state = State()
|
||||
test(state)
|
||||
assertEquals(8, state.value)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package foo
|
||||
|
||||
// CHECK_LABELS_COUNT: function=test name=loop count=1
|
||||
// CHECK_LABELS_COUNT: function=test name=loop_0 count=1
|
||||
// CHECK_LABELS_COUNT: function=test name=loop_1 count=1
|
||||
|
||||
class State() {
|
||||
public var value: Int = 0
|
||||
}
|
||||
|
||||
internal fun test(state: State) {
|
||||
inline fun test3() {
|
||||
inline fun test2() {
|
||||
inline fun test1() {
|
||||
loop@ for (i in 1..10) {
|
||||
state.value++
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
}
|
||||
|
||||
loop@ for (i in 1..10) {
|
||||
test1()
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
}
|
||||
|
||||
loop@ for (i in 1..10) {
|
||||
test2()
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
}
|
||||
|
||||
test3()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val state = State()
|
||||
test(state)
|
||||
assertEquals(8, state.value)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package foo
|
||||
|
||||
// CHECK_LABELS_COUNT: function=test name=loop count=2
|
||||
|
||||
fun test() {
|
||||
var i = 0
|
||||
var j = 0
|
||||
|
||||
loop@ for (m in 1..10) {
|
||||
if (m == 4) break@loop
|
||||
j = m
|
||||
}
|
||||
|
||||
loop@ for (k in 1..10) {
|
||||
if (k == 4) break@loop
|
||||
i = k
|
||||
}
|
||||
|
||||
assertEquals(3, j)
|
||||
assertEquals(3, i)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
test()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: testInline
|
||||
// CHECK_LABELS_COUNT: function=testNoinline name=loop count=2
|
||||
|
||||
inline fun testInline(): Int {
|
||||
var c = 0
|
||||
|
||||
loop@ for (i in 1..9) {
|
||||
c++
|
||||
if (c == 2) break@loop
|
||||
}
|
||||
|
||||
|
||||
loop@ for (j in 1..9) {
|
||||
c++
|
||||
if (c == 4) break@loop
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
fun testNoinline(): Int {
|
||||
return testInline()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(4, testNoinline())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: testLabelInline
|
||||
// CHECK_LABELS_COUNT: function=testLabel name=loop count=1
|
||||
// CHECK_LABELS_COUNT: function=testLabel name=loop_0 count=2
|
||||
|
||||
inline fun testLabelInline(): Int {
|
||||
var a = 0
|
||||
|
||||
loop@ for (i in 1..10) {
|
||||
if (i == 1) continue@loop
|
||||
|
||||
a += i
|
||||
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
|
||||
loop@ for (i in 1..10) {
|
||||
if (i == 1) continue@loop
|
||||
|
||||
a += i
|
||||
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
fun testLabel(): String {
|
||||
var a = 0
|
||||
|
||||
loop@ for (i in 1..10) {
|
||||
if (i == 1) continue@loop
|
||||
|
||||
a += testLabelInline()
|
||||
|
||||
if (i == 2) break@loop
|
||||
}
|
||||
|
||||
if (a != 4) return a.toString()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("OK", testLabel())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package foo
|
||||
|
||||
// CHECK_LABELS_COUNT: function=testBreak name=loop count=1
|
||||
// CHECK_LABELS_COUNT: function=testContinue name=loop count=1
|
||||
|
||||
fun testBreak() {
|
||||
var i = 0
|
||||
|
||||
loop@ for (j in 1..10) {
|
||||
if (j == 5) break@loop
|
||||
|
||||
i = j
|
||||
}
|
||||
|
||||
assertEquals(4, i, "break")
|
||||
}
|
||||
|
||||
fun testContinue() {
|
||||
var sum = 0
|
||||
|
||||
loop@ for (j in 1..5) {
|
||||
if (j % 2 != 0) continue@loop
|
||||
|
||||
sum += j
|
||||
}
|
||||
|
||||
assertEquals(6, sum, "continue")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testBreak()
|
||||
testContinue()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package foo
|
||||
|
||||
// CHECK_NOT_CALLED: testBreak
|
||||
// CHECK_NOT_CALLED: testContinue
|
||||
// CHECK_LABELS_COUNT: function=testBreakNoinline name=loop count=1
|
||||
// CHECK_LABELS_COUNT: function=testContinueNoinline name=loop count=1
|
||||
|
||||
inline fun testBreak(): Int {
|
||||
var i = 0
|
||||
|
||||
loop@ for (j in 1..10) {
|
||||
if (j == 5) break@loop
|
||||
|
||||
i = j
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
fun testBreakNoinline() {
|
||||
assertEquals(4, testBreak(), "break")
|
||||
}
|
||||
|
||||
inline fun testContinue(): Int {
|
||||
var sum = 0
|
||||
|
||||
loop@ for (j in 1..5) {
|
||||
if (j % 2 != 0) continue@loop
|
||||
|
||||
sum += j
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
fun testContinueNoinline() {
|
||||
assertEquals(6, testContinue(), "continue")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testContinue()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
operator fun div(other: A) = "OK"
|
||||
|
||||
}
|
||||
|
||||
fun box() = A() / A()
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
|
||||
class A(t: Int) {
|
||||
var i = t
|
||||
operator fun compareTo(other: A) = (this.i - other.i)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (A(3) <= A(2)) return "fail1"
|
||||
if (A(2) < A(2)) return "fail2"
|
||||
if (A(1) < A(0)) return "fail3"
|
||||
if (A(2) > A(2)) return "fail4"
|
||||
if (A(3) > A(4)) return "fail5"
|
||||
if (A(0) >= A(100)) return "fail6"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
|
||||
class A(t: Int) {
|
||||
var i = t
|
||||
infix operator fun compareTo(other: A) = (this.i - other.i)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (A(3) compareTo A(2) <= 0) return "fail1"
|
||||
if (A(2) compareTo A(2) != 0) return "fail2"
|
||||
if (A(1) compareTo A(0) <= 0) return "fail3"
|
||||
if (A(3) compareTo A(4) >= 0) return "fail4"
|
||||
if (A(0) compareTo A(100) >= 0) return "fail5"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package foo
|
||||
|
||||
var global = ""
|
||||
|
||||
fun log(message: String) {
|
||||
global += message
|
||||
}
|
||||
|
||||
fun test1(): String {
|
||||
val list = mutableListOf<(Int) -> Unit>()
|
||||
|
||||
var result = ""
|
||||
list += { log("<$it>") }
|
||||
list += { result += "($it)" }
|
||||
list += { result += "[$it]"; result += "{$it}" }
|
||||
|
||||
for(f in list) f(1)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("(1)[1]{1}", test1())
|
||||
assertEquals(global, "<1>")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
operator fun not() = "OK"
|
||||
|
||||
}
|
||||
|
||||
fun box() = !A()
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package foo
|
||||
|
||||
class MyInt(i: Int) {
|
||||
var b = i
|
||||
operator fun inc(): MyInt {
|
||||
b = b + 1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
class A() {
|
||||
|
||||
var gc = 0
|
||||
var sc = 0
|
||||
|
||||
|
||||
var b = MyInt(0)
|
||||
get() {
|
||||
gc = gc + 1;
|
||||
return field;
|
||||
}
|
||||
set(a: MyInt) {
|
||||
sc = sc + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val t = A()
|
||||
val d = t.b++
|
||||
if (t.sc != 1) return "fail1"
|
||||
if (t.gc != 1) return "fail2"
|
||||
return "OK"
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
import java.util.*
|
||||
|
||||
class ArrayWrapper<T>() {
|
||||
val contents = ArrayList<T>()
|
||||
|
||||
fun add(item: T) {
|
||||
contents.add(item)
|
||||
}
|
||||
|
||||
operator fun plusAssign(rhs: ArrayWrapper<T>) {
|
||||
contents.addAll(rhs.contents)
|
||||
}
|
||||
|
||||
fun get(index: Int): T {
|
||||
return contents.get(index)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var v1 = ArrayWrapper<String>()
|
||||
val v2 = ArrayWrapper<String>()
|
||||
v1.add("foo")
|
||||
v2.add("bar")
|
||||
v1 += v2
|
||||
return if (v1.contents.size == 2) "OK" else "fail"
|
||||
}
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
package foo
|
||||
|
||||
import java.util.*
|
||||
|
||||
class A<T>(val list: MutableList<T>) {
|
||||
fun addAll(c: Collection<T>) {
|
||||
list.addAll(c)
|
||||
}
|
||||
}
|
||||
|
||||
operator fun <T> A<T>.plusAssign(other: Collection<T>) {
|
||||
addAll(other)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var v1 = arrayListOf("foo")
|
||||
val v2 = listOf("bar")
|
||||
|
||||
val a = A(v1)
|
||||
a += v2
|
||||
|
||||
if (v1.size != 2) return "fail1: ${v1.size}"
|
||||
if (v1[0] != "foo") return "fail2: ${v1[0]}"
|
||||
if (v1[1] != "bar") return "fail3: ${v1[1]}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
open class Foo<out T>(open val value: T)
|
||||
open class MutableFoo<T>(override var value: T): Foo<T>(value)
|
||||
|
||||
operator fun <T> Foo<T>.plus(x: T): Foo<T> = Foo(x)
|
||||
|
||||
// overloading:
|
||||
operator fun <T> MutableFoo<T>.plus(x: T): MutableFoo<T> = MutableFoo(x)
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var f = MutableFoo(1)
|
||||
f += 2
|
||||
return if (f is MutableFoo && f.value == 2) "OK" else "fail"
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package foo
|
||||
|
||||
import java.util.*
|
||||
|
||||
operator fun <T> ArrayList<T>.plus(other: Collection<T>): List<T> {
|
||||
val c = ArrayList<T>()
|
||||
c.addAll(this)
|
||||
c.addAll(other)
|
||||
return c
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var v1 = ArrayList<String>()
|
||||
v1.add("foo")
|
||||
val v2 = ArrayList<String>()
|
||||
v2.add("bar")
|
||||
val v = v1 + v2
|
||||
|
||||
if (v.size != 2) return "fail1: ${v.size}"
|
||||
if (v[0] != "foo") return "fail2: ${v[0]}"
|
||||
if (v[1] != "bar") return "fail3: ${v[1]}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
class A(val c: Int) {
|
||||
}
|
||||
|
||||
|
||||
operator fun A.inc() = A(5)
|
||||
operator fun A.dec() = A(10)
|
||||
|
||||
fun box(): String {
|
||||
var a = A(1)
|
||||
|
||||
if ((++a).c != 5) return "fail1"
|
||||
if ((a++).c != 5) return "fail2"
|
||||
if ((--a).c != 10) return "fail3"
|
||||
if ((a--).c != 10) return "fail4"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
var a = MyInt()
|
||||
|
||||
class MyInt() {
|
||||
var b = 0
|
||||
|
||||
operator fun inc(): MyInt {
|
||||
val res = MyInt();
|
||||
res.b = b;
|
||||
res.b++;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
a++;
|
||||
a++;
|
||||
return if (a.b == 2) "OK" else "fail"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
operator fun unaryPlus() = "O"
|
||||
operator fun unaryMinus() = "K"
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var c = A()
|
||||
return +c + -c
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
|
||||
var message = ""
|
||||
operator fun plusAssign(other: A) {
|
||||
message = message + "!"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var c = A()
|
||||
c += A()
|
||||
c += A()
|
||||
return if (c.message == "!!") return "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
class myInt(a: Int) {
|
||||
val value = a;
|
||||
|
||||
operator fun plus(other: myInt): myInt = myInt(value + other.value)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
return if ((myInt(3) + myInt(5)).value == 8) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
class MyInt() {
|
||||
var b = 0
|
||||
|
||||
operator fun inc(): MyInt {
|
||||
val res = MyInt()
|
||||
res.b++;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var c = MyInt()
|
||||
c++;
|
||||
return if (c.b == 1) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
var a = MyInt()
|
||||
|
||||
class MyInt() {
|
||||
var b = 0
|
||||
|
||||
operator fun inc(): MyInt {
|
||||
b = b + 1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val d = a++;
|
||||
|
||||
if (a.b != 1) return "fail1: ${a.b}"
|
||||
if (d.b != 1) return "fail2: ${d.b}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package foo
|
||||
|
||||
class MyInt() {
|
||||
var b = 0
|
||||
|
||||
operator fun dec(): MyInt {
|
||||
val res = MyInt()
|
||||
res.b++;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var c = MyInt()
|
||||
--c;
|
||||
return if (c.b == 1) "OK" else "fail: ${c.b}"
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package foo
|
||||
|
||||
class MyInt() {
|
||||
var b = 0
|
||||
|
||||
operator fun dec(): MyInt {
|
||||
b = b + 1;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
var c = MyInt()
|
||||
val d = --c;
|
||||
return if (c.b == 1) "OK" else "fail: ${c.b}"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
class MyInt(i: Int) {
|
||||
var b = i
|
||||
operator fun inc(): MyInt {
|
||||
b = b++;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var t = MyInt(0)
|
||||
t++;
|
||||
return if (t.b == 0) "OK" else "fail: ${t.b}"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
class MyInt(i: Int) {
|
||||
var b = i
|
||||
operator fun inc(): MyInt {
|
||||
b++;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var t = MyInt(0)
|
||||
t++;
|
||||
return if (t.b == 1) "OK" else "fail: ${t.b}"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
class A() {
|
||||
var p = "yeah"
|
||||
operator fun mod(other: A): A {
|
||||
return A();
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var c = A()
|
||||
val d = c;
|
||||
c %= A();
|
||||
return if ((c != d) && (c.p == "yeah")) "OK" else "fail"
|
||||
}
|
||||
Reference in New Issue
Block a user