Skip to content

Commit bbd48b4

Browse files
Add tests for functions 19-32 LA01
1 parent d008afa commit bbd48b4

35 files changed

+1069
-46
lines changed

src/large_assignment_01/large_assignment_01.sml

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ fun triangle(0, _, _) = false
3333
fun triangleR(a, b, c) =
3434
let
3535
val epsilon = 1.0E~10
36-
fun is_zero(x) = abs(x) < epsilon
37-
fun sum_geq_to(a, b, to) = a + b >= to
36+
fun isZero(x) = abs(x) < epsilon
37+
fun sumGeqTo(a, b, to) = a + b >= to
3838
in
39-
not(is_zero(a)) andalso not(is_zero(b)) andalso not(is_zero(c)) andalso
40-
sum_geq_to(a, b, c) andalso sum_geq_to(a, c, b) andalso sum_geq_to(b, c, a)
39+
not(isZero(a)) andalso not(isZero(b)) andalso not(isZero(c)) andalso
40+
sumGeqTo(a, b, c) andalso sumGeqTo(a, c, b) andalso sumGeqTo(b, c, a)
4141
end;
4242

4343

@@ -71,8 +71,8 @@ fun triangleR(a, b, c) =
7171
* contains all the elements in l that are greater than
7272
* n. Keep the same relative order of items.
7373
*)
74-
fun gtList(_, []) = []
75-
| gtList(n, x::li) = if x > n then x::gtList(n, li) else gtList(n, li);
74+
fun gtList([], _) = []
75+
| gtList(x::li, n) = if x > n then x::gtList(li, n) else gtList(li, n);
7676

7777

7878
(*
@@ -316,6 +316,7 @@ fun dropNth _ [] = []
316316
else subList(li, 0, n - 2) @ dropNth n (subList(li, n, liLen - 1))
317317
end;
318318

319+
319320
(*
320321
* Type: `'a list list → 'a list`
321322
* Desc: Takes a list of lists and flattens it so that it is
@@ -335,7 +336,7 @@ fun flatten li = foldr (fn(cur, acc) => cur @ acc) [] li;
335336
* list of the results for each inner list. This should
336337
* be a one-liner using map, foldr, and/or foldl.
337338
*)
338-
fun condenseLists f startVal li = foldl (fn(cur, acc) => f(cur, acc)) startVal li;
339+
fun condenseLists f startVal li = foldr (fn(cur, acc) => collapse(cur, startVal, f)::acc) [] li;
339340

340341

341342
(*
@@ -360,8 +361,8 @@ fun triplist li = foldr (fn(cur, acc) => cur::cur::cur::acc) [] li;
360361
* Desc: Take a list l and an integer n and create a list that
361362
* repeats l n times.
362363
*)
363-
fun repeat _ 0 = []
364-
| repeat li n = li @ repeat(li, n - 1);
364+
fun repeat li 0 = []
365+
| repeat li n = li @ repeat li (n - 1);
365366

366367

367368
(*
@@ -371,7 +372,7 @@ fun repeat _ 0 = []
371372
* which f is true have g applied to them. You should do
372373
* this in one line with map, foldr, and/or foldl.
373374
*)
374-
fun filterApply li f g = foldl (fn(cur, acc) => if f(cur) then g(cur)::acc else cur::acc) [] li;
375+
fun filterApply li f g = foldr (fn(cur, acc) => if f(cur) then g(cur)::acc else cur::acc) [] li;
375376

376377

377378
(*
@@ -402,17 +403,7 @@ fun element x [] = false
402403
* otherwise.
403404
*)
404405
fun isSet([]) = true
405-
| isSet(x::li) =
406-
let
407-
fun countOccurences(target, []) = 0
408-
| countOccurences(target, x::li) =
409-
if target = x
410-
then 1 + countOccurences(target, li)
411-
else countOccurences(target, li)
412-
in
413-
if countOccurences(x, li) > 0 then false
414-
else isSet(li)
415-
end;
406+
| isSet(x::li) = if indexOf x li = ~1 then isSet(li) else false
416407

417408

418409
(*
@@ -421,10 +412,10 @@ fun isSet([]) = true
421412
* sets, to produce the union of the two sets. The
422413
* resulting order of the elements does not matter.
423414
*)
424-
fun union([], []) = [];
425-
| union(x::li1, []) = x::li1
426-
| union([], y::li2) = y::li2
427-
| union(x::li1, y::li2) = if element x li2 then union(li1, y::li2) else x::union(li1, y::li2)
415+
fun union([], []) = []
416+
| union(li1, []) = li1
417+
| union([], li2) = li2
418+
| union(x::li1, li2) = if element x li2 then union(li1, li2) else x::union(li1, li2);
428419

429420

430421
(*

tests/tests-large_assignment_01/test_apply.sml renamed to src/sml_practice/staging/test_apply.sml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ val testCasesApplyInt = [
99

1010
(* 2. Single-element list *)
1111
(apply, ([2], fn x => x * 2), [4]),
12+
13+
(apply, ([1,2,3,4,5], fn x => x + 2), [3,4,5,6,7]),
1214

1315
(* 3. List of integers with addition function *)
1416
(apply, ([1, 2, 3], fn x => x + 1), [2, 3, 4])
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
(* Large Assignment 01 Tests *)
2+
3+
use "src/large_assignment_01/large_assignment_01.sml";
4+
use "tests/utils.sml";
5+
6+
(* Test cases for arithSeq *)
7+
val testCasesArithSeq = [
8+
(* 1. Basic arithmetic sequence starting at 1 with a difference of 2 *)
9+
(fn (a, d, l) => arithSeq a d l, (1, 2, 5), [1, 3, 5, 7, 9]),
10+
11+
(* 2. Arithmetic sequence with a negative difference *)
12+
(fn (a, d, l) => arithSeq a d l, (10, ~2, 5), [10, 8, 6, 4, 2]),
13+
14+
(* 3. Sequence with a difference of 0 (all elements are the same) *)
15+
(fn (a, d, l) => arithSeq a d l, (5, 0, 4), [5, 5, 5, 5]),
16+
17+
(* 4. Sequence of length 1 *)
18+
(fn (a, d, l) => arithSeq a d l, (7, 3, 1), [7]),
19+
20+
(* 5. Empty sequence (length 0) *)
21+
(fn (a, d, l) => arithSeq a d l, (1, 1, 0), []),
22+
23+
(* 6. Sequence with a starting value of 0 and a positive difference *)
24+
(fn (a, d, l) => arithSeq a d l, (0, 4, 3), [0, 4, 8]),
25+
26+
(* 7. Sequence with a negative starting value *)
27+
(fn (a, d, l) => arithSeq a d l, (~3, 2, 4), [~3, ~1, 1, 3]),
28+
29+
(* 8. Large sequence to test performance *)
30+
(fn (a, d, l) => arithSeq a d l, (1, 1, 100), List.tabulate(100, fn i => i + 1)),
31+
32+
(fn (a, d, l) => arithSeq a d l, (0, 5, 4), [0,5,10,15]),
33+
34+
(* 9. Sequence where the common difference is larger than the starting value *)
35+
(fn (a, d, l) => arithSeq a d l, (2, 10, 3), [2, 12, 22]),
36+
37+
(* 10. Sequence with a large negative difference *)
38+
(fn (a, d, l) => arithSeq a d l, (100, ~20, 4), [100, 80, 60, 40]),
39+
40+
(* 11. Sequence with large positive starting value and difference *)
41+
(fn (a, d, l) => arithSeq a d l, (1000, 500, 3), [1000, 1500, 2000]),
42+
43+
(* 12. Edge case: Sequence with starting value and common difference both 0 *)
44+
(fn (a, d, l) => arithSeq a d l, (0, 0, 5), [0, 0, 0, 0, 0]),
45+
46+
(* 13. Large sequence with a mix of positive and negative terms *)
47+
(fn (a, d, l) => arithSeq a d l, (50, ~5, 10), [50, 45, 40, 35, 30, 25, 20, 15, 10, 5]),
48+
49+
(* 14. Common difference larger than sequence length *)
50+
(fn (a, d, l) => arithSeq a d l, (1, 10, 2), [1, 11]),
51+
52+
(* 15. Large sequence with only decreasing values *)
53+
(fn (a, d, l) => arithSeq a d l, (500, ~50, 5), [500, 450, 400, 350, 300])
54+
];
55+
56+
runTestsCustomComparator(
57+
testCasesArithSeq,
58+
fn (a, d, l) => "arithSeq(" ^ Int.toString a ^ ", " ^ Int.toString d ^ ", " ^ Int.toString l ^ ")",
59+
valueToStringIntList,
60+
isEqualList
61+
);

tests/tests-large_assignment_01/test_bubbleSort.sml renamed to src/sml_practice/staging/test_bubbleSort.sml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ val testCasesBubbleSortInt = [
1010
(fn li => bubbleSort (fn (a, b) => a < b) li, [5, 4, 3, 2, 1], [1, 2, 3, 4, 5]),
1111
(fn li => bubbleSort (fn (a, b) => a < b) li, [3, 1, 4, 1, 5], [1, 1, 3, 4, 5]),
1212
(fn li => bubbleSort (fn (a, b) => a > b) li, [1, 2, 3, 4, 5], [5, 4, 3, 2, 1]),
13+
14+
(fn li => bubbleSort (op >=) li, [1,1,4,2,3], [4,3,2,1,1]),
15+
1316
(fn li => bubbleSort (fn (a, b) => a < b) li, [3, ~1, ~2, 5, 0], [~2, ~1, 0, 3, 5]),
1417
(fn li => bubbleSort (fn (a, b) => a < b) li, [], []),
1518
(fn li => bubbleSort (fn (a, b) => a < b) li, [1, 1, 1, 1], [1, 1, 1, 1])

tests/tests-large_assignment_01/test_collapse.sml renamed to src/sml_practice/staging/test_collapse.sml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ val testCasesCollapseInt = [
1515

1616
(* 4. Product of multiple elements *)
1717
(collapse, ([1, 2, 3, 4], 1, op * ), 24),
18+
19+
(collapse, ([1,2,3,4], 5, op + ), 15),
1820

1921
(* 5. List with negative numbers (sum) *)
2022
(collapse, ([~1, ~2, ~3], 0, op +), ~6),
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(* Large Assignment 01 Tests *)
2+
3+
use "src/large_assignment_01/large_assignment_01.sml";
4+
use "tests/utils.sml";
5+
6+
(* Test cases for integer lists *)
7+
val testCasesCondenseListsInt = [
8+
(* 1. Condense with addition (sum) *)
9+
(fn li => condenseLists (op +) 0 li, [[1, 2], [3, 4], [5, 6]], [3, 7, 11]),
10+
11+
(* 2. Condense with multiplication *)
12+
(fn li => condenseLists (op * ) 1 li, [[1, 2], [3, 4], [5, 6]], [2, 12, 30]),
13+
14+
(* 3. Condense with addition and empty lists *)
15+
(fn li => condenseLists (op +) 0 li, [[], [1, 2, 3], []], [0, 6, 0]),
16+
17+
(fn li => condenseLists (op +) 0 li, [[], [1, 2, 3, 4], [5,6]], [0, 10, 11]),
18+
19+
(* 4. Condense single-element lists *)
20+
(fn li => condenseLists (op +) 0 li, [[1], [2], [3]], [1, 2, 3]),
21+
22+
(* 5. Condense with multiplication on nested lists *)
23+
(fn li => condenseLists (op * ) 1 li, [[2], [3, 3], [4, 5]], [2, 9, 20])
24+
];
25+
26+
runTestsCustomComparator(
27+
testCasesCondenseListsInt,
28+
fn li => "condenseLists (op +) 0 " ^ valueToStringIntListList li,
29+
valueToStringIntList,
30+
isEqualList
31+
);
32+
33+
(* Test cases for string lists *)
34+
val testCasesCondenseListsString = [
35+
(* 1. Condense with string concatenation *)
36+
(fn li => condenseLists (op ^) "" li, [["a"], ["b", "c"], ["d"]], ["a", "bc", "d"]),
37+
38+
(* 2. Concatenate with separator *)
39+
(fn li => condenseLists (fn (x, acc) => x ^ ", " ^ acc) "" li, [["a"], ["b", "c"], ["d"]], ["a, ", "b, c, ", "d, "]),
40+
41+
(* 3. Condense with empty strings *)
42+
(fn li => condenseLists (op ^) "" li, [[""], ["a"], ["b", ""]], ["", "a", "b"]),
43+
44+
(* 4. Condense a single-element list *)
45+
(fn li => condenseLists (op ^) "" li, [["hello"]], ["hello"])
46+
];
47+
48+
runTestsCustomComparator(
49+
testCasesCondenseListsString,
50+
fn li => "condenseLists (op ^) \"\" " ^ valueToStringStringListList li,
51+
valueToStringStringList,
52+
isEqualList
53+
);

tests/tests-large_assignment_01/test_cycle.sml renamed to src/sml_practice/staging/test_cycle.sml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ val testCycle = [
99

1010
(* 2. Cycling by 0 should return the list unchanged *)
1111
(cycle, (0, [1,2,3,4,5,6,7]), [1,2,3,4,5,6,7]),
12+
13+
(cycle, (4, [1,2,3,4,5]), [5,1,2,3,4]),
1214

1315
(* 3. Cycling by 1 should move the first element to the end *)
1416
(cycle, (1, [1,2,3,4,5]), [2,3,4,5,1]),

tests/tests-large_assignment_01/test_dec2BaseN.sml renamed to src/sml_practice/staging/test_dec2BaseN.sml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ val testCasesToBaseN = [
1717

1818
(* 3. Convert a number to binary (base 2) *)
1919
(fn (base, num) => dec2BaseN base num, (2, 5), "101"),
20+
21+
(fn (base, num) => dec2BaseN base num, (2, 12), "1100"),
22+
2023
(fn (base, num) => dec2BaseN base num, (2, 13), "1101"),
2124
(fn (num, base) => dec2BaseN num base, (2, 10), "1010"),
2225

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
(* Large Assignment 01 Tests *)
2+
3+
use "src/large_assignment_01/large_assignment_01.sml";
4+
use "tests/utils.sml";
5+
6+
(* Helper function to compare lists as sets (order doesn't matter) *)
7+
fun isEqualSet([], []) = true
8+
| isEqualSet(x::xs, ys) = List.exists (fn y => y = x) ys
9+
andalso isEqualSet(xs, List.filter (fn y => y <> x) ys)
10+
| isEqualSet(_, _) = false;
11+
12+
(* Test cases for integer lists *)
13+
val testCasesDifferenceInt = [
14+
(* 1. Difference of two disjoint sets (should return the first set) *)
15+
(fn (li1, li2) => difference(li1, li2), ([1, 2], [3, 4]), [1, 2]),
16+
17+
(* 2. Difference of two sets with some common elements *)
18+
(fn (li1, li2) => difference(li1, li2), ([1, 2, 3], [2, 3, 4]), [1]),
19+
20+
(* 3. Difference when the first set is empty *)
21+
(fn (li1, li2) => difference(li1, li2), ([], [1, 2, 3]), []),
22+
23+
(fn (li1, li2) => difference(li1, li2), ([1, 2, 3], [2, 3, 4, 5]), [1]),
24+
25+
(* 4. Difference when the second set is empty *)
26+
(fn (li1, li2) => difference(li1, li2), ([1, 2, 3], []), [1, 2, 3]),
27+
28+
(* 5. Difference of two identical sets (should return empty) *)
29+
(fn (li1, li2) => difference(li1, li2), ([1, 2, 3], [1, 2, 3]), []),
30+
31+
(* 6. Difference with negative numbers *)
32+
(fn (li1, li2) => difference(li1, li2), ([~1, ~2, 3], [3]), [~1, ~2]),
33+
34+
(* 7. List 1 longer by more than 3 elements *)
35+
(fn (li1, li2) => difference(li1, li2), ([1, 2, 3, 4, 5, 6], [2, 4]), [1, 3, 5, 6]),
36+
37+
(* 8. List 2 longer by more than 3 elements *)
38+
(fn (li1, li2) => difference(li1, li2), ([1, 2], [2, 3, 4, 5, 6]), [1]),
39+
40+
(* 9. List 1 longer by 1 element *)
41+
(fn (li1, li2) => difference(li1, li2), ([1, 2, 3], [2, 3]), [1]),
42+
43+
(* 10. List 2 longer by 1 element *)
44+
(fn (li1, li2) => difference(li1, li2), ([2, 3], [1, 2, 3]), [])
45+
];
46+
47+
runTestsCustomComparator(
48+
testCasesDifferenceInt,
49+
fn (li1, li2) => "difference(" ^ valueToStringIntList li1 ^ ", " ^ valueToStringIntList li2 ^ ")",
50+
valueToStringIntList,
51+
isEqualSet
52+
);
53+
54+
(* Test cases for string lists *)
55+
val testCasesDifferenceString = [
56+
(* 1. Difference of two disjoint sets of strings (should return the first set) *)
57+
(fn (li1, li2) => difference(li1, li2), (["a", "b"], ["c", "d"]), ["a", "b"]),
58+
59+
(* 2. Difference of two sets with some common strings *)
60+
(fn (li1, li2) => difference(li1, li2), (["a", "b", "c"], ["b", "c", "d"]), ["a"]),
61+
62+
(* 3. Difference when the first set is empty *)
63+
(fn (li1, li2) => difference(li1, li2), ([], ["a", "b", "c"]), []),
64+
65+
(* 4. Difference when the second set is empty *)
66+
(fn (li1, li2) => difference(li1, li2), (["a", "b", "c"], []), ["a", "b", "c"]),
67+
68+
(* 5. Difference of two identical sets of strings (should return empty) *)
69+
(fn (li1, li2) => difference(li1, li2), (["a", "b", "c"], ["a", "b", "c"]), []),
70+
71+
(* 6. Difference with one common element *)
72+
(fn (li1, li2) => difference(li1, li2), (["x", "y"], ["y", "z"]), ["x"]),
73+
74+
(* 7. List 1 longer by more than 3 elements *)
75+
(fn (li1, li2) => difference(li1, li2), (["a", "b", "c", "d", "e", "f"], ["c", "e"]), ["a", "b", "d", "f"]),
76+
77+
(* 8. List 2 longer by more than 3 elements *)
78+
(fn (li1, li2) => difference(li1, li2), (["a", "b"], ["b", "c", "d", "e", "f"]), ["a"]),
79+
80+
(* 9. Difference with repeated strings (sets are assumed) *)
81+
(fn (li1, li2) => difference(li1, li2), (["foo"], ["foo"]), [])
82+
];
83+
84+
runTestsCustomComparator(
85+
testCasesDifferenceString,
86+
fn (li1, li2) => "difference(" ^ valueToStringStringList li1 ^ ", " ^ valueToStringStringList li2 ^ ")",
87+
valueToStringStringList,
88+
isEqualSet
89+
);

tests/tests-large_assignment_01/test_dropNth.sml renamed to src/sml_practice/staging/test_dropNth.sml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ val testCasesDropNthInt = [
1616

1717
(* 4. Drop every nth element where n > length of list (should return the same list) *)
1818
(fn (n, li) => dropNth n li, (10, [1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]),
19+
20+
(fn (n, li) => dropNth n li, (2, [1, 2, 3, 4, 5]), [1, 3, 5]),
1921

2022
(* 5. Drop every nth element for an empty list *)
2123
(fn (n, li) => dropNth n li, (3, []), []),

0 commit comments

Comments
 (0)