nx_classical_unpatented_3_test.nx source
↩ module page · 101 lines · 3748 B
1// nx_classical_unpatented_3_test.nx -- umbrella smoke for batch 3.
2
3import "nx_syscalls.nx"
4import "nx_tier.nx"
5import "nx_classical_unpatented_3.nx"
6
7func main() -> nx_exit {
8 // ----- 190s: FNV-1a -----
9 // empty buffer
10 let empty: *u8 = sys_mmap(16)
11 if nx_fnv1a_32_buf(empty, 0) != 2166136261 { return 191 }
12 // single byte 'a' (0x61)
13 let one: *u8 = sys_mmap(16)
14 one[0] = 0x61
15 // FNV-1a 32 of "a" = 0xe40c292c = 3826002220
16 if nx_fnv1a_32_buf(one, 1) != 3826002220 { return 192 }
17 // deterministic: same input twice
18 if nx_fnv1a_32_buf(one, 1) != nx_fnv1a_32_buf(one, 1) { return 193 }
19
20 // ----- 200s: djb2 -----
21 if nx_djb2_buf(empty, 0) != 5381 { return 201 }
22 // single byte 'a' (0x61): h = 5381*33 + 0x61 = 177574
23 if nx_djb2_buf(one, 1) != 177574 { return 202 }
24 // determinism
25 if nx_djb2_buf(one, 1) != nx_djb2_buf(one, 1) { return 203 }
26
27 // ----- 210s: Jenkins one-at-a-time -----
28 // empty input: final mixer applied to h=0, all shifts of 0 are 0.
29 if nx_jenkins_oaat_buf(empty, 0) != 0 { return 211 }
30 // different inputs give different hashes
31 let two: *u8 = sys_mmap(16)
32 two[0] = 0x61; two[1] = 0x62
33 if nx_jenkins_oaat_buf(one, 1) == nx_jenkins_oaat_buf(two, 2) { return 212 }
34 // determinism
35 if nx_jenkins_oaat_buf(two, 2) != nx_jenkins_oaat_buf(two, 2) { return 213 }
36
37 // ----- 220s: Adler-32 -----
38 // empty input: a=1, b=0 -> 0x00000001 = 1
39 if nx_adler32_buf(empty, 0) != 1 { return 221 }
40 // "Wikipedia" gives 0x11E60398 in Adler-32 spec; we test "abc".
41 // For "abc": a = 1 + 'a' + 'b' + 'c' = 1 + 97 + 98 + 99 = 295
42 // b = 1+97 + 1+97+98 + 1+97+98+99 = 98 + 196 + 295 = 589
43 // result = (589 << 16) | 295 = 38600999
44 let three: *u8 = sys_mmap(16)
45 three[0] = 0x61; three[1] = 0x62; three[2] = 0x63
46 if nx_adler32_buf(three, 3) != 38600999 { return 222 }
47
48 // ----- 230s: reverse_inplace -----
49 let buf_r: *u8 = sys_mmap(80)
50 let ar: *nx_int = buf_r as *nx_int
51 ar[0] = 1; ar[1] = 2; ar[2] = 3; ar[3] = 4; ar[4] = 5
52 nx_array_reverse_inplace(ar, 5)
53 if ar[0] != 5 { return 231 }
54 if ar[1] != 4 { return 232 }
55 if ar[2] != 3 { return 233 }
56 if ar[3] != 2 { return 234 }
57 if ar[4] != 1 { return 235 }
58
59 // ----- 240s: rotate_left -----
60 let buf_rot: *u8 = sys_mmap(80)
61 let aro: *nx_int = buf_rot as *nx_int
62 aro[0] = 1; aro[1] = 2; aro[2] = 3; aro[3] = 4; aro[4] = 5
63 nx_array_rotate_left(aro, 5, 2)
64 // expected: 3 4 5 1 2
65 if aro[0] != 3 { return 241 }
66 if aro[1] != 4 { return 242 }
67 if aro[2] != 5 { return 243 }
68 if aro[3] != 1 { return 244 }
69 if aro[4] != 2 { return 245 }
70
71 // ----- 250s: partition_lomuto -----
72 let buf_p: *u8 = sys_mmap(80)
73 let ap: *nx_int = buf_p as *nx_int
74 ap[0] = 3; ap[1] = 1; ap[2] = 4; ap[3] = 1; ap[4] = 5; ap[5] = 2
75 let pi: nx_idx = nx_partition_lomuto(ap, 6) // pivot = 2 (ap[5])
76 // After partition: elements <= 2 are before pi, > 2 after; pivot at pi.
77 if ap[pi] != 2 { return 251 }
78 // All elements before pi <= 2
79 var k: nx_idx = 0
80 while k < pi {
81 if ap[k] > 2 { return 252 }
82 k = k + 1
83 }
84 // All elements after pi > 2
85 k = pi + 1
86 while k < 6 {
87 if ap[k] <= 2 { return 253 }
88 k = k + 1
89 }
90
91 // ----- 260s: count_occurrences -----
92 let buf_c: *u8 = sys_mmap(80)
93 let ac: *nx_int = buf_c as *nx_int
94 ac[0] = 5; ac[1] = 3; ac[2] = 5; ac[3] = 7; ac[4] = 5
95 if nx_count_occurrences(ac, 5, 5) != 3 { return 261 }
96 if nx_count_occurrences(ac, 5, 3) != 1 { return 262 }
97 if nx_count_occurrences(ac, 5, 99) != 0 { return 263 }
98 if nx_count_occurrences(ac, 0, 5) != 0 { return 264 }
99
100 return 0
101}