| 1 | import os |
| 2 | |
| 3 | const local_fixed_array_vexe = @VEXE |
| 4 | const local_fixed_array_tests_dir = os.dir(@FILE) |
| 5 | const local_fixed_array_v3_dir = os.dir(local_fixed_array_tests_dir) |
| 6 | const local_fixed_array_vlib_dir = os.dir(local_fixed_array_v3_dir) |
| 7 | const local_fixed_array_v3_src = os.join_path(local_fixed_array_v3_dir, 'v3.v') |
| 8 | |
| 9 | fn local_fixed_array_build_v3() string { |
| 10 | v3_bin := os.join_path(os.temp_dir(), 'v3_fixed_array_local_zero_test_${os.getpid()}') |
| 11 | os.rm(v3_bin) or {} |
| 12 | build := |
| 13 | os.execute('${local_fixed_array_vexe} -gc none -path "${local_fixed_array_vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${local_fixed_array_v3_src}') |
| 14 | assert build.exit_code == 0, build.output |
| 15 | return v3_bin |
| 16 | } |
| 17 | |
| 18 | fn test_local_fixed_array_zero_init_declarations_use_direct_c_arrays() { |
| 19 | v3_bin := local_fixed_array_build_v3() |
| 20 | src := os.join_path(os.temp_dir(), 'v3_fixed_array_local_zero_${os.getpid()}.v') |
| 21 | os.write_file(src, 'module main |
| 22 | |
| 23 | type Handle = voidptr |
| 24 | |
| 25 | fn local_score() int { |
| 26 | mut direct := [4]int{} |
| 27 | direct[0] = 1 |
| 28 | mut wrapped := unsafe { [4]int{} } |
| 29 | wrapped[1] = 2 |
| 30 | mut handles := unsafe { [32]Handle{} } |
| 31 | handles[0] = voidptr(0) |
| 32 | mut nested := unsafe { [2][3]int{} } |
| 33 | nested[1][2] = 3 |
| 34 | handle_score := if handles[0] == voidptr(0) { 4 } else { 0 } |
| 35 | return direct[0] + wrapped[1] + nested[1][2] + handle_score |
| 36 | } |
| 37 | |
| 38 | fn main() { |
| 39 | println(int_str(local_score())) |
| 40 | } |
| 41 | ') or { |
| 42 | panic(err) |
| 43 | } |
| 44 | bin := os.join_path(os.temp_dir(), 'v3_fixed_array_local_zero_${os.getpid()}') |
| 45 | compile := os.execute('${v3_bin} ${src} -b c -o ${bin}') |
| 46 | assert compile.exit_code == 0, compile.output |
| 47 | run := os.execute(bin) |
| 48 | assert run.exit_code == 0, run.output |
| 49 | assert run.output.trim_space() == '10' |
| 50 | generated := os.read_file(bin + '.c') or { panic(err) } |
| 51 | assert generated.contains('int direct[4] = {0};'), generated |
| 52 | assert generated.contains('int wrapped[4] = {0};'), generated |
| 53 | assert generated.contains('handles[32] = {0};'), generated |
| 54 | assert generated.contains('int nested[2][3] = {0};'), generated |
| 55 | assert !generated.contains(' = (Array_fixed_'), generated |
| 56 | assert !generated.contains('Array_fixed_voidptr_32 handles'), generated |
| 57 | } |
| 58 | |