v4 / vlib / v3 / tests / c_string_literal_pointer_codegen_test.v
30 lines · 27 sloc · 1.45 KB · 288feee4702b35beadb4037d02b96b5c8674156b
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
8fn test_c_string_literal_address_codegen_preserves_regular_addresses() {
9 v3_bin := os.join_path(os.temp_dir(), 'v3_c_string_literal_pointer_test')
10 build := os.execute('${vexe} -gc none -o ${v3_bin} ${v3_src}')
11 assert build.exit_code == 0, build.output
12
13 src := os.join_path(os.temp_dir(), 'v3_c_string_literal_pointer_input.v')
14 os.write_file(src,
15 "fn C.puts(&char) int\n\nfn takes_string_ptr(s &string) int {\n\treturn s.len\n}\n\nfn takes_byte_ptr(b &u8) int {\n\treturn int(*b)\n}\n\nfn main() {\n\tC.puts(&c'canary')\n\ttext := 'ordinary'\n\ttext_len := takes_string_ptr(&text)\n\tch := u8(65)\n\tch_value := takes_byte_ptr(&ch)\n\tprintln(int_str(text_len + ch_value))\n}\n") or {
16 panic(err)
17 }
18 bin := os.join_path(os.temp_dir(), 'v3_c_string_literal_pointer_input')
19 compile := os.execute('${v3_bin} ${src} -b c -o ${bin}')
20 assert compile.exit_code == 0, compile.output
21 assert !compile.output.contains('C compilation failed'), compile.output
22 c_code := os.read_file(bin + '.c') or { panic(err) }
23 assert c_code.contains('puts("canary");'), c_code
24 assert !c_code.contains('puts(&"canary");'), c_code
25 assert c_code.contains('takes_string_ptr(&text)'), c_code
26 assert c_code.contains('takes_byte_ptr(&ch)'), c_code
27 run := os.execute(bin)
28 assert run.exit_code == 0, run.output
29 assert run.output.trim_space() == 'canary\n73'
30}
31