code wiki / (root) / nx_essentials_test.nx

nx_essentials_test.nx source

↩ module page · 147 lines · 6132 B

1// nx_essentials_test.nx -- smoke for the 25-micro-primitives batch. 2 3import "nx_syscalls.nx" 4import "nx_tier.nx" 5import "nx_essentials.nx" 6 7func main() -> nx_int { 8 // === Family 1: comparison / clamp === 9 if nx_min2(3, 7) != 3 { return 1 } 10 if nx_max2(3, 7) != 7 { return 2 } 11 if nx_min3(5, 2, 8) != 2 { return 3 } 12 if nx_max3(5, 2, 8) != 8 { return 4 } 13 if nx_clamp(15, 0, 10) != 10 { return 5 } 14 if nx_clamp(-5, 0, 10) != 0 { return 6 } 15 if nx_clamp(5, 0, 10) != 5 { return 7 } 16 if nx_clamp_q10(1500) != 1024 { return 8 } 17 if nx_clamp_q10(-10) != 0 { return 9 } 18 if nx_clamp_signed_q10(-2000) != -1024 { return 10 } 19 if nx_clamp_signed_q10(2000) != 1024 { return 11 } 20 21 // === Family 2: sign / abs / swap === 22 if nx_abs(-7) != 7 { return 20 } 23 if nx_abs(7) != 7 { return 21 } 24 if nx_abs(0) != 0 { return 22 } 25 if nx_sign(-3) != -1 { return 23 } 26 if nx_sign(0) != 0 { return 24 } 27 if nx_sign(3) != 1 { return 25 } 28 // swap 29 let pair: *nx_int = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *nx_int 30 pair[0] = 42 31 pair[1] = 99 32 nx_swap_via_ptr(pair, pair + 1) 33 // Note: pointer arithmetic on *nx_int isn't expressed via "+1" idiom in 34 // all NishiLang dialects; use index into [1] equivalent. 35 // Skip the pointer-arith dependent assertion and validate alternate. 36 let a_buf: *nx_int = (sys_mmap(1 * NX_SIZEOF_NX_INT)) as *nx_int 37 let b_buf: *nx_int = (sys_mmap(1 * NX_SIZEOF_NX_INT)) as *nx_int 38 a_buf[0] = 10 39 b_buf[0] = 20 40 nx_swap_via_ptr(a_buf, b_buf) 41 if a_buf[0] != 20 { return 26 } 42 if b_buf[0] != 10 { return 27 } 43 44 // === Family 3: saturating arithmetic === 45 if nx_sat_add(5, 7) != 12 { return 30 } 46 if nx_sat_add(NX_ESS_I64_MAX, 1) != NX_ESS_I64_MAX { return 31 } 47 if nx_sat_sub(NX_ESS_I64_MIN, 1) != NX_ESS_I64_MIN { return 32 } 48 if nx_sat_mul(NX_ESS_I64_MAX, 2) != NX_ESS_I64_MAX { return 33 } 49 if nx_sat_mul(NX_ESS_I64_MAX, -2) != NX_ESS_I64_MIN { return 34 } 50 51 // === Family 4: Q10 helpers === 52 if nx_q10_mul(NX_ESS_Q, NX_ESS_Q) != NX_ESS_Q { return 40 } // 1.0 * 1.0 = 1.0 53 if nx_q10_mul(NX_ESS_Q / 2, NX_ESS_Q / 2) != NX_ESS_Q / 4 { return 41 } // 0.5*0.5=0.25 54 if nx_q10_div(NX_ESS_Q / 2, NX_ESS_Q / 4) != NX_ESS_Q * 2 { return 42 } // 0.5/0.25=2.0 55 if nx_lerp_q10(0, 100, NX_ESS_Q / 2) != 50 { return 43 } // halfway between 0..100 56 if nx_lerp_q10(10, 30, 0) != 10 { return 44 } 57 if nx_lerp_q10(10, 30, NX_ESS_Q) != 30 { return 45 } 58 if nx_pct_to_q10(50) != 512 { return 46 } 59 if nx_pct_to_q10(100) != 1024 { return 47 } 60 if nx_pct_to_q10(0) != 0 { return 48 } 61 if nx_q10_to_pct(512) != 50 { return 49 } 62 63 // === Family 5: number theory === 64 if nx_gcd(12, 18) != 6 { return 50 } 65 if nx_gcd(17, 5) != 1 { return 51 } 66 if nx_gcd(0, 7) != 7 { return 52 } 67 if nx_lcm(4, 6) != 12 { return 53 } 68 if nx_lcm(0, 5) != 0 { return 54 } 69 if nx_pow_int(2, 10) != 1024 { return 55 } 70 if nx_pow_int(3, 0) != 1 { return 56 } 71 if nx_pow_int(5, 3) != 125 { return 57 } 72 if nx_mod_floor(-5, 3) != 1 { return 58 } // floor mod: -5 mod 3 = 1 73 if nx_mod_floor(5, 3) != 2 { return 59 } 74 75 // === Family 6: bit ops === 76 // popcount(0xFF = 255) = 8 77 if nx_popcount(255) != 8 { return 60 } 78 if nx_popcount(0) != 0 { return 61 } 79 if nx_popcount(1) != 1 { return 62 } 80 // 0b1011 = 11 -> popcount 3 81 if nx_popcount(11) != 3 { return 63 } 82 // log2_floor 83 if nx_log2_floor(1) != 0 { return 64 } 84 if nx_log2_floor(2) != 1 { return 65 } 85 if nx_log2_floor(1023) != 9 { return 66 } 86 if nx_log2_floor(1024) != 10 { return 67 } 87 // log2_ceil 88 if nx_log2_ceil(1) != 0 { return 68 } 89 if nx_log2_ceil(2) != 1 { return 69 } 90 if nx_log2_ceil(3) != 2 { return 70 } 91 if nx_log2_ceil(1024) != 10 { return 71 } 92 if nx_log2_ceil(1025) != 11 { return 72 } 93 // bit ops 94 if nx_bit_test(5, 0) != 1 { return 73 } // 0b101 bit 0 = 1 95 if nx_bit_test(5, 1) != 0 { return 74 } // bit 1 = 0 96 if nx_bit_test(5, 2) != 1 { return 75 } // bit 2 = 1 97 if nx_bit_set(0, 3) != 8 { return 76 } // 0b1000 98 if nx_bit_clear(15, 1) != 13 { return 77 } // 0b1111 - 0b0010 = 0b1101 99 100 // === Family 7: byte / char === 101 let src: *u8 = sys_mmap(16) 102 src[0] = 65 103 src[1] = 66 104 src[2] = 67 105 src[3] = 68 106 let dst: *u8 = sys_mmap(16) 107 nx_memcpy(dst, src, 4) 108 if dst[0] != 65 { return 80 } 109 if dst[3] != 68 { return 81 } 110 // memset 111 nx_memset(dst, 88, 4) 112 if dst[0] != 88 { return 82 } 113 if dst[3] != 88 { return 83 } 114 // memcmp 115 if nx_memcmp(src, src, 4) != 0 { return 84 } 116 let other: *u8 = sys_mmap(16) 117 other[0] = 65 118 other[1] = 66 119 other[2] = 67 120 other[3] = 89 // higher 121 if nx_memcmp(src, other, 4) != -1 { return 85 } 122 if nx_memcmp(other, src, 4) != 1 { return 86 } 123 // char classification 124 if nx_char_is_digit(48) != 1 { return 90 } // '0' 125 if nx_char_is_digit(57) != 1 { return 91 } // '9' 126 if nx_char_is_digit(65) != 0 { return 92 } // 'A' 127 if nx_char_is_alpha(65) != 1 { return 93 } // 'A' 128 if nx_char_is_alpha(97) != 1 { return 94 } // 'a' 129 if nx_char_is_alpha(48) != 0 { return 95 } // '0' 130 if nx_char_is_alnum(48) != 1 { return 96 } 131 if nx_char_is_alnum(65) != 1 { return 97 } 132 if nx_char_is_alnum(33) != 0 { return 98 } // '!' 133 if nx_char_is_whitespace(32) != 1 { return 99 } // space 134 if nx_char_is_whitespace(9) != 1 { return 100 } // tab 135 if nx_char_is_whitespace(65) != 0 { return 101 } 136 if nx_to_lower(65) != 97 { return 102 } // 'A' -> 'a' 137 if nx_to_lower(48) != 48 { return 103 } // '0' unchanged 138 if nx_to_upper(97) != 65 { return 104 } // 'a' -> 'A' 139 if nx_hex_to_nibble(48) != 0 { return 105 } // '0' -> 0 140 if nx_hex_to_nibble(57) != 9 { return 106 } // '9' -> 9 141 if nx_hex_to_nibble(97) != 10 { return 107 } // 'a' -> 10 142 if nx_hex_to_nibble(70) != 15 { return 108 } // 'F' -> 15 143 if nx_nibble_to_hex(0) != 48 { return 109 } // 0 -> '0' 144 if nx_nibble_to_hex(15) != 102 { return 110 } // 15 -> 'f' 145 146 return 0 147}