v / vlib / v2 / gen / cleanc / tests / run_tests.v
102 lines · 86 sloc · 2.58 KB · b831b0eec9b5b2756784b5dabf3808d47d6a39ae
Raw
1// Test runner for cleanc backend regression tests
2// Run with: ./vnew run vlib/v2/gen/cleanc/tests/run_tests.v
3// Or: cd vlib/v2/gen/cleanc/tests && v run run_tests.v
4
5module main
6
7import os
8import time
9
10fn main() {
11 t0 := time.now()
12
13 vroot := os.dir(@VEXE)
14 v2_source := os.join_path(vroot, 'cmd', 'v2', 'v2.v')
15 v2_binary := os.join_path(vroot, 'cmd', 'v2', 'v2')
16 tests_dir := os.join_path(vroot, 'vlib', 'v2', 'gen', 'cleanc', 'tests')
17
18 // Build v2 compiler (with v1).
19 println('[*] Building v2...')
20 build_res := os.execute('${@VEXE} -gc none -cc cc ${v2_source} -o ${v2_binary}')
21 if build_res.exit_code != 0 {
22 eprintln('Error: Failed to build v2')
23 eprintln(build_res.output)
24 exit(1)
25 }
26
27 // Find all test files (*.v except run_tests.v).
28 test_files := os.ls(tests_dir) or {
29 eprintln('Error: Cannot list tests directory')
30 exit(1)
31 }
32
33 mut passed := 0
34 mut failed := 0
35 mut test_count := 0
36
37 for file in test_files {
38 if !file.ends_with('.v') || file == 'run_tests.v' {
39 continue
40 }
41 test_count++
42 test_path := os.join_path(tests_dir, file)
43 test_name := file.replace('.v', '')
44
45 println('\n[*] Testing: ${file}')
46
47 // Run v2 with cleanc backend.
48 v2_output := os.join_path(tests_dir, 'test_${test_name}')
49 v2_cmd := '${v2_binary} -gc none -backend cleanc ${test_path} -o ${v2_output}'
50 v2_res := os.execute(v2_cmd)
51 if v2_res.exit_code != 0 {
52 eprintln(' [FAIL] v2 compilation failed')
53 eprintln(v2_res.output)
54 failed++
55 continue
56 }
57
58 // Run the generated binary.
59 gen_res := os.execute(v2_output)
60 if gen_res.exit_code != 0 {
61 eprintln(' [FAIL] Generated binary exited with code ${gen_res.exit_code}')
62 eprintln(gen_res.output)
63 failed++
64 continue
65 }
66
67 // Run reference compiler (v1).
68 ref_res := os.execute('${@VEXE} -gc none -n -w -enable-globals run ${test_path}')
69 if ref_res.exit_code != 0 {
70 eprintln(' [FAIL] Reference compilation failed')
71 eprintln(ref_res.output)
72 failed++
73 continue
74 }
75
76 // Compare outputs.
77 expected := ref_res.output.trim_space().replace('\r\n', '\n')
78 actual := gen_res.output.trim_space().replace('\r\n', '\n')
79
80 if expected == actual {
81 println(' [PASS] Output matches reference')
82 passed++
83 } else {
84 println(' [FAIL] Output mismatch')
85 println(' Expected:\n${expected}')
86 println(' Got:\n${actual}')
87 failed++
88 }
89
90 // Clean up.
91 os.rm(v2_output) or {}
92 os.rm('${v2_output}.c') or {}
93 }
94
95 println('\n========================================')
96 println('Results: ${passed}/${test_count} tests passed')
97 if failed > 0 {
98 println('${failed} tests FAILED')
99 exit(1)
100 }
101 println('Total time: ${time.since(t0)}')
102}
103