add test files

This commit is contained in:
pTalanov
2012-02-27 21:55:57 +04:00
parent 645a3a9f2f
commit c21ea8aa86
240 changed files with 16998 additions and 0 deletions
@@ -0,0 +1,67 @@
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()
}
fun main(args:Array<String>)
{
val v = Viewable<Int>()
val x = ArrayList<Int>()
v.add(1)
v.add(2)
lifetime(
{
v.view(it, {(itemLifetime, item)->
x.add(item)
Dump(x)
itemLifetime.attach { x.remove(item as Any); Dump(x) }
})
})
}
@@ -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
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,15 @@
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,12 @@
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() : (scalar:Int)->Point
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,13 @@
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,17 @@
package foo
var b = 0
fun loop(var times : Int) {
while(times > 0) {
val u = {(value : Int) ->
b = b + 1
}
u(times--)
}
}
fun box() : Boolean {
loop(5)
return b == 5
}
@@ -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,17 @@
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 box() = testSize(0) && testSum(0) && testSize(3, 1, 1, 1) && testSum(3, 1, 1, 1) && testSize(6, 1, 1, 1, 2, 3, 4) &&
testSum(30, 10, 20, 0)