forked from dhlorenz/CS236319
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[Homework link](https://docs.google.com/document/d/14rkTShnErNoh0DLebtplGDpJK5Frzv0InnWHr2vlfJY/edit?usp=sharing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
val it = () : unit | ||
val it = [] : int list | ||
val it = [1,0,1] : int list | ||
val it = [1] : int list | ||
val it = [0,0,0,0,1] : int list | ||
val it = [0,1,0,1,1] : int list | ||
val it = [0,1,0,0,1,1,0,0,1] : int list | ||
val it = [1,0,0,1,1] : int list | ||
val it = [1,0,1,1,0,0,0,0,1] : int list | ||
val it = [0,1,1,1,0,0,0,0,1] : int list | ||
val it = [0,1,1] : int list | ||
val it = [1,1,0,1] : int list | ||
val it = [1,0,1,1,1,1] : int list | ||
val it = [0,0,0,1] : int list | ||
val it = [0,1,0,0,0,0] : int list | ||
val it = [1,0,0,0,0,0] : int list | ||
val it = [1,1] : int list | ||
- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
print "===TEST START===\n"; | ||
(* Testing to_binary function *) | ||
to_binary 0; | ||
to_binary 5; | ||
to_binary 1; | ||
to_binary 16; | ||
|
||
(* Testing encode function *) | ||
encode [1, 1, 0, 1]; | ||
encode [1, 0, 1, 1, 1, 1]; | ||
encode [0, 0, 0, 1]; | ||
encode [0, 1, 0, 0, 0, 0]; | ||
encode [1, 0, 0, 0, 0, 0]; | ||
encode [1, 1]; | ||
|
||
(* Testing decode function *) | ||
decode ([0,1,0,1,1], 4); | ||
decode ([0,1,0,0,1,1,0,0,1], 6); | ||
decode ([1,0,0,1,1], 4); | ||
decode ([1,0,1,1,0,0,0,0,1], 6); | ||
decode ([0,1,1,1,0,0,0,0,1], 6); | ||
decode ([0,1,1], 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
val it = () : unit | ||
val test1 = 1 ::: "a" ::: 2 ::: "b" ::: NIL : (int,string) heterolist | ||
val test2 = "hello" ::: 3.14 ::: "world" ::: 42 ::: NIL | ||
: (string,real) heterolist | ||
val test3 = true ::: 1 ::: false ::: 2 ::: NIL : (bool,int) heterolist | ||
val test4 = #"a" ::: "hello" ::: #"b" ::: "world" ::: NIL | ||
: (char,string) heterolist | ||
val test5 = ([1,2],["a","b"]) : int list * string list | ||
val test6 = (["x","y"],[1.1,2.2]) : string list * real list | ||
val test7 = ([#"a",#"b"],["hello","world"]) : char list * string list | ||
val test8 = ([true,false],[1,2]) : bool list * int list | ||
val testlol = | ||
(["never","gonna","give","you","up"],["never","gonna","let","you","down"]) | ||
: string list * string list | ||
val test9 = 1 ::: "a" ::: 2 ::: "b" ::: NIL : (int,string) heterolist | ||
val test10 = "x" ::: 1.1 ::: "y" ::: 2.2 ::: NIL : (string,real) heterolist | ||
val test11 = #"a" ::: "hello" ::: #"b" ::: "world" ::: NIL | ||
: (char,string) heterolist | ||
val test12 = true ::: 1 ::: false ::: 2 ::: NIL : (bool,int) heterolist | ||
val test13 = NIL : (int,string) heterolist | ||
val test14 = NIL : (string,real) heterolist | ||
- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
print "===TEST START===\n"; | ||
(* Testing the build4 function *) | ||
val test1 = build4 (1, "a", 2, "b"); | ||
val test2 = build4 ("hello", 3.14, "world", 42.0); | ||
val test3 = build4 (true, 1, false, 2); | ||
val test4 = build4 (#"a", "hello", #"b", "world"); | ||
|
||
(* Testing the unzip function *) | ||
val test5 = unzip (1::: "a"::: 2::: "b"::: NIL); | ||
val test6 = unzip ("x"::: 1.1::: "y"::: 2.2::: NIL); | ||
val test7 = unzip (#"a"::: "hello"::: #"b"::: "world"::: NIL); | ||
val test8 = unzip (true:::1:::false:::2:::NIL); | ||
val testlol = unzip ("never":::"never":::"gonna":::"gonna":::"give":::"let":::"you":::"you":::"up":::"down":::NIL) | ||
|
||
(* Testing the zip function *) | ||
val test9 = zip ([1, 2], ["a", "b"]); | ||
val test10 = zip (["x", "y"], [1.1, 2.2]); | ||
val test11 = zip ([#"a", #"b"], ["hello", "world"]); | ||
val test12 = zip ([true, false], [1, 2]); | ||
|
||
(* Handling exceptions in zip function tests *) | ||
val test13 = zip ([1, 2], ["a"]) handle Empty => NIL; | ||
val test14 = zip (["x"], [1.1, 2.2]) handle Empty => NIL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
use "hw3_q3.sml"; | ||
|
||
(* | ||
To run this test, place hw3_q3.sml in the terminal's working directory, | ||
then execute: | ||
smlnj test_parse.sml | ||
*) | ||
|
||
exception TestFailure of string; | ||
|
||
local | ||
fun sexp_to_string (ATOM(SYMBOL(s))) = s | ||
| sexp_to_string (ATOM(NIL)) = "NIL" | ||
| sexp_to_string (CONS(x, y)) = "(" ^ sexp_to_string x ^ "." ^ sexp_to_string y ^ ")"; | ||
in | ||
fun assert_equal (expected, actual, msg) = | ||
if expected = actual then () | ||
else raise TestFailure(msg ^ ": expected " ^ sexp_to_string expected ^ ", but got " ^ sexp_to_string actual); | ||
end; | ||
|
||
let | ||
fun run_tests tests = | ||
let | ||
fun runTest (name, test) = | ||
(print ("Running " ^ name ^ "... "); | ||
(test (); | ||
print "Passed\n") handle | ||
TestFailure msg => print ("Failed - " ^ msg ^ "\n")) | ||
in | ||
List.app runTest tests | ||
end; | ||
|
||
fun tokenize x = | ||
String.tokens (fn c: char => c = #" ") | ||
(String.translate (fn #"(" => "( " | #")" => " )" | c => str c) x); | ||
|
||
val parse_tests = [ | ||
("Test single symbol", | ||
fn () => let | ||
val input = "x" | ||
val expected = ATOM(SYMBOL "x") | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test single symbol") | ||
end), | ||
|
||
("Test list with single element", | ||
fn () => let | ||
val input = "(x)" | ||
val expected = CONS(ATOM(SYMBOL "x"), ATOM NIL) | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test list with single element") | ||
end), | ||
|
||
("Test empty list", | ||
fn () => let | ||
val input = "()" | ||
val expected = ATOM NIL | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test empty list") | ||
end), | ||
|
||
("Test nested lists", | ||
fn () => let | ||
val input = "(a (b c) d)" | ||
val expected = CONS( | ||
ATOM(SYMBOL "a"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "b"), | ||
CONS( | ||
ATOM(SYMBOL "c"), | ||
ATOM NIL)), | ||
CONS( | ||
ATOM(SYMBOL "d"), | ||
ATOM NIL))) | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test nested lists") | ||
end), | ||
|
||
("Test simple list", | ||
fn () => let | ||
val input = "(a b c)" | ||
val expected = CONS( | ||
ATOM(SYMBOL "a"), | ||
CONS( | ||
ATOM(SYMBOL "b"), | ||
CONS( | ||
ATOM(SYMBOL "c"), | ||
ATOM NIL))) | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test simple list") | ||
end), | ||
|
||
("Test list with nested empty lists", | ||
fn () => let | ||
val input = "(a () b)" | ||
val expected = CONS( | ||
ATOM(SYMBOL "a"), | ||
CONS( | ||
ATOM NIL, | ||
CONS( | ||
ATOM(SYMBOL "b"), | ||
ATOM NIL))) | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test list with nested empty lists") | ||
end), | ||
|
||
("Test deeply nested lists", | ||
fn () => let | ||
val input = "(a (b (c (d e))))" | ||
val expected = CONS( | ||
ATOM(SYMBOL "a"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "b"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "c"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "d"), | ||
CONS( | ||
ATOM(SYMBOL "e"), | ||
ATOM NIL)), | ||
ATOM NIL)), | ||
ATOM NIL)), | ||
ATOM NIL)) | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test deeply nested lists") | ||
end), | ||
|
||
("Test complex expression", | ||
fn () => let | ||
val input = "(define (square x) (* x x))" | ||
val expected = CONS( | ||
ATOM(SYMBOL "define"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "square"), | ||
CONS( | ||
ATOM(SYMBOL "x"), | ||
ATOM NIL)), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "*"), | ||
CONS( | ||
ATOM(SYMBOL "x"), | ||
CONS( | ||
ATOM(SYMBOL "x"), | ||
ATOM NIL))), | ||
ATOM NIL))) | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test complex expression") | ||
end), | ||
|
||
("Test mixed atoms and lists", | ||
fn () => let | ||
val input = "(a (b) c (d e) f)" | ||
val expected = CONS( | ||
ATOM(SYMBOL "a"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "b"), | ||
ATOM NIL), | ||
CONS( | ||
ATOM(SYMBOL "c"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "d"), | ||
CONS( | ||
ATOM(SYMBOL "e"), | ||
ATOM NIL)), | ||
CONS( | ||
ATOM(SYMBOL "f"), | ||
ATOM NIL))))) | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test mixed atoms and lists") | ||
end), | ||
|
||
("Test complex lambda", | ||
fn () => let | ||
val input = "(lambda (x) (lambda (y) (lambda (z) (f x y z))))" | ||
val expected = CONS( | ||
ATOM(SYMBOL "lambda"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "x"), | ||
ATOM NIL), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "lambda"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "y"), | ||
ATOM NIL), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "lambda"), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "z"), | ||
ATOM NIL), | ||
CONS( | ||
CONS( | ||
ATOM(SYMBOL "f"), | ||
CONS( | ||
ATOM(SYMBOL "x"), | ||
CONS( | ||
ATOM(SYMBOL "y"), | ||
CONS( | ||
ATOM(SYMBOL "z"), | ||
ATOM NIL)))), | ||
ATOM NIL))), | ||
ATOM NIL))), | ||
ATOM NIL))) | ||
val actual = parse (tokenize input) | ||
in | ||
assert_equal (expected, actual, "Test complex lambda") | ||
end) | ||
]; | ||
in | ||
run_tests parse_tests | ||
end; |