Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up hw-tests/hw3.sml into multiple files (one for each question).
- Loading branch information
1 parent
dd688c1
commit f9ec304
Showing
5 changed files
with
284 additions
and
184 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,231 @@ | ||
(* Save everything needed for Q1 in hw/hw3-1.sml to test it. Otherwise, weird problems | ||
due to datatype redeclarations etc. The trie datatype and function declarations should | ||
all be in hw/hw3-1.sml. *) | ||
use "hw/hw3-1.sml"; | ||
|
||
(* Q1.1: insert and Q1.2: all_entries *) | ||
val t1 = insert ("b", []); | ||
|
||
val Node (t1_c, t1_l) = !(hd t1); | ||
|
||
assertEqual t1_c #"b" "first trie - b"; | ||
assertEqual (!(hd t1_l)) Empty "first trie - b, then empty"; | ||
assertEqual (tl t1_l) [] "first trie - b, no other children"; | ||
|
||
assertEqual (all_entries t1) ["b"] "first tree - all_entries"; | ||
|
||
|
||
|
||
val t2 = insert ("be", []); | ||
|
||
val Node (t2_c, t2_l) = !(hd t2); | ||
val Node (t2_1_c, t2_1_l) = !(hd t2_l); | ||
|
||
assertEqual t2_c #"b" "second trie - b"; | ||
assertEqual t2_1_c #"e" "second trie - be"; | ||
assertEqual (!(hd t2_1_l)) Empty "second trie - be, then empty"; | ||
assertEqual (tl t2_1_l) [] "second trie - be, no other children"; | ||
|
||
assertEqual (all_entries t2) ["be"] "second trie - all_entries"; | ||
|
||
|
||
|
||
val t3 = insert ("boo", t2); | ||
|
||
val Node (t3_c, t3_l) = !(hd t3); | ||
val Node (t3_1_c, t3_1_l) = !(hd t3_l); | ||
val Node (t3_2_c, t3_2_l) = !(hd (tl t3_l)); | ||
val Node (t3_2_1_c, t3_2_1_l) = !(hd t3_2_l); | ||
|
||
assertEqual t3_c #"b" "third trie - b"; | ||
assertEqual t3_1_c #"e" "third trie - be"; | ||
assertEqual (!(hd t3_1_l)) Empty "third trie - be, then empty"; | ||
assertEqual (tl t3_1_l) [] "third trie - be, no other children"; | ||
assertEqual t3_2_c #"o" "third trie - bo"; | ||
assertEqual t3_2_1_c #"o" "third trie - boo"; | ||
assertEqual (!(hd t3_2_1_l)) Empty "third trie - boo, then empty"; | ||
assertEqual (tl t3_2_1_l) [] "third trie - boo, no other children"; | ||
|
||
assertEqual (all_entries t3) ["be", "boo"] "third trie - all_entries"; | ||
|
||
|
||
|
||
val t4 = insert ("boo", t3); | ||
|
||
val Node (t4_c, t4_l) = !(hd t4); | ||
val Node (t4_1_c, t4_1_l) = !(hd t4_l); | ||
val Node (t4_2_c, t4_2_l) = !(hd (tl t4_l)); | ||
val Node (t4_2_1_c, t4_2_1_l) = !(hd t4_2_l); | ||
|
||
assertEqual t4_c #"b" "fourth trie - b"; | ||
assertEqual t4_1_c #"e" "fourth trie - be"; | ||
assertEqual (!(hd t4_1_l)) Empty "fourth trie - be, then empty"; | ||
assertEqual (tl t4_1_l) [] "fourth trie - be, no other children"; | ||
assertEqual t4_2_c #"o" "fourth trie - bo"; | ||
assertEqual t4_2_1_c #"o" "fourth trie - boo"; | ||
assertEqual (!(hd t4_2_1_l)) Empty "fourth trie - boo, then empty"; | ||
assertEqual (!(hd (tl t4_2_1_l))) Empty "fourth trie - boo, then another empty"; | ||
assertEqual (tl (tl t4_2_1_l)) [] "fourth trie - boo, no other children"; | ||
|
||
assertEqual (all_entries t4) ["be", "boo", "boo"] "fourth trie - all_entries"; | ||
|
||
|
||
|
||
val t5 = insert ("book", t4); | ||
|
||
val Node (t5_c, t5_l) = !(hd t5); | ||
val Node (t5_1_c, t5_1_l) = !(hd t5_l); | ||
val Node (t5_2_c, t5_2_l) = !(hd (tl t5_l)); | ||
val Node (t5_2_1_c, t5_2_1_l) = !(hd t5_2_l); | ||
val Node (t5_2_1_3_c, t5_2_1_3_l) = !(hd (tl (tl t5_2_1_l))); | ||
|
||
assertEqual t5_c #"b" "fifth trie - b"; | ||
assertEqual t5_1_c #"e" "fifth trie - be"; | ||
assertEqual (!(hd t5_1_l)) Empty "fifth trie - be, then empty"; | ||
assertEqual (tl t5_1_l) [] "fifth trie - be, no other children"; | ||
assertEqual t5_2_c #"o" "fifth trie - bo"; | ||
assertEqual t5_2_1_c #"o" "fifth trie - boo"; | ||
assertEqual (!(hd t5_2_1_l)) Empty "fifth trie - boo, then empty"; | ||
assertEqual (!(hd (tl t5_2_1_l))) Empty "fifth trie - boo, then another empty"; | ||
assertEqual t5_2_1_3_c #"k" "fifth trie - book"; | ||
assertEqual (!(hd t5_2_1_3_l)) Empty "fifth trie - book, then empty"; | ||
assertEqual (tl t5_2_1_3_l) [] "fifth trie - book, no other children"; | ||
|
||
assertEqual (all_entries t5) ["be", "boo", "boo", "book"] "fifth trie - all_entries"; | ||
|
||
|
||
|
||
val t6 = insert ("bool", t5); | ||
|
||
val Node (t6_c, t6_l) = !(hd t6); | ||
val Node (t6_1_c, t6_1_l) = !(hd t6_l); | ||
val Node (t6_2_c, t6_2_l) = !(List.last t6_l); | ||
val Node (t6_2_1_c, t6_2_1_l) = !(hd t6_2_l); | ||
val Node (t6_2_1_3_c, t6_2_1_3_l) = !(hd (tl (tl t6_2_1_l))); | ||
val Node (t6_2_1_4_c, t6_2_1_4_l) = !(List.last t6_2_1_l); | ||
|
||
assertEqual t6_c #"b" "sixth trie - b"; | ||
assertEqual t6_1_c #"e" "sixth trie - be"; | ||
assertEqual (!(hd t6_1_l)) Empty "sixth trie - be, then empty"; | ||
assertEqual (tl t6_1_l) [] "sixth trie - be, no other children"; | ||
assertEqual t6_2_c #"o" "sixth trie - bo"; | ||
assertEqual t6_2_1_c #"o" "sixth trie - boo"; | ||
assertEqual (!(hd t6_2_1_l)) Empty "sixth trie - boo, then empty"; | ||
assertEqual (!(hd (tl t6_2_1_l))) Empty "sixth trie - boo, then another empty"; | ||
assertEqual t6_2_1_3_c #"k" "sixth trie - book"; | ||
assertEqual (!(hd t6_2_1_3_l)) Empty "sixth trie - book, then empty"; | ||
assertEqual (tl t6_2_1_3_l) [] "sixth trie - book, no other children"; | ||
assertEqual t6_2_1_4_c #"l" "sixth trie - bool"; | ||
assertEqual (!(hd t6_2_1_4_l)) Empty "sixth trie - bool, then empty"; | ||
assertEqual (tl t6_2_1_4_l) [] "sixth trie - bool, no other children"; | ||
|
||
assertEqual (all_entries t6) ["be", "boo", "boo", "book", "bool"] "sixth trie - all_entries"; | ||
|
||
|
||
|
||
val t7 = insert ("d", t1); | ||
|
||
val Node (t7_1_c, t7_1_l) = !(hd t7); | ||
val Node (t7_2_c, t7_2_l) = !(List.last t7); | ||
|
||
assertEqual t7_1_c #"b" "seventh trie - b"; | ||
assertEqual (!(hd t7_1_l)) Empty "seventh trie - b, then empty"; | ||
assertEqual t7_2_c #"d" "seventh trie - d"; | ||
assertEqual (!(List.last t7_2_l)) Empty "seventh tree - d, then empty"; | ||
assertEqual (all_entries t7) ["b", "d"] "seventh trie - all_entries"; | ||
|
||
|
||
|
||
(* Test case from the hw3.sml file (right above the section for qustion 1.3. Note that the diagram | ||
in the assignment specs itself does NOT correspond to the tree produced by the test case in the code, | ||
despite the fact that it says "For a representation of the tree above using the SML data structure, see | ||
the file provided". The diagram in the PDF is produced by storing the words mentioned in the text next | ||
to the diagram itself, and for whatever reason, these words are not the same nor in the same order as | ||
those used in the test case. | ||
The diagram for the tree produced by the test case in the code looks like this: | ||
b | ||
/ \ | ||
e r | ||
/ \ \ | ||
e a e | ||
/|\ \ |\ | ||
r * f r a e | ||
| / \ | | | | ||
* * y d d * | ||
| | | | ||
* * * | ||
where the asterisks mean, of course, "empty". Note the slightly different ordering and the fact | ||
that there is no "bear". *) | ||
val t8 = insert ("beer", []); | ||
val _ = (insert ("bread", t8); insert ("beard", t8); insert ("bee", t8); insert ("beef", t8); insert ("beefy", t8); insert ("bree", t8)); | ||
|
||
val Node (t8_c, t8_l) = !(hd t8); | ||
val Node (t8_1_c, t8_1_l) = !(hd t8_l); | ||
val Node (t8_1_1_c, t8_1_1_l) = !(hd t8_1_l); | ||
val Node (t8_1_1_1_c, t8_1_1_1_l) = !(hd t8_1_1_l); | ||
val Node (t8_1_1_3_c, t8_1_1_3_l) = !(List.last t8_1_1_l); | ||
val Node (t8_1_1_3_2_c, t8_1_1_3_2_l) = !(List.last t8_1_1_3_l); | ||
val Node (t8_1_2_c, t8_1_2_l) = !(List.last t8_1_l); | ||
val Node (t8_1_2_1_c, t8_1_2_1_l) = !(hd t8_1_2_l); | ||
val Node (t8_1_2_1_1_c, t8_1_2_1_1_l) = !(hd t8_1_2_1_l); | ||
val Node (t8_2_c, t8_2_l) = !(List.last t8_l); | ||
val Node (t8_2_1_c, t8_2_1_l) = !(hd t8_2_l); | ||
val Node (t8_2_1_1_c, t8_2_1_1_l) = !(hd t8_2_1_l); | ||
val Node (t8_2_1_1_1_c, t8_2_1_1_1_l) = !(hd t8_2_1_1_l); | ||
val Node (t8_2_1_2_c, t8_2_1_2_l) = !(List.last t8_2_1_l); | ||
|
||
assertEqual t8_c #"b" "eighth tree - b"; | ||
assertEqual t8_1_c #"e" "eighth tree - be"; | ||
assertEqual t8_1_1_c #"e" "eighth tree - bee"; | ||
assertEqual t8_1_1_1_c #"r" "eighth tree - beer"; | ||
assertEqual (!(hd t8_1_1_1_l)) Empty "eighth tree - beer, then empty"; | ||
assertEqual (!(hd (tl t8_1_1_l))) Empty "eighth tree - bee, then empty"; | ||
assertEqual t8_1_1_3_c #"f" "eighth tree - beef"; | ||
assertEqual (!(hd t8_1_1_3_l)) Empty "eighth tree - beef, then empty"; | ||
assertEqual t8_1_1_3_2_c #"y" "eighth tree - beefy"; | ||
assertEqual (!(hd t8_1_1_3_2_l)) Empty "eighth tree - beefy, then empty"; | ||
assertEqual t8_1_2_c #"a" "eighth tree - bea"; | ||
assertEqual t8_1_2_1_c #"r" "eighth tree - bear"; | ||
assertEqual t8_1_2_1_1_c #"d" "eighth tree - beard"; | ||
assertEqual (!(hd t8_1_2_1_1_l)) Empty "eighth tree - beard, then empty"; | ||
assertEqual t8_2_c #"r" "eighth tree - br"; | ||
assertEqual t8_2_1_c #"e" "eighth tree - bre"; | ||
assertEqual t8_2_1_1_c #"a" "eighth tree - brea"; | ||
assertEqual t8_2_1_1_1_c #"d" "eighth tree - bread"; | ||
assertEqual (!(hd t8_2_1_1_1_l)) Empty "eighth tree - bread, then empty"; | ||
assertEqual t8_2_1_2_c #"e" "eighth tree - bree"; | ||
assertEqual (!(hd t8_2_1_2_l)) Empty "eighth tree - bree, then empty"; | ||
|
||
assertEqual (all_entries t8) ["beer", "bee", "beef", "beefy", "beard", "bread", "bree"] "eighth tree - all_entries"; | ||
|
||
|
||
|
||
val t9 = insert("fm", insert("am", [])); | ||
|
||
val Node (t9_1_c, t9_1_l) = !(hd t9); | ||
val Node (t9_1_1_c, t9_1_1_l) = !(hd t9_1_l); | ||
val Node (t9_2_c, t9_2_l) = !(List.last t9); | ||
val Node (t9_2_1_c, t9_2_1_l) = !(hd t9_2_l); | ||
|
||
assertEqual t9_1_c #"a" "ninth tree - a"; | ||
assertEqual t9_1_1_c #"m" "ninth tree - am"; | ||
assertEqual (!(hd t9_1_1_l)) Empty "ninth tree - am, then empty"; | ||
assertEqual t9_2_c #"f" "ninth tree - f"; | ||
assertEqual t9_2_1_c #"m" "ninth tree - fm"; | ||
assertEqual (!(hd t9_2_1_l)) Empty "ninth tree - fm, then empty"; | ||
|
||
assertEqual (all_entries t9) ["am", "fm"] "ninth tree - all_entries"; | ||
|
||
(* Now check that certain trees were modified by reference *) | ||
|
||
assertEqual (all_entries t1) ["b"] "first tree - all_entries, again"; | ||
assertEqual (all_entries t2) ["be","boo","boo","book","bool"] "second tree - all_entries, again"; | ||
assertEqual (all_entries t3) ["be","boo","boo","book","bool"] "third tree - all_entries, again"; | ||
assertEqual (all_entries t4) ["be","boo","boo","book","bool"] "fourth tree - all_entries, again"; | ||
assertEqual (all_entries t5) ["be","boo","boo","book","bool"] "fifth tree - all_entries, again"; | ||
assertEqual (all_entries t6) ["be","boo","boo","book","bool"] "sixth tree - all_entries, again"; | ||
assertEqual (all_entries t7) ["b","d"] "seventh tree - all_entries, again"; | ||
assertEqual (all_entries t8) ["beer","bee","beef","beefy","beard","bread","bree"] "eighth tree - all_entries, again"; |
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,17 @@ | ||
use "hw/hw3-2.sml"; | ||
|
||
(* Q2: collect *) | ||
(* doesn't check if you're implementing it with exceptions, just that you do it correctly *) | ||
val evens = fn x => (x mod 2) = 0 | ||
val odds = fn x => (x mod 2) = 1 | ||
val two = fn x => x = 2 | ||
val smallerThanTen = fn x => x < 10 | ||
|
||
val L = Node (Node (Empty, 2, Empty), 5, Node (Empty, 6, Empty)); | ||
val R = Node (Empty, 12, Empty); | ||
val T = Node (Node (L, 7, Node (Empty, 8, Empty)), 11, R); | ||
|
||
assertEqual (collect(evens,T)) [2,6,8,12] "evens"; | ||
assertEqual (collect(odds,T)) [5,7,11] "odds"; | ||
assertEqual (collect(two,T)) [2] "a single element"; | ||
assertEqual (collect(smallerThanTen,T)) [2,5,6,7,8] "a subsection"; |
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,6 @@ | ||
use "hw/hw3-3.sml"; | ||
|
||
assertEqual (mon_fact(0)) (1,2) "zero"; | ||
assertEqual (mon_fact(1)) (1,8) "one"; | ||
assertEqual (mon_fact(5)) (120,32) "bigger number"; | ||
assertEqual (mon_fact(6)) (720,38) "another bigger number"; |
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,24 @@ | ||
use "hw/hw3-4.sml"; | ||
(* converts a finite stream to list, to test) *) | ||
fun viewStream s = case force s of Empty => nil | | ||
Cons(l,s) => [l]@(viewStream s) | ||
|
||
|
||
(* Q4.1 interleave *) | ||
val nilList = viewStream (interleave 10 []); | ||
val normalList = viewStream (interleave 10 [1,2,3]); | ||
|
||
assertEqual nilList [[10]] "zero length list"; | ||
assertEqual normalList [[10,1,2,3],[1,10,2,3],[1,2,10,3],[1,2,3,10]] "normal list"; | ||
|
||
(* Q4.2 flattenS *) | ||
val stream_of_streams = cons(interleave 10 [1, 2, 3], cons((interleave 9 [4,5,6]), empty)); | ||
val expected = [[10,1,2,3],[1,10,2,3],[1,2,10,3],[1,2,3,10],[9,4,5,6],[4,9,5,6],[4,5,9,6],[4,5,6,9]]; | ||
assertEqual (viewStream (flattenS stream_of_streams)) expected "flattenS (uses interleave)"; | ||
|
||
(* Q4.3 permute *) | ||
|
||
val l = viewStream(permute [1,2,3]); | ||
val onelength = viewStream(permute [1]); | ||
assertEqual l [[1,2,3],[2,1,3],[2,3,1],[1,3,2],[3,1,2],[3,2,1]] "liiiist"; | ||
assertEqual onelength [[1]] |
Oops, something went wrong.