Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
162 lines (128 sloc) 6.88 KB
{-
Copyright © 2012 Daniel Tahara, Kartik Venkatraman
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-}
module DTKV_Implementation_Test where
import DTKV_Implementation
-- Tests
test_DTKV = all (\x -> x) tests
tests = [test1, test2, test3, test4, test5, test6, test7, test8,
test9, test10, test11, test12, test13, test14, test15, test16]
--- Pre-made variables to cut down repetition
nullMem = ([],[])
u = ("u", Integer)
v = ("v", Integer)
w = ("w", Integer)
x = ("x", Integer)
y = ("y", Integer)
z = ("z", Integer)
a = ("a", Str)
b = ("b", Str)
p = ("p", Boolean)
q = ("q", Boolean)
--- testProgram_strings
stm1 = Assignment a (Data (StrAtom "hello"))
stm2 = Assignment b (Data (StrAtom "ice"))
expr1 = FnCall ("head", Str) [Var ("m", Str)]
expr2 = FnCall ("head", Str) [Var ("n", Str)]
expr4 = FnCall ("head", Str) [Var a]
expr5 = FnCall ("head", Str) [Var b]
stm3 = FnDeclaration "concatHeads" (Fn Str [("m", Str), ("n", Str)] [] (FnCall ("++", Str) [expr1, expr2]))
expr3 = FnCall ("concatHeads", Str) [(Var a), (Var b)]
stm4 = Assignment ("answer", Str) (expr3)
testProgram_strings = [stm1, stm2, stm3, stm4]
testReturnExpr1 = Var a
testReturnExpr2 = FnCall ("++", Str) [Var a, Var b] -- StrAtom "helloice"
testReturnExpr3 = FnCall ("++", Str) [expr4, expr5] -- StrAtom "hi"
testReturnExpr4 = FnCall ("head", Str) [Var a] -- StrAtom "h"
testReturnExpr5 = Var ("answer", Str) -- StrAtom "hi"
environment1 = ((testProgram_strings, testReturnExpr1), nullMem) -- StrAtom "hello"
test1 = (run environment1) == (StrAtom "hello")
environment2 = ((testProgram_strings, testReturnExpr2), nullMem) -- StrAtom "helloice"
test2 = (run environment2) == (StrAtom "helloice")
environment3 = ((testProgram_strings, testReturnExpr3), nullMem) -- StrAtom "hi"
test3 = (run environment3) == (StrAtom "hi")
environment4 = ((testProgram_strings, testReturnExpr4), nullMem) -- StrAtom "h"
test4 = (run environment4) == (StrAtom "h")
environment5 = ((testProgram_strings, testReturnExpr5), nullMem) -- StrAtom "hi"
test5 = (run environment5) == (StrAtom "hi")
---- test_Program Ints
stm5 = Assignment x (Data (IntAtom 8))
stm6 = Assignment y (Data (IntAtom 7))
expr6 = FnCall ("neg", Integer) [Var a]
stm7 = Assignment z (FnCall ("-", Integer) [Var y, Var x])
expr7 = FnCall ("abs", Integer) [Var z]
expr8 = FnCall (">", Boolean) [expr7, (Data (IntAtom 0))]
expr9 = FnCall (">", Boolean) [Var z, (Data (IntAtom 0))]
stm8 = CondBlock (IfElse expr8 [Assignment p (Data (BoolAtom True))] [Assignment p (Data (BoolAtom False))])
stm9 = CondBlock (IfElse expr9 [Assignment q (Data (BoolAtom True))] [Assignment q (Data (BoolAtom False))])
testProgram_ints = [stm5, stm6, stm7, stm8, stm9]
testReturnExpr11 = Var z -- IntAtom (-1)
testReturnExpr12 = Var p -- BoolAtom True
testReturnExpr13 = Var q -- BoolAtom False
testReturnExpr14 = FnCall ("!", Boolean) [Var p] -- BoolAtom False
testReturnExpr15 = FnCall ("&&", Boolean) [Var p, Data (BoolAtom True)] -- BoolAtom True
testReturnExpr16 = FnCall ("&&", Boolean) [Var p, Var q] -- BoolAtom False
testReturnExpr17 = FnCall ("||", Boolean) [Data (BoolAtom False), Data (BoolAtom False)] -- BoolAtom False
environment6 = ((testProgram_ints, testReturnExpr11), nullMem) -- IntAtom (-1)
test6 = (run environment6) == (IntAtom (-1))
environment7 = ((testProgram_ints, testReturnExpr12), nullMem) -- BoolAtom True
test7 = (run environment7) == (BoolAtom True)
environment8 = ((testProgram_ints, testReturnExpr13), nullMem) -- BoolAtom False
test8 = (run environment8) == (BoolAtom False)
environment9 = ((testProgram_ints, testReturnExpr14), nullMem) -- BoolAtom False
test9 = (run environment9) == (BoolAtom False)
environment10 = ((testProgram_ints, testReturnExpr15), nullMem) -- BoolAtom True
test10 = (run environment10) == (BoolAtom True)
environment11 = ((testProgram_ints, testReturnExpr16), nullMem) -- BoolAtom False
test11 = (run environment11) == (BoolAtom False)
environment12 = ((testProgram_ints, testReturnExpr17), nullMem) -- BoolAtom False
test12 = (run environment12) == (BoolAtom False)
--- testProgram_loopsAndNesting
stm10 = Assignment ("w", Integer) (Data (IntAtom 0))
stm11 = Assignment ("v", Integer) (Data (IntAtom (-5)))
stm12 = LoopBlock (For (Data (IntAtom 1)) (Data (IntAtom 10)) [Assignment ("w", Integer) (FnCall ("+", Integer) [Var ("w", Integer), (Var ("loopVar", Integer))])])
expr10 = FnCall ("abs", Integer) [Var ("v", Integer)]
stm13 = LoopBlock (While (FnCall ("<", Integer) [expr10, Data (IntAtom 101)])
[Assignment v (FnCall ("*", Integer) [Var v, Data (IntAtom 2)])])
testProgram_loopsAndNesting = testProgram_ints ++ [stm10, stm11, stm12, stm13]
testReturnExpr31 = Var w
testReturnExpr32 = Var v
environment13 = ((testProgram_loopsAndNesting, testReturnExpr31), nullMem) -- IntAtom 55
test13 = (run environment13) == (IntAtom 55)
environment14 = ((testProgram_loopsAndNesting, testReturnExpr32), nullMem) -- IntAtom (-160)
test14 = (run environment14) == (IntAtom (-160))
-- testProgram_loopInUserFn
stm14 = Assignment x (Data (IntAtom 1000))
stm15 = FnDeclaration "ProdOfDif"
(Fn Integer
[("n", Integer), x, ("a", Integer)]
[Assignment u (Data (IntAtom 0)),
Assignment ("diff", Integer) (FnCall ("-", Integer) [Var x, Var ("a", Integer)]),
LoopBlock (For (Data (IntAtom 1)) (Var ("n", Integer))
[Assignment u (FnCall ("+", Integer) [Var u, Var ("diff", Integer)])])]
(Var u))
expr11 = FnCall ("ProdOfDif", Integer) [Data (IntAtom 3), Data (IntAtom 2), Data (IntAtom 5)]
stm16 = Assignment ("answer", Integer) expr11
-- using 'a' here makes sure not colliding with my namespace
testProgram_loopInUserFn = [stm14, stm15, stm16]
testReturnExpr41 = Var ("answer", Integer)
testReturnExpr42 = Var x -- IntAtom 1000; checks that local variable doesn't affect outer scope
environment15 = ((testProgram_loopInUserFn, testReturnExpr41), nullMem) -- IntAtom (-9)
test15 = (run environment15) == (IntAtom (-9))
environment16 = ((testProgram_loopInUserFn, testReturnExpr42), nullMem) -- IntAtom 1000
test16 = (run environment16) == (IntAtom 1000)