sketch_sliding_window_test.nx source
↩ module page · 86 lines · 3747 B
1// sketch_sliding_window_test.nx -- last-W-samples rolling aggregator tests.
2
3import "syscalls.nx"
4import "sketch_sliding_window.nx"
5import "sketch_types.nx"
6
7func main() -> i64 {
8 // ---- alloc ----
9 let s: *SlidingWindow = nx_sw_alloc(5)
10 if s == (0 as *SlidingWindow) { return __syscall(93, 5, 0, 0, 0, 0, 0) }
11 // Reject W<2.
12 if nx_sw_alloc(1) != (0 as *SlidingWindow) {
13 return __syscall(93, 6, 0, 0, 0, 0, 0)
14 }
15 // Reject W>MAX.
16 if nx_sw_alloc(2000000) != (0 as *SlidingWindow) {
17 return __syscall(93, 7, 0, 0, 0, 0, 0)
18 }
19
20 // ---- empty queries ----
21 if nx_sw_count(s) != 0 { return __syscall(93, 10, 0, 0, 0, 0, 0) }
22 if nx_sw_sum(s) != 0 { return __syscall(93, 11, 0, 0, 0, 0, 0) }
23 if nx_sw_mean(s) != 0 { return __syscall(93, 12, 0, 0, 0, 0, 0) }
24
25 // ---- warm-up: insert [10, 20, 30] ----
26 nx_sw_push(s, 10)
27 nx_sw_push(s, 20)
28 nx_sw_push(s, 30)
29 if nx_sw_count(s) != 3 { return __syscall(93, 20, 0, 0, 0, 0, 0) }
30 if nx_sw_sum(s) != 60 { return __syscall(93, 21, 0, 0, 0, 0, 0) }
31 if nx_sw_mean(s) != 20 { return __syscall(93, 22, 0, 0, 0, 0, 0) }
32 if nx_sw_min(s) != 10 { return __syscall(93, 23, 0, 0, 0, 0, 0) }
33 if nx_sw_max(s) != 30 { return __syscall(93, 24, 0, 0, 0, 0, 0) }
34
35 // ---- fill to window: [10, 20, 30, 40, 50] ----
36 nx_sw_push(s, 40)
37 nx_sw_push(s, 50)
38 if nx_sw_count(s) != 5 { return __syscall(93, 30, 0, 0, 0, 0, 0) }
39 if nx_sw_sum(s) != 150 { return __syscall(93, 31, 0, 0, 0, 0, 0) }
40 if nx_sw_mean(s) != 30 { return __syscall(93, 32, 0, 0, 0, 0, 0) }
41
42 // ---- overflow: push 60 -- evicts 10 -- window is [20,30,40,50,60] ----
43 nx_sw_push(s, 60)
44 if nx_sw_count(s) != 5 { return __syscall(93, 40, 0, 0, 0, 0, 0) }
45 if nx_sw_sum(s) != 200 { return __syscall(93, 41, 0, 0, 0, 0, 0) }
46 // mean = 200/5 = 40.
47 if nx_sw_mean(s) != 40 { return __syscall(93, 42, 0, 0, 0, 0, 0) }
48 // The evicted value 10 was the min; cache invalidated; recompute -> 20.
49 if nx_sw_min(s) != 20 { return __syscall(93, 43, 0, 0, 0, 0, 0) }
50 if nx_sw_max(s) != 60 { return __syscall(93, 44, 0, 0, 0, 0, 0) }
51
52 // ---- continued sliding: push 100, 100, 100, 100, 100 ----
53 // window becomes [100, 100, 100, 100, 100] gradually.
54 nx_sw_push(s, 100) // evicts 20; window=[30,40,50,60,100]
55 nx_sw_push(s, 100) // evicts 30; window=[40,50,60,100,100]
56 nx_sw_push(s, 100) // evicts 40; window=[50,60,100,100,100]
57 nx_sw_push(s, 100) // evicts 50; window=[60,100,100,100,100]
58 nx_sw_push(s, 100) // evicts 60; window=[100,100,100,100,100]
59 if nx_sw_sum(s) != 500 { return __syscall(93, 50, 0, 0, 0, 0, 0) }
60 if nx_sw_mean(s) != 100 { return __syscall(93, 51, 0, 0, 0, 0, 0) }
61 if nx_sw_min(s) != 100 { return __syscall(93, 52, 0, 0, 0, 0, 0) }
62 if nx_sw_max(s) != 100 { return __syscall(93, 53, 0, 0, 0, 0, 0) }
63 if nx_sw_range(s) != 0 { return __syscall(93, 54, 0, 0, 0, 0, 0) }
64
65 // ---- clear ----
66 nx_sw_clear(s)
67 if nx_sw_count(s) != 0 { return __syscall(93, 60, 0, 0, 0, 0, 0) }
68 if nx_sw_sum(s) != 0 { return __syscall(93, 61, 0, 0, 0, 0, 0) }
69 // After clear, can push again.
70 nx_sw_push(s, 42)
71 if nx_sw_mean(s) != 42 { return __syscall(93, 62, 0, 0, 0, 0, 0) }
72
73 // ---- typed envelope ----
74 let q: *ApproxI64 = nx_sw_query_mean(s)
75 if q.envelope_kind != NX_ENV_ABS {
76 return __syscall(93, 70, 0, 0, 0, 0, 0)
77 }
78 if q.param_a != 0 {
79 return __syscall(93, 71, 0, 0, 0, 0, 0)
80 }
81 if q.maturity != NX_MATURITY_PRODUCTION {
82 return __syscall(93, 72, 0, 0, 0, 0, 0)
83 }
84
85 return 0
86}