Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. package miniscala
  2.  
  3. import miniscala.Ast._
  4. import miniscala.Interpreter._
  5. import miniscala.TypeChecker._
  6. import miniscala.parser.Parser.parse
  7.  
  8. object Test69 {
  9.  
  10. def main(args: Array[String]): Unit = {
  11. // //Old tests
  12. // test("{ def f(x: Int): Int = x; f(2) }", IntVal(2), IntType)
  13. // testFail("{ def f(x: Int): Int = x; f(2, 3) }")
  14. // testFail("{ def f(x: Int): String = x; f(2) }")
  15. // testFail("{ def f(x: String): Int = x; f(2) }")
  16. // test("{ def f(x: Int): Int = if (x == 0) 0 else x + f(x - 1); f(10) }",IntVal(55),IntType)
  17. // 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)
  18. // 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)
  19. // test("{ val x = 10; def f(y: Int): Int = x - 10; f(11)}",IntVal(0),IntType)
  20. // test("{ val x = 10; def f(x: Int): Int = x - 10; f(11)}",IntVal(1),IntType)
  21. //
  22. // //New tests specific for 69
  23. //
  24. // //Simple lambda with typeannotation
  25. // test("{val y: Int => Int = (x: Int) => x + 40; y(2) }",IntVal(42),IntType)
  26. //
  27. // //Function with lambda as parameter
  28. // 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)
  29. //
  30. // //Direct recurssion
  31. // 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)
  32. //
  33. // //Mutual
  34. // test("{ def f(n: Int, l: Int => Int): Boolean = if (l(n) == 0) true else g(n-1, l); " +
  35. // "def g(n: Int, l: Int => Int): Boolean = if (l(n) < 0) false else f(n-1, l); " +
  36. // "f(10,(n: Int) => n)}",BoolVal(true),BoolType)
  37. //
  38. // //Lambda using lambda
  39. // test("{val x = 2; val f = (a: Int) => a + x; f(2)}", IntVal(4), IntType)
  40. //
  41. // //Lambdaer i karry
  42. // test("{ val sum = (a: Int, b: Int, c: Int) => a + b + c; sum(20, 17, 5) }", IntVal(42), IntType)
  43. // test("{ val sum = (a: Int) => ((b: Int) => (((c: Int) => a + b + c))); sum(20)(17)(5) }", IntVal(42), IntType)
  44. //
  45. // // ;-)
  46. // test("{ val subtract = (a: Int) => (b: Int) => b - a; " +
  47. // "val decrement = subtract(1); " +
  48. // "val double = (x: Int) => x*2; " +
  49. // "def tripple(f: Int => Int, x: Int): Int = f(f(f(x))); " +
  50. // "decrement(decrement(decrement(decrement(decrement(decrement(double(tripple(double,3))))))))}",IntVal(42),IntType)
  51. //
  52. // //Negatives
  53. // testFail("{ val sum = (a: Int) => ((b: Int) => (((c: Int) => a + b + c))); sum(20)(17)(5)(6) }")
  54. // testFail(" { def f(x: Int): Int = x; f(2)(2) } ")
  55.  
  56. // test("{ val f = (n: Int, l: Int => Int) => if (l(n) == 0) true else g(n-1, l); " +
  57. // "val g = (n: Int, l: Int => Int) => if (l(n) < 0) false else f(n-1, l); " +
  58. // "f(10,(n: Int) => n)}", BoolVal(true), BoolType)
  59. //
  60. // test("{ def f(n: Int, l: Int => Int): Boolean = if (l(n) == 0) true else g(n-1, l); " +
  61. // "def g(n: Int, l: Int => Int): Boolean = if (l(n) < 0) false else f(n-1, l); " +
  62. // "f(10,(n: Int) => n)}",BoolVal(true),BoolType)
  63. test(" { val f = (n: Int, l: Int => Int) => g(n) + n; val g = (n: Int) => n; l(21,g)} ",IntVal(42),IntType)
  64. }
  65.  
  66.  
  67.  
  68. def test(prg: String, rval: Val, rtype: Type) = {
  69. testVal(prg, rval)
  70. testType(prg, rtype)
  71. }
  72.  
  73. def testFail(prg: String) = {
  74. testValFail(prg)
  75. testTypeFail(prg)
  76. }
  77.  
  78. def testVal(prg: String, value: Val, env: Env = Map[Id, Val]()) = {
  79. assert(eval(parse(prg), env) == value)
  80. }
  81.  
  82. def testType(prg: String, out: Type, tenv: TypeEnv = Map[Id, Type]()) = {
  83. assert(typeCheck(parse(prg), tenv) == out)
  84. }
  85.  
  86. def testValFail(prg: String) = {
  87. try {
  88. eval(parse(prg), Map[Id, Val]())
  89. assert(false)
  90. } catch {
  91. case _: InterpreterError => assert(true)
  92. }
  93. }
  94.  
  95. def testTypeFail(prg: String) = {
  96. try {
  97. typeCheck(parse(prg), Map[Id, Type]())
  98. assert(false)
  99. } catch {
  100. case _: TypeError => assert(true)
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement