nx_chemotaxis_test.nx source
↩ module page · 66 lines · 2580 B
1// nx_chemotaxis_test.nx -- smoke for nx_chemotaxis.
2
3import "nx_syscalls.nx"
4import "nx_chemotaxis.nx"
5
6func main() -> i64 {
7 let now: nx_size = 1000000
8
9 // 1: enum validity
10 if nx_ct_v_is_valid(NX_CT_V_FOUND) != 1 { return 1 }
11 if nx_ct_v_is_valid(NX_CT_V_NULL) != 1 { return 2 }
12 if nx_ct_v_is_valid(-1) != 0 { return 3 }
13 if nx_ct_v_is_valid(4) != 0 { return 4 }
14
15 // 2: invalid signal strength -> null
16 if nx_ct_target_new(1, 0, -1, 0, now) != (0 as *NxChemoTarget) { return 5 }
17 if nx_ct_target_new(1, 0, 1025, 0, now) != (0 as *NxChemoTarget) { return 6 }
18 if nx_ct_target_new(1, 0, 512, -1, now) != (0 as *NxChemoTarget) { return 7 }
19
20 // 3: valid construction
21 let t: *NxChemoTarget = nx_ct_target_new(42, 0xCAFE, 800, 2, now)
22 if t.target_id != 42 { return 8 }
23 if t.signal_strength_q10 != 800 { return 9 }
24 if t.distance_hops != 2 { return 10 }
25
26 // 4: score = 800 / (1+2) = 266
27 if nx_ct_target_score_q10(t) != 266 { return 11 }
28
29 // 5: zero-hop = full signal
30 let t_close: *NxChemoTarget = nx_ct_target_new(99, 0, 1024, 0, now)
31 if nx_ct_target_score_q10(t_close) != 1024 { return 12 }
32
33 // 6: fresh predicate
34 if nx_ct_target_is_fresh(t, now, 1000000) != 1 { return 13 }
35 if nx_ct_target_is_fresh(t, now + 2000000, 1000000) != 0 { return 14 }
36
37 // 7: classify -- strong (score 266 >= threshold 200)
38 if nx_ct_classify(t, 200) != NX_CT_V_FOUND { return 15 }
39
40 // 8: classify -- weak (score 266 < threshold 500)
41 if nx_ct_classify(t, 500) != NX_CT_V_GRADIENT_WEAK { return 16 }
42
43 // 9: classify -- not found (score 0)
44 let t_dead: *NxChemoTarget = nx_ct_target_new(1, 0, 0, 5, now)
45 if nx_ct_classify(t_dead, 200) != NX_CT_V_NOT_FOUND { return 17 }
46
47 // 10: pick strongest
48 let t1: *NxChemoTarget = nx_ct_target_new(1, 0, 800, 1, now) // score 400
49 let t2: *NxChemoTarget = nx_ct_target_new(2, 0, 600, 0, now) // score 600
50 let chosen: *NxChemoTarget = nx_ct_pick_strongest(t1, t2)
51 if chosen.target_id != 2 { return 18 }
52
53 // 11: null handling
54 let null_t: *NxChemoTarget = (0 as i64) as *NxChemoTarget
55 if nx_ct_target_score_q10(null_t) != 0 { return 19 }
56 if nx_ct_target_is_fresh(null_t, now, 1000) != 0 { return 20 }
57 if nx_ct_classify(null_t, 100) != NX_CT_V_NULL { return 21 }
58
59 // 12: pick_strongest with one null
60 let picked: *NxChemoTarget = nx_ct_pick_strongest(null_t, t1)
61 if picked != t1 { return 22 }
62 let picked2: *NxChemoTarget = nx_ct_pick_strongest(t1, null_t)
63 if picked2 != t1 { return 23 }
64
65 return 0
66}