code wiki / (root) / nx_q14_format_test.nx

nx_q14_format_test.nx source

↩ module page · 86 lines · 2716 B

1// nx_q14_format_test.nx -- Q14 decimal formatter against known values. 2// 3// Closed-form invariants: 4// (a) nx_int_to_decimal(0) -> "0" (1 byte) 5// (b) nx_int_to_decimal(305) -> "305" (3 bytes) 6// (c) nx_int_to_decimal(-42) -> "-42" (3 bytes) 7// (d) nx_int_to_decimal(123456789) -> "123456789" (9 bytes) 8// (e) nx_q14_to_decimal(0, 3 frac) -> "0.000" (5 bytes) 9// (f) nx_q14_to_decimal(16384, 3 frac) -> "1.000" (5 bytes) 10// (g) nx_q14_to_decimal(-24576, 3 frac) -> "-1.500" (6 bytes) 11// (h) nx_q14_to_decimal(4997120, 3 frac) -> "305.000" (Christus 12// statue typical height in mm; 305 * 16384 = 4997120 Q14) 13// (i) Capacity check: writing into a 2-byte buf returns -1 for any 14// value needing >= 3 chars. 15// (j) frac_digits = 0 produces no decimal point. 16// 17// expect_exit: 0 18// license_tier: ORIGINAL 19 20import "nx_syscalls.nx" 21import "nx_q14_format.nx" 22 23// Verify the first n bytes of buf match a literal C string. 24func smoke_buf_eq(buf: *u8, expected: *u8, n: i64) -> i64 { 25 var i: i64 = 0 26 while i < n { 27 if (buf[i] as i64) != (expected[i] as i64) { return 0 } 28 i = i + 1 29 } 30 return 1 31} 32 33func main() -> i64 { 34 let buf: *u8 = sys_mmap(64) 35 36 // --- (a) --- 37 var n: i64 = nx_int_to_decimal(0, buf, 64) 38 if n != 1 { return 10 } 39 if smoke_buf_eq(buf, "0", 1) != 1 { return 11 } 40 41 // --- (b) --- 42 n = nx_int_to_decimal(305, buf, 64) 43 if n != 3 { return 20 } 44 if smoke_buf_eq(buf, "305", 3) != 1 { return 21 } 45 46 // --- (c) --- 47 n = nx_int_to_decimal(-42, buf, 64) 48 if n != 3 { return 30 } 49 if smoke_buf_eq(buf, "-42", 3) != 1 { return 31 } 50 51 // --- (d) --- 52 n = nx_int_to_decimal(123456789, buf, 64) 53 if n != 9 { return 40 } 54 if smoke_buf_eq(buf, "123456789", 9) != 1 { return 41 } 55 56 // --- (e) --- 57 n = nx_q14_to_decimal(0, buf, 64, 3) 58 if n != 5 { return 50 } 59 if smoke_buf_eq(buf, "0.000", 5) != 1 { return 51 } 60 61 // --- (f) --- 62 n = nx_q14_to_decimal(16384, buf, 64, 3) 63 if n != 5 { return 60 } 64 if smoke_buf_eq(buf, "1.000", 5) != 1 { return 61 } 65 66 // --- (g) --- 67 n = nx_q14_to_decimal(-24576, buf, 64, 3) 68 if n != 6 { return 70 } 69 if smoke_buf_eq(buf, "-1.500", 6) != 1 { return 71 } 70 71 // --- (h) --- 72 n = nx_q14_to_decimal(4997120, buf, 64, 3) 73 if n != 7 { return 80 } 74 if smoke_buf_eq(buf, "305.000", 7) != 1 { return 81 } 75 76 // --- (i) Capacity --- 77 n = nx_int_to_decimal(100, buf, 2) 78 if n != -1 { return 90 } 79 80 // --- (j) frac_digits = 0 --- 81 n = nx_q14_to_decimal(16384, buf, 64, 0) 82 if n != 1 { return 100 } 83 if smoke_buf_eq(buf, "1", 1) != 1 { return 101 } 84 85 return 0 86}