v / vlib / compress / zlib / interop / zlib_interop.vsh
185 lines · 162 sloc · 5.28 KB · 83fd036a98cc7d2e91992898416169f47329240f
Raw
1#!/usr/bin/env -S v
2
3import compress.zlib
4
5struct TestVector {
6 name string
7 data []u8
8}
9
10fn main() {
11 tmp_root := join_path(temp_dir(), 'v_zlib_interop_${getpid()}')
12 mkdir_all(tmp_root) or { panic(err) }
13 defer {
14 rmdir_all(tmp_root) or {}
15 }
16
17 c_bin := compile_c_helper(tmp_root) or {
18 eprintln('SKIP: ${err.msg()}')
19 exit(2)
20 }
21 py_script := join_path(@DIR, 'zlib_ref.py')
22 ensure_python(py_script) or {
23 eprintln('SKIP: ${err.msg()}')
24 exit(2)
25 }
26
27 vectors := make_test_vectors()
28 mut total_checks := 0
29 for i, vec in vectors {
30 total_checks += run_case(tmp_root, c_bin, py_script, i, vec) or {
31 eprintln('FAIL: ${vec.name}: ${err.msg()}')
32 exit(1)
33 0
34 }
35 println('ok ${i + 1}/${vectors.len}: ${vec.name} (${vec.data.len} bytes)')
36 }
37 println('PASS: ${vectors.len} vectors, ${total_checks} cross-checks')
38}
39
40fn compile_c_helper(tmp_root string) !string {
41 cc := choose_cc()
42 if cc == '' {
43 return error('no C compiler found (tried cc, gcc, clang)')
44 }
45 src := join_path(@DIR, 'zlib_ref.c')
46 bin := join_path(tmp_root, 'zlib_interop_ref')
47 must_succeed('${cc} -O2 ${shell_quote(src)} -lz -o ${shell_quote(bin)}',
48 'C zlib helper build failed')!
49 return bin
50}
51
52fn choose_cc() string {
53 for cc in ['cc', 'gcc', 'clang'] {
54 if execute('${cc} --version >/dev/null 2>&1').exit_code == 0 {
55 return cc
56 }
57 }
58 return ''
59}
60
61fn ensure_python(py_script string) ! {
62 must_succeed("python3 -c 'import zlib' >/dev/null 2>&1",
63 'python3 with zlib module is not available')!
64 if !exists(py_script) {
65 return error('missing Python helper: ${py_script}')
66 }
67}
68
69fn make_test_vectors() []TestVector {
70 mut vectors := []TestVector{}
71 vectors << TestVector{'empty', []u8{}}
72 vectors << TestVector{'ascii_text', 'The quick brown fox jumps over the lazy dog.\n'.repeat(64).bytes()}
73 vectors << TestVector{'repeated_byte', []u8{len: 10000, init: `A`}}
74 vectors << TestVector{'all_bytes_x4', all_bytes_repeated(4)}
75 vectors << TestVector{'lcg_64k', lcg_bytes(65536)}
76 return vectors
77}
78
79fn run_case(tmp_root string, c_bin string, py_script string, case_idx int, vec TestVector) !int {
80 case_dir := join_path(tmp_root, 'case_${case_idx:02}_${vec.name}')
81 mkdir_all(case_dir)!
82
83 v_z := zlib.compress(vec.data)!
84 c_z := c_compress(case_dir, c_bin, vec.data)!
85 py_z := py_compress(case_dir, py_script, vec.data)!
86
87 mut checks := 0
88 producers := {
89 'v': v_z
90 'c': c_z
91 'py': py_z
92 }
93 for producer, compressed in producers {
94 v_plain := zlib.decompress(compressed)!
95 assert_equal_bytes('v.decompress(${producer}.compress)', vec.data, v_plain)!
96 checks++
97
98 c_plain := c_decompress(case_dir, c_bin, producer, compressed)!
99 assert_equal_bytes('c.decompress(${producer}.compress)', vec.data, c_plain)!
100 checks++
101
102 py_plain := py_decompress(case_dir, py_script, producer, compressed)!
103 assert_equal_bytes('python.decompress(${producer}.compress)', vec.data, py_plain)!
104 checks++
105 }
106 return checks
107}
108
109fn c_compress(case_dir string, c_bin string, plain []u8) ![]u8 {
110 in_path := join_path(case_dir, 'plain.in')
111 out_path := join_path(case_dir, 'c.zlib')
112 write_file_array(in_path, plain)!
113 must_succeed('${shell_quote(c_bin)} compress ${shell_quote(in_path)} ${shell_quote(out_path)}',
114 'C zlib compression failed')!
115 return read_bytes(out_path)!
116}
117
118fn c_decompress(case_dir string, c_bin string, producer string, compressed []u8) ![]u8 {
119 in_path := join_path(case_dir, '${producer}.for_c.zlib')
120 out_path := join_path(case_dir, '${producer}.from_c.out')
121 write_file_array(in_path, compressed)!
122 must_succeed('${shell_quote(c_bin)} decompress ${shell_quote(in_path)} ${shell_quote(out_path)}',
123 'C zlib decompression failed')!
124 return read_bytes(out_path)!
125}
126
127fn py_compress(case_dir string, py_script string, plain []u8) ![]u8 {
128 in_path := join_path(case_dir, 'plain_py.in')
129 out_path := join_path(case_dir, 'py.zlib')
130 write_file_array(in_path, plain)!
131 must_succeed('python3 ${shell_quote(py_script)} compress ${shell_quote(in_path)} ${shell_quote(out_path)}',
132 'Python zlib compression failed')!
133 return read_bytes(out_path)!
134}
135
136fn py_decompress(case_dir string, py_script string, producer string, compressed []u8) ![]u8 {
137 in_path := join_path(case_dir, '${producer}.for_py.zlib')
138 out_path := join_path(case_dir, '${producer}.from_py.out')
139 write_file_array(in_path, compressed)!
140 must_succeed('python3 ${shell_quote(py_script)} decompress ${shell_quote(in_path)} ${shell_quote(out_path)}',
141 'Python zlib decompression failed')!
142 return read_bytes(out_path)!
143}
144
145fn all_bytes_repeated(times int) []u8 {
146 mut out := []u8{cap: 256 * times}
147 for _ in 0 .. times {
148 for i in 0 .. 256 {
149 out << u8(i)
150 }
151 }
152 return out
153}
154
155fn lcg_bytes(n int) []u8 {
156 mut out := []u8{len: n}
157 mut x := u32(0x12345678)
158 for i in 0 .. n {
159 x = x * u32(1664525) + u32(1013904223)
160 out[i] = u8((x >> 16) & u32(0xff))
161 }
162 return out
163}
164
165fn assert_equal_bytes(label string, expected []u8, got []u8) ! {
166 if expected.len != got.len {
167 return error('${label}: length mismatch expected=${expected.len} got=${got.len}')
168 }
169 for i in 0 .. expected.len {
170 if expected[i] != got[i] {
171 return error('${label}: byte mismatch at offset ${i}')
172 }
173 }
174}
175
176fn must_succeed(command string, context string) ! {
177 res := execute(command)
178 if res.exit_code != 0 {
179 return error('${context}\ncommand: ${command}\nexit_code: ${res.exit_code}\n${res.output}')
180 }
181}
182
183fn shell_quote(s string) string {
184 return "'${s.replace("'", "'\\''")}'"
185}
186