diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-09-21 17:44:08 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-09-21 17:44:08 -0400 |
| commit | c941b9a3325228eba404455afea7ccea0da45095 (patch) | |
| tree | d4ca88c6848ac2e6ceee635bb87add87ba6d2322 /test/lists.tm | |
| parent | 1cec086a6034ad546977cae7aeaf4bb876d21970 (diff) | |
Fix tests
Diffstat (limited to 'test/lists.tm')
| -rw-r--r-- | test/lists.tm | 157 |
1 files changed, 57 insertions, 100 deletions
diff --git a/test/lists.tm b/test/lists.tm index 2342c570..176c3d6d 100644 --- a/test/lists.tm +++ b/test/lists.tm @@ -1,106 +1,85 @@ func main() do - >> nums : [Num32] = [] - = [] + nums : [Num32] = [] + assert nums == [] do - >> nums : [Num32] - = [] + nums : [Num32] + assert nums == [] do - >> list := [10, 20, 30] - = [10, 20, 30] + list := [10, 20, 30] + assert list == [10, 20, 30] - >> list[1] - = 10 - >> list[-1] - = 30 + assert list[1] == 10 + assert list[-1] == 30 - >> list.length - = 3 + assert list.length == 3 sum := 0 for x in list sum += x - >> sum - = 60 + assert sum == 60 str := "" for i,x in list str ++= "($i,$x)" - >> str - = "(1,10)(2,20)(3,30)" + assert str == "(1,10)(2,20)(3,30)" do - >> list := [10, 20] ++ [30, 40] - = [10, 20, 30, 40] + list := [10, 20] ++ [30, 40] + assert list == [10, 20, 30, 40] >> list ++= [50, 60] - >> list - = [10, 20, 30, 40, 50, 60] + assert list == [10, 20, 30, 40, 50, 60] do >> list := [10, 20] >> copy := list >> list ++= [30] - >> list - = [10, 20, 30] - >> copy - = [10, 20] + assert list == [10, 20, 30] + assert copy == [10, 20] do - >> [10*i for i in 5] - = [10, 20, 30, 40, 50] + assert [10*i for i in 5] == [10, 20, 30, 40, 50] - >> [i*10 for i in 5] - = [10, 20, 30, 40, 50] + assert [i*10 for i in 5] == [10, 20, 30, 40, 50] - >> [i*10 for i in 5 if i mod 2 != 0] - = [10, 30, 50] + assert [i*10 for i in 5 if i mod 2 != 0] == [10, 30, 50] - >> [x for x in y if x > 1 for y in [3, 4, 5] if y < 5] - = [2, 3, 2, 3, 4] + assert [x for x in y if x > 1 for y in [3, 4, 5] if y < 5] == [2, 3, 2, 3, 4] do >> list := @[10, 20] >> copy := list[] >> list.insert(30) - >> list[] - = [10, 20, 30] - >> copy - = [10, 20] + assert list[] == [10, 20, 30] + assert copy == [10, 20] >> list[1] = 999 - >> list[] - = [999, 20, 30] + assert list[] == [999, 20, 30] do >> list := &[10, 20, 30] - >> reversed := list.reversed() - = [30, 20, 10] + reversed := list.reversed() + assert reversed == [30, 20, 10] # Ensure the copy-on-write behavior triggers: >> list[1] = 999 - >> reversed - = [30, 20, 10] + assert reversed == [30, 20, 10] do >> nums := @[10, -20, 30] # Sorted function doesn't mutate original: - >> nums.sorted() - = [-20, 10, 30] - >> nums[] - = [10, -20, 30] + assert nums.sorted() == [-20, 10, 30] + assert nums[] == [10, -20, 30] # Sort function does mutate in place: >> nums.sort() - >> nums[] - = [-20, 10, 30] + assert nums[] == [-20, 10, 30] # Custom sort functions: >> nums.sort(func(x,y:&Int) x.abs() <> y.abs()) - >> nums[] - = [10, -20, 30] + assert nums[] == [10, -20, 30] >> nums.sort(func(x,y:&Int) y[] <> x[]) - >> nums[] - = [30, 10, -20] + assert nums[] == [30, 10, -20] >> ["A", "B", "C"].sample(10, [1.0, 0.5, 0.0]) @@ -121,30 +100,20 @@ func main() assert heap_order[] == heap_order.sorted() do - >> [i*10 for i in 5].from(3) - = [30, 40, 50] - >> [i*10 for i in 5].to(3) - = [10, 20, 30] - >> [i*10 for i in 5].to(-2) - = [10, 20, 30, 40] - >> [i*10 for i in 5].from(-2) - = [40, 50] - - >> [i*10 for i in 5].by(2) - = [10, 30, 50] - >> [i*10 for i in 5].by(-1) - = [50, 40, 30, 20, 10] - - >> [10, 20, 30, 40].by(2) - = [10, 30] - >> [10, 20, 30, 40].by(-2) - = [40, 20] - - >> [i*10 for i in 10].by(2).by(2) - = [10, 50, 90] - - >> [i*10 for i in 10].by(2).by(-1) - = [90, 70, 50, 30, 10] + assert [i*10 for i in 5].from(3) == [30, 40, 50] + assert [i*10 for i in 5].to(3) == [10, 20, 30] + assert [i*10 for i in 5].to(-2) == [10, 20, 30, 40] + assert [i*10 for i in 5].from(-2) == [40, 50] + + assert [i*10 for i in 5].by(2) == [10, 30, 50] + assert [i*10 for i in 5].by(-1) == [50, 40, 30, 20, 10] + + assert [10, 20, 30, 40].by(2) == [10, 30] + assert [10, 20, 30, 40].by(-2) == [40, 20] + + assert [i*10 for i in 10].by(2).by(2) == [10, 50, 90] + + assert [i*10 for i in 10].by(2).by(-1) == [90, 70, 50, 30, 10] # Test iterating over list.from() and list.to() xs := ["A", "B", "C", "D"] @@ -155,36 +124,24 @@ func main() do >> nums := @[-7, -4, -1, 2, 5] >> nums.sort() - >> [nums.binary_search(i) for i in nums[]] - = [1, 2, 3, 4, 5] + assert [nums.binary_search(i) for i in nums[]] == [1, 2, 3, 4, 5] >> nums.sort(func(a,b:&Int) a.abs() <> b.abs()) - >> [nums.binary_search(i, func(a,b:&Int) a.abs() <> b.abs()) for i in nums[]] - = [1, 2, 3, 4, 5] + assert [nums.binary_search(i, func(a,b:&Int) a.abs() <> b.abs()) for i in nums[]] == [1, 2, 3, 4, 5] - >> ["a", "b", "c"].find("b") - = 2 - >> ["a", "b", "c"].find("XXX") - = none + assert ["a", "b", "c"].find("b") == 2 + assert ["a", "b", "c"].find("XXX") == none - >> [10, 20].where(func(i:&Int) i.is_prime()) - = none - >> [4, 5, 6].where(func(i:&Int) i.is_prime()) - = 2 + assert [10, 20].where(func(i:&Int) i.is_prime()) == none + assert [4, 5, 6].where(func(i:&Int) i.is_prime()) == 2 do >> nums := &[10, 20, 30, 40, 50] - >> nums.pop() - = 50 - >> nums[] - = [10, 20, 30, 40] - >> nums.pop(2) - = 20 - >> nums[] - = [10, 30, 40] + assert nums.pop() == 50 + assert nums[] == [10, 20, 30, 40] + assert nums.pop(2) == 20 + assert nums[] == [10, 30, 40] >> nums.clear() - >> nums[] - = [] - >> nums.pop() - = none + assert nums[] == [] + assert nums.pop() == none assert [1,2,1,2,3].unique() == [1,2,3] |
