Debian Codegraph @

test/integers_repr.ml

raw symbols SCIP
1 open Core
2 open Poly
3 open Import
4
5 (* This module generates reference serialized output for various functions of
6 [Bin_prot.Write]. The expected output is checked in the files [integers_repr_32bit.ml]
7 [integers_repr_64bit.ml] using inline expect tests. This way when we change something
8 in bin_prot there is a check that the representation of integers doesn't change. The
9 diff is easy to understand.
10
11 We can't generate reference output for every possible values as it would be huge,
12 instead we choose a few interesting points and generate tests in a window around
13 them. The points we choose are:
14
15 - 0
16 - min value
17 - max value
18 - all powers of 2 between min and max
19 - all points where the length of the serialized output change
20
21 For the last item, instead of hard-coding these points for every function, we find
22 them. This is to avoid errors. To find them we make the assumption that functions using
23 a variable length encoding respect the following:
24
25 - the encoded size is decreasing from min value to 0
26 - the encoded size is increasing from 0 to max value
27
28 Which is the basic assumption make by bin_prot: integers close to 0 are more frequent
29 and should occupy less space.
30 *)
31
32 (* Number of tests to generate around each point *)
33 let test_window_len = 16L
34
35 module Read = Bin_prot.Read
36 module Write = Bin_prot.Write
37
38 type 'a to_test =
39 { name : string
40 ; writer : 'a Write.writer
41 ; writer_local : 'a Write.writer_local
42 ; reader : 'a Read.reader
43 ; to_int64 : 'a -> Int64.t
44 ; of_int64 : Int64.t -> 'a
45 ; min : 'a
46 ; max : 'a (* Bounds on the bin_protted size *)
47 ; hi_bound : int
48 ; lo_bound : int
49 }
50
51 type to_test_packed = T : _ to_test -> to_test_packed
52
53 let min_int_32bit, max_int_32bit, min_int_64bit, max_int_64bit =
54 match Word_size.word_size with
55 | W32 -> -1 lsl 30, (1 lsl 30) - 1, -1 lsl 30, (1 lsl 30) - 1
56 | W64 -> -1 lsl 31, (1 lsl 31) - 1, -1 lsl 62, (1 lsl 62) - 1
57 ;;
58
59 module Nat0 = Bin_prot.Nat0
60
61 let tests =
62 [ T
63 { name = "int"
64 ; writer = Write.bin_write_int
65 ; writer_local = Write.bin_write_int__local
66 ; reader = Read.bin_read_int
67 ; to_int64 = Int64.of_int
68 ; of_int64 = Int64.to_int_exn
69 ; min = Int.min_value
70 ; max = Int.max_value
71 ; hi_bound = Maximum.bin_size_int
72 ; lo_bound = Minimum.bin_size_int
73 }
74 ; T
75 { name = "int32"
76 ; writer = Write.bin_write_int32
77 ; writer_local = Write.bin_write_int32__local
78 ; reader = Read.bin_read_int32
79 ; to_int64 = Int64.of_int32
80 ; of_int64 = Int64.to_int32_exn
81 ; min = Int32.min_value
82 ; max = Int32.max_value
83 ; hi_bound = Maximum.bin_size_int32
84 ; lo_bound = Minimum.bin_size_int32
85 }
86 ; T
87 { name = "int64"
88 ; writer = Write.bin_write_int64
89 ; writer_local = Write.bin_write_int64__local
90 ; reader = Read.bin_read_int64
91 ; to_int64 = Fn.id
92 ; of_int64 = Fn.id
93 ; min = Int64.min_value
94 ; max = Int64.max_value
95 ; hi_bound = Maximum.bin_size_int64
96 ; lo_bound = Minimum.bin_size_int64
97 }
98 ; T
99 { name = "nat0"
100 ; writer = Write.bin_write_nat0
101 ; writer_local = Write.bin_write_nat0__local
102 ; reader = Read.bin_read_nat0
103 ; to_int64 = (fun x -> Int64.of_int (x : Nat0.t :> int))
104 ; of_int64 = (fun x -> Nat0.of_int (Int64.to_int_exn x))
105 ; min = Nat0.of_int 0
106 ; max = Nat0.of_int Int.max_value
107 ; hi_bound = Maximum.bin_size_nat0
108 ; lo_bound = Minimum.bin_size_nat0
109 }
110 ; T
111 { name = "variant_int"
112 ; writer = Write.bin_write_variant_int
113 ; writer_local = Write.bin_write_variant_int__local
114 ; reader = Read.bin_read_variant_int
115 ; to_int64 = Int64.of_int
116 ; of_int64 = Int64.to_int_exn
117 ; min = -1 lsl 30
118 ; max = (1 lsl 30) - 1
119 ; hi_bound = Maximum.bin_size_variant_int
120 ; lo_bound = Minimum.bin_size_variant_int
121 }
122 ; T
123 { name = "int_16bit"
124 ; writer = Write.bin_write_int_16bit
125 ; writer_local = Write.bin_write_int_16bit__local
126 ; reader = Read.bin_read_int_16bit
127 ; to_int64 = Int64.of_int
128 ; of_int64 = Int64.to_int_exn
129 ; min = 0
130 ; max = (1 lsl 16) - 1
131 ; hi_bound = Maximum.bin_size_int_16bit
132 ; lo_bound = Minimum.bin_size_int_16bit
133 }
134 ; T
135 { name = "int_32bit"
136 ; writer = Write.bin_write_int_32bit
137 ; writer_local = Write.bin_write_int_32bit__local
138 ; reader = Read.bin_read_int_32bit
139 ; to_int64 = Int64.of_int
140 ; of_int64 = Int64.to_int_exn
141 ; min = min_int_32bit
142 ; max = max_int_32bit
143 ; hi_bound = Maximum.bin_size_int_32bit
144 ; lo_bound = Minimum.bin_size_int_32bit
145 }
146 ; T
147 { name = "int_64bit"
148 ; writer = Write.bin_write_int_64bit
149 ; writer_local = Write.bin_write_int_64bit__local
150 ; reader = Read.bin_read_int_64bit
151 ; to_int64 = Int64.of_int
152 ; of_int64 = Int64.to_int_exn
153 ; min = min_int_64bit
154 ; max = max_int_64bit
155 ; hi_bound = Maximum.bin_size_int_64bit
156 ; lo_bound = Minimum.bin_size_int_64bit
157 }
158 ; T
159 { name = "int64_bits"
160 ; writer = Write.bin_write_int64_bits
161 ; writer_local = Write.bin_write_int64_bits__local
162 ; reader = Read.bin_read_int64_bits
163 ; to_int64 = Fn.id
164 ; of_int64 = Fn.id
165 ; min = Int64.min_value
166 ; max = Int64.max_value
167 ; hi_bound = Maximum.bin_size_int64_bits
168 ; lo_bound = Minimum.bin_size_int64_bits
169 }
170 ; T
171 { name = "network16_int"
172 ; writer = Write.bin_write_network16_int
173 ; writer_local = Write.bin_write_network16_int__local
174 ; reader = Read.bin_read_network16_int
175 ; to_int64 = Int64.of_int
176 ; of_int64 = Int64.to_int_exn
177 ; min = 0
178 ; max = (1 lsl 16) - 1
179 ; hi_bound = Maximum.bin_size_network16_int
180 ; lo_bound = Minimum.bin_size_network16_int
181 }
182 ; T
183 { name = "network32_int"
184 ; writer = Write.bin_write_network32_int
185 ; writer_local = Write.bin_write_network32_int__local
186 ; reader = Read.bin_read_network32_int
187 ; to_int64 = Int64.of_int
188 ; of_int64 = Int64.to_int_exn
189 ; min = min_int_32bit
190 ; max = max_int_32bit
191 ; hi_bound = Maximum.bin_size_network32_int
192 ; lo_bound = Minimum.bin_size_network32_int
193 }
194 ; T
195 { name = "network64_int"
196 ; writer = Write.bin_write_network64_int
197 ; writer_local = Write.bin_write_network64_int__local
198 ; reader = Read.bin_read_network64_int
199 ; to_int64 = Int64.of_int
200 ; of_int64 = Int64.to_int_exn
201 ; min = min_int_64bit
202 ; max = max_int_64bit
203 ; hi_bound = Maximum.bin_size_network64_int
204 ; lo_bound = Minimum.bin_size_network64_int
205 }
206 ; T
207 { name = "network32_int32"
208 ; writer = Write.bin_write_network32_int32
209 ; writer_local = Write.bin_write_network32_int32__local
210 ; reader = Read.bin_read_network32_int32
211 ; to_int64 = Int64.of_int32
212 ; of_int64 = Int64.to_int32_exn
213 ; min = Int32.min_value
214 ; max = Int32.max_value
215 ; hi_bound = Maximum.bin_size_network32_int32
216 ; lo_bound = Minimum.bin_size_network32_int32
217 }
218 ; T
219 { name = "network64_int64"
220 ; writer = Write.bin_write_network64_int64
221 ; writer_local = Write.bin_write_network64_int64__local
222 ; reader = Read.bin_read_network64_int64
223 ; to_int64 = Fn.id
224 ; of_int64 = Fn.id
225 ; min = Int64.min_value
226 ; max = Int64.max_value
227 ; hi_bound = Maximum.bin_size_network64_int64
228 ; lo_bound = Minimum.bin_size_network64_int64
229 }
230 ]
231 ;;
232
233 let buf = Bigstring.create 32
234 let bin_protted_size_of t n = t.writer buf ~pos:0 (t.of_int64 n)
235
236 let mean a b =
237 if Bool.equal (a < 0L) (b < 0L)
238 then Int64.O.(b + Int64.shift_right (a - b) 1)
239 else Int64.O.(Int64.shift_right (a + b) 1)
240 ;;
241
242 let rec find_size_increase t ~size a b =
243 assert (a < b);
244 let m = mean a b in
245 let n = Int64.succ m in
246 assert (n <= b);
247 let size_m = bin_protted_size_of t m in
248 let size_n = bin_protted_size_of t n in
249 assert (size_m <= size_n);
250 if size_m = size && size_m < size_n
251 then m
252 else if size_m <= size
253 then find_size_increase t ~size (Int64.succ m) b
254 else find_size_increase t ~size a m
255 ;;
256
257 let rec find_size_decrease t ~size a b =
258 assert (a < b);
259 let m = mean a b in
260 let n = Int64.succ m in
261 assert (n <= b);
262 let size_m = bin_protted_size_of t m in
263 let size_n = bin_protted_size_of t n in
264 assert (size_m >= size_n);
265 if size_m = size && size_m > size_n
266 then m
267 else if size_n >= size
268 then find_size_decrease t ~size n b
269 else find_size_decrease t ~size a m
270 ;;
271
272 let rec find_size_increase_points t ~size1 ~size2 a b acc =
273 if size1 = size2
274 then acc
275 else (
276 let p = find_size_increase t ~size:size1 a b in
277 let a = Int64.succ p in
278 let size1 = bin_protted_size_of t a in
279 find_size_increase_points t ~size1 ~size2 a b (Set.add acc p))
280 ;;
281
282 let rec find_size_decrease_points t ~size1 ~size2 a b acc =
283 if size1 = size2
284 then acc
285 else (
286 let p = find_size_decrease t ~size:size1 a b in
287 let a = Int64.succ p in
288 let size1 = bin_protted_size_of t a in
289 find_size_decrease_points t ~size1 ~size2 a b (Set.add acc p))
290 ;;
291
292 let find_interesting_points t =
293 let a = t.to_int64 t.min in
294 let b = t.to_int64 t.max in
295 assert (a <= 0L && b >= 0L);
296 let size0 = bin_protted_size_of t 0L in
297 let acc = Int64.Set.of_list [ 0L; a; b ] in
298 let acc =
299 if a < 0L
300 then
301 find_size_decrease_points t ~size1:(bin_protted_size_of t a) ~size2:size0 a 0L acc
302 else acc
303 in
304 let acc =
305 if b > 0L
306 then
307 find_size_increase_points t ~size1:size0 ~size2:(bin_protted_size_of t b) 0L b acc
308 else acc
309 in
310 acc
311 ;;
312
313 (* { 2 ^ n | 0 <= n <= 63 } \/ { -(2 ^ n) | 0 <= n <= 63 } *)
314 let power_of_twos =
315 let rec loop n acc =
316 if n = 64
317 then acc
318 else (
319 let x = Int64.shift_left 1L n in
320 loop (n + 1) (Set.add (Set.add acc x) (Int64.neg x)))
321 in
322 loop 0 Int64.Set.empty
323 ;;
324
325 let valid_power_of_twos t =
326 let min = t.to_int64 t.min in
327 let max = t.to_int64 t.max in
328 Set.filter power_of_twos ~f:(fun n -> n >= min && n <= max)
329 ;;
330
331 let add_windows_around_points t points =
332 let min = t.to_int64 t.min in
333 let max = t.to_int64 t.max in
334 let rec add_between a b acc =
335 let acc = Set.add acc a in
336 if a = b then acc else add_between (Int64.succ a) b acc
337 in
338 let rec loop points acc =
339 match points with
340 | [] -> acc
341 | i :: rest ->
342 let d = Int64.( / ) test_window_len 2L in
343 let a = if i <= Int64.( + ) min d then min else Int64.( - ) i d in
344 let b = if i >= Int64.( - ) max d then max else Int64.( + ) i d in
345 loop rest (add_between a b acc)
346 in
347 loop (Set.elements points) Int64.Set.empty
348 ;;
349
350 let gen_tests (T t) =
351 let points =
352 Set.union (find_interesting_points t) (valid_power_of_twos t)
353 |> add_windows_around_points t
354 in
355 let min, max =
356 Set.fold points ~init:(Int.max_value, 0) ~f:(fun (min, max) n ->
357 let len = t.writer buf ~pos:0 (t.of_int64 n) in
358 let s = Bigstring.To_string.sub buf ~pos:0 ~len in
359 printf "%s| %s -> %Ld" t.name (to_hex s 9) n;
360 let len_local = t.writer_local buf ~pos:0 (t.of_int64 n) in
361 let s_local = Bigstring.To_string.sub buf ~pos:0 ~len:len_local in
362 if String.( <> ) s s_local
363 then printf ", write_local output (%s) differs from write output (%s)" s_local s;
364 let pos_ref = ref 0 in
365 let n' = t.reader buf ~pos_ref |> t.to_int64 in
366 let len' = !pos_ref in
367 if len < t.lo_bound || len > t.hi_bound
368 then printf ", bin_size outside of range %d..%d: %d" t.lo_bound t.hi_bound len;
369 if n <> n' || len <> len'
370 then
371 printf
372 ", read test failed: read %d byte%s as %Ld"
373 len'
374 (if len' = 1 then "" else "s")
375 n';
376 Out_channel.output_char stdout '\n';
377 Int.min min len, Int.max max len)
378 in
379 if min <> t.lo_bound || max <> t.hi_bound
380 then
381 printf
382 "%s| invalid bounds: %d..%d, expected: %d..%d\n"
383 t.name
384 min
385 max
386 t.lo_bound
387 t.hi_bound
388 ;;
389
390 let run_tests () = List.iter tests ~f:gen_tests