| 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_in_headerless_preamble validates this v3 regression case. |
| 45 | fn test_c99_flag_emits_linux_feature_macros_in_headerless_preamble() { |
| 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 | typedef_idx := c_code.index('typedef signed char i8;') or { -1 } |
| 58 | assert gnu_idx >= 0, c_code[..200] |
| 59 | assert posix_idx >= 0, c_code[..200] |
| 60 | assert typedef_idx >= 0, c_code[..200] |
| 61 | assert gnu_idx < typedef_idx, c_code[..200] |
| 62 | assert posix_idx < typedef_idx, c_code[..200] |
| 63 | assert !c_code.contains('#include'), c_code[..200] |
| 64 | } |
| 65 | |
| 66 | // test_c99_flag_uses_headerless_stdatomic_fallback validates this v3 regression case. |
| 67 | fn test_c99_flag_uses_headerless_stdatomic_fallback() { |
| 68 | v3_bin := os.join_path(os.temp_dir(), 'v3_c99_stdatomic_test') |
| 69 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 70 | assert build.exit_code == 0, build.output |
| 71 | |
| 72 | src := os.join_path(os.temp_dir(), 'v3_c99_stdatomic_swap.v') |
| 73 | os.write_file(src, |
| 74 | "import sync.stdatomic\n\nfn main() {\n\tmut b := u8(1)\n\told_b_add := C.atomic_fetch_add_byte(voidptr(&b), u8(2))\n\tassert old_b_add == u8(1)\n\tassert b == u8(3)\n\told_b_sub := C.atomic_fetch_sub_byte(voidptr(&b), u8(1))\n\tassert old_b_sub == u8(3)\n\tassert b == u8(2)\n\n\tmut h := u16(10)\n\told_h_add := C.atomic_fetch_add_u16(voidptr(&h), u16(4))\n\tassert old_h_add == u16(10)\n\told_h_sub := C.atomic_fetch_sub_u16(voidptr(&h), u16(3))\n\tassert old_h_sub == u16(14)\n\tassert h == u16(11)\n\n\tmut w := u32(20)\n\told_w_sub := C.atomic_fetch_sub_u32(voidptr(&w), u32(5))\n\tassert old_w_sub == u32(20)\n\tassert w == u32(15)\n\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\tmut expected := u64(7)\n\tassert C.atomic_compare_exchange_strong_u64(voidptr(&x), &expected, u64(9))\n\tassert x == u64(9)\n\tprintln('atomic helpers ok')\n}\n") or { |
| 75 | panic(err) |
| 76 | } |
| 77 | |
| 78 | out_bin := os.join_path(os.temp_dir(), 'v3_c99_stdatomic_swap') |
| 79 | compile := os.execute('${v3_bin} -c99 ${src} -o ${out_bin}') |
| 80 | assert compile.exit_code == 0, compile.output |
| 81 | assert !compile.output.contains('C compilation failed'), compile.output |
| 82 | |
| 83 | run := os.execute(out_bin) |
| 84 | assert run.exit_code == 0, run.output |
| 85 | assert run.output.trim_space() == 'atomic helpers ok' |
| 86 | } |
| 87 | |
| 88 | // test_c99_flag_system_stdatomic_include_is_headerless validates this v3 regression case. |
| 89 | fn test_c99_flag_system_stdatomic_include_is_headerless() { |
| 90 | v3_bin := os.join_path(os.temp_dir(), 'v3_c99_system_stdatomic_test') |
| 91 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 92 | assert build.exit_code == 0, build.output |
| 93 | |
| 94 | src := os.join_path(os.temp_dir(), 'v3_c99_system_stdatomic.v') |
| 95 | os.write_file(src, |
| 96 | "#include <stdatomic.h>\n\nfn main() {\n\tprintln('system stdatomic ok')\n}\n") or { |
| 97 | panic(err) |
| 98 | } |
| 99 | |
| 100 | out_c := os.join_path(os.temp_dir(), 'v3_c99_system_stdatomic.c') |
| 101 | gen_c := os.execute('${v3_bin} -c99 ${src} -o ${out_c}') |
| 102 | assert gen_c.exit_code == 0, gen_c.output |
| 103 | c_code := os.read_file(out_c) or { panic(err) } |
| 104 | assert !c_code.contains('#include'), c_code |
| 105 | assert !c_code.contains('typedef volatile uintptr_t atomic_uintptr_t;'), c_code |
| 106 | assert c_code.contains('typedef void* atomic_uintptr_t;'), c_code |
| 107 | assert c_code.contains('static inline byte atomic_fetch_add_byte'), c_code |
| 108 | assert c_code.contains('static inline u16 atomic_fetch_add_u16'), c_code |
| 109 | assert c_code.contains('static inline u64 atomic_fetch_add_u64'), c_code |
| 110 | assert c_code.contains('static inline u16 atomic_fetch_sub_u16'), c_code |
| 111 | assert c_code.contains('static inline u32 atomic_fetch_sub_u32'), c_code |
| 112 | assert c_code.contains('static inline u64 atomic_exchange_u64'), c_code |
| 113 | assert c_code.contains('static inline bool atomic_compare_exchange_strong_u64'), c_code |
| 114 | |
| 115 | out_bin := os.join_path(os.temp_dir(), 'v3_c99_system_stdatomic') |
| 116 | compile := os.execute('${v3_bin} -prod -c99 ${src} -o ${out_bin}') |
| 117 | assert compile.exit_code == 0, compile.output |
| 118 | |
| 119 | run := os.execute(out_bin) |
| 120 | assert run.exit_code == 0, run.output |
| 121 | assert run.output.trim_space() == 'system stdatomic ok' |
| 122 | |
| 123 | gnu_bin := os.join_path(os.temp_dir(), 'v3_gnu11_system_stdatomic') |
| 124 | gnu_compile := os.execute('${v3_bin} -prod ${src} -o ${gnu_bin}') |
| 125 | assert gnu_compile.exit_code == 0, gnu_compile.output |
| 126 | |
| 127 | gnu_run := os.execute(gnu_bin) |
| 128 | assert gnu_run.exit_code == 0, gnu_run.output |
| 129 | assert gnu_run.output.trim_space() == 'system stdatomic ok' |
| 130 | } |
| 131 | |