| 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 | |
| 8 | // Slicing a fixed array must evaluate the slice bounds exactly once, so |
| 9 | // side-effecting bound expressions are not run multiple times in the generated C. |
| 10 | fn test_fixed_array_slice_evaluates_bounds_once() { |
| 11 | v3_bin := os.join_path(os.temp_dir(), 'v3_fixed_slice_test') |
| 12 | build := os.execute('${vexe} -o ${v3_bin} ${v3_src}') |
| 13 | assert build.exit_code == 0, build.output |
| 14 | |
| 15 | src := ' |
| 16 | fn start_idx() int { |
| 17 | println("eval") |
| 18 | return 1 |
| 19 | } |
| 20 | |
| 21 | fn main() { |
| 22 | mut a := [3]int{} |
| 23 | s := a[start_idx()..3] |
| 24 | println(s.len) |
| 25 | } |
| 26 | ' |
| 27 | src_file := os.join_path(os.temp_dir(), 'v3_fixed_slice.v') |
| 28 | os.write_file(src_file, src) or { panic(err) } |
| 29 | bin := os.join_path(os.temp_dir(), 'v3_fixed_slice_bin') |
| 30 | os.rm(bin) or {} |
| 31 | compile := os.execute('${v3_bin} ${src_file} -b c -o ${bin}') |
| 32 | assert compile.exit_code == 0, compile.output |
| 33 | run := os.execute(bin) |
| 34 | assert run.exit_code == 0, run.output |
| 35 | assert run.output.count('eval') == 1, run.output |
| 36 | } |
| 37 | |