nx_consumable_test.nx source
↩ module page · 75 lines · 3107 B
1// nx_consumable_test.nx -- gate for the consumable-depletion kernel.
2//
3// Proves the replace verdicts from cumulative usage, and the MEASURED EXCEED
4// (liar-kill): the SAME 2000-unit usage yields DUE under severe duty (x2.0) but
5// OK under gentle duty (x0.5) -- the verdict tracks MEASURED severity, beating
6// a fixed OEM interval and a vendor black box.
7//
8// expect_exit: 0
9//
10// license_tier: ORIGINAL
11
12import "nx_syscalls_x86_64.nx"
13import "nx_consumable.nx"
14
15func main() -> i64 {
16 if nx_cons_verdict_is_valid(NX_CONS_OVERDUE) != 1 { return 1 }
17 if nx_cons_verdict_is_valid(NX_CONS_N) != 0 { return 2 }
18 if nx_cons_verdict_is_valid(-1) != 0 { return 3 }
19
20 let usage: *i64 = sys_mmap(128) as *i64
21 let sev: *i64 = sys_mmap(128) as *i64
22 let out: *ConsEff = sys_mmap(64) as *ConsEff
23 let spec: *ConsSpec = sys_mmap(64) as *ConsSpec
24 spec.rated_life = 8000
25 spec.soon_permille = 150 // flag when <=15% left
26 spec.overdue_permille = 100 // 10% past life = OVERDUE
27
28 // ===== OK: 50% consumed ========================================
29 nx_cons_analyze(usage, sev, 0, 4000, spec, out)
30 if out.verdict != NX_CONS_OK { return 10 }
31 if out.remaining_permille != 500 { return 11 }
32
33 // ===== DUE_SOON: ~12% left =====================================
34 nx_cons_analyze(usage, sev, 0, 7000, spec, out)
35 if out.verdict != NX_CONS_DUE_SOON { return 20 }
36
37 // ===== DUE: just past life =====================================
38 nx_cons_analyze(usage, sev, 0, 8200, spec, out)
39 if out.verdict != NX_CONS_DUE { return 30 }
40
41 // ===== OVERDUE: well past ======================================
42 nx_cons_analyze(usage, sev, 0, 9000, spec, out)
43 if out.verdict != NX_CONS_OVERDUE { return 40 }
44
45 // ===== LIAR-KILL: same usage, severity decides =================
46 // severe duty (x2.0): 2000 units consume 4000 -> past life -> DUE
47 usage[0] = 2000; sev[0] = 200
48 nx_cons_analyze(usage, sev, 1, 4500, spec, out)
49 if out.verdict != NX_CONS_DUE { return 50 }
50 let sev_total: i64 = out.total_consumed
51 let sev_verdict: i64 = out.verdict
52 if sev_total != 8500 { return 51 }
53 // gentle duty (x0.5): SAME 2000 units consume 1000 -> healthy -> OK
54 usage[0] = 2000; sev[0] = 50
55 nx_cons_analyze(usage, sev, 1, 4500, spec, out)
56 if out.verdict != NX_CONS_OK { return 52 }
57 if out.total_consumed != 5500 { return 53 }
58 if out.verdict == sev_verdict { return 54 } // measured severity flips the verdict
59
60 // ===== BAD_ARG: negative usage =================================
61 usage[0] = 0 - 1; sev[0] = 100
62 nx_cons_analyze(usage, sev, 1, 0, spec, out)
63 if out.verdict != NX_CONS_BAD_ARG { return 60 }
64
65 // ===== INSUFFICIENT_DATA: negative prior =======================
66 nx_cons_analyze(usage, sev, 0, 0 - 1, spec, out)
67 if out.verdict != NX_CONS_INSUFFICIENT_DATA { return 70 }
68
69 // ===== BAD_ARG: invalid rated_life =============================
70 spec.rated_life = 0
71 nx_cons_analyze(usage, sev, 0, 4000, spec, out)
72 if out.verdict != NX_CONS_BAD_ARG { return 80 }
73
74 return 0
75}