plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


8ab9beb — Peter John 2026-11-10T22:04:49+05:30
tests/std: sort stability at every run and pass size, Str's Unicode edges
tests/std/map_model.plum CHANGED
@@ -1,7 +1,6 @@
1
1
  import std/Int
2
2
  import std/Float
3
3
  import std/Byte
4
- import std/Io
5
4
  import std/Str
6
5
  import std/Bool
7
6
  import std/Pair
tests/std/slice_sort.plum ADDED
@@ -0,0 +1,44 @@
1
+ import std/Int
2
+ import std/Str
3
+ import std/Pair
4
+ import std/Option
5
+ import std/Slice
6
+ import std/collections/List
7
+
8
+ # `n` pairs (key, position) with few distinct keys, so many keys repeat.
9
+ fun keyed(n: Int, kinds: Int) -> Slice<Pair<Int, Int>> =
10
+ out := Slice<Pair<Int, Int>>()
11
+ for i := range n
12
+ out.push(Pair(first: {i * 7919 + 13} % kinds, second: i))
13
+ out
14
+
15
+ # Sorted by key, equal keys in their original order, nothing lost or repeated.
16
+ fun stableSorted(xs: Slice<Pair<Int, Int>>, n: Int) -> Bool =
17
+ ordered := 0.until(xs.length() - 1).find(|i|
18
+ a := xs.at(i)
19
+ b := xs.at(i + 1)
20
+ a.first > b.first || a.first == b.first && a.second > b.second
21
+ ).isNone()
22
+ seen := Slice<Bool>()
23
+ for i := range n
24
+ seen.push(false)
25
+ for p := range xs
26
+ _ := seen.set(p.second, true)
27
+ ordered && xs.length() == n && seen.all(|s| s)
28
+
29
+ # Sizes around the insertion-sort runs of 16 and the merge passes (16, 32, 64, ..): each
30
+ # number of passes ends its merge in either array.
31
+ test "sort is stable and complete at every size: 0, 1, a run, a run and one, several passes, 100,003"
32
+ for n := range [0, 1, 2, 15, 16, 17, 31, 32, 33, 64, 65, 100, 1000, 4097, 100003]
33
+ for kinds := range [1, 3, 1000]
34
+ xs := keyed(n, kinds)
35
+ assert stableSorted(xs.sort(|a, b| a.first < b.first), n)
36
+ assert xs.length() == n && {n < 2 || xs.at(1).second == 1}
37
+
38
+ test "sort leaves the receiver and its views as they were, and sorts a view on its own"
39
+ xs := [5, 3, 9, 1, 7].toSlice()
40
+ v := xs.slice(1, 4)
41
+ assert v.sort(|a, b| a < b).join(",") == "1,3,9"
42
+ assert xs.sort(|a, b| a > b).join(",") == "9,7,5,3,1"
43
+ assert xs.join(",") == "5,3,9,1,7" && v.join(",") == "3,9,1"
44
+ assert Slice<Int>().sort(|a, b| a < b).isEmpty()
tests/std/str_unicode.plum ADDED
@@ -0,0 +1,29 @@
1
+ import std/Int
2
+ import std/Str
3
+ import std/Option
4
+
5
+ test "trim strips Unicode White_Space at both ends, and nothing else"
6
+ assert "   x
 \u0085".trim() == "x"
7
+ assert " x ".trimStart() == "x " && " x ".trimEnd() == " x"
8
+ assert "é ".trim() == "é" && " 世".trim() == "世" && " ".trim() == "" && "".trim() == ""
9
+ # U+200B (zero width space) is not White_Space; U+180E no longer is
10
+ assert "​x".trim() == "​x"
11
+ assert "\t\n\u000b\u000c\r x \r\n".trim() == "x"
12
+
13
+ test "truncate never cuts a character, and keeps the ellipsis inside the length"
14
+ assert "aéééé".truncate(5) == "a..." && "aéééé".truncate(6) == "aé..."
15
+ assert "hello".truncate(5) == "hello" && "hello!".truncate(5) == "he..." && "hello".truncate(2) == ".."
16
+ assert "世界世界".truncate(9) == "世界..." && "世界世界".truncate(8) == "世..."
17
+
18
+ test "runeAt counts characters, None past the end or below 0"
19
+ s := "aé世"
20
+ assert s.runeAt(0)! == "a" && s.runeAt(1)! == "é" && s.runeAt(2)! == "世"
21
+ assert s.runeAt(3).isNone() && s.runeAt(-1).isNone() && "".runeAt(0).isNone()
22
+
23
+ test "indexOf, concat and comparison work on substring views by their own bytes"
24
+ big := "xxabcabcxx"
25
+ v := big.sub(2, 8)
26
+ assert v.indexOf("abc")! == 0 && v.indexOf("cab")! == 2 && v.indexOf("x").isNone() && v.indexOf("abcabcx").isNone()
27
+ assert v.concat("!") == "abcabc!" && "".concat(v) == v && v.concat("") == "abcabc"
28
+ assert v < "abd" && v > "abc" && v >= "abcabc" && v <= "abcabc" && !{v < v}
29
+ assert "" < "a" && "a" < "aa" && "Z" < "a" && "é" > "z" && "世" > "é"