| 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 | fn 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 | |