code wiki / (root) / nx_observatory_test.nx

nx_observatory_test.nx source

↩ module page · 84 lines · 2654 B

1// nx_observatory_test.nx -- smoke for nx_observatory. 2 3import "nx_syscalls.nx" 4import "nx_attention_class.nx" 5import "nx_dyad.nx" 6import "nx_observatory.nx" 7 8func main() -> i64 { 9 let a_in: *u8 = (sys_mmap(32)) as *u8 10 let a_out: *u8 = (sys_mmap(32)) as *u8 11 let b_in: *u8 = (sys_mmap(32)) as *u8 12 let b_out: *u8 = (sys_mmap(32)) as *u8 13 14 let d: *NxDyad = nx_dyad_new( 15 100, NX_AC_DEV, a_in, 32, a_out, 32, 16 200, NX_AC_DEV, b_in, 32, b_out, 32) 17 18 // 1: construction 19 let o: *NxObservatory = nx_observatory_new(d, 16) 20 if o.capacity != 16 { return 1 } 21 if o.sample_count != 0 { return 2 } 22 if o.agree_count != 0 { return 3 } 23 if o.diverge_count != 0 { return 4 } 24 25 // 2: null dyad rejected 26 let null_d: *NxDyad = (0 as i64) as *NxDyad 27 if (nx_observatory_new(null_d, 16) as i64) != 0 { return 5 } 28 29 // 3: both outputs zero -- agreement on sample 30 nx_observatory_sample(o, 16, 1000) 31 if o.sample_count != 1 { return 6 } 32 if o.agree_count != 1 { return 7 } 33 if o.diverge_count != 0 { return 8 } 34 if nx_observatory_agreement_q10(o) != 1024 { return 9 } 35 36 // 4: write divergent outputs -- next sample is divergence 37 var i: nx_size = 0 38 while i < 16 { 39 a_out[i] = (50 + i) as u8 40 b_out[i] = (60 + i) as u8 // every position differs 41 i = i + 1 42 } 43 nx_observatory_sample(o, 16, 2000) 44 if o.sample_count != 2 { return 10 } 45 if o.diverge_count != 1 { return 11 } 46 if nx_observatory_agreement_q10(o) != 512 { return 12 } // 50/50 47 48 // 5: is_divergent threshold check 49 if nx_observatory_is_divergent(o, 256) != 1 { return 13 } // diverged 50% > 25% 50 if nx_observatory_is_divergent(o, 768) != 0 { return 14 } // diverged 50% < 75% 51 52 // 6: total diff bytes accumulates 53 if nx_observatory_total_diff_bytes(o) != 16 { return 15 } 54 55 // 7: many agreements bring agreement_q10 up 56 var k: nx_size = 0 57 while k < 16 { 58 b_out[k] = a_out[k] 59 k = k + 1 60 } 61 var s: nx_int = 0 62 while s < 6 { 63 nx_observatory_sample(o, 16, 3000 + s * 100) 64 s = s + 1 65 } 66 // Now have 1 agree + 1 diverge + 6 agree = 7/8 agree 67 let q10: nx_int = nx_observatory_agreement_q10(o) 68 if q10 < 800 { return 16 } 69 if q10 > 1024 { return 17 } 70 71 // 8: ring wraps cleanly at capacity 72 var w: nx_int = 0 73 while w < 20 { 74 nx_observatory_sample(o, 16, 5000 + w * 100) 75 w = w + 1 76 } 77 if o.sample_count < 16 { return 18 } 78 if o.head >= o.capacity { return 19 } 79 80 // 9: sample_count getter 81 if nx_observatory_sample_count(o) != o.sample_count { return 20 } 82 83 return 0 84}