JS backend: testFiles -> testData

This commit is contained in:
Zalim Bashorov
2014-02-26 15:39:46 +04:00
parent bcd579acdd
commit 442215e829
490 changed files with 0 additions and 0 deletions
@@ -0,0 +1,64 @@
import java.util.*
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) {
print(item.toString() + ", ")
}
println("end")
}
fun main(args: Array<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);
println("!")
}
}
}
}
@@ -0,0 +1,9 @@
package foo
fun box(): Boolean {
var sum = 0
val adder = {(a: Int) -> sum += a }
adder(3)
adder(2)
return sum == 5
}
@@ -0,0 +1,9 @@
package foo
class A()
private val doInit = {
A()
}()
fun box(): Boolean = doInit is A
@@ -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)
}
@@ -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(): Boolean {
if (f(1, 2) != 3) return false;
if (f(1, 3) != 4) return false;
if (f(3) != 6) return false;
if (f() != 5) return false;
return true;
}
@@ -0,0 +1,14 @@
package foo
class Point(val x: Int, val y: Int) {
fun mul(): (scalar: Int) -> Point {
return {(scalar: Int): Point -> 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,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(): Boolean {
if (chain(0) != 0) {
return false;
}
list.add(d);
if (list.get(0)(0) != 1) {
return false;
}
list.add(p);
if (list.get(1)(10) != 30) {
return false;
}
if (chain(0) != 3) {
return false;
}
list.add({ it * it });
list.add({ it - 100 });
if (chain(2) != -19) {
return false;
}
if (({(a: Int) -> a * a }(3)) != 9) {
return false;
}
return true;
}
@@ -0,0 +1,19 @@
package foo
fun box(): Boolean {
fun f() = 3
return (((f() + f()) == 6) && (b() == 24))
}
fun b(): Int {
fun a(): Int {
fun c() = 4
return c() * 3
}
val a = 2
return a() * a
}
@@ -0,0 +1,8 @@
package foo
fun box(): Boolean {
var sum = 0
val addFive = {(a: Int) -> a + 5 }
sum = addFive(sum)
return sum == 5
}
@@ -0,0 +1,14 @@
package foo
fun f(a: (Int) -> Int) = a(1)
fun box(): Boolean {
if (f() {
it + 2
} != 3) return false
if (f() {(a: Int) -> a * 300 } != 300) return false;
return true
}
@@ -0,0 +1,10 @@
package foo
fun apply(f: (Int) -> Int, t: Int): Int {
return f(t)
}
fun box(): Boolean {
return apply({(a: Int) -> a + 5 }, 3) == 8
}
@@ -0,0 +1,12 @@
package foo
fun box(): Boolean {
return f()
}
fun f(): Boolean {
return true
}
@@ -0,0 +1,9 @@
package foo
fun sum(param1: Int, param2: Int): Int {
return param1 + param2;
}
fun box(): Boolean {
return (sum(1, 5) == 6)
}
@@ -0,0 +1,12 @@
package foo
fun test(f: (Int) -> Boolean, p: Int) = f(p)
fun box(): Boolean {
if (!test({ it + 1 == 2 }, 1)) return false;
if (!test({ it > 1 }, 3)) return false;
return (test({ ((it < 1) == false) }, 1))
}
@@ -0,0 +1,18 @@
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(): Boolean {
loop(5)
return b == 5
}
@@ -0,0 +1,238 @@
package foo
public fun public_baz(i: Int) {
}
native public fun public_baz(a: String) {
}
fun internal_baz(i: Int) {
}
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) {
}
fun internal_baz(i: Int) {
}
native 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") }
}
class InternalClass {
public fun public_baz(i: Int) {
}
native public fun public_baz(a: String) {
}
fun internal_baz(i: Int) {
}
native 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) {
}
fun internal_baz(i: Int) {
}
native 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) {
}
fun internal_baz(i: Int) {
}
native 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 class OpenInternalClass {
public fun public_baz(i: Int) {
}
native public fun public_baz(a: String) {
}
fun internal_baz(i: Int) {
}
native 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) {
}
fun internal_baz(i: Int) {
}
native 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
native
fun String.search(regexp: RegExp): Int = noImpl
native
class RegExp(regexp: String, flags: String = "") {
fun exec(s: String): Array<String>? = noImpl
}
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,99 @@
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.identityEquals(a)
override fun hashCode(): Int = 0
override fun toString(): String = "PublicClass"
}
internal class InternalClass {
override fun equals(a: Any?): Boolean = this.identityEquals(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.identityEquals(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
native
fun String.search(regexp: RegExp): Int = noImpl
native
class RegExp(regexp: String, flags: String = "") {
fun exec(s: String): Array<String>? = noImpl
}
val CALEE_NAME = RegExp("""((?:equals|hashCode|toString)[^(]*)""")
fun <T> Function0<T>.extractNames(): Array<String> {
val names = CALEE_NAME.exec(this.toString(): String)
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 = "hashCode"
val SIMPLE_TO_STRING = "toString"
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) { InternalClassWithPublics().equals(0, 1) }
//test(SIMPLE_HASH_CODE) { InternalClassWithPublics().hashCode(2) }
//test(SIMPLE_TO_STRING) { InternalClassWithPublics().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) { PrivateClassWithPublics().equals(0, 1) }
//test(SIMPLE_HASH_CODE) { PrivateClassWithPublics().hashCode(2) }
//test(SIMPLE_TO_STRING) { PrivateClassWithPublics().toString("3") }
return "OK"
}
@@ -0,0 +1,148 @@
package foo
fun internal_foo(): Int = 1
native fun internal_foo(a: Array<Int>) = "should be ingnored"
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 ingnored"
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 ingnored"
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 ingnored"
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 ingnored"
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 ingnored"
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 ingnored"
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 ingnored"
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
// Helpers
native
fun String.replace(regexp: RegExp, replacement: String): String = noImpl
fun String.replaceAll(regexp: String, replacement: String): String = replace(RegExp(regexp, "g"), replacement)
native
class RegExp(regexp: String, flags: String)
//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)
return "OK"
}
@@ -0,0 +1,16 @@
package foo
fun test(x: Int, y: Int) = y - x
fun box(): Boolean {
if (test(1, 2) != 1) {
return false;
}
if (test(x = 1, y = 2) != 1) {
return false;
}
if (test(y = 2, x = 1) != 1) {
return false;
}
return true
}
@@ -0,0 +1,24 @@
package foo
trait A {
fun foo(i: Int) = "A"
}
trait B {
fun foo(s: String) = "B"
}
class C : A, B {
fun foo() = "C"
}
fun assertEquals(expected: Any, actual: Any) {
if (expected != actual) throw Exception("expected = $expected\nactual = $actual")
}
fun box(): String {
assertEquals(C().foo(1), "A")
assertEquals(C().foo(""), "B")
assertEquals(C().foo(), "C")
return "OK"
}
@@ -0,0 +1,69 @@
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 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"
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"
}