| 1 | import os |
| 2 | |
| 3 | const vexe = @VEXE |
| 4 | const tests_dir = os.dir(@FILE) |
| 5 | const v3_dir = os.dir(tests_dir) |
| 6 | const v3_src = os.join_path(v3_dir, 'v3.v') |
| 7 | const hello_src = os.join_path(tests_dir, 'hello.v') |
| 8 | |
| 9 | // test_prod_flag_before_input_uses_optimized_c_compile validates this v3 regression case. |
| 10 | fn test_prod_flag_before_input_uses_optimized_c_compile() { |
| 11 | v3_bin := os.join_path(os.temp_dir(), 'v3_prod_flag_test') |
| 12 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 13 | assert build.exit_code == 0, build.output |
| 14 | |
| 15 | out_bin := os.join_path(os.temp_dir(), 'v3_prod_hello') |
| 16 | compile := os.execute('${v3_bin} -prod ${hello_src} -o ${out_bin}') |
| 17 | assert compile.exit_code == 0, compile.output |
| 18 | assert compile.output.contains('cc -std=gnu11 -O2') |
| 19 | assert !compile.output.contains('tcc.exe') |
| 20 | |
| 21 | run := os.execute(out_bin) |
| 22 | assert run.exit_code == 0, run.output |
| 23 | assert run.output.trim_space() == 'hello world' |
| 24 | } |
| 25 | |
| 26 | // test_c99_flag_uses_c99_c_compile_mode validates this v3 regression case. |
| 27 | fn test_c99_flag_uses_c99_c_compile_mode() { |
| 28 | v3_bin := os.join_path(os.temp_dir(), 'v3_c99_flag_test') |
| 29 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 30 | assert build.exit_code == 0, build.output |
| 31 | |
| 32 | out_bin := os.join_path(os.temp_dir(), 'v3_c99_hello') |
| 33 | compile := os.execute('${v3_bin} -prod -c99 ${hello_src} -o ${out_bin}') |
| 34 | assert compile.exit_code == 0, compile.output |
| 35 | assert compile.output.contains('cc -std=c99 -O2') |
| 36 | assert !compile.output.contains('cc -std=gnu11') |
| 37 | assert !compile.output.contains('tcc.exe') |
| 38 | |
| 39 | run := os.execute(out_bin) |
| 40 | assert run.exit_code == 0, run.output |
| 41 | assert run.output.trim_space() == 'hello world' |
| 42 | } |
| 43 | |
| 44 | // test_c99_flag_emits_linux_feature_macros_before_includes validates this v3 regression case. |
| 45 | fn test_c99_flag_emits_linux_feature_macros_before_includes() { |
| 46 | v3_bin := os.join_path(os.temp_dir(), 'v3_c99_feature_macro_test') |
| 47 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 48 | assert build.exit_code == 0, build.output |
| 49 | |
| 50 | out_c := os.join_path(os.temp_dir(), 'v3_c99_feature_macro_hello.c') |
| 51 | compile := os.execute('${v3_bin} -c99 ${hello_src} -o ${out_c}') |
| 52 | assert compile.exit_code == 0, compile.output |
| 53 | |
| 54 | c_code := os.read_file(out_c) or { panic(err) } |
| 55 | gnu_idx := c_code.index('#define _GNU_SOURCE') or { -1 } |
| 56 | posix_idx := c_code.index('#define _POSIX_C_SOURCE 200809L') or { -1 } |
| 57 | include_idx := c_code.index('#include <stdio.h>') or { -1 } |
| 58 | assert gnu_idx >= 0, c_code[..200] |
| 59 | assert posix_idx >= 0, c_code[..200] |
| 60 | assert include_idx >= 0, c_code[..200] |
| 61 | assert gnu_idx < include_idx, c_code[..200] |
| 62 | assert posix_idx < include_idx, c_code[..200] |
| 63 | } |
| 64 | |
| 65 | // test_c99_flag_preserves_stdatomic_header validates this v3 regression case. |
| 66 | fn test_c99_flag_preserves_stdatomic_header() { |
| 67 | v3_bin := os.join_path(os.temp_dir(), 'v3_c99_stdatomic_test') |
| 68 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 69 | assert build.exit_code == 0, build.output |
| 70 | |
| 71 | src := os.join_path(os.temp_dir(), 'v3_c99_stdatomic_swap.v') |
| 72 | os.write_file(src, |
| 73 | "import sync.stdatomic\n\nfn main() {\n\tmut x := u64(1)\n\told := C.atomic_exchange_u64(voidptr(&x), u64(7))\n\tassert old == u64(1)\n\tassert x == u64(7)\n\tprintln('atomic exchange ok')\n}\n") or { |
| 74 | panic(err) |
| 75 | } |
| 76 | |
| 77 | out_bin := os.join_path(os.temp_dir(), 'v3_c99_stdatomic_swap') |
| 78 | compile := os.execute('${v3_bin} -c99 ${src} -o ${out_bin}') |
| 79 | assert compile.exit_code == 0, compile.output |
| 80 | assert !compile.output.contains('C compilation failed'), compile.output |
| 81 | |
| 82 | run := os.execute(out_bin) |
| 83 | assert run.exit_code == 0, run.output |
| 84 | assert run.output.trim_space() == 'atomic exchange ok' |
| 85 | } |
| 86 | |
| 87 | // test_c99_flag_system_stdatomic_include_suppresses_fallback_typedef validates this v3 regression case. |
| 88 | fn test_c99_flag_system_stdatomic_include_suppresses_fallback_typedef() { |
| 89 | v3_bin := os.join_path(os.temp_dir(), 'v3_c99_system_stdatomic_test') |
| 90 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 91 | assert build.exit_code == 0, build.output |
| 92 | |
| 93 | src := os.join_path(os.temp_dir(), 'v3_c99_system_stdatomic.v') |
| 94 | os.write_file(src, |
| 95 | "#include <stdatomic.h>\n\nfn main() {\n\tprintln('system stdatomic ok')\n}\n") or { |
| 96 | panic(err) |
| 97 | } |
| 98 | |
| 99 | out_c := os.join_path(os.temp_dir(), 'v3_c99_system_stdatomic.c') |
| 100 | gen_c := os.execute('${v3_bin} -c99 ${src} -o ${out_c}') |
| 101 | assert gen_c.exit_code == 0, gen_c.output |
| 102 | c_code := os.read_file(out_c) or { panic(err) } |
| 103 | assert c_code.contains('#include <stdatomic.h>'), c_code |
| 104 | assert !c_code.contains('typedef volatile uintptr_t atomic_uintptr_t;'), c_code |
| 105 | assert c_code.contains('static inline u64 atomic_fetch_add_u64'), c_code |
| 106 | |
| 107 | out_bin := os.join_path(os.temp_dir(), 'v3_c99_system_stdatomic') |
| 108 | compile := os.execute('${v3_bin} -prod -c99 ${src} -o ${out_bin}') |
| 109 | assert compile.exit_code == 0, compile.output |
| 110 | |
| 111 | run := os.execute(out_bin) |
| 112 | assert run.exit_code == 0, run.output |
| 113 | assert run.output.trim_space() == 'system stdatomic ok' |
| 114 | |
| 115 | gnu_bin := os.join_path(os.temp_dir(), 'v3_gnu11_system_stdatomic') |
| 116 | gnu_compile := os.execute('${v3_bin} -prod ${src} -o ${gnu_bin}') |
| 117 | assert gnu_compile.exit_code == 0, gnu_compile.output |
| 118 | |
| 119 | gnu_run := os.execute(gnu_bin) |
| 120 | assert gnu_run.exit_code == 0, gnu_run.output |
| 121 | assert gnu_run.output.trim_space() == 'system stdatomic ok' |
| 122 | } |
| 123 | |