Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package miniscala
- import miniscala.Ast._
- import miniscala.Interpreter._
- import miniscala.TypeChecker._
- import miniscala.parser.Parser.parse
- object Test69 {
- def main(args: Array[String]): Unit = {
- // //Old tests
- // test("{ def f(x: Int): Int = x; f(2) }", IntVal(2), IntType)
- // testFail("{ def f(x: Int): Int = x; f(2, 3) }")
- // testFail("{ def f(x: Int): String = x; f(2) }")
- // testFail("{ def f(x: String): Int = x; f(2) }")
- // test("{ def f(x: Int): Int = if (x == 0) 0 else x + f(x - 1); f(10) }",IntVal(55),IntType)
- // test("{ def f(x: Int): Boolean = if (x == 0) true else g(x); def g(x: Int): Boolean = if (x < 0) false else f(x-5); f(55)}",BoolVal(true),BoolType)
- // test("{ val x = 2; { def p(): Int = x * 2; { def q(): Int = p(); { val x = 5; { def p(): Int = x + 1; q() } } } } }",IntVal(4),IntType)
- // test("{ val x = 10; def f(y: Int): Int = x - 10; f(11)}",IntVal(0),IntType)
- // test("{ val x = 10; def f(x: Int): Int = x - 10; f(11)}",IntVal(1),IntType)
- //
- // //New tests specific for 69
- //
- // //Simple lambda with typeannotation
- // test("{val y: Int => Int = (x: Int) => x + 40; y(2) }",IntVal(42),IntType)
- //
- // //Function with lambda as parameter
- // test("{ def f(n: Int, g: Int => Int): Int = if (n <= 0) 1 else g(n); f(5, (n: Int) => n / 2) }",IntVal(2),IntType)
- //
- // //Direct recurssion
- // test("{ def f(n: Int, g: Int => Int): Boolean = if (g(n) <= 0) if (n == -1) true else false else f(n-2, g); f(10,(n: Int) => n)}",BoolVal(false),BoolType)
- //
- // //Mutual
- // test("{ def f(n: Int, l: Int => Int): Boolean = if (l(n) == 0) true else g(n-1, l); " +
- // "def g(n: Int, l: Int => Int): Boolean = if (l(n) < 0) false else f(n-1, l); " +
- // "f(10,(n: Int) => n)}",BoolVal(true),BoolType)
- //
- // //Lambda using lambda
- // test("{val x = 2; val f = (a: Int) => a + x; f(2)}", IntVal(4), IntType)
- //
- // //Lambdaer i karry
- // test("{ val sum = (a: Int, b: Int, c: Int) => a + b + c; sum(20, 17, 5) }", IntVal(42), IntType)
- // test("{ val sum = (a: Int) => ((b: Int) => (((c: Int) => a + b + c))); sum(20)(17)(5) }", IntVal(42), IntType)
- //
- // // ;-)
- // test("{ val subtract = (a: Int) => (b: Int) => b - a; " +
- // "val decrement = subtract(1); " +
- // "val double = (x: Int) => x*2; " +
- // "def tripple(f: Int => Int, x: Int): Int = f(f(f(x))); " +
- // "decrement(decrement(decrement(decrement(decrement(decrement(double(tripple(double,3))))))))}",IntVal(42),IntType)
- //
- // //Negatives
- // testFail("{ val sum = (a: Int) => ((b: Int) => (((c: Int) => a + b + c))); sum(20)(17)(5)(6) }")
- // testFail(" { def f(x: Int): Int = x; f(2)(2) } ")
- // test("{ val f = (n: Int, l: Int => Int) => if (l(n) == 0) true else g(n-1, l); " +
- // "val g = (n: Int, l: Int => Int) => if (l(n) < 0) false else f(n-1, l); " +
- // "f(10,(n: Int) => n)}", BoolVal(true), BoolType)
- //
- // test("{ def f(n: Int, l: Int => Int): Boolean = if (l(n) == 0) true else g(n-1, l); " +
- // "def g(n: Int, l: Int => Int): Boolean = if (l(n) < 0) false else f(n-1, l); " +
- // "f(10,(n: Int) => n)}",BoolVal(true),BoolType)
- test(" { val f = (n: Int, l: Int => Int) => g(n) + n; val g = (n: Int) => n; l(21,g)} ",IntVal(42),IntType)
- }
- def test(prg: String, rval: Val, rtype: Type) = {
- testVal(prg, rval)
- testType(prg, rtype)
- }
- def testFail(prg: String) = {
- testValFail(prg)
- testTypeFail(prg)
- }
- def testVal(prg: String, value: Val, env: Env = Map[Id, Val]()) = {
- assert(eval(parse(prg), env) == value)
- }
- def testType(prg: String, out: Type, tenv: TypeEnv = Map[Id, Type]()) = {
- assert(typeCheck(parse(prg), tenv) == out)
- }
- def testValFail(prg: String) = {
- try {
- eval(parse(prg), Map[Id, Val]())
- assert(false)
- } catch {
- case _: InterpreterError => assert(true)
- }
- }
- def testTypeFail(prg: String) = {
- try {
- typeCheck(parse(prg), Map[Id, Type]())
- assert(false)
- } catch {
- case _: TypeError => assert(true)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement