Adding tests, adding miniFun implementation
This commit is contained in:
@ -1,3 +1,7 @@
|
||||
(test
|
||||
(name testingImp)
|
||||
(libraries lang))
|
||||
|
||||
(test
|
||||
(name testingFun)
|
||||
(libraries lang))
|
||||
7
test/testingFun.expected
Normal file
7
test/testingFun.expected
Normal file
@ -0,0 +1,7 @@
|
||||
1
|
||||
1
|
||||
5
|
||||
6
|
||||
4
|
||||
-4
|
||||
55
|
||||
107
test/testingFun.ml
Normal file
107
test/testingFun.ml
Normal file
@ -0,0 +1,107 @@
|
||||
open Lang.MiniFun
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Identity program *)
|
||||
let program =
|
||||
Function
|
||||
(["a"],
|
||||
(Variable "a")
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 1)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Constant program *)
|
||||
let program =
|
||||
Function
|
||||
(["a"],
|
||||
(Integer 1)
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 10)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Partial application of function program *)
|
||||
let program =
|
||||
LetIn
|
||||
("f",
|
||||
(Function (["x"; "y"], Plus (Variable "x", Variable "y"))),
|
||||
(Application (Variable "f", [Integer 3]))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 2)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Partial application of function program *)
|
||||
let program =
|
||||
LetFun
|
||||
("f",
|
||||
["x"],
|
||||
(Function (["y"], Plus (Variable "x", Variable "y"))),
|
||||
(Application (Variable "f", [Integer 3]))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 3)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Passing functions to functions program *)
|
||||
let program =
|
||||
LetIn
|
||||
("f",
|
||||
(Function (
|
||||
["z"],
|
||||
(Function (
|
||||
["y"],
|
||||
Function (
|
||||
["x"],
|
||||
(IfThenElse (
|
||||
CmpLess (Variable "x", Integer 0),
|
||||
(Application (Variable "y", [Variable "x"])),
|
||||
(Application (Variable "z", [Variable "x"]))
|
||||
)))
|
||||
))
|
||||
)),
|
||||
(Application
|
||||
(
|
||||
(Application
|
||||
(Variable "f",
|
||||
[Function (["x"], Plus (Variable "x", Integer 1))]
|
||||
)
|
||||
),
|
||||
[Function (["x"], Minus (Variable "x", Integer 1))]
|
||||
)
|
||||
)
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program (3));
|
||||
Printf.printf "%d\n" (reduce program (-3))
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Recursive function program *)
|
||||
let program =
|
||||
LetFun
|
||||
("f",
|
||||
["x"],
|
||||
(IfThenElse (CmpLess (Variable "x", Integer 2),Integer 1, Plus (Variable "x", Application (Variable "f", [Minus (Variable "x", Integer 1)])))),
|
||||
(Variable "f")
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 10)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Scope program *)
|
||||
let program =
|
||||
LetIn
|
||||
("f",
|
||||
(LetIn ("a", Integer 1, (Function (["y"], Plus (Variable "y", Variable "a"))))),
|
||||
(LetIn ("a", Integer 2, Variable "f"))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 4)
|
||||
9
test/testingImp.expected
Normal file
9
test/testingImp.expected
Normal file
@ -0,0 +1,9 @@
|
||||
1
|
||||
The variable y is not defined.
|
||||
3628800
|
||||
351
|
||||
35553600
|
||||
true
|
||||
4807526976
|
||||
0
|
||||
1
|
||||
222
test/testingImp.ml
Normal file
222
test/testingImp.ml
Normal file
@ -0,0 +1,222 @@
|
||||
open Lang.MiniImp
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Identity program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Assignment ("b", (Variable "a")))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 1 program)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* y not defined program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence (
|
||||
(Assignment ("x", (Integer 1))),
|
||||
(Assignment ("b",
|
||||
(Plus ((Plus (Variable "a", Variable "x")), (Variable "y")))))
|
||||
)
|
||||
)
|
||||
)
|
||||
;;
|
||||
|
||||
try
|
||||
Printf.printf "%d\n" (reduce 100 program)
|
||||
with AbsentAssignment s ->
|
||||
Printf.printf "%s\n" s
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* factorial program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence (
|
||||
(Assignment ("b", (Integer 1))),
|
||||
(For (
|
||||
(Assignment ("i", (Integer 1))),
|
||||
(BCmpLessEq (Variable "i", Variable "a")),
|
||||
(Assignment ("i", (Plus ((Variable "i"), (Integer 1))))),
|
||||
(Assignment ("b", (Times (Variable "b", Variable "i"))))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 10 program)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* hailstone sequence's lenght program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence (
|
||||
(Assignment ("b", (Integer 1))),
|
||||
(While (
|
||||
(BNot (BCmp ((Variable "a"), (Integer 1)))),
|
||||
(Sequence (
|
||||
(Assignment ("b", (Plus (Variable "b", Integer 1)))),
|
||||
(If (
|
||||
(BCmp (Modulo (Variable "a", Integer 2), Integer 1)),
|
||||
(Assignment ("a", Plus (Times (Integer 3, Variable "a"), Integer 1))),
|
||||
(Assignment ("a", Division (Variable "a", Integer 2)))
|
||||
))
|
||||
))
|
||||
))
|
||||
))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 77031 program)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* sum multiples of 3 and 5 program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence (
|
||||
(Assignment ("b", (Integer 0))),
|
||||
(For (
|
||||
(Assignment ("i", Integer 0)),
|
||||
(BCmpLess (Variable "i", Variable "a")),
|
||||
(Assignment ("i", Plus (Variable "i", Integer 1))),
|
||||
(If (
|
||||
(BOr ((BCmp (Modulo (Variable "i", Integer 3), Integer 0)),
|
||||
(BCmp (Modulo (Variable "i", Integer 5), Integer 0)))),
|
||||
(Assignment ("b", Plus (Variable "b", Variable "i"))),
|
||||
(Skip)
|
||||
))
|
||||
))
|
||||
))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 12345 program)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* rand program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Assignment ("b", Rand (Variable "a")))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%b\n" ((reduce 10 program) < 10)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* fibonacci program *)
|
||||
let program =
|
||||
Main
|
||||
("n",
|
||||
"fnext",
|
||||
(Sequence (
|
||||
Sequence (
|
||||
(Assignment ("fnow", Integer 0)),
|
||||
(Assignment ("fnext", Integer 1))
|
||||
),
|
||||
(While (
|
||||
(BCmpGreater (Variable "n", Integer 1)),
|
||||
(Sequence (
|
||||
(Sequence (
|
||||
(Assignment ("tmp", Plus (Variable "fnow", Variable "fnext"))),
|
||||
(Assignment ("fnow", Variable "fnext"))
|
||||
)),
|
||||
(Sequence (
|
||||
(Assignment ("fnext", Variable "tmp")),
|
||||
(Assignment ("n", Minus (Variable "n", Integer 1)))
|
||||
))
|
||||
))))
|
||||
))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 48 program)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Miller-Rabin primality test program *)
|
||||
let program =
|
||||
Main
|
||||
("n",
|
||||
"result",
|
||||
Sequence (
|
||||
(Assignment ("result", Integer 0)),
|
||||
(Sequence (
|
||||
(Sequence (
|
||||
(Assignment ("s", Integer 0)),
|
||||
(While (
|
||||
(BCmp (Integer 0,
|
||||
Modulo (
|
||||
Division (
|
||||
(Minus (Variable "n", Integer 1)),
|
||||
(Power (Integer 2, Variable "s"))),
|
||||
(Integer 2)
|
||||
)
|
||||
)
|
||||
),
|
||||
(Assignment ("s", Plus (Variable "s", Integer 1)))
|
||||
))
|
||||
)),
|
||||
(Sequence (
|
||||
(Assignment ("d", Division (Minus (Variable "n", Integer 1), Power (Integer 2, Variable "s")))),
|
||||
(For (
|
||||
(Assignment ("i", Integer 20)),
|
||||
(BCmpGreater (Variable "i", Integer 0)),
|
||||
(Assignment ("i", Minus (Variable "i", Integer 1))),
|
||||
(Sequence (
|
||||
Sequence (
|
||||
(Assignment ("a", Plus (Rand (Minus (Variable "n", Integer 4)), Integer 2))),
|
||||
(Assignment ("x", PowerMod (Variable "a", Variable "d", Variable "n")))),
|
||||
Sequence (
|
||||
(For (
|
||||
(Assignment ("j", Integer 0)),
|
||||
(BCmpLess (Variable "j", Variable "s")),
|
||||
(Assignment ("j", Plus (Variable "j", Integer 1))),
|
||||
(Sequence (
|
||||
Sequence (
|
||||
(Assignment ("y", PowerMod (Variable "x", Integer 2, Variable "n"))),
|
||||
(If (
|
||||
(BAnd (BAnd (BCmp (Variable "y", Integer 1), BNot (BCmp (Variable "x", Integer 1))), BNot (BCmp (Variable "x", Minus (Variable "n", Integer 1))))),
|
||||
(Assignment ("result", Integer 1)),
|
||||
(Skip)
|
||||
))),
|
||||
(Assignment ("x", Variable "y"))
|
||||
))
|
||||
)),
|
||||
(If (
|
||||
(BNot (BCmp (Variable "y", Integer 1))),
|
||||
(Assignment ("result", Integer 1)),
|
||||
(Skip)
|
||||
)))
|
||||
))
|
||||
))
|
||||
))
|
||||
))
|
||||
)
|
||||
)
|
||||
;;
|
||||
|
||||
(* should return 0 because prime *)
|
||||
Printf.printf "%d\n" (reduce 179424673 program)
|
||||
;;
|
||||
(* sould return 1 because not prime *)
|
||||
Printf.printf "%d\n" (reduce 179424675 program)
|
||||
;;
|
||||
Reference in New Issue
Block a user