code wiki / (root) / nx_intake_profile_gate.nx

nx_intake_profile_gate.nx source

↩ module page · 68 lines · 3748 B

1// nx_intake_profile_gate.nx -- R6c GATE: proves the "latest profile" loader. A submitted intake (ih_record, 2// which now also writes the latest pointer) -> ip_get returns the SUBMITTED numbers; a respondent with no 3// submission -> the DEFAULT (graceful fallback so the dashboard shows the worked example until they fill it 4// in). Hermetic /tmp prefix. Exits 0 iff ALL pass. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_intake_handler.nx" 7import "nx_intake_profile.nx" 8 9func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10func g_putn(v: i64) -> i64 { 11 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 12 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 13 let d: *u8 = sys_mmap(24); var k: i64 = 0 14 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 15 var j: i64 = k - 1 16 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 17 return 0 18} 19func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 20 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") } 21 else { st[1] = st[1] + 1; g_puts(" FAIL "); g_puts(name); g_puts(" got="); g_putn(got); g_puts(" want="); g_putn(want); g_puts("\n") } 22 return 0 23} 24 25func main() -> i64 { 26 let st: *i64 = sys_mmap(16) as *i64 27 st[0] = 0; st[1] = 0 28 29 // hermetic prefix "/tmp/iprof_<us>-" 30 let pfx: *u8 = sys_mmap(64) 31 var pp: i64 = 0 32 let pre: *u8 = "/tmp/iprof_\x00" as *u8 33 while pre[pp] != (0 as u8) { pfx[pp] = pre[pp]; pp = pp + 1 } 34 let us: i64 = sys_now_us() 35 let tmp: *u8 = sys_mmap(32); var m: i64 = us; var kk: i64 = 0 36 if m == 0 { tmp[0] = 0x30 as u8; kk = 1 } 37 while m > 0 { tmp[kk] = (0x30 + (m % 10)) as u8; m = m / 10; kk = kk + 1 } 38 var z: i64 = kk - 1 39 while z >= 0 { pfx[pp] = tmp[z]; pp = pp + 1; z = z - 1 } 40 pfx[pp] = 0x2d as u8; pp = pp + 1; pfx[pp] = 0 as u8 41 42 // before any submission: ip_get falls back to the default 43 chk("no profile yet -> ip_exists = 0", ip_exists(pfx, "operator\x00" as *u8), 0, st) 44 chk("no profile yet -> ip_get returns the default", ip_get(pfx, "operator\x00" as *u8, "debt_balance\x00" as *u8, 2000000), 2000000, st) 45 46 // submit (dollars) 47 let body: *u8 = "debt_balance=20000&hourly_wage=25&has_adhd=1&monthly_payment=600&debt_apr_pct=24\x00" as *u8 48 var bn: i64 = 0 49 while body[bn] != (0 as u8) { bn = bn + 1 } 50 let cid: *u8 = sys_mmap(96) 51 chk("intake submitted", ih_record(pfx, "operator\x00" as *u8, body, bn, cid), 0, st) 52 53 // now ip_get returns THEIR numbers 54 chk("profile now exists", ip_exists(pfx, "operator\x00" as *u8), 1, st) 55 chk("ip_get debt_balance = 2000000 ($20,000 submitted)", ip_get(pfx, "operator\x00" as *u8, "debt_balance\x00" as *u8, 999), 2000000, st) 56 chk("ip_get hourly_wage = 2500 ($25 submitted)", ip_get(pfx, "operator\x00" as *u8, "hourly_wage\x00" as *u8, 999), 2500, st) 57 chk("ip_get has_adhd = 1 (submitted)", ip_get(pfx, "operator\x00" as *u8, "has_adhd\x00" as *u8, 0), 1, st) 58 chk("ip_get debt_apr_pct = 24 (submitted, not converted)", ip_get(pfx, "operator\x00" as *u8, "debt_apr_pct\x00" as *u8, 99), 24, st) 59 60 // absent field -> default; absent respondent -> default 61 chk("absent field -> default", ip_get(pfx, "operator\x00" as *u8, "owns_yacht\x00" as *u8, 777), 777, st) 62 chk("absent respondent -> default", ip_get(pfx, "ghost\x00" as *u8, "debt_balance\x00" as *u8, 555), 555, st) 63 64 g_puts("nx_intake_profile_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n") 65 if st[1] == 0 { g_puts("SITUATION R6c nx_intake_profile: GREEN\n"); return 0 } 66 g_puts("SITUATION R6c nx_intake_profile: RED\n") 67 return 1 68}