v4 / vlib / v3 / tests / union_byte_contains_codegen_test.v
36 lines · 31 sloc · 1.5 KB · 6c4d26f1f98c5464b012a375667ab345e462f7e0
Raw
1import os
2
3const vexe = @VEXE
4const tests_dir = os.dir(@FILE)
5const v3_dir = os.dir(tests_dir)
6const v3_src = os.join_path(v3_dir, 'v3.v')
7
8// build_v3 builds v3 data for v3 tests.
9fn build_v3() string {
10 v3_bin := os.join_path(os.temp_dir(), 'v3_union_byte_contains_codegen_test')
11 build := os.execute('${vexe} -o ${v3_bin} ${v3_src}')
12 assert build.exit_code == 0
13 return v3_bin
14}
15
16// run_good supports run good handling for v3 tests.
17fn run_good(v3_bin string, name string, source string) string {
18 src := os.join_path(os.temp_dir(), 'v3_${name}.v')
19 os.write_file(src, source) or { panic(err) }
20 bin := os.join_path(os.temp_dir(), 'v3_${name}')
21 compile := os.execute('${v3_bin} ${src} -b c -o ${bin}')
22 assert compile.exit_code == 0
23 assert !compile.output.contains('C compilation failed')
24
25 run := os.execute(bin)
26 assert run.exit_code == 0
27 return run.output.trim_space()
28}
29
30// test_union_aliasing_and_byte_array_contains_codegen validates this v3 regression case.
31fn test_union_aliasing_and_byte_array_contains_codegen() {
32 v3_bin := build_v3()
33 out := run_good(v3_bin, 'union_byte_contains_codegen_input',
34 "union Bits {\nmut:\n\tf f64\n\tu u64\n}\n\nfn main() {\n\tmut u := Bits{}\n\tu.f = 12.5\n\tif u.u != u64(0) {\n\t\tprintln('union ok')\n\t} else {\n\t\tprintln('union bad')\n\t}\n\tmut bytes := []u8{len: 4, init: 0}\n\tbytes[1] = `.`\n\tif `.` in bytes {\n\t\tprintln('byte contains ok')\n\t} else {\n\t\tprintln('byte contains bad')\n\t}\n\tprintln('${12.5}')\n}\n")
35 assert out == 'union ok\nbyte contains ok\n12.5'
36}
37