v2 / vlib / v / tests / builtin_arrays / array_access_optimisation_test.v
53 lines · 46 sloc · 1.07 KB · bf29124d3b7d766aca92767dd63a4eb7b4a3ba81
Raw
1// vtest retry: 3
2import os
3
4const test = @VEXEROOT + '/vlib/v/tests/testdata/test_array_bound.v'
5
6fn direct(line string) {
7 if !line.contains('\tmain__direct(') {
8 return
9 }
10 trimmed := line.trim_space()
11 if trimmed.contains('array_get') {
12 assert trimmed == 'this should have been a direct access in ${test} line ${line}'
13 }
14}
15
16fn access(line string) {
17 if !line.contains('\tmain__access(') {
18 return
19 }
20 trimmed := line.trim_space()
21 if !trimmed.contains('array_get') {
22 assert trimmed == 'this should have been an array access in ${test} line ${line}'
23 }
24}
25
26fn test_array_optimisation() {
27 mut args := []string{cap: 4}
28 args << '-prod'
29 args << test
30 args << '-o'
31 args << '-'
32
33 mut p := os.new_process(@VEXE)
34 p.set_args(args)
35 p.set_redirect_stdio()
36 p.run()
37 stdout := p.stdout_slurp()
38 p.wait()
39 p.close()
40
41 ending := '// THE END.'
42 it_ends_properly := stdout.contains(ending)
43 if !it_ends_properly {
44 dump(stdout)
45 }
46 assert it_ends_properly
47
48 for line in stdout.split('\n') {
49 direct(line)
50 access(line)
51 }
52 println('ok, could not detect any wrong memory access optimisation')
53}
54