Skip to content

Instantly share code, notes, and snippets.

@beala
Created April 5, 2012 01:40
Show Gist options
  • Save beala/2307309 to your computer and use it in GitHub Desktop.
Save beala/2307309 to your computer and use it in GitHub Desktop.
inferType() and step() test functions
def testInferType(inferType: (TypEnv, Expr) => Typ): Boolean = {
def tester( test: (Expr, Typ) ) = {
val (sourceExpr, expectRes) = test
val res = inferType(typenvEmpty,sourceExpr) == expectRes
if (res == false) {
println("Failed: " + test)
println("Result: " + inferType(typenvEmpty,sourceExpr))
}
res
}
val p = ExprParser.parse _
val tests: List[(Expr, Typ)] = List( (N(1), TInt),
(B(true), TBoolean),
(U, TUnit),
(p("1+2"), TInt),
(p("1;true"), TBoolean),
(p("1+2"), TInt),
(p("1-2"), TInt),
(p("1*2"), TInt),
(p("1<2"), TBoolean),
(p("2==1"), TBoolean),
(p("true && false"), TBoolean),
(p("true || false"), TBoolean),
(p("if(1==2) {f:1} else {f:2}"), TRecord(Map("f"->TInt))),
(p("((x:Int)=>())(1)"),TUnit ),
(p("{a:1,b:(),c:(x:Int)=>x}"), TRecord(Map("a"->TInt, "b"->TUnit, "c"->TFun(TInt,TInt)))),
(p("inj[I:Int,B:Boolean](I:1)"), TUnion(Map("I"->TInt, "B"->TBoolean))),
(p("inj[I:Int,B:Boolean](I:1) match[Int] { case I:x => x case B:x => if (x) 1 else 0 }"), TInt),
(p("((x:Int)=>x)(1)"), TInt),
(p("def g(x:Int):Int=x in def f(y:Int):Int= y+1 in f(g(1))"), TInt),
(p("def g(x:Int):Int=if(x==0) x else g(x-1) in g(10)"), TInt),
(p("def g(x:Int):Int=x in g(1)"), TInt)
)
tests.foldLeft(true)((last, cur) => last && tester(cur))
}
def testStep(step: Expr => Expr): Boolean = {
def tester( test: (Expr, Expr) ) = {
val (sourceExpr, expectRes) = test
val res = step(sourceExpr) == expectRes
if (res == false) {
println("Failed: " + test)
println("Result: " + step(sourceExpr))
}
res
}
val p = ExprParser.parse _
val tests: List[(Expr, Expr)] = List( (p("1+1"), p("2")), // Easy mode
(p("{y:1}.y"), p("1")), // Grab a field from a record
(p("((l:{y:Int}) => l.y)({y:1})"), p("{y:1}.y")), // Func that grabs a field from rec
(p("inj[CInt:Int](CInt:1) match[Int] { case CInt:x => x }"), p("1")), // Match on one simple case
(p("inj[I:Int, B:Boolean](B:true) match[Int] { case I:x => x case B:x => if(x) 1 else 0 }"), p("if(true) 1 else 0")), // Return Int val of Bool or Int
(p("{a:1+1, b:1}"), p("{a:2, b:1}")), // Evaluate a record
(p("{a:1+1, b:1}.b"), p("{a:2, b:1}.b")), // Eval a GetField with a Record expr
(p("inj[I:Int,B:Boolean](I:1+1)"), p("inj[I:Int,B:Boolean](I:2)")), // Eval an Inj if it has an Expr
(p("def g(i:Int):Int = i in g(1)"), p("((i:Int) => i)(1)")) // Eval an Inj if it has an Expr
)
tests.foldLeft(true)((last, cur) => last && tester(cur))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment