sketch_hash_map_test.nx source
↩ module page · 135 lines · 4660 B
1// sketch_hash_map_test.nx -- hash map foundational primitive verification.
2
3import "syscalls.nx"
4import "sketch_hash_map.nx"
5import "sketch_types.nx"
6
7func main() -> i64 {
8 // ---- alloc ----
9 let m: *HashMap = nx_hmap_alloc(64)
10 if m == (0 as *HashMap) { return __syscall(93, 5, 0, 0, 0, 0, 0) }
11 if nx_hmap_size(m) != 0 { return __syscall(93, 6, 0, 0, 0, 0, 0) }
12 // Reject non-power-of-2.
13 if nx_hmap_alloc(100) != (0 as *HashMap) {
14 return __syscall(93, 7, 0, 0, 0, 0, 0)
15 }
16 // Reject too-small.
17 if nx_hmap_alloc(8) != (0 as *HashMap) {
18 return __syscall(93, 8, 0, 0, 0, 0, 0)
19 }
20
21 // ---- put + get + has ----
22 nx_hmap_put(m, 42, 100)
23 nx_hmap_put(m, 17, 200)
24 nx_hmap_put(m, 99, 300)
25 if nx_hmap_size(m) != 3 { return __syscall(93, 10, 0, 0, 0, 0, 0) }
26 if nx_hmap_get(m, 42) != 100 { return __syscall(93, 11, 0, 0, 0, 0, 0) }
27 if nx_hmap_get(m, 17) != 200 { return __syscall(93, 12, 0, 0, 0, 0, 0) }
28 if nx_hmap_get(m, 99) != 300 { return __syscall(93, 13, 0, 0, 0, 0, 0) }
29 // Absent key returns 0.
30 if nx_hmap_get(m, 777) != 0 { return __syscall(93, 14, 0, 0, 0, 0, 0) }
31 if nx_hmap_has(m, 42) != 1 { return __syscall(93, 15, 0, 0, 0, 0, 0) }
32 if nx_hmap_has(m, 777) != 0 { return __syscall(93, 16, 0, 0, 0, 0, 0) }
33
34 // ---- upsert (put existing key) ----
35 nx_hmap_put(m, 42, 9999)
36 if nx_hmap_get(m, 42) != 9999 { return __syscall(93, 20, 0, 0, 0, 0, 0) }
37 if nx_hmap_size(m) != 3 { return __syscall(93, 21, 0, 0, 0, 0, 0) }
38
39 // ---- remove + re-insert (tombstone path) ----
40 nx_hmap_remove(m, 17)
41 if nx_hmap_has(m, 17) != 0 { return __syscall(93, 30, 0, 0, 0, 0, 0) }
42 if nx_hmap_size(m) != 2 { return __syscall(93, 31, 0, 0, 0, 0, 0) }
43 // Re-insert different value.
44 nx_hmap_put(m, 17, 555)
45 if nx_hmap_get(m, 17) != 555 { return __syscall(93, 32, 0, 0, 0, 0, 0) }
46 if nx_hmap_size(m) != 3 { return __syscall(93, 33, 0, 0, 0, 0, 0) }
47 // Other keys still present.
48 if nx_hmap_get(m, 42) != 9999 { return __syscall(93, 34, 0, 0, 0, 0, 0) }
49 if nx_hmap_get(m, 99) != 300 { return __syscall(93, 35, 0, 0, 0, 0, 0) }
50
51 // ---- bulk insertion + load factor ----
52 let m2: *HashMap = nx_hmap_alloc(128)
53 var i: i64 = 1
54 while i <= 80 { // 80/128 = 62.5%, under 70% threshold
55 nx_hmap_put(m2, i, i * 10)
56 i = i + 1
57 }
58 if nx_hmap_size(m2) != 80 { return __syscall(93, 40, 0, 0, 0, 0, 0) }
59 // All retrievable.
60 i = 1
61 while i <= 80 {
62 if nx_hmap_get(m2, i) != i * 10 {
63 return __syscall(93, 41, 0, 0, 0, 0, 0)
64 }
65 i = i + 1
66 }
67 // Load factor.
68 if nx_hmap_load_ppt(m2) != 625 { // 80/128 = 0.625 = 625 ppt
69 return __syscall(93, 42, 0, 0, 0, 0, 0)
70 }
71
72 // ---- load cap rejection ----
73 // Filling past 70% returns -2.
74 let m3: *HashMap = nx_hmap_alloc(16)
75 i = 1
76 var rejected: i64 = 0
77 while i <= 100 {
78 let r: i64 = nx_hmap_put(m3, i, i)
79 if r == -2 { rejected = rejected + 1 }
80 i = i + 1
81 }
82 if rejected == 0 { return __syscall(93, 50, 0, 0, 0, 0, 0) }
83
84 // ---- sentinel key (0) rejected ----
85 if nx_hmap_put(m, 0, 100) != -1 {
86 return __syscall(93, 60, 0, 0, 0, 0, 0)
87 }
88 // -1 (tombstone) rejected.
89 if nx_hmap_put(m, -1, 100) != -1 {
90 return __syscall(93, 61, 0, 0, 0, 0, 0)
91 }
92
93 // ---- iteration ----
94 let m4: *HashMap = nx_hmap_alloc(32)
95 nx_hmap_put(m4, 1, 10)
96 nx_hmap_put(m4, 2, 20)
97 nx_hmap_put(m4, 3, 30)
98 var visited: i64 = 0
99 var idx: i64 = nx_hmap_next(m4, 0)
100 while idx >= 0 {
101 visited = visited + 1
102 idx = nx_hmap_next(m4, idx + 1)
103 }
104 if visited != 3 {
105 return __syscall(93, 70, 0, 0, 0, 0, 0)
106 }
107
108 // ---- clear ----
109 nx_hmap_clear(m4)
110 if nx_hmap_size(m4) != 0 {
111 return __syscall(93, 80, 0, 0, 0, 0, 0)
112 }
113 if nx_hmap_has(m4, 1) != 0 {
114 return __syscall(93, 81, 0, 0, 0, 0, 0)
115 }
116 // After clear, can put again.
117 nx_hmap_put(m4, 5, 50)
118 if nx_hmap_get(m4, 5) != 50 {
119 return __syscall(93, 82, 0, 0, 0, 0, 0)
120 }
121
122 // ---- typed envelope ----
123 let q: *ApproxI64 = nx_hmap_query_size(m2)
124 if q.envelope_kind != NX_ENV_ABS {
125 return __syscall(93, 90, 0, 0, 0, 0, 0)
126 }
127 if q.param_a != 0 {
128 return __syscall(93, 91, 0, 0, 0, 0, 0)
129 }
130 if q.maturity != NX_MATURITY_PRODUCTION {
131 return __syscall(93, 92, 0, 0, 0, 0, 0)
132 }
133
134 return 0
135}