JS: move expressions test to box tests
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import java.util.*
|
||||
|
||||
var log = ""
|
||||
|
||||
private fun printLog(message: String) {
|
||||
log += message
|
||||
}
|
||||
|
||||
private fun printlnLog(message: String) = printLog("$message\n")
|
||||
|
||||
class Lifetime() {
|
||||
val attached = ArrayList<Function0<Unit>>()
|
||||
|
||||
public fun attach(action: () -> Unit) {
|
||||
attached.add(action)
|
||||
}
|
||||
|
||||
fun close() {
|
||||
for (x in attached) x()
|
||||
attached.clear()
|
||||
}
|
||||
}
|
||||
|
||||
public class Viewable<T>() {
|
||||
val items = ArrayList<T>()
|
||||
|
||||
fun add(item: T) {
|
||||
items.add(item)
|
||||
}
|
||||
|
||||
fun remove(item: T) {
|
||||
items.remove(item)
|
||||
}
|
||||
|
||||
fun view(lifetime: Lifetime, viewer: (itemLifetime: Lifetime, item: T) -> Unit) {
|
||||
for (item in items) {
|
||||
viewer(lifetime, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun lifetime(body: (Lifetime) -> Unit) {
|
||||
val l = Lifetime()
|
||||
body(l)
|
||||
l.close()
|
||||
}
|
||||
|
||||
fun<T> Dump(items: ArrayList<T>) {
|
||||
for (item in items) {
|
||||
printLog(item.toString() + ", ")
|
||||
}
|
||||
printlnLog("end")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val v = Viewable<Int>()
|
||||
val x = ArrayList<Int>()
|
||||
v.add(1)
|
||||
v.add(2)
|
||||
v.add(3)
|
||||
lifetime() {
|
||||
v.view(it) { itemLifetime, item ->
|
||||
x.add(item)
|
||||
Dump(x)
|
||||
itemLifetime.attach() {
|
||||
x.remove(item as Any);
|
||||
Dump(x);
|
||||
printlnLog("!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val expected =
|
||||
"1, end\n" +
|
||||
"1, 2, end\n" +
|
||||
"1, 2, 3, end\n" +
|
||||
"2, 3, end\n" +
|
||||
"!\n" +
|
||||
"3, end\n" +
|
||||
"!\n" +
|
||||
"end\n" +
|
||||
"!\n"
|
||||
|
||||
if (log != expected) return "fail: $log"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
@native
|
||||
fun String.replace(regexp: RegExp, replacement: String): String = noImpl
|
||||
|
||||
fun String.replaceAll(regexp: String, replacement: String): String = replace(RegExp(regexp, "g"), replacement)
|
||||
|
||||
@native
|
||||
fun String.search(regexp: RegExp): Int = noImpl
|
||||
|
||||
@native
|
||||
class RegExp(regexp: String, flags: String = "") {
|
||||
fun exec(s: String): Array<String>? = noImpl
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var sum = 0
|
||||
val adder = { a: Int -> sum += a }
|
||||
adder(3)
|
||||
adder(2)
|
||||
|
||||
if (sum != 5) return "fail: $sum"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
class A()
|
||||
|
||||
private val doInit = {
|
||||
A()
|
||||
}()
|
||||
|
||||
fun box(): String = if (doInit is A) "OK" else "fail"
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
return apply("OK", { arg: String -> arg })
|
||||
}
|
||||
|
||||
fun apply(arg: String, f: (p: String) -> String): String {
|
||||
return f(arg)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
return if (apply(5, { arg: Int -> arg + 13 }) == 18) "OK" else "fail"
|
||||
}
|
||||
|
||||
fun apply(arg: Int, f: (p: Int) -> Int): Int {
|
||||
return f(arg)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package foo
|
||||
|
||||
fun f(a: Int = 2, b: Int = 3) = a + b
|
||||
|
||||
fun box(): String {
|
||||
if (f(1, 2) != 3) return "fail1"
|
||||
if (f(1, 3) != 4) return "fail2"
|
||||
if (f(3) != 6) return "fail3"
|
||||
if (f() != 5) return "fail4"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
class Point(val x: Int, val y: Int) {
|
||||
fun mul(): (scalar: Int) -> Point {
|
||||
return { scalar: Int -> Point(x * scalar, y * scalar) }
|
||||
}
|
||||
}
|
||||
|
||||
val m = Point(2, 3).mul()
|
||||
|
||||
fun box(): String {
|
||||
val answer = m(5)
|
||||
return if (answer.x == 10 && answer.y == 15) "OK" else "FAIL"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val a = 23.(fun Int.(a: Int): Int = a * a + this)(3)
|
||||
if (a != 32) return "a != 32, a = $a";
|
||||
|
||||
return "OK";
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package foo
|
||||
|
||||
import java.util.*;
|
||||
|
||||
val d = { a: Int -> a + 1 }
|
||||
val p = { a: Int -> a * 3 }
|
||||
|
||||
val list = ArrayList<Function1<Int, Int>>();
|
||||
|
||||
fun chain(start: Int): Int {
|
||||
var res = start;
|
||||
for (func in list) {
|
||||
res = (func)(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (chain(0) != 0) {
|
||||
return "fail1"
|
||||
}
|
||||
list.add(d);
|
||||
if (list.get(0)(0) != 1) {
|
||||
return "fail2"
|
||||
}
|
||||
list.add(p);
|
||||
if (list.get(1)(10) != 30) {
|
||||
return "fail3"
|
||||
}
|
||||
if (chain(0) != 3) {
|
||||
return "fail4"
|
||||
}
|
||||
list.add({ it * it });
|
||||
list.add({ it - 100 });
|
||||
if (chain(2) != -19) {
|
||||
return "fail5"
|
||||
}
|
||||
if (({ a: Int -> a * a }(3)) != 9) {
|
||||
return "fail7"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package foo
|
||||
|
||||
fun Any.foo1(): () -> String {
|
||||
return { "239" + this }
|
||||
}
|
||||
|
||||
fun Int.foo2(): (i: Int) -> Int {
|
||||
return { x -> x + this }
|
||||
}
|
||||
|
||||
fun <T> fooT1(t: T) = { t.toString() }
|
||||
|
||||
fun <T> fooT2(t: T) = { x: T -> t.toString() + x.toString() }
|
||||
|
||||
fun box(): Any? {
|
||||
if ( (10.foo1())() != "23910") return "foo1 fail"
|
||||
if ( (10.foo2())(1) != 11 ) return "foo2 fail"
|
||||
|
||||
if (1.(fun Int.(): Int = this + 1)() != 2) return "test 3 failed";
|
||||
if ( { 1 }() != 1) return "test 4 failed";
|
||||
if ( { x: Int -> x }(1) != 1) return "test 5 failed";
|
||||
if ( 1.(fun Int.(x: Int): Int = x + this)(1) != 2) return "test 6 failed";
|
||||
val tmp = 1.(fun Int.(): Int = this)()
|
||||
if (+tmp != 1) return "test 7 failed, res: $tmp ${tmp is Int}";
|
||||
if ( (fooT1<String>("mama"))() != "mama") return "test 8 failed";
|
||||
if ( (fooT2<String>("mama"))("papa") != "mamapapa") return "test 9 failed";
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
fun f() = 3
|
||||
|
||||
if ((f() + f()) != 6) return "fail1"
|
||||
if (b() != 24) return "fail2"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
fun b(): Int {
|
||||
|
||||
fun a(): Int {
|
||||
fun c() = 4
|
||||
return c() * 3
|
||||
}
|
||||
val a = 2
|
||||
return a() * a
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var sum = 0
|
||||
val addFive = { a: Int -> a + 5 }
|
||||
sum = addFive(sum)
|
||||
if (sum != 5) return "fail: $sum"
|
||||
return "OK"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
fun f(a: (Int) -> Int) = a(1)
|
||||
|
||||
fun box(): String {
|
||||
|
||||
if (f() {
|
||||
it + 2
|
||||
} != 3) return "fail1"
|
||||
|
||||
if (f() { a: Int -> a * 300 } != 300) return "fail2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package foo
|
||||
|
||||
fun apply(f: (Int) -> Int, t: Int): Int {
|
||||
return f(t)
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return if (apply({ a: Int -> a + 5 }, 3) == 8) return "OK" else "fail"
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
|
||||
fun box(): String {
|
||||
return f()
|
||||
}
|
||||
|
||||
fun f(): String {
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun sum(param1: Int, param2: Int): Int {
|
||||
return param1 + param2;
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if (sum(1, 5) == 6) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
fun test(f: (Int) -> Boolean, p: Int) = f(p)
|
||||
|
||||
fun box(): String {
|
||||
if (!test({ it + 1 == 2 }, 1)) return "fail1"
|
||||
|
||||
if (!test({ it > 1 }, 3)) return "fail2"
|
||||
|
||||
return if (test({ ((it < 1) == false) }, 1)) "OK" else "fail3"
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
enum class Foo {
|
||||
BAR;
|
||||
|
||||
fun test(): () -> String {
|
||||
fun local() = 29
|
||||
val lambda = { "OK" + local() }
|
||||
|
||||
assertEquals(29, local())
|
||||
assertEquals("OK29", lambda())
|
||||
|
||||
return lambda
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("OK29", Foo.BAR.test()())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
fun String.test(i: Int) = this + i + "OK"
|
||||
val a = "foo".test(32)
|
||||
if (a != "foo32OK") return "$a"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package foo
|
||||
|
||||
var b = 0
|
||||
|
||||
fun loop(times: Int) {
|
||||
var left = times
|
||||
while (left > 0) {
|
||||
val u = { value: Int ->
|
||||
b = b + 1
|
||||
}
|
||||
u(left--)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
loop(5)
|
||||
if (b != 5) return "fail: $b"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
package foo
|
||||
|
||||
public fun public_baz(i: Int) {
|
||||
}
|
||||
@native public fun public_baz(a: String) {
|
||||
}
|
||||
|
||||
internal fun internal_baz(i: Int) {
|
||||
}
|
||||
internal @native fun internal_baz(a: String) {
|
||||
}
|
||||
|
||||
private fun private_baz(i: Int) {
|
||||
}
|
||||
@native private fun private_baz(a: String) {
|
||||
}
|
||||
|
||||
public class PublicClass {
|
||||
public fun public_baz(i: Int) {
|
||||
}
|
||||
@native public fun public_baz(a: String) {
|
||||
}
|
||||
|
||||
internal fun internal_baz(i: Int) {
|
||||
}
|
||||
@native internal fun internal_baz(a: String) {
|
||||
}
|
||||
|
||||
private fun private_baz(i: Int) {
|
||||
}
|
||||
@native private fun private_baz(a: String) {
|
||||
}
|
||||
|
||||
val call_private_baz = { private_baz(0) }
|
||||
val call_private_native_baz = { private_baz("native") }
|
||||
}
|
||||
|
||||
internal class InternalClass {
|
||||
public fun public_baz(i: Int) {
|
||||
}
|
||||
@native public fun public_baz(a: String) {
|
||||
}
|
||||
|
||||
internal fun internal_baz(i: Int) {
|
||||
}
|
||||
@native internal fun internal_baz(a: String) {
|
||||
}
|
||||
|
||||
private fun private_baz(i: Int) {
|
||||
}
|
||||
@native private fun private_baz(a: String) {
|
||||
}
|
||||
|
||||
val call_private_baz = { private_baz(0) }
|
||||
val call_private_native_baz = { private_baz("native") }
|
||||
}
|
||||
|
||||
private class PrivateClass {
|
||||
public fun public_baz(i: Int) {
|
||||
}
|
||||
@native public fun public_baz(a: String) {
|
||||
}
|
||||
|
||||
internal fun internal_baz(i: Int) {
|
||||
}
|
||||
@native internal fun internal_baz(a: String) {
|
||||
}
|
||||
|
||||
private fun private_baz(i: Int) {
|
||||
}
|
||||
@native private fun private_baz(a: String) {
|
||||
}
|
||||
|
||||
val call_private_baz = { private_baz(0) }
|
||||
val call_private_native_baz = { private_baz("native") }
|
||||
}
|
||||
|
||||
open public class OpenPublicClass {
|
||||
public fun public_baz(i: Int) {
|
||||
}
|
||||
@native public fun public_baz(a: String) {
|
||||
}
|
||||
|
||||
internal fun internal_baz(i: Int) {
|
||||
}
|
||||
@native internal fun internal_baz(a: String) {
|
||||
}
|
||||
|
||||
private fun private_baz(i: Int) {
|
||||
}
|
||||
@native private fun private_baz(a: String) {
|
||||
}
|
||||
|
||||
val call_private_baz = { private_baz(0) }
|
||||
val call_private_native_baz = { private_baz("native") }
|
||||
}
|
||||
|
||||
internal open class OpenInternalClass {
|
||||
public fun public_baz(i: Int) {
|
||||
}
|
||||
@native public fun public_baz(a: String) {
|
||||
}
|
||||
|
||||
internal fun internal_baz(i: Int) {
|
||||
}
|
||||
@native internal fun internal_baz(a: String) {
|
||||
}
|
||||
|
||||
private fun private_baz(i: Int) {
|
||||
}
|
||||
@native private fun private_baz(a: String) {
|
||||
}
|
||||
|
||||
val call_private_baz = { private_baz(0) }
|
||||
val call_private_native_baz = { private_baz("native") }
|
||||
}
|
||||
|
||||
open private class OpenPrivateClass {
|
||||
public fun public_baz(i: Int) {
|
||||
}
|
||||
@native public fun public_baz(a: String) {
|
||||
}
|
||||
|
||||
internal fun internal_baz(i: Int) {
|
||||
}
|
||||
@native internal fun internal_baz(a: String) {
|
||||
}
|
||||
|
||||
private fun private_baz(i: Int) {
|
||||
}
|
||||
@native private fun private_baz(a: String) {
|
||||
}
|
||||
|
||||
val call_private_baz = { private_baz(0) }
|
||||
val call_private_native_baz = { private_baz("native") }
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
val CALEE_NAME = RegExp("""\b\w*(baz[^(]*)""")
|
||||
|
||||
fun Function0<Unit>.extractNames(): Array<String> {
|
||||
val names = CALEE_NAME.exec(this.toString())
|
||||
|
||||
if (names == null || names.size != 2) {
|
||||
throw Exception("Cannot extract function name, $names for actual = \"$this\"")
|
||||
}
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
// Testing
|
||||
|
||||
var testGroup = ""
|
||||
|
||||
fun test(expected: String, f: () -> Unit) {
|
||||
val actual = f.extractNames()
|
||||
|
||||
if (expected != actual[1]) {
|
||||
throw Exception("Failed on '$testGroup' group: expected = \"$expected\", actual[1] = \"${actual[1]}\"\n actual = $actual")
|
||||
}
|
||||
}
|
||||
|
||||
public fun stable_mangled_baz(i: Int) {
|
||||
}
|
||||
|
||||
val SIMPLE = "baz"
|
||||
val SIMPLE1 = "${SIMPLE}_1"
|
||||
val NATIVE = SIMPLE
|
||||
val STABLE = { stable_mangled_baz(0) }.extractNames()[1]
|
||||
|
||||
fun box(): String {
|
||||
testGroup = "Top Level"
|
||||
test(STABLE) { public_baz(0) }
|
||||
test(NATIVE) { public_baz("native") }
|
||||
test(SIMPLE1) { internal_baz(0) }
|
||||
test(NATIVE) { internal_baz("native") }
|
||||
test(SIMPLE1) { private_baz(0) }
|
||||
test(NATIVE) { private_baz("native") }
|
||||
|
||||
testGroup = "Public Class"
|
||||
test(STABLE) { PublicClass().public_baz(0) }
|
||||
test(NATIVE) { PublicClass().public_baz("native") }
|
||||
test(SIMPLE1) { PublicClass().internal_baz(0) }
|
||||
test(NATIVE) { PublicClass().internal_baz("native") }
|
||||
test(SIMPLE1, PublicClass().call_private_baz)
|
||||
test(NATIVE, PublicClass().call_private_native_baz)
|
||||
|
||||
testGroup = "Internal Class"
|
||||
test(SIMPLE1) { InternalClass().public_baz(0) }
|
||||
test(NATIVE) { InternalClass().public_baz("native") }
|
||||
test(SIMPLE1) { InternalClass().internal_baz(0) }
|
||||
test(NATIVE) { InternalClass().internal_baz("native") }
|
||||
test(SIMPLE1, InternalClass().call_private_baz)
|
||||
test(NATIVE, InternalClass().call_private_native_baz)
|
||||
|
||||
testGroup = "Private Class"
|
||||
test(SIMPLE1) { PrivateClass().public_baz(0) }
|
||||
test(NATIVE) { PrivateClass().public_baz("native") }
|
||||
test(SIMPLE1) { PrivateClass().internal_baz(0) }
|
||||
test(NATIVE) { PrivateClass().internal_baz("native") }
|
||||
test(SIMPLE1, PrivateClass().call_private_baz)
|
||||
test(NATIVE, PrivateClass().call_private_native_baz)
|
||||
|
||||
testGroup = "Open Public Class"
|
||||
test(STABLE) { OpenPublicClass().public_baz(0) }
|
||||
test(NATIVE) { OpenPublicClass().public_baz("native") }
|
||||
test(STABLE) { OpenPublicClass().internal_baz(0) }
|
||||
test(NATIVE) { OpenPublicClass().internal_baz("native") }
|
||||
test(STABLE, OpenPublicClass().call_private_baz)
|
||||
test(NATIVE, OpenPublicClass().call_private_native_baz)
|
||||
|
||||
testGroup = "Open Internal Class"
|
||||
test(STABLE) { OpenInternalClass().public_baz(0) }
|
||||
test(NATIVE) { OpenInternalClass().public_baz("native") }
|
||||
test(STABLE) { OpenInternalClass().internal_baz(0) }
|
||||
test(NATIVE) { OpenInternalClass().internal_baz("native") }
|
||||
test(STABLE, OpenInternalClass().call_private_baz)
|
||||
test(NATIVE, OpenInternalClass().call_private_native_baz)
|
||||
|
||||
testGroup = "Open Private Class"
|
||||
test(STABLE) { OpenPrivateClass().public_baz(0) }
|
||||
test(NATIVE) { OpenPrivateClass().public_baz("native") }
|
||||
test(STABLE) { OpenPrivateClass().internal_baz(0) }
|
||||
test(NATIVE) { OpenPrivateClass().internal_baz("native") }
|
||||
test(STABLE, OpenPrivateClass().call_private_baz)
|
||||
test(NATIVE, OpenPrivateClass().call_private_native_baz)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package foo
|
||||
|
||||
public fun equals(a: Any?): Boolean = true
|
||||
public fun hashCode(): Int = 0
|
||||
public fun toString(): String = ""
|
||||
|
||||
public class PublicClass {
|
||||
override fun equals(a: Any?): Boolean = this === a
|
||||
override fun hashCode(): Int = 0
|
||||
override fun toString(): String = "PublicClass"
|
||||
}
|
||||
|
||||
internal class InternalClass {
|
||||
override fun equals(a: Any?): Boolean = this === a
|
||||
override fun hashCode(): Int = 1
|
||||
override fun toString(): String = "InternalClass"
|
||||
|
||||
// overloads
|
||||
public fun equals(a: Any?, b: Any?): Boolean = a == b
|
||||
public fun hashCode(i: Int): Int = i
|
||||
public fun toString(s: String): String = s
|
||||
}
|
||||
|
||||
private class PrivateClass {
|
||||
override fun equals(a: Any?): Boolean = this === a
|
||||
override fun hashCode(): Int = 2
|
||||
override fun toString(): String = "InternalClass"
|
||||
|
||||
// overloads
|
||||
public fun equals(a: Any?, b: Any?): Boolean = a == b
|
||||
public fun hashCode(i: Int): Int = i
|
||||
public fun toString(s: String): String = s
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
val CALEE_NAME = RegExp("""((?:equals|hashCode|toString)[^(]*)""")
|
||||
|
||||
fun <T> Function0<T>.extractNames(): Array<String> {
|
||||
val names = CALEE_NAME.exec(this.toString())
|
||||
|
||||
if (names == null || names.size != 2) {
|
||||
throw Exception("Cannot extract function name, $names for actual = \"$this\"")
|
||||
}
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
// Testing
|
||||
|
||||
var testGroup = ""
|
||||
|
||||
fun test(expected: String, f: () -> Unit) {
|
||||
val actual = f.extractNames()
|
||||
|
||||
if (expected != actual[1]) {
|
||||
throw Exception("Failed on '$testGroup' group: expected = \"$expected\", actual[1] = \"${actual[1]}\"\n actual = $actual")
|
||||
}
|
||||
}
|
||||
|
||||
val SIMPLE_EQUALS = "equals"
|
||||
val SIMPLE_HASH_CODE_1 = "hashCode_1"
|
||||
val SIMPLE_TO_STRING_1 = "toString_1"
|
||||
val STABLE_EQUALS = { equals(0) }.extractNames()[1]
|
||||
val STABLE_HASH_CODE = { hashCode() }.extractNames()[1]
|
||||
val STABLE_TO_STRING = { toString() }.extractNames()[1]
|
||||
|
||||
fun box(): String {
|
||||
testGroup = "Public Class"
|
||||
test(STABLE_EQUALS) { PublicClass().equals(0) }
|
||||
test(STABLE_HASH_CODE) { PublicClass().hashCode() }
|
||||
test(STABLE_TO_STRING) { PublicClass().toString() }
|
||||
|
||||
testGroup = "Internal Class"
|
||||
test(STABLE_EQUALS) { InternalClass().equals(0) }
|
||||
test(STABLE_HASH_CODE) { InternalClass().hashCode() }
|
||||
test(STABLE_TO_STRING) { InternalClass().toString() }
|
||||
test(SIMPLE_EQUALS) { InternalClass().equals(0, 1) }
|
||||
test(SIMPLE_HASH_CODE_1) { InternalClass().hashCode(2) }
|
||||
test(SIMPLE_TO_STRING_1) { InternalClass().toString("3") }
|
||||
|
||||
testGroup = "Private Class"
|
||||
test(STABLE_EQUALS) { PrivateClass().equals(0) }
|
||||
test(STABLE_HASH_CODE) { PrivateClass().hashCode() }
|
||||
test(STABLE_TO_STRING) { PrivateClass().toString() }
|
||||
test(SIMPLE_EQUALS) { PrivateClass().equals(0, 1) }
|
||||
test(SIMPLE_HASH_CODE_1) { PrivateClass().hashCode(2) }
|
||||
test(SIMPLE_TO_STRING_1) { PrivateClass().toString("3") }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package foo
|
||||
|
||||
public class A
|
||||
|
||||
internal fun A(a: Int){}
|
||||
|
||||
public class B(a: Int)
|
||||
|
||||
internal fun B(){}
|
||||
|
||||
internal fun C(a: Int){}
|
||||
|
||||
public class C
|
||||
|
||||
internal fun D(){}
|
||||
|
||||
public class D(a: Int)
|
||||
|
||||
//Testing
|
||||
|
||||
internal fun testClass(name: String, f: () -> Unit) {
|
||||
val fs = f.toString()
|
||||
|
||||
if ("$name(" !in fs) throw Exception("Name of class '$name' unexpectedly mangled: $fs")
|
||||
}
|
||||
|
||||
internal fun testFun(name: String, f: () -> Unit) {
|
||||
val fs = f.toString()
|
||||
|
||||
if ("$name(" in fs) throw Exception("Name of fun '$name' unexpectedly not mangled: $fs")
|
||||
}
|
||||
|
||||
|
||||
fun box(): String {
|
||||
testClass("A") { A() }
|
||||
testFun("A") { A(1) }
|
||||
testFun("B") { B() }
|
||||
testClass("B") { B(1) }
|
||||
testClass("C") { C() }
|
||||
testFun("C") { C(1) }
|
||||
testFun("D") { D() }
|
||||
testClass("D") { D(1) }
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+49
@@ -0,0 +1,49 @@
|
||||
package foo
|
||||
|
||||
fun foo(i: Int): String = "foo" + i
|
||||
public fun foo(): Int = 4
|
||||
public fun boo(): Int = 23
|
||||
fun boo(i: Int): String = "boo" + i
|
||||
|
||||
interface T {
|
||||
public fun foo(): Int
|
||||
public fun boo(): Int
|
||||
}
|
||||
|
||||
public class A : T {
|
||||
fun foo(i: Int): String = "A.foo" + i
|
||||
override fun foo(): Int = 42
|
||||
override fun boo(): Int = 2
|
||||
fun boo(i: Int): String = "A.boo" + i
|
||||
}
|
||||
|
||||
//Testing
|
||||
|
||||
fun test(testName: String, ff: Any, fb: Any) {
|
||||
val f = ff.toString()
|
||||
val b = fb.toString().replaceAll("boo", "foo")
|
||||
|
||||
if (f != b) throw Exception("FAILED on ${testName}:\n f = \"$f\"\n b = \"$b\"")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
|
||||
test("foo()", { foo() }, { boo() })
|
||||
test("foo(Int)", { foo(1) }, { boo(1) })
|
||||
|
||||
test("a.foo()", { a.foo() }, { a.boo() })
|
||||
test("a.foo(Int)", { a.foo(1) }, { a.boo(1) })
|
||||
|
||||
assertEquals("foo3", foo(3))
|
||||
assertEquals(4, foo())
|
||||
assertEquals(23, boo())
|
||||
assertEquals("boo6", boo(6))
|
||||
|
||||
assertEquals("A.foo3", a.foo(3))
|
||||
assertEquals(42, a.foo())
|
||||
assertEquals(2, a.boo())
|
||||
assertEquals("A.boo35", a.boo(35))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package foo
|
||||
|
||||
fun internal_foo(): Int = 1
|
||||
@native fun internal_foo(a: Array<Int>) = "should be ignored"
|
||||
fun internal_foo(i: Int): Int = 2
|
||||
|
||||
fun internal_boo(i: Int): Int = 2
|
||||
fun internal_boo(s: String): Int = 3
|
||||
fun internal_boo(): Int = 1
|
||||
@native fun internal_boo(a: Array<Int>) = "should be ignored"
|
||||
|
||||
val internal_f = { internal_foo() + internal_foo(1) }
|
||||
val internal_b = { internal_boo() + internal_boo(1) }
|
||||
|
||||
|
||||
public fun public_foo(): Int = 1
|
||||
@native public fun public_foo(a: Array<Int>): String = "should be ignored"
|
||||
public fun public_foo(i: Int): Int = 2
|
||||
|
||||
public fun public_boo(i: Int): Int = 2
|
||||
public fun public_boo(s: String): Int = 3
|
||||
public fun public_boo(): Int = 1
|
||||
@native public fun public_boo(a: Array<Int>): String = "should be ignored"
|
||||
|
||||
val public_f = { public_foo() + public_foo(1) }
|
||||
val public_b = { public_boo() + public_boo(1) }
|
||||
|
||||
|
||||
@native private fun private_foo(a: Array<Int>): String = "should be ignored"
|
||||
private fun private_foo(): Int = 1
|
||||
private fun private_foo(i: Int): Int = 2
|
||||
|
||||
@native private fun private_boo(a: Array<Int>): String = "should be ignored"
|
||||
private fun private_boo(i: Int): Int = 2
|
||||
private fun private_boo(s: String): Int = 3
|
||||
private fun private_boo(): Int = 1
|
||||
|
||||
val private_f = { private_foo() + private_foo(1) }
|
||||
val private_b = { private_boo() + private_boo(1) }
|
||||
|
||||
|
||||
public fun mixed_foo(s: String): Int = 3
|
||||
fun mixed_foo(): Int = 1
|
||||
@native fun mixed_foo(a: Array<Int>) = "should be ignored"
|
||||
private fun mixed_foo(s: String, i: Int): Int = 4
|
||||
fun mixed_foo(i: Int): Int = 2
|
||||
|
||||
fun mixed_boo(i: Int): Int = 2
|
||||
private fun mixed_boo(s: String, i: Int): Int = 4
|
||||
@native fun mixed_boo(a: Array<Int>) = "should be ignored"
|
||||
public fun mixed_boo(s: String): Int = 3
|
||||
fun mixed_boo(): Int = 1
|
||||
|
||||
val mixed_f = { mixed_foo() + mixed_foo(1) + mixed_foo("str") + mixed_foo("str", 44) }
|
||||
val mixed_b = { mixed_boo() + mixed_boo(1) + mixed_boo("str") + mixed_boo("str", 44) }
|
||||
|
||||
|
||||
class TestInternal {
|
||||
fun foo(): Int = 1
|
||||
fun foo(i: Int): Int = 2
|
||||
|
||||
fun boo(i: Int): Int = 2
|
||||
fun boo(s: String): Int = 3
|
||||
fun boo(): Int = 1
|
||||
}
|
||||
|
||||
val internal_in_class_f = { TestInternal().foo() + TestInternal().foo(1) }
|
||||
val internal_in_class_b = { TestInternal().boo() + TestInternal().boo(1) }
|
||||
|
||||
|
||||
class TestPublic {
|
||||
public fun foo(): Int = 1
|
||||
public fun foo(i: Int): Int = 2
|
||||
|
||||
public fun boo(i: Int): Int = 2
|
||||
public fun boo(s: String): Int = 3
|
||||
public fun boo(): Int = 1
|
||||
}
|
||||
|
||||
val public_in_class_f = { TestPublic().foo() + TestPublic().foo(1) }
|
||||
val public_in_class_b = { TestPublic().boo() + TestPublic().boo(1) }
|
||||
|
||||
|
||||
class TestPrivate {
|
||||
private fun foo(): Int = 1
|
||||
private fun foo(i: Int): Int = 2
|
||||
|
||||
private fun boo(i: Int): Int = 2
|
||||
private fun boo(s: String): Int = 3
|
||||
private fun boo(): Int = 1
|
||||
|
||||
val f = { foo() + foo(1) }
|
||||
val b = { boo() + boo(1) }
|
||||
}
|
||||
|
||||
val private_in_class_f = TestPrivate().f
|
||||
val private_in_class_b = TestPrivate().b
|
||||
|
||||
|
||||
class TestMixed {
|
||||
public fun foo(s: String): Int = 3
|
||||
fun foo(): Int = 1
|
||||
private fun foo(s: String, i: Int): Int = 4
|
||||
fun foo(i: Int): Int = 2
|
||||
|
||||
fun boo(i: Int): Int = 2
|
||||
private fun boo(s: String, i: Int): Int = 4
|
||||
public fun boo(s: String): Int = 3
|
||||
fun boo(): Int = 1
|
||||
|
||||
val f = { foo() + foo(1) }
|
||||
val b = { boo() + boo(1) }
|
||||
}
|
||||
|
||||
val mixed_in_class_f = TestMixed().f
|
||||
val mixed_in_class_b = TestMixed().b
|
||||
|
||||
class A
|
||||
|
||||
public fun A.foo(): Int = 2
|
||||
public val A.foo: Int
|
||||
get() = 1
|
||||
public val A.boo: Int
|
||||
get() = 1
|
||||
public fun A.boo(): Int = 2
|
||||
|
||||
val public_ext_f: A.() -> Int = { -> this.foo() + this.foo }
|
||||
val public_ext_b: A.() -> Int = { -> this.boo() + this.boo }
|
||||
|
||||
interface TestPublicInTrait {
|
||||
public fun foo(): Int = 2
|
||||
public val foo: Int
|
||||
public val boo: Int
|
||||
public fun boo(): Int = 2
|
||||
}
|
||||
|
||||
val public_in_trait_f = { obj: TestPublicInTrait -> obj.foo() + obj.foo }
|
||||
val public_in_trait_b = { obj: TestPublicInTrait -> obj.boo() + obj.boo }
|
||||
|
||||
interface TestInternalInTrait {
|
||||
fun foo(): Int = 2
|
||||
val foo: Int
|
||||
val boo: Int
|
||||
fun boo(): Int = 2
|
||||
}
|
||||
|
||||
val internal_in_trait_f = { obj: TestInternalInTrait -> obj.foo() + obj.foo }
|
||||
val internal_in_trait_b = { obj: TestInternalInTrait -> obj.boo() + obj.boo }
|
||||
|
||||
//Testing
|
||||
|
||||
fun test(testName: String, ff: Any, fb: Any) {
|
||||
val f = ff.toString()
|
||||
val b = fb.toString().replaceAll("boo", "foo")
|
||||
|
||||
if (f != b) throw Exception("FAILED on ${testName}:\n f = \"$f\"\n b = \"$b\"")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
test("internal", internal_f, internal_b)
|
||||
test("public", public_f, public_b)
|
||||
test("private", private_f, private_b)
|
||||
test("mixed", mixed_f, mixed_b)
|
||||
|
||||
test("internal_in_class", internal_in_class_f, internal_in_class_b)
|
||||
test("public_in_class", public_in_class_f, public_in_class_b)
|
||||
test("private_in_class", private_in_class_f, private_in_class_b)
|
||||
test("mixed_in_class", mixed_in_class_f, mixed_in_class_b)
|
||||
|
||||
test("public_ext_prop", public_ext_f, public_ext_b)
|
||||
test("public_in_trait", public_in_trait_f, public_in_trait_b)
|
||||
test("internal_in_trait", internal_in_trait_f, internal_in_trait_b)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
fun test(x: Int, y: Int) = y - x
|
||||
|
||||
fun box(): String {
|
||||
if (test(1, 2) != 1) {
|
||||
return "fail1"
|
||||
}
|
||||
if (test(x = 1, y = 2) != 1) {
|
||||
return "fail2"
|
||||
}
|
||||
if (test(y = 2, x = 1) != 1) {
|
||||
return "fail3"
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
// KT-2995 creating factory methods to simulate overloaded constructors don't work in JavaScript
|
||||
|
||||
package foo
|
||||
|
||||
class Foo(val name: String)
|
||||
|
||||
fun Foo() = Foo("<default-name>")
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("<default-name>", Foo().name)
|
||||
assertEquals("BarBaz", Foo("BarBaz").name)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// KT-2219 if function overload overridden function its name doesn't translated correctly
|
||||
|
||||
package foo
|
||||
|
||||
interface I {
|
||||
fun test(): String
|
||||
}
|
||||
|
||||
class P : I {
|
||||
override fun test(): String = "foo" + test("bar")
|
||||
|
||||
private fun test(p: String) = p
|
||||
|
||||
fun test(s: String, i: Int) = "$i $s"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("foobar", P().test())
|
||||
assertEquals("35 baz", P().test("baz", 35))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
interface A {
|
||||
fun foo(i: Int) = "A"
|
||||
}
|
||||
|
||||
interface B {
|
||||
fun foo(s: String) = "B"
|
||||
}
|
||||
|
||||
class C : A, B {
|
||||
fun foo() = "C"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("A", C().foo(1))
|
||||
assertEquals("B", C().foo(""))
|
||||
assertEquals("C", C().foo())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package foo
|
||||
|
||||
fun testSize(expectedSize: Int, vararg i: Int): Boolean {
|
||||
return (i.size == expectedSize)
|
||||
}
|
||||
|
||||
fun testSum(expectedSum: Int, vararg i: Int): Boolean {
|
||||
var sum = 0
|
||||
for (j in i) {
|
||||
sum += j
|
||||
}
|
||||
|
||||
return (expectedSum == sum)
|
||||
}
|
||||
|
||||
fun testSpreadOperator(vararg args: Int): Boolean {
|
||||
var sum = 0;
|
||||
for (a in args) sum += a
|
||||
|
||||
return testSize(args.size, *args) && testSum(sum, *args)
|
||||
}
|
||||
|
||||
class Bar(val size: Int, val sum: Int) {
|
||||
fun test(vararg args: Int) = testSize(size, *args) && testSum(sum, *args)
|
||||
}
|
||||
|
||||
object obj {
|
||||
fun test(size: Int, sum: Int, vararg args: Int) = testSize(size, *args) && testSum(sum, *args)
|
||||
}
|
||||
|
||||
fun spreadInMethodCall(size: Int, sum: Int, vararg args: Int) = Bar(size, sum).test(*args)
|
||||
|
||||
fun spreadInObjectMethodCall(size: Int, sum: Int, vararg args: Int) = obj.test(size, sum, *args)
|
||||
|
||||
fun testVarargWithFunLit(vararg args: Int, f: (a: IntArray) -> Boolean): Boolean = f(args)
|
||||
|
||||
fun <T> idVarArgs(vararg a: T) = a
|
||||
|
||||
fun <T> idArrayVarArg(vararg a: Array<T>) = a
|
||||
|
||||
fun sumFunValuesOnParameters(x: Int, y: Int, vararg a: Int, f: (Int) -> Int): Int {
|
||||
var result = f(x) + f(y)
|
||||
for(u in a) {
|
||||
result += f(u)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (!testSize(0))
|
||||
return "wrong vararg size when call function without args"
|
||||
|
||||
if (!testSum(0))
|
||||
return "wrong vararg sum (arguments) when call function without args"
|
||||
|
||||
if (!testSize(6, 1, 1, 1, 2, 3, 4))
|
||||
return "wrong vararg size when call function with some args (1)"
|
||||
|
||||
if (!testSum(30, 10, 20, 0))
|
||||
return "wrong vararg sum (arguments) when call function with some args (1)"
|
||||
|
||||
if (!testSpreadOperator(30, 10, 20, 0))
|
||||
return "failed when call function using spread operator"
|
||||
|
||||
if (!Bar(3, 30).test(10, 20, 0))
|
||||
return "failed when call method"
|
||||
|
||||
if (!spreadInMethodCall(2, 3, 1, 2))
|
||||
return "failed when call method using spread operator"
|
||||
|
||||
if (!obj.test(5, 15, 1, 2, 3, 4, 5))
|
||||
return "failed when call method of object"
|
||||
|
||||
if (!spreadInObjectMethodCall(2, 3, 1, 2))
|
||||
return "failed when call method of object using spread operator"
|
||||
|
||||
if (!testVarargWithFunLit(1, 2, 3) { args -> args.size == 3 })
|
||||
return "failed when call function with vararg and fun literal"
|
||||
|
||||
val a = arrayOf(1, 2, 3)
|
||||
val b = arrayOf(4, 5)
|
||||
|
||||
assertEquals(5, arrayOf(*a, *b).size)
|
||||
assertEquals(8, arrayOf(10, *a, 20, *b, 30).size)
|
||||
|
||||
assertEquals(5, idVarArgs(*a, *b).size)
|
||||
assertEquals(8, idVarArgs(10, *a, 20, *b, 30).size)
|
||||
|
||||
assertEquals(9, arrayOf(1, *a, *a, 1, 2).size)
|
||||
assertEquals(9, idVarArgs(1, *a, *a, 1, 2).size)
|
||||
|
||||
assertEquals(9, arrayOf(1, *a, *arrayOf(1, 2, 3), 1, 2).size)
|
||||
assertEquals(9, idVarArgs(1, *a, *arrayOf(1, 2, 3), 1, 2).size)
|
||||
|
||||
assertEquals(90, sumFunValuesOnParameters(1, 2, 3, 4, 5, 6, 7, 8, 9) { 2*it })
|
||||
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5, 6, 7, 8, 9)) { 2*it })
|
||||
assertEquals(90, sumFunValuesOnParameters(1, 2, 3, 4, *intArrayOf(5, 6, 7, 8, 9)) { 2*it })
|
||||
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5, 6, 7), 8, 9) { 2*it })
|
||||
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5), *intArrayOf(6, 7, 8, 9)) { 2*it })
|
||||
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4), 5, 6, *intArrayOf(7, 8, 9)) { 2*it })
|
||||
|
||||
assertEquals(2, idArrayVarArg(arrayOf(1), *arrayOf(arrayOf(2, 3, 4))).size)
|
||||
assertEquals(3, idArrayVarArg(arrayOf(1, 2), *arrayOf(arrayOf(3, 4), arrayOf(5, 6))).size)
|
||||
assertEquals(6, idArrayVarArg(arrayOf(1, 2), *arrayOf(arrayOf(3, 4), arrayOf(5, 6)), arrayOf(7), *arrayOf(arrayOf(8, 9), arrayOf(10, 11))).size)
|
||||
|
||||
val c = arrayOf(*a)
|
||||
assertFalse(a === c, "Spread operator should copy its argument")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
fun foo(a: Int): Int = when {
|
||||
a == 1 || a + 2 == 3 -> 5
|
||||
else -> 6
|
||||
}
|
||||
fun box(): String {
|
||||
if (foo(1) != 5) return "fail1: ${foo(1)}"
|
||||
if (foo(2) != 6) return "fail2: ${foo(1)}"
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user