| 1 | import os |
| 2 | |
| 3 | const c_pointer_null_vexe = @VEXE |
| 4 | const c_pointer_null_tests_dir = os.dir(@FILE) |
| 5 | const c_pointer_null_v3_dir = os.dir(c_pointer_null_tests_dir) |
| 6 | const c_pointer_null_vlib_dir = os.dir(c_pointer_null_v3_dir) |
| 7 | const c_pointer_null_v3_src = os.join_path(c_pointer_null_v3_dir, 'v3.v') |
| 8 | |
| 9 | fn c_pointer_null_build_v3() string { |
| 10 | v3_bin := os.join_path(os.temp_dir(), 'v3_c_pointer_null_test_${os.getpid()}') |
| 11 | os.rm(v3_bin) or {} |
| 12 | build := |
| 13 | os.execute('${c_pointer_null_vexe} -gc none -path "${c_pointer_null_vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${c_pointer_null_v3_src}') |
| 14 | assert build.exit_code == 0, build.output |
| 15 | return v3_bin |
| 16 | } |
| 17 | |
| 18 | fn c_pointer_null_write_source() string { |
| 19 | src := os.join_path(os.temp_dir(), 'v3_c_pointer_null_${os.getpid()}.v') |
| 20 | os.write_file(src, 'module main |
| 21 | |
| 22 | fn C.take_charptr(ptr &char) |
| 23 | fn C.take_charptrptr(ptr &&char) |
| 24 | |
| 25 | fn main() { |
| 26 | C.take_charptrptr(nil) |
| 27 | C.take_charptr(&char(0)) |
| 28 | } |
| 29 | ') or { |
| 30 | panic(err) |
| 31 | } |
| 32 | return src |
| 33 | } |
| 34 | |
| 35 | fn test_nil_for_c_pointer_to_pointer_arg_emits_null() { |
| 36 | v3_bin := c_pointer_null_build_v3() |
| 37 | src := c_pointer_null_write_source() |
| 38 | c_path := src + '.c' |
| 39 | compile := os.execute('${v3_bin} ${src} -b c -o ${c_path}') |
| 40 | assert compile.exit_code == 0, compile.output |
| 41 | |
| 42 | c_code := os.read_file(c_path) or { panic(err) } |
| 43 | compact := c_code.replace('\t', '').replace(' ', '').replace('\n', '') |
| 44 | assert compact.contains('take_charptrptr(NULL);'), c_code |
| 45 | assert !compact.contains('take_charptrptr((char*)(0));'), c_code |
| 46 | assert compact.contains('take_charptr((char*)(0));'), c_code |
| 47 | } |
| 48 | |