code wiki / (root) / nx_engram_test.nx

nx_engram_test.nx source

↩ module page · 67 lines · 2375 B

1// nx_engram_test.nx -- smoke for nx_engram. 2 3import "nx_syscalls.nx" 4import "nx_engram.nx" 5 6func main() -> i64 { 7 let now: nx_size = 1000 8 9 // 1: enum validity 10 if nx_eg_v_is_valid(NX_EG_V_HIT) != 1 { return 1 } 11 if nx_eg_v_is_valid(NX_EG_V_NULL) != 1 { return 2 } 12 if nx_eg_v_is_valid(-1) != 0 { return 3 } 13 if nx_eg_v_is_valid(5) != 0 { return 4 } 14 15 // 2: invalid capacity -> null 16 if nx_eg_cache_new(0) != (0 as *NxEngramCache) { return 5 } 17 18 // 3: fresh cache 19 let c: *NxEngramCache = nx_eg_cache_new(4) 20 if c.capacity != 4 { return 6 } 21 if c.n_entries != 0 { return 7 } 22 23 // 4: miss on empty 24 if nx_eg_lookup(c, 0xCAFE, now) != NX_EG_V_MISS { return 8 } 25 26 // 5: insert + lookup 27 let v_ins: nx_int = nx_eg_insert(c, 0xCAFE, 0xBEEF, now) 28 if v_ins != NX_EG_V_OK { return 9 } 29 if c.n_entries != 1 { return 10 } 30 let v_hit: nx_int = nx_eg_lookup(c, 0xCAFE, now + 100) 31 if v_hit != NX_EG_V_HIT { return 11 } 32 33 // 6: value retrievable 34 if nx_eg_value_of(c, 0xCAFE) != 0xBEEF { return 12 } 35 if nx_eg_value_of(c, 0xFFFF) != 0 { return 13 } 36 37 // 7: insert duplicate key updates 38 let v_dup: nx_int = nx_eg_insert(c, 0xCAFE, 0xDEAD, now + 200) 39 if v_dup != NX_EG_V_OK { return 14 } 40 if c.n_entries != 1 { return 15 } // not double-counted 41 if nx_eg_value_of(c, 0xCAFE) != 0xDEAD { return 16 } 42 43 // 8: consolidation after 3+ accesses 44 let e: *NxEngramEntry = nx_eg_find(c, 0xCAFE) 45 // Already 3 access events (initial insert + lookup + insert-as-update) 46 if e.access_count < 3 { return 17 } 47 if nx_eg_should_consolidate(c, 0xCAFE) != 1 { return 18 } 48 49 // 9: ring eviction at capacity 50 nx_eg_insert(c, 1, 100, now) 51 nx_eg_insert(c, 2, 200, now) 52 nx_eg_insert(c, 3, 300, now) 53 if c.n_entries != 4 { return 19 } 54 // capacity=4, all full; next insert evicts 55 let v_ev: nx_int = nx_eg_insert(c, 4, 400, now) 56 if v_ev != NX_EG_V_EVICTED { return 20 } 57 if c.n_entries != 4 { return 21 } // unchanged total 58 59 // 10: null handling 60 let null_c: *NxEngramCache = (0 as i64) as *NxEngramCache 61 if nx_eg_lookup(null_c, 0, now) != NX_EG_V_NULL { return 22 } 62 if nx_eg_insert(null_c, 0, 0, now) != NX_EG_V_NULL { return 23 } 63 if nx_eg_value_of(null_c, 0) != 0 { return 24 } 64 if nx_eg_should_consolidate(null_c, 0) != 0 { return 25 } 65 66 return 0 67}