code wiki / (root) / nx_macro_v3_test.nx

nx_macro_v3_test.nx source

↩ module page · 107 lines · 3024 B

1// nx_macro_v3_test.nx -- exercise @ifdef / @ifndef / @endif. 2 3import "nx_kernel_v2.nx" 4 5// Define a feature flag. Any @macro -- even a zero-arg empty one -- 6// is enough to satisfy @ifdef. 7@macro FEATURE_FAST 1 8@macro FEATURE_VERBOSE 1 9// FEATURE_SLOW intentionally NOT defined. 10 11// Block #1: kept (FEATURE_FAST is defined) 12@ifdef FEATURE_FAST 13func get_fast_path() -> nx_int { 14 return 100 15} 16@endif 17 18// Block #2: dropped (FEATURE_SLOW is not defined) 19@ifdef FEATURE_SLOW 20func get_slow_path() -> nx_int { 21 return BROKEN_SHOULD_NOT_COMPILE_AT_ALL 22} 23@endif 24 25// Block #3: kept (FEATURE_SLOW is undefined, @ifndef matches) 26@ifndef FEATURE_SLOW 27func get_no_slow_path() -> nx_int { 28 return 200 29} 30@endif 31 32// Block #4: dropped (FEATURE_FAST IS defined, @ifndef inverts) 33@ifndef FEATURE_FAST 34func ALSO_BROKEN_SHOULD_NOT_COMPILE() -> nx_int { 35 return BROKEN_SHOULD_NOT_COMPILE_AT_ALL_EITHER 36} 37@endif 38 39// Block #5: nested. Outer kept, inner kept. 40@ifdef FEATURE_FAST 41 @ifdef FEATURE_VERBOSE 42 func get_both_features() -> nx_int { 43 return 300 44 } 45 @endif 46@endif 47 48// Block #6: nested. Outer kept, inner DROPPED. 49@ifdef FEATURE_FAST 50 @ifdef FEATURE_SLOW 51 func SHOULD_NEVER_COMPILE() -> nx_int { 52 return BROKEN_SHOULD_NEVER_COMPILE 53 } 54 @endif 55@endif 56 57// Block #7: nested. Outer DROPPED (so inner also dropped regardless). 58@ifdef FEATURE_SLOW 59 @ifdef FEATURE_FAST 60 func OUTER_DROPPED_INNER_MOOT() -> nx_int { 61 return BROKEN_PROOF_THE_LEXER_NEVER_SEES_THIS 62 } 63 @endif 64@endif 65 66// ===== Tests ===== 67 68func test_fast_kept() -> nx_int { 69 let v: nx_int = get_fast_path() 70 if v != 100 { return 1 } 71 return 0 72} 73 74func test_no_slow_kept() -> nx_int { 75 let v: nx_int = get_no_slow_path() 76 if v != 200 { return 2 } 77 return 0 78} 79 80func test_nested_kept() -> nx_int { 81 let v: nx_int = get_both_features() 82 if v != 300 { return 3 } 83 return 0 84} 85 86func main() -> nx_exit { 87 println("=== nx_macro_v3 -- @ifdef / @ifndef / @endif smoke ===" as *u8) 88 89 let r1: nx_int = test_fast_kept() 90 if r1 != 0 { println("T1 fast_kept FAIL" as *u8); return r1 } 91 println("T1 fast_kept PASS @ifdef FEATURE_FAST kept block compiled + ran" as *u8) 92 93 let r2: nx_int = test_no_slow_kept() 94 if r2 != 0 { println("T2 no_slow_kept FAIL" as *u8); return r2 } 95 println("T2 no_slow_kept PASS @ifndef FEATURE_SLOW kept block compiled + ran" as *u8) 96 97 let r3: nx_int = test_nested_kept() 98 if r3 != 0 { println("T3 nested_kept FAIL" as *u8); return r3 } 99 println("T3 nested_kept PASS nested @ifdef FAST + @ifdef VERBOSE kept" as *u8) 100 101 println("" as *u8) 102 println("=== Drop verification (compile-time, no runtime check needed) ===" as *u8) 103 println("If @ifdef FEATURE_SLOW had been retained, this file would have failed" as *u8) 104 println("to compile -- BROKEN_SHOULD_NOT_COMPILE_AT_ALL is not a real identifier." as *u8) 105 println("The fact that compilation succeeded proves the drop paths were honored." as *u8) 106 return 0 107}