plum

#treesitter#compiler#wasm

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

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


2ceb998 — Peter John 2026-11-10T22:01:10+05:30
Map: a model test of every operation; the million-entry test takes 1 s, not 8
Files changed (2) hide show
  1. std/collections/Map.plum +13 -9
  2. tests/std/map_model.plum +159 -0
std/collections/Map.plum CHANGED
@@ -1070,17 +1070,21 @@ test "10,000 ascending keys stay balanced"
1070
1070
  assert u.length() == 5000 && mapValid(u) && u.min()!.first == 1 && u.indexOf(5001)! == 2500
1071
1071
  assert t.length() == 10000 && t.min()!.first == 0
1072
1072
 
1073
- test "1,000,000 inserts, lookups, walks and removals in constant stack"
1073
+ test "1,000,000 entries: built, read, walked and emptied in constant stack"
1074
+ # Every walk is O(log n) deep, so a million entries only test that the loops around them
1075
+ # are tail calls; 200,000 inserts and removals one by one cover the same paths for less.
1074
- t := mapRange(Map.empty(), 0, 1000000)
1076
+ sorted := Slice<Pair<Int, Int>>()
1075
- assert t.length() == 1000000
1077
+ for i := range 1000000
1076
- assert t.height() <= 2 * 20 + 2
1078
+ sorted.push(Pair(first: i, second: i * 2))
1079
+ t := Map.fromSlice(sorted)
1080
+ assert t.length() == 1000000 && t.height() <= 2 * 20 + 2 && mapCheckedSize(t) == 1000000
1077
1081
  assert mapBadFrom(t, 0, 1000000, 0) == 0
1078
1082
  assert t.fold(0, |acc, k, v| acc + v) == 999999000000
1079
- assert t.entries().length() == 1000000
1083
+ assert t.entries().length() == 1000000 && t.max()!.first == 999999
1080
- assert mapCheckedSize(t) == 1000000
1084
+ assert t.range(250000, 750000).length() == 500000 && t.filter(|k, v| k % 4 == 0).length() == 250000
1081
- u := mapRemoveFrom(t, 0, 1000000, 1)
1085
+ w := mapRange(Map.empty(), 0, 200000)
1082
- assert u.isEmpty()
1086
+ assert w.length() == 200000 && w.height() <= 2 * 18 + 2 && w == t.until(200000)
1083
- assert t.max()!.first == 999999
1087
+ assert mapRemoveFrom(w, 0, 200000, 1).isEmpty() && w.length() == 200000
1084
1088
 
1085
1089
  test "the invariant checker rejects wrong sizes, unbalanced nodes and unordered keys"
1086
1090
  e: Map<Int, Int> := MapTip
tests/std/map_model.plum ADDED
@@ -0,0 +1,159 @@
1
+ import std/Int
2
+ import std/Float
3
+ import std/Byte
4
+ import std/Io
5
+ import std/Str
6
+ import std/Bool
7
+ import std/Pair
8
+ import std/Option
9
+ import std/Slice
10
+ import std/collections/List
11
+ import std/collections/Map
12
+ import std/collections/Set
13
+ import std/math/Random
14
+
15
+ # Every Map operation against a model, on 150 seeded random pairs of maps (built three ways:
16
+ # fromSlice on unsorted keys with duplicates, fromSlice on sorted unique keys, and one with
17
+ # at a time): after each operation the result must be a valid weight-balanced tree
18
+ # (map.mapValid) holding exactly the model's entries, and the inputs must be unchanged.
19
+ # The model of a map is its entries read back key by key over the whole key range.
20
+ fun modelStr(xs: Slice<Pair<Int, Int>>) -> Str =
21
+ "[{xs.map(|e| "{e.first} => {e.second}").join(", ")}]"
22
+
23
+ fun sortedModel(m: Map<Int, Int>, b: Int, keep: fn(Int) -> Bool) -> Str =
24
+ out := Slice<Pair<Int, Int>>()
25
+ for k := range {0 - 20}.until(b + 20)
26
+ if keep(k) && m.has(k)
27
+ out.push(Pair(first: k, second: m.get(k)!))
28
+ modelStr(out)
29
+
30
+ struct St(mut rng: Random, mut bad: Int, mut log: Slice<Str>)
31
+
32
+ fun draw(st: St, n: Int) -> Int =
33
+ r := st.rng.nextIntBelow(n)
34
+ st.rng = r.rng
35
+ r.value
36
+
37
+ fun randMap(st: St, count: Int, bound: Int, tag: Int) -> Map<Int, Int> =
38
+ kind := draw(st, 3)
39
+ xs := Slice<Pair<Int, Int>>()
40
+ for i := range count
41
+ k := draw(st, bound)
42
+ xs.push(Pair(first: k, second: k * 10 + tag))
43
+ if kind == 0
44
+ Map.fromSlice(xs)
45
+ else if kind == 1
46
+ sorted := xs.sort(|a, b| a.first < b.first)
47
+ ded := Slice<Pair<Int, Int>>()
48
+ for e := range sorted
49
+ if ded.isEmpty() || ded.last()!.first != e.first
50
+ ded.push(e)
51
+ Map.fromSlice(ded)
52
+ else
53
+ xs.fold(Map<Int, Int>(), |m, e| m.with(e.first, e.second))
54
+
55
+ fun check(st: St, what: Str, m: Map<Int, Int>, expect: Str) =
56
+ if !map.mapValid(m)
57
+ st.bad = st.bad + 1
58
+ st.log.push("invalid {what}")
59
+ if m.toStr() != expect
60
+ st.bad = st.bad + 1
61
+ st.log.push("wrong {what}: {m.toStr()} vs {expect}")
62
+
63
+ test "every operation keeps the tree valid and agrees with the model; inputs stay unchanged"
64
+ st := St(rng: Random.seeded(77), bad: 0, log: Slice<Str>())
65
+ bnd := 200
66
+ for round := range 150
67
+ n1 := draw(st, 120)
68
+ n2 := draw(st, 120)
69
+ a := randMap(st, n1, bnd, 1)
70
+ b := randMap(st, n2, bnd, 2)
71
+ aS := a.toStr()
72
+ bS := b.toStr()
73
+ check(st, "a", a, sortedModel(a, bnd, |k| true))
74
+ k := draw(st, bnd + 20) - 10
75
+ k2 := draw(st, bnd + 20) - 10
76
+ check(st, "with", a.with(k, -1), sortedModel(a.with(k, -1), bnd, |q| true))
77
+ w := a.with(k, -1)
78
+ if w.getOr(k, 0) != -1 || w.length() != a.length() + {if a.has(k) then 0 else 1}
79
+ st.bad = st.bad + 1
80
+ check(st, "remove", a.remove(k), sortedModel(a, bnd, |q| q != k))
81
+ check(st, "union", a.union(b), sortedModel(a.union(b), bnd, |q| a.has(q) || b.has(q)))
82
+ if !a.union(b).all(|q, v| v == a.getOr(q, b.getOr(q, -99)))
83
+ st.bad = st.bad + 1
84
+ st.log.push("union values")
85
+ uw := a.unionWith(b, |x, y| x * 1000 + y)
86
+ if !map.mapValid(uw) || uw.length() != a.union(b).length() || !uw.all(|q, v| v == {if a.has(q) && b.has(q) then a.get(q)! * 1000 + b.get(q)! else a.getOr(q, b.getOr(q, -99))})
87
+ st.bad = st.bad + 1
88
+ st.log.push("unionWith")
89
+ check(st, "intersection", a.intersection(b), sortedModel(a, bnd, |q| b.has(q)))
90
+ check(st, "difference", a.difference(b), sortedModel(a, bnd, |q| !b.has(q)))
91
+ p := a.split(k)
92
+ check(st, "split.first", p.first, sortedModel(a, bnd, |q| q < k))
93
+ check(st, "split.second", p.second, sortedModel(a, bnd, |q| q > k))
94
+ check(st, "join", p.first.join(p.second), sortedModel(a, bnd, |q| q != k))
95
+ check(st, "from", a.from(k), sortedModel(a, bnd, |q| q >= k))
96
+ check(st, "until", a.until(k), sortedModel(a, bnd, |q| q < k))
97
+ check(st, "range", a.range(k, k2), sortedModel(a, bnd, |q| q >= k && q < k2))
98
+ if a.foldRange(k, k2, 0, |acc, q, v| acc * 7 + q + v) != a.range(k, k2).fold(0, |acc, q, v| acc * 7 + q + v)
99
+ st.bad = st.bad + 1
100
+ st.log.push("foldRange")
101
+ ks := a.keys()
102
+ if a.rank(k) != ks.filter(|q| q < k).length()
103
+ st.bad = st.bad + 1
104
+ st.log.push("rank")
105
+ i := draw(st, n1 + 3) - 1
106
+ e := a.entryAt(i)
107
+ okE := if i < 0 || i >= ks.length() then e.isNone() else e.isSome() && e!.first == ks.at(i)
108
+ if !okE
109
+ st.bad = st.bad + 1
110
+ st.log.push("entryAt {i}")
111
+ idx := a.indexOf(k)
112
+ okI := match ks.indexOf(k)
113
+ None => idx.isNone()
114
+ Some(j) => idx.isSome() && idx! == j
115
+ if !okI
116
+ st.bad = st.bad + 1
117
+ st.log.push("indexOf")
118
+ check(st, "removeMin", a.removeMin(), sortedModel(a, bnd, |q| a.min().isNone() || q != a.min()!.first))
119
+ check(st, "removeMax", a.removeMax(), sortedModel(a, bnd, |q| a.max().isNone() || q != a.max()!.first))
120
+ check(st, "filter", a.filter(|q, v| q % 3 == 0), sortedModel(a, bnd, |q| q % 3 == 0))
121
+ check(st, "alter", a.alter(k, |o| None), sortedModel(a, bnd, |q| q != k))
122
+ # a chain of removes keeps validity at every step
123
+ c := ks.fold(a, |m, q|
124
+ if q % 2 == round % 2
125
+ nm := m.remove(q)
126
+ if !map.mapValid(nm)
127
+ st.bad = st.bad + 1
128
+ st.log.push("remove chain")
129
+ nm
130
+ else
131
+ m
132
+ )
133
+ check(st, "remove chain end", c, sortedModel(a, bnd, |q| q % 2 != round % 2))
134
+ # persistence
135
+ if a.toStr() != aS || b.toStr() != bS
136
+ st.bad = st.bad + 1
137
+ st.log.push("persistence broken")
138
+ # Set mirrors
139
+ sa := Set.fromSlice(ks)
140
+ sb := Set.fromSlice(b.keys())
141
+ if sa.union(sb).toSlice().toStr() != a.union(b).keys().toStr() || sa.difference(sb).toSlice().toStr() != a.difference(b).keys().toStr() || sa.intersection(sb).toSlice().toStr() != a.intersection(b).keys().toStr()
142
+ st.bad = st.bad + 1
143
+ st.log.push("set ops")
144
+ assert st.log.slice(0, st.log.length().min(2)).join("; ") == ""
145
+ assert st.bad == 0
146
+
147
+ test "compare on every std key type: NaN, -0, prefixes, the empty string, UTF-8, Bool, Byte, Int extremes"
148
+ nan := 0.0 / 0.0
149
+ assert nan.compare(nan) == 0 && nan.compare(1.0 / 0.0) == 1 && {1.0 / 0.0}.compare(nan) == -1
150
+ assert {-0.0}.compare(0.0) == 0 && 0.0.compare(-0.0) == 0 && {-1.0}.compare(nan) == -1 && nan.compare(-1.0 / 0.0) == 1
151
+ assert "".compare("") == 0 && "".compare("a") == -1 && "a".compare("") == 1
152
+ assert "ab".compare("abc") == -1 && "abc".compare("ab") == 1 && "b".compare("ab") == 1
153
+ assert "é".compare("z") == 1 && "z".compare("é") == -1 && "€".compare("𝄞") == -1
154
+ assert "a" < "b" && "a" <= "a" && "b" > "a" && "b" >= "b" && !{"b" < "a"}
155
+ sub := "xabcx".slice(1, 4)
156
+ assert sub.compare("abc") == 0 && sub.compare("abd") == -1 && sub.compare("abcx") == -1
157
+ assert false.compare(true) == -1 && true.compare(false) == 1 && true.compare(true) == 0
158
+ assert Byte(255).compare(Byte(0)) == 1 && Byte(0).compare(Byte(255)) == -1 && Byte(7).compare(Byte(7)) == 0
159
+ assert Int.Min.compare(Int.Max) == -1 && Int.Max.compare(Int.Min) == 1 && Int.Min.compare(Int.Min) == 0