Skip to content

Commit

Permalink
Refactor tests, add professor test LA01
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-byrne committed Oct 15, 2024
1 parent bbd48b4 commit 9b84558
Show file tree
Hide file tree
Showing 34 changed files with 187 additions and 39 deletions.
12 changes: 7 additions & 5 deletions src/large_assignment_01/large_assignment_01.sml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
(*
* Large Assignment #01
* Author: Christian Byrne
* Date: 9/27/24
* Large Assignment #01
* Desc: Practice functions in SML, focusing on features of the
* language such as pattern matching, currying, partial
* application, and higher-order functions.
*)



(*
* Type: `int * int * int -> bool`
* Desc: The triangle inequality theorem states that the sum of any
Expand Down Expand Up @@ -87,13 +86,16 @@ fun suffix([], _) = true
let
fun listLength([]) = 0
| listLength(x::li) = 1 + listLength(li)
fun truncatePrefix(count, x::li) = if count = 0 then x::li else truncatePrefix(count - 1, li)
fun truncatePrefix(_, []) = []
| truncatePrefix(0, li) = li
| truncatePrefix(count, x::li) = truncatePrefix(count - 1, li)
fun suffixEqual([], []) = true
| suffixEqual([], li) = false
| suffixEqual(li, []) = false
| suffixEqual(x::a, y::b) = if x = y then suffixEqual(a, b) else false
val truncateCount = listLength(li2) - listLength(li1)
in
(* Handle case: li1 longer than li2 *)
if truncateCount < 0 then false
if truncateCount < 0 then false (* Handle case: list 1 longer than list 2 *)
else suffixEqual(li1, truncatePrefix(truncateCount, li2))
end;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
214 changes: 180 additions & 34 deletions tests/tests-large_assignment_01/test_manual_tests.sml
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,220 @@
use "src/large_assignment_01/large_assignment_01.sml";
use "tests/utils.sml";

fun raiseIfFalse(b: bool, msg: string) =
if not b then raise Fail msg else ();

print "triangle\n";
triangle(2,3,4) = true;
raiseIfFalse(triangle(2,3,4) = true, "triangle(2,3,4) = true");

print "triangleR\n";
triangleR(2.1,3.2,7.8) = false;
raiseIfFalse(triangleR(2.1,3.2,7.8) = false, "triangleR(2.1,3.2,7.8) = false");

print "cycle\n";
cycle(4,[1,2,3,4,5]) = [5,1,2,3,4];
raiseIfFalse(cycle(4,[1,2,3,4,5]) = [5,1,2,3,4], "cycle(4,[1,2,3,4,5]) = [5,1,2,3,4]");

print "mirror\n";
mirror([1,2,3,4]) = [1,2,3,4,4,3,2,1];
raiseIfFalse(mirror([1,2,3,4]) = [1,2,3,4,4,3,2,1], "mirror([1,2,3,4]) = [1,2,3,4,4,3,2,1]");

print "gtList\n";
gtList([1,2,3,4,5],2) = [3,4,5];
raiseIfFalse(gtList([1,2,3,4,5],2) = [3,4,5], "gtList([1,2,3,4,5],2) = [3,4,5]");

print "suffix\n";
suffix([3,4,5],[1,2,3,4,5]) = true;
raiseIfFalse(suffix([3,4,5],[1,2,3,4,5]) = true, "suffix([3,4,5],[1,2,3,4,5]) = true");

print "get\n";
get([1,2,3,4,5],3) = 4;
raiseIfFalse(get([1,2,3,4,5],3) = 4, "get([1,2,3,4,5],3) = 4");

print "subList\n";
subList([1,2,3,4,5],1,3) = [2,3,4];
raiseIfFalse(subList([1,2,3,4,5],1,3) = [2,3,4], "subList([1,2,3,4,5],1,3) = [2,3,4]");

print "reverse\n";
reverse([1,2,3,4]) = [4,3,2,1];
raiseIfFalse(reverse([1,2,3,4]) = [4,3,2,1], "reverse([1,2,3,4]) = [4,3,2,1]");

print "apply\n";
apply([1,2,3,4,5],(fn x => x + 2)) = [3,4,5,6,7];
raiseIfFalse(apply([1,2,3,4,5],(fn x => x + 2)) = [3,4,5,6,7], "apply([1,2,3,4,5],(fn x => x + 2)) = [3,4,5,6,7]");

print "collapse\n";
collapse([1,2,3,4],5,(op +)) = 15;
raiseIfFalse(collapse([1,2,3,4],5,(op +)) = 15, "collapse([1,2,3,4],5,(op +)) = 15");

print "quicksort\n";
quicksort (op <=) [3,5,1,2,4] = [1,2,3,4,5];
raiseIfFalse(quicksort (op <=) [3,5,1,2,4] = [1,2,3,4,5], "quicksort (op <=) [3,5,1,2,4] = [1,2,3,4,5]");

print "bubbleSort\n";
bubbleSort (op >=) [1,1,4,2,3] = [4,3,2,1,1];
raiseIfFalse(bubbleSort (op >=) [1,1,4,2,3] = [4,3,2,1,1], "bubbleSort (op >=) [1,1,4,2,3] = [4,3,2,1,1]");

print "insertionSort\n";
insertionSort (op <=) [~4,1,8,9,0,~2] = [~4,~2,0,1,8,9];
raiseIfFalse(insertionSort (op <=) [~4,1,8,9,0,~2] = [~4,~2,0,1,8,9], "insertionSort (op <=) [~4,1,8,9,0,~2] = [~4,~2,0,1,8,9]");

print "substring\n";
substring "zon" "arizona" = true;
raiseIfFalse(substring "zon" "arizona" = true, "substring \"zon\" \"arizona\" = true");

print "indexOf\n";
indexOf 3 [1,2,3,4,5] = 2;
raiseIfFalse(indexOf 3 [1,2,3,4,5] = 2, "indexOf 3 [1,2,3,4,5] = 2");

print "dec2BaseN\n";
dec2BaseN 2 10 = "1010";
raiseIfFalse(dec2BaseN 2 10 = "1010", "dec2BaseN 2 10 = \"1010\"");

print "dropNth\n";
dropNth 2 [1,2,3,4,5] = [1,3,5];
raiseIfFalse(dropNth 2 [1,2,3,4,5] = [1,3,5], "dropNth 2 [1,2,3,4,5] = [1,3,5]");

print "flatten\n";
flatten [[],[1,2,3],[4,5,6],[],[8,9]] = [1,2,3,4,5,6,8,9];
raiseIfFalse(flatten [[],[1,2,3],[4,5,6],[],[8,9]] = [1,2,3,4,5,6,8,9], "flatten [[],[1,2,3],[4,5,6],[],[8,9]] = [1,2,3,4,5,6,8,9]");

print "condenseLists\n";
condenseLists (op +) 0 [[],[1,2,3,4],[5,6]] = [0,10,11];
raiseIfFalse(condenseLists (op +) 0 [[],[1,2,3,4],[5,6]] = [0,10,11], "condenseLists (op +) 0 [[],[1,2,3,4],[5,6]] = [0,10,11]");

print "remove\n";
remove (fn x => x mod 3 = 0) [1,2,3,4,5,6,7,8,9] = [1,2,4,5,7,8];
raiseIfFalse(remove (fn x => x mod 3 = 0) [1,2,3,4,5,6,7,8,9] = [1,2,4,5,7,8], "remove (fn x => x mod 3 = 0) [1,2,3,4,5,6,7,8,9] = [1,2,4,5,7,8]");

print "triplist\n";
triplist [1,2,3] = [1,1,1,2,2,2,3,3,3];
raiseIfFalse(triplist [1,2,3] = [1,1,1,2,2,2,3,3,3], "triplist [1,2,3] = [1,1,1,2,2,2,3,3,3]");

print "repeat\n";
repeat [1,2] 4 = [1,2,1,2,1,2,1,2];
raiseIfFalse(repeat [1,2] 4 = [1,2,1,2,1,2,1,2], "repeat [1,2] 4 = [1,2,1,2,1,2,1,2]");

print "filterApply\n";
filterApply [1,2,3,4,5] (fn x => x mod 2 = 0) (fn x => x + 1) = [1,3,3,5,5];
raiseIfFalse(filterApply [1,2,3,4,5] (fn x => x mod 2 = 0) (fn x => x + 1) = [1,3,3,5,5], "filterApply [1,2,3,4,5] ... = [1,3,3,5,5]");

print "arithSeq\n";
arithSeq 0 5 4 = [0,5,10,15];
raiseIfFalse(arithSeq 0 5 4 = [0,5,10,15], "arithSeq 0 5 4 = [0,5,10,15]");

print "element\n";
element 5 [1,2,3,4,5,6,7,8] = true;
raiseIfFalse(element 5 [1,2,3,4,5,6,7,8] = true, "element 5 [1,2,3,4,5,6,7,8] = true");

print "isSet\n";
isSet [1,2,3,4,5] = true;
raiseIfFalse(isSet [1,2,3,4,5] = true, "isSet [1,2,3,4,5] = true");

print "union\n";
union ([1,2,3],[2,3,4,5]) = [1,2,3,4,5];
raiseIfFalse(union ([1,2,3],[2,3,4,5]) = [1,2,3,4,5], "union ([1,2,3],[2,3,4,5]) = [1,2,3,4,5]");

print "intersection\n";
intersection ([1,2,3],[2,3,4,5]) = [2,3];
raiseIfFalse(intersection ([1,2,3],[2,3,4,5]) = [2,3], "intersection ([1,2,3],[2,3,4,5]) = [2,3]");

print "difference\n";
difference ([1,2,3],[2,3,4,5]) = [1];
raiseIfFalse(difference ([1,2,3],[2,3,4,5]) = [1], "difference ([1,2,3],[2,3,4,5]) = [1]");

print "xor\n";
xor ([1,2,3],[2,3,4,5]) = [1,4,5];
(* Tests Provided by Professor *)

use "src/large_assignment_01/large_assignment_01.sml";
use "tests/utils.sml";

fun raiseIfFalse(b: bool, msg: string) =
if not b then raise Fail msg else ();

print "triangle\n";
raiseIfFalse(triangle(2,3,4) = true, "triangle(2,3,4) = true");

print "triangleR\n";
raiseIfFalse(triangleR(2.1,3.2,7.8) = false, "triangleR(2.1,3.2,7.8) = false");

print "cycle\n";
raiseIfFalse(cycle(4,[1,2,3,4,5]) = [5,1,2,3,4], "cycle(4,[1,2,3,4,5]) = [5,1,2,3,4]");

print "mirror\n";
raiseIfFalse(mirror([1,2,3,4]) = [1,2,3,4,4,3,2,1], "mirror([1,2,3,4]) = [1,2,3,4,4,3,2,1]");

print "gtList\n";
raiseIfFalse(gtList([1,2,3,4,5],2) = [3,4,5], "gtList([1,2,3,4,5],2) = [3,4,5]");

print "suffix\n";
raiseIfFalse(suffix([3,4,5],[1,2,3,4,5]) = true, "suffix([3,4,5],[1,2,3,4,5]) = true");

print "get\n";
raiseIfFalse(get([1,2,3,4,5],3) = 4, "get([1,2,3,4,5],3) = 4");

print "subList\n";
raiseIfFalse(subList([1,2,3,4,5],1,3) = [2,3,4], "subList([1,2,3,4,5],1,3) = [2,3,4]");

print "reverse\n";
raiseIfFalse(reverse([1,2,3,4]) = [4,3,2,1], "reverse([1,2,3,4]) = [4,3,2,1]");

print "apply\n";
raiseIfFalse(apply([1,2,3,4,5],(fn x => x + 2)) = [3,4,5,6,7], "apply([1,2,3,4,5],(fn x => x + 2)) = [3,4,5,6,7]");

print "collapse\n";
raiseIfFalse(collapse([1,2,3,4],5,(op +)) = 15, "collapse([1,2,3,4],5,(op +)) = 15");

print "quicksort\n";
raiseIfFalse(quicksort (op <=) [3,5,1,2,4] = [1,2,3,4,5], "quicksort (op <=) [3,5,1,2,4] = [1,2,3,4,5]");

print "bubbleSort\n";
raiseIfFalse(bubbleSort (op >=) [1,1,4,2,3] = [4,3,2,1,1], "bubbleSort (op >=) [1,1,4,2,3] = [4,3,2,1,1]");

print "insertionSort\n";
raiseIfFalse(insertionSort (op <=) [~4,1,8,9,0,~2] = [~4,~2,0,1,8,9], "insertionSort (op <=) [~4,1,8,9,0,~2] = [~4,~2,0,1,8,9]");

print "substring\n";
raiseIfFalse(substring "zon" "arizona" = true, "substring \"zon\" \"arizona\" = true");

print "indexOf\n";
raiseIfFalse(indexOf 3 [1,2,3,4,5] = 2, "indexOf 3 [1,2,3,4,5] = 2");

print "dec2BaseN\n";
raiseIfFalse(dec2BaseN 2 10 = "1010", "dec2BaseN 2 10 = \"1010\"");

print "dropNth\n";
raiseIfFalse(dropNth 2 [1,2,3,4,5] = [1,3,5], "dropNth 2 [1,2,3,4,5] = [1,3,5]");

print "flatten\n";
raiseIfFalse(flatten [[],[1,2,3],[4,5,6],[],[8,9]] = [1,2,3,4,5,6,8,9], "flatten [[],[1,2,3],[4,5,6],[],[8,9]] = [1,2,3,4,5,6,8,9]");

print "condenseLists\n";
raiseIfFalse(condenseLists (op +) 0 [[],[1,2,3,4],[5,6]] = [0,10,11], "condenseLists (op +) 0 [[],[1,2,3,4],[5,6]] = [0,10,11]");

print "remove\n";
raiseIfFalse(remove (fn x => x mod 3 = 0) [1,2,3,4,5,6,7,8,9] = [1,2,4,5,7,8], "remove (fn x => x mod 3 = 0) [1,2,3,4,5,6,7,8,9] = [1,2,4,5,7,8]");

print "triplist\n";
raiseIfFalse(triplist [1,2,3] = [1,1,1,2,2,2,3,3,3], "triplist [1,2,3] = [1,1,1,2,2,2,3,3,3]");

print "repeat\n";
raiseIfFalse(repeat [1,2] 4 = [1,2,1,2,1,2,1,2], "repeat [1,2] 4 = [1,2,1,2,1,2,1,2]");

print "filterApply\n";
raiseIfFalse(filterApply [1,2,3,4,5] (fn x => x mod 2 = 0) (fn x => x + 1) = [1,3,3,5,5], "filterApply [1,2,3,4,5] ... = [1,3,3,5,5]");

print "arithSeq\n";
raiseIfFalse(arithSeq 0 5 4 = [0,5,10,15], "arithSeq 0 5 4 = [0,5,10,15]");

print "element\n";
raiseIfFalse(element 5 [1,2,3,4,5,6,7,8] = true, "element 5 [1,2,3,4,5,6,7,8] = true");

print "isSet\n";
raiseIfFalse(isSet [1,2,3,4,5] = true, "isSet [1,2,3,4,5] = true");

print "union\n";
raiseIfFalse(union ([1,2,3],[2,3,4,5]) = [1,2,3,4,5], "union ([1,2,3],[2,3,4,5]) = [1,2,3,4,5]");

print "intersection\n";
raiseIfFalse(intersection ([1,2,3],[2,3,4,5]) = [2,3], "intersection ([1,2,3],[2,3,4,5]) = [2,3]");

print "difference\n";
raiseIfFalse(difference ([1,2,3],[2,3,4,5]) = [1], "difference ([1,2,3],[2,3,4,5]) = [1]");

print "xor\n";
raiseIfFalse(xor ([1,2,3],[2,3,4,5]) = [1,4,5], "xor ([1,2,3],[2,3,4,5]) = [1,4,5]");

val ps = powerset [0,1];
fun member (e,l) = foldl (fn (el,b) => e = el orelse b) false l;
member([],ps);
fun member (e,l) = foldl (fn (el,b) => e = el orelse b) false l;
raiseIfFalse(xor ([1,2,3],[2,3,4,5]) = [1,4,5], "xor ([1,2,3],[2,3,4,5]) = [1,4,5]");

val ps = powerset [0,1];
fun member (e,l) = foldl (fn (el,b) => e = el orelse b) false l;


(* member([],ps);
member([0],ps);
member([1],ps);
member([0,1],ps);
print "end\n";
print "end\n"; *)

raiseIfFalse(member([],ps) = true, "member([],ps) = true");
raiseIfFalse(member([0],ps) = true, "member([0],ps) = true");
raiseIfFalse(member([1],ps) = true, "member([1],ps) = true");
raiseIfFalse(member([0,1],ps) = true, "member([0,1],ps) = true");

print "end\n";raiseIfFalse(cycle(4,[1,2,3,4,5]) = [5,1,2,3,4], "cycle(4,[1,2,3,4,5]) = [5,1,2,3,4]");


File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 9b84558

Please sign in to comment.