nx_aerobic_test.nx source
↩ module page · 60 lines · 2320 B
1// nx_aerobic_test.nx -- smoke for nx_aerobic.
2
3import "nx_syscalls.nx"
4import "nx_aerobic.nx"
5
6func main() -> i64 {
7 if NX_AR_N_REASONS != 5 { return 1 }
8 if nx_ar_is_valid(NX_AR_EXPIRED) != 1 { return 2 }
9 if nx_ar_is_valid(5) != 0 { return 3 }
10
11 let p: *NxAerobicPile = nx_aerobic_pile_new(8, 1000)
12 if p.capacity != 8 { return 4 }
13 if p.count != 0 { return 5 }
14 if nx_aerobic_total_decomposed(p) != 0 { return 6 }
15
16 // Bad reason rejected
17 if nx_aerobic_add(p, 100, 0xaa, 99, 2000) != NX_AE_ERR_BAD_REASON { return 7 }
18
19 // Add 3 items
20 nx_aerobic_add(p, 100, 0xaa, NX_AR_EXPIRED, 2000)
21 nx_aerobic_add(p, 200, 0xbb, NX_AR_SUPERSEDED, 3000)
22 nx_aerobic_add(p, 300, 0xcc, NX_AR_USER_REQUESTED, 4000)
23 if p.count != 3 { return 8 }
24
25 // Count by reason
26 if nx_aerobic_count_by_reason(p, NX_AR_EXPIRED) != 1 { return 9 }
27 if nx_aerobic_count_by_reason(p, NX_AR_SUPERSEDED) != 1 { return 10 }
28 if nx_aerobic_count_by_reason(p, NX_AR_VERSION_RETIRED) != 0 { return 11 }
29
30 // Drain one -- returns oldest
31 let out_id: *i64 = (sys_mmap(8)) as *i64
32 let out_hash: *i64 = (sys_mmap(8)) as *i64
33 if nx_aerobic_drain_one(p, out_id, out_hash, 5000) != NX_AE_OK { return 12 }
34 if out_id[0] != 100 { return 13 }
35 if out_hash[0] != 0xaa { return 14 }
36 if nx_aerobic_total_decomposed(p) != 1 { return 15 }
37
38 // Drain twice more
39 nx_aerobic_drain_one(p, out_id, out_hash, 6000)
40 if out_id[0] != 200 { return 16 }
41 nx_aerobic_drain_one(p, out_id, out_hash, 7000)
42 if out_id[0] != 300 { return 17 }
43 if nx_aerobic_total_decomposed(p) != 3 { return 18 }
44
45 // No more items -> ERR_EMPTY
46 if nx_aerobic_drain_one(p, out_id, out_hash, 8000) != NX_AE_ERR_EMPTY { return 19 }
47
48 // Stagnant detection: just-active pile is NOT stagnant
49 if nx_aerobic_is_stagnant(p, 8000) != 0 { return 20 }
50 // Window past stagnant threshold (24h = 86400000000 us) is stagnant
51 if nx_aerobic_is_stagnant(p, 100000000000) != 1 { return 21 }
52
53 // FULL when at capacity
54 let small: *NxAerobicPile = nx_aerobic_pile_new(2, 1000)
55 nx_aerobic_add(small, 1, 0x11, NX_AR_EXPIRED, 2000)
56 nx_aerobic_add(small, 2, 0x22, NX_AR_EXPIRED, 2000)
57 if nx_aerobic_add(small, 3, 0x33, NX_AR_EXPIRED, 2000) != NX_AE_ERR_FULL { return 22 }
58
59 return 0
60}