code wiki / (root) / nx_i256_test.nx

nx_i256_test.nx source

↩ module page · 78 lines · 2516 B

1// nx_i256_test.nx -- smoke for the N3 tier (256-bit integer). 2 3import "syscalls.nx" 4import "nx_i256.nx" 5 6func main() -> i64 { 7 // === Test 1: alloc + set_i64 + is_zero === 8 let a: *I256 = nx_i256_alloc() 9 if nx_i256_is_zero(a) != 1 { return 1 } 10 nx_i256_set_i64(a, 42) 11 if nx_i256_is_zero(a) != 0 { return 2 } 12 if a.l0 != 42 { return 3 } 13 if a.l1 != 0 { return 4 } 14 15 // === Test 2: set_i64 sign-extend for negative === 16 nx_i256_set_i64(a, -1) 17 if a.l0 != -1 { return 10 } 18 if a.l1 != -1 { return 11 } 19 if a.l2 != -1 { return 12 } 20 if a.l3 != -1 { return 13 } 21 if nx_i256_is_neg(a) != 1 { return 14 } 22 23 // === Test 3: add small numbers === 24 nx_i256_set_i64(a, 100) 25 let b: *I256 = nx_i256_alloc() 26 nx_i256_set_i64(b, 200) 27 nx_i256_add(a, b) 28 if a.l0 != 300 { return 20 } 29 if a.l1 != 0 { return 21 } 30 31 // === Test 4: add that overflows i64 limb (carry into l1) === 32 nx_i256_set_i64(a, 0x7FFFFFFFFFFFFFFF) // i64 max 33 nx_i256_set_i64(b, 1) 34 nx_i256_add(a, b) 35 // Result = 2^63. l0 should be 0x8000000000000000 (i.e., negative as i64). 36 if a.l0 != -9223372036854775808 { return 30 } // = 1 << 63 37 // No carry into l1 yet (we added positives, so no overflow flag) 38 // but the sum is 2^63 which fits in the unsigned interpretation. 39 if a.l1 != 0 { return 31 } 40 41 // === Test 5: shl by 64 moves l0 -> l1 === 42 nx_i256_set_i64(a, 42) 43 nx_i256_shl(a, 64) 44 if a.l0 != 0 { return 40 } 45 if a.l1 != 42 { return 41 } 46 if a.l2 != 0 { return 42 } 47 48 // === Test 6: shl by 128 === 49 nx_i256_set_i64(a, 7) 50 nx_i256_shl(a, 128) 51 if a.l0 != 0 { return 50 } 52 if a.l1 != 0 { return 51 } 53 if a.l2 != 7 { return 52 } 54 if a.l3 != 0 { return 53 } 55 56 // === Test 7: multiply by small k === 57 nx_i256_set_i64(a, 1000000) 58 nx_i256_mul_i64(a, 1000000) // 10^12 fits in l0 59 if a.l0 != 1000000000000 { return 60 } 60 if a.l1 != 0 { return 61 } 61 62 // === Test 8: negate symmetry === 63 nx_i256_set_i64(a, 42) 64 nx_i256_neg(a) 65 if nx_i256_is_neg(a) != 1 { return 70 } 66 nx_i256_neg(a) 67 if a.l0 != 42 { return 71 } 68 if nx_i256_is_neg(a) != 0 { return 72 } 69 70 // === Test 9: subtract === 71 nx_i256_set_i64(a, 1000) 72 nx_i256_set_i64(b, 300) 73 nx_i256_sub(a, b) 74 if a.l0 != 700 { return 80 } 75 if nx_i256_is_neg(a) != 0 { return 81 } 76 77 return 0 78}