nx_choker_test.nx source
↩ module page · 80 lines · 3509 B
1// nx_choker_test.nx -- KAT for the choke algorithm (tit-for-tat + optimistic unchoke).
2// Native sovereign lane; exit 0 = pass, N = assertion N failed.
3
4import "nx_str.nx"
5import "nx_choker.nx"
6
7func main() -> i64 {
8 let rates: *i64 = sys_mmap(8 * 8) as *i64
9 let intr: *i64 = sys_mmap(8 * 8) as *i64
10 let out: *i64 = sys_mmap(8 * 8) as *i64
11
12 // ---- top-k by rate, all interested ----
13 // rates [10,50,30,5,40], k=2 -> top two = idx1(50), idx4(40)
14 rates[0]=10; rates[1]=50; rates[2]=30; rates[3]=5; rates[4]=40
15 intr[0]=1; intr[1]=1; intr[2]=1; intr[3]=1; intr[4]=1
16 if nx_ch_select_top_k(rates, intr, 5, 2, out) != 2 { return 1 }
17 if out[1] != 1 { return 2 }
18 if out[4] != 1 { return 3 }
19 if out[0] != 0 { return 4 }
20 if out[2] != 0 { return 5 }
21
22 // ---- tie on rate -> lowest index wins ----
23 rates[0]=20; rates[1]=20; rates[2]=5
24 intr[0]=1; intr[1]=1; intr[2]=1
25 if nx_ch_select_top_k(rates, intr, 3, 1, out) != 1 { return 6 }
26 if out[0] != 1 { return 7 }
27 if out[1] != 0 { return 8 }
28
29 // ---- not-interested peers are never unchoked ----
30 rates[0]=100; rates[1]=50
31 intr[0]=0; intr[1]=1
32 if nx_ch_select_top_k(rates, intr, 2, 2, out) != 1 { return 9 }
33 if out[0] != 0 { return 10 }
34 if out[1] != 1 { return 11 }
35
36 // ---- k larger than eligible count -> fills what it can ----
37 rates[0]=1; rates[1]=2; rates[2]=3
38 intr[0]=1; intr[1]=1; intr[2]=1
39 if nx_ch_select_top_k(rates, intr, 3, 5, out) != 3 { return 12 }
40
41 // ---- optimistic slot: round-robin over interested-but-choked peers ----
42 // interested [1,1,1,1], already unchoked [0,1,0,0] -> eligible = {0,2,3}, count 3
43 intr[0]=1; intr[1]=1; intr[2]=1; intr[3]=1
44 out[0]=0; out[1]=1; out[2]=0; out[3]=0
45 if nx_ch_optimistic_slot(intr, out, 4, 0, 3) != 0 { return 13 } // (0/3)%3=0 -> 1st eligible=0
46 if nx_ch_optimistic_slot(intr, out, 4, 3, 3) != 2 { return 14 } // (3/3)%3=1 -> 2nd eligible=2
47 if nx_ch_optimistic_slot(intr, out, 4, 6, 3) != 3 { return 15 } // (6/3)%3=2 -> 3rd eligible=3
48 if nx_ch_optimistic_slot(intr, out, 4, 9, 3) != 0 { return 16 } // wraps back to 0
49 if nx_ch_optimistic_slot(intr, out, 4, 1, 3) != 0 { return 17 } // same interval as round 0
50
51 // ---- no eligible peer for optimistic slot -> -1 ----
52 intr[0]=0; intr[1]=0
53 out[0]=0; out[1]=0
54 if nx_ch_optimistic_slot(intr, out, 2, 0, 3) != (0 - 1) { return 18 }
55
56 // ---- full compute: top-2 + 1 optimistic ----
57 rates[0]=10; rates[1]=50; rates[2]=30; rates[3]=5; rates[4]=40
58 intr[0]=1; intr[1]=1; intr[2]=1; intr[3]=1; intr[4]=1
59 // top2 = {1,4}; eligible-for-opt = {0,2,3}, round 0 -> idx0
60 if nx_ch_compute(rates, intr, 5, 2, 0, 3, out) != 3 { return 19 }
61 if out[1] != 1 { return 20 }
62 if out[4] != 1 { return 21 }
63 if out[0] != 1 { return 22 } // optimistic slot
64 if out[2] != 0 { return 23 }
65 if out[3] != 0 { return 24 }
66
67 // ---- compute where every interested peer is already in top-k -> no optimistic ----
68 rates[0]=10; rates[1]=20
69 intr[0]=1; intr[1]=1
70 if nx_ch_compute(rates, intr, 2, 2, 0, 3, out) != 2 { return 25 }
71
72 // ---- should_rotate ----
73 if nx_ch_should_rotate(0, 3) != 1 { return 26 }
74 if nx_ch_should_rotate(3, 3) != 1 { return 27 }
75 if nx_ch_should_rotate(1, 3) != 0 { return 28 }
76 if nx_ch_should_rotate(2, 3) != 0 { return 29 }
77 if nx_ch_should_rotate(5, 0) != 1 { return 30 } // period<=0 -> treated as 1 -> always rotate
78
79 return 0
80}