code wiki / (root) / nx_classical_unpatented_test.nx

nx_classical_unpatented_test.nx source

↩ module page · 96 lines · 3617 B

1// nx_classical_unpatented_test.nx -- umbrella smoke for the 10 classical 2// unpatented algorithms in nx_classical_unpatented.nx. One exit-code 3// space per algorithm (10s 20s 30s ...) so failures point at the broken 4// algorithm directly. 5 6import "nx_syscalls.nx" 7import "nx_tier.nx" 8import "nx_classical_unpatented.nx" 9 10func main() -> nx_exit { 11 // ----- 10s: GCD ----- 12 if nx_gcd_euclidean(48, 18) != 6 { return 11 } 13 if nx_gcd_euclidean(100, 75) != 25 { return 12 } 14 if nx_gcd_euclidean(13, 7) != 1 { return 13 } 15 if nx_gcd_euclidean(0, 5) != 5 { return 14 } 16 if nx_gcd_euclidean(5, 0) != 5 { return 15 } 17 18 // ----- 20s: LCM ----- 19 if nx_lcm(4, 6) != 12 { return 21 } 20 if nx_lcm(15, 20) != 60 { return 22 } 21 if nx_lcm(0, 7) != 0 { return 23 } 22 if nx_lcm(7, 0) != 0 { return 24 } 23 24 // ----- 30s: Fibonacci ----- 25 if nx_fibonacci(0) != 0 { return 31 } 26 if nx_fibonacci(1) != 1 { return 32 } 27 if nx_fibonacci(10) != 55 { return 33 } 28 if nx_fibonacci(20) != 6765 { return 34 } 29 30 // ----- 40s: Bubble sort ----- 31 let buf_b: *u8 = sys_mmap(80) 32 let ab: *nx_int = buf_b as *nx_int 33 ab[0] = 5; ab[1] = 3; ab[2] = 8; ab[3] = 1; ab[4] = 4 34 nx_bubble_sort(ab, 5) 35 if ab[0] != 1 { return 41 } 36 if ab[1] != 3 { return 42 } 37 if ab[2] != 4 { return 43 } 38 if ab[3] != 5 { return 44 } 39 if ab[4] != 8 { return 45 } 40 41 // ----- 50s: Insertion sort ----- 42 let buf_i: *u8 = sys_mmap(80) 43 let ai: *nx_int = buf_i as *nx_int 44 ai[0] = 9; ai[1] = 2; ai[2] = 7; ai[3] = 4; ai[4] = 1 45 nx_insertion_sort(ai, 5) 46 if ai[0] != 1 { return 51 } 47 if ai[1] != 2 { return 52 } 48 if ai[2] != 4 { return 53 } 49 if ai[3] != 7 { return 54 } 50 if ai[4] != 9 { return 55 } 51 52 // ----- 60s: Binary search ----- 53 let buf_s: *u8 = sys_mmap(80) 54 let as_arr: *nx_int = buf_s as *nx_int 55 as_arr[0] = 1; as_arr[1] = 3; as_arr[2] = 5; as_arr[3] = 7; as_arr[4] = 11 56 if nx_binary_search(as_arr, 5, 5) != 2 { return 61 } 57 if nx_binary_search(as_arr, 5, 1) != 0 { return 62 } 58 if nx_binary_search(as_arr, 5, 11) != 4 { return 63 } 59 if nx_binary_search(as_arr, 5, 6) != -1 { return 64 } 60 if nx_binary_search(as_arr, 5, 100) != -1 { return 65 } 61 62 // ----- 70s: Linear search ----- 63 let buf_l: *u8 = sys_mmap(80) 64 let al: *nx_int = buf_l as *nx_int 65 al[0] = 42; al[1] = 17; al[2] = 99; al[3] = 17 66 if nx_linear_search_idx(al, 4, 99) != 2 { return 71 } 67 if nx_linear_search_idx(al, 4, 17) != 1 { return 72 } 68 if nx_linear_search_idx(al, 4, 999) != -1 { return 73 } 69 if nx_linear_search_idx(al, 0, 42) != -1 { return 74 } 70 71 // ----- 80s: SWAR popcount ----- 72 if nx_popcount_swar(0) != 0 { return 81 } 73 if nx_popcount_swar(1) != 1 { return 82 } 74 if nx_popcount_swar(7) != 3 { return 83 } 75 if nx_popcount_swar(255) != 8 { return 84 } 76 if nx_popcount_swar(0x7FFFFFFFFFFFFFFF) != 63 { return 85 } 77 78 // ----- 90s: log2_floor ----- 79 if nx_log2_floor_int(1) != 0 { return 91 } 80 if nx_log2_floor_int(2) != 1 { return 92 } 81 if nx_log2_floor_int(3) != 1 { return 93 } 82 if nx_log2_floor_int(1024) != 10 { return 94 } 83 if nx_log2_floor_int(0) != -1 { return 95 } 84 85 // ----- 100s: Integer sqrt ----- 86 if nx_isqrt_newton(0) != 0 { return 101 } 87 if nx_isqrt_newton(1) != 1 { return 102 } 88 if nx_isqrt_newton(4) != 2 { return 103 } 89 if nx_isqrt_newton(9) != 3 { return 104 } 90 if nx_isqrt_newton(15) != 3 { return 105 } 91 if nx_isqrt_newton(16) != 4 { return 106 } 92 if nx_isqrt_newton(100) != 10 { return 107 } 93 if nx_isqrt_newton(99) != 9 { return 108 } 94 95 return 0 96}