Skip to content

Commit 58c5404

Browse files
committed
Update tests for encode and decode
1 parent e8f9e48 commit 58c5404

5 files changed

Lines changed: 116 additions & 56 deletions

File tree

Changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
behaviour.
88
- Improve copying of comments from .proto files into ocaml code using
99
omd to parse markdown
10-
10+
- Fix problem with deserializing large singed integers (#45)
1111

1212
## 6.1.0: 2024-04-25
1313
- Fix name resolution leading to wrongly mapped names

src/ocaml_protoc_plugin/deserialize.ml

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -327,30 +327,57 @@ let deserialize_fast: type constr a. (constr, a) compound_list -> constr -> Read
327327
let values = make_values spec in
328328
fun reader -> deserialize_fast extension_ranges values constr reader
329329

330-
let%expect_test "zigzag encoding" =
331-
let test vl =
332-
let v = Int64.to_int vl in
333-
Printf.printf "zigzag_decoding(%LdL) = %LdL\n" vl (decode_zigzag vl);
334-
Printf.printf "zigzag_decoding_unboxed(%d) = %d\n" v (decode_zigzag_unboxed v);
330+
let%expect_test "zigzag decoding" =
331+
let n2l = Int64.of_int in
332+
let i2l = Int64.of_int32 in
333+
let test_values =
334+
[Int64.min_int; n2l Int.min_int; i2l Int32.min_int; -2L;
335+
0L; 3L; i2l Int32.max_int; n2l Int.max_int; Int64.max_int]
336+
|> List.concat_map ~f:(
337+
let open Infix.Int64 in
338+
function
339+
| v when v > 0L -> [pred v; v]
340+
| v -> [v; succ v]
341+
)
335342
in
336-
List.iter ~f:test [0L; -1L; 1L; -2L; 2L; 2147483647L; -2147483648L; Int64.max_int; Int64.min_int; ];
343+
List.iter ~f:(fun vl -> Printf.printf "zigzag_decoding(0x%016Lx) = 0x%016Lx\n" vl (decode_zigzag vl)) test_values;
344+
List.iter ~f:(fun v -> Printf.printf "zigzag_decoding_unboxed(0x%016x) = 0x%016x\n" (Int64.to_int v) (Int64.to_int v |> decode_zigzag_unboxed)) test_values;
345+
(* The results should display alternation between positive and negative numbers. If the right most bit is set, the number is negative *)
337346
[%expect {|
338-
zigzag_decoding(0L) = 0L
339-
zigzag_decoding_unboxed(0) = 0
340-
zigzag_decoding(-1L) = -9223372036854775808L
341-
zigzag_decoding_unboxed(-1) = -4611686018427387904
342-
zigzag_decoding(1L) = -1L
343-
zigzag_decoding_unboxed(1) = -1
344-
zigzag_decoding(-2L) = 9223372036854775807L
345-
zigzag_decoding_unboxed(-2) = 4611686018427387903
346-
zigzag_decoding(2L) = 1L
347-
zigzag_decoding_unboxed(2) = 1
348-
zigzag_decoding(2147483647L) = -1073741824L
349-
zigzag_decoding_unboxed(2147483647) = -1073741824
350-
zigzag_decoding(-2147483648L) = 9223372035781033984L
351-
zigzag_decoding_unboxed(-2147483648) = 4611686017353646080
352-
zigzag_decoding(9223372036854775807L) = -4611686018427387904L
353-
zigzag_decoding_unboxed(-1) = -4611686018427387904
354-
zigzag_decoding(-9223372036854775808L) = 4611686018427387904L
355-
zigzag_decoding_unboxed(0) = 0
347+
zigzag_decoding(0x8000000000000000) = 0x4000000000000000
348+
zigzag_decoding(0x8000000000000001) = 0xbfffffffffffffff
349+
zigzag_decoding(0xc000000000000000) = 0x6000000000000000
350+
zigzag_decoding(0xc000000000000001) = 0x9fffffffffffffff
351+
zigzag_decoding(0xffffffff80000000) = 0x7fffffffc0000000
352+
zigzag_decoding(0xffffffff80000001) = 0x800000003fffffff
353+
zigzag_decoding(0xfffffffffffffffe) = 0x7fffffffffffffff
354+
zigzag_decoding(0xffffffffffffffff) = 0x8000000000000000
355+
zigzag_decoding(0x0000000000000000) = 0x0000000000000000
356+
zigzag_decoding(0x0000000000000001) = 0xffffffffffffffff
357+
zigzag_decoding(0x0000000000000002) = 0x0000000000000001
358+
zigzag_decoding(0x0000000000000003) = 0xfffffffffffffffe
359+
zigzag_decoding(0x000000007ffffffe) = 0x000000003fffffff
360+
zigzag_decoding(0x000000007fffffff) = 0xffffffffc0000000
361+
zigzag_decoding(0x3ffffffffffffffe) = 0x1fffffffffffffff
362+
zigzag_decoding(0x3fffffffffffffff) = 0xe000000000000000
363+
zigzag_decoding(0x7ffffffffffffffe) = 0x3fffffffffffffff
364+
zigzag_decoding(0x7fffffffffffffff) = 0xc000000000000000
365+
zigzag_decoding_unboxed(0x0000000000000000) = 0x0000000000000000
366+
zigzag_decoding_unboxed(0x0000000000000001) = 0x7fffffffffffffff
367+
zigzag_decoding_unboxed(0x4000000000000000) = 0x2000000000000000
368+
zigzag_decoding_unboxed(0x4000000000000001) = 0x5fffffffffffffff
369+
zigzag_decoding_unboxed(0x7fffffff80000000) = 0x3fffffffc0000000
370+
zigzag_decoding_unboxed(0x7fffffff80000001) = 0x400000003fffffff
371+
zigzag_decoding_unboxed(0x7ffffffffffffffe) = 0x3fffffffffffffff
372+
zigzag_decoding_unboxed(0x7fffffffffffffff) = 0x4000000000000000
373+
zigzag_decoding_unboxed(0x0000000000000000) = 0x0000000000000000
374+
zigzag_decoding_unboxed(0x0000000000000001) = 0x7fffffffffffffff
375+
zigzag_decoding_unboxed(0x0000000000000002) = 0x0000000000000001
376+
zigzag_decoding_unboxed(0x0000000000000003) = 0x7ffffffffffffffe
377+
zigzag_decoding_unboxed(0x000000007ffffffe) = 0x000000003fffffff
378+
zigzag_decoding_unboxed(0x000000007fffffff) = 0x7fffffffc0000000
379+
zigzag_decoding_unboxed(0x3ffffffffffffffe) = 0x1fffffffffffffff
380+
zigzag_decoding_unboxed(0x3fffffffffffffff) = 0x6000000000000000
381+
zigzag_decoding_unboxed(0x7ffffffffffffffe) = 0x3fffffffffffffff
382+
zigzag_decoding_unboxed(0x7fffffffffffffff) = 0x4000000000000000
356383
|}]

src/ocaml_protoc_plugin/infix.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Int64 = struct
99
let (/) = div
1010
let ( * ) = mul
1111
let (-) = sub
12+
let succ = succ
13+
let pred = pred
1214
end
1315

1416
module Int = struct
@@ -22,4 +24,6 @@ module Int = struct
2224
let (/) = div
2325
let ( * ) = mul
2426
let (-) = sub
27+
let succ = succ
28+
let pred = pred
2529
end

src/ocaml_protoc_plugin/serialize.ml

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ let write_fixed64 ~f v =
1616
let write_fixed32 ~f v =
1717
Writer.write_fixed32_value (f v)
1818

19-
let zigzag_encoding v =
19+
let encode_zigzag v =
2020
let open Infix.Int64 in
2121
let v = match v < 0L with
2222
| true -> v lsl 1 lxor (-1L)
2323
| false -> v lsl 1
2424
in
2525
v
2626

27-
let zigzag_encoding_unboxed v =
27+
let encode_zigzag_unboxed v =
2828
let v = match v < 0 with
2929
| true -> v lsl 1 lxor (-1)
3030
| false -> v lsl 1
@@ -55,16 +55,16 @@ let write_value : type a b. (a, b) spec -> a -> Writer.t -> unit = function
5555
| SFixed32_int -> write_fixed32 ~f:Int32.of_int
5656
| Int64 -> Writer.write_varint_value
5757
| UInt64 -> Writer.write_varint_value
58-
| SInt64 -> write_varint ~f:zigzag_encoding
58+
| SInt64 -> write_varint ~f:encode_zigzag
5959
| Int32 -> write_varint_unboxed ~f:Int32.to_int
6060
| UInt32 -> write_varint_unboxed ~f:Int32.to_int
61-
| SInt32 -> write_varint_unboxed ~f:(Int32.to_int @@ zigzag_encoding_unboxed)
61+
| SInt32 -> write_varint_unboxed ~f:(Int32.to_int @@ encode_zigzag_unboxed)
6262
| Int64_int -> Writer.write_varint_unboxed_value
6363
| UInt64_int -> Writer.write_varint_unboxed_value
6464
| Int32_int -> Writer.write_varint_unboxed_value
6565
| UInt32_int -> Writer.write_varint_unboxed_value
66-
| SInt64_int -> write_varint_unboxed ~f:zigzag_encoding_unboxed
67-
| SInt32_int -> write_varint_unboxed ~f:zigzag_encoding_unboxed
66+
| SInt64_int -> write_varint_unboxed ~f:encode_zigzag_unboxed
67+
| SInt32_int -> write_varint_unboxed ~f:encode_zigzag_unboxed
6868

6969
| Bool -> write_varint_unboxed ~f:(function true -> 1 | false -> 0)
7070
| String -> fun v -> Writer.write_length_delimited_value ~data:v ~offset:0 ~len:(String.length v)
@@ -177,29 +177,55 @@ let rec serialize: type a. (a, unit) compound_list -> Writer.t -> a = function
177177
cont writer
178178

179179
let%expect_test "zigzag encoding" =
180-
let test vl =
181-
let v = Int64.to_int vl in
182-
Printf.printf "zigzag_encoding(%LdL) = %LdL\n" vl (zigzag_encoding vl);
183-
Printf.printf "zigzag_encoding_unboxed(%d) = %d\n" v (zigzag_encoding_unboxed v);
180+
let n2l = Int64.of_int in
181+
let i2l = Int64.of_int32 in
182+
let test_values =
183+
[Int64.min_int; n2l Int.min_int; i2l Int32.min_int; -2L;
184+
0L; 3L; i2l Int32.max_int; n2l Int.max_int; Int64.max_int]
185+
|> List.concat_map ~f:(
186+
let open Infix.Int64 in
187+
function
188+
| v when v > 0L -> [pred v; v]
189+
| v -> [v; succ v]
190+
)
184191
in
185-
List.iter ~f:test [0L; -1L; 1L; -2L; 2L; 2147483647L; -2147483648L; Int64.max_int; Int64.min_int; ];
192+
List.iter ~f:(fun vl -> Printf.printf "zigzag_encoding 0x%016Lx = 0x%016Lx\n" vl (encode_zigzag vl)) test_values;
193+
List.iter ~f:(fun v -> Printf.printf "zigzag_encoding_unboxed 0x%016x = 0x%016x\n" (Int64.to_int v) (Int64.to_int v |> encode_zigzag_unboxed)) test_values;
186194
[%expect {|
187-
zigzag_encoding(0L) = 0L
188-
zigzag_encoding_unboxed(0) = 0
189-
zigzag_encoding(-1L) = 1L
190-
zigzag_encoding_unboxed(-1) = 1
191-
zigzag_encoding(1L) = 2L
192-
zigzag_encoding_unboxed(1) = 2
193-
zigzag_encoding(-2L) = 3L
194-
zigzag_encoding_unboxed(-2) = 3
195-
zigzag_encoding(2L) = 4L
196-
zigzag_encoding_unboxed(2) = 4
197-
zigzag_encoding(2147483647L) = 4294967294L
198-
zigzag_encoding_unboxed(2147483647) = 4294967294
199-
zigzag_encoding(-2147483648L) = 4294967295L
200-
zigzag_encoding_unboxed(-2147483648) = 4294967295
201-
zigzag_encoding(9223372036854775807L) = -2L
202-
zigzag_encoding_unboxed(-1) = 1
203-
zigzag_encoding(-9223372036854775808L) = -1L
204-
zigzag_encoding_unboxed(0) = 0
195+
zigzag_encoding 0x8000000000000000 = 0xffffffffffffffff
196+
zigzag_encoding 0x8000000000000001 = 0xfffffffffffffffd
197+
zigzag_encoding 0xc000000000000000 = 0x7fffffffffffffff
198+
zigzag_encoding 0xc000000000000001 = 0x7ffffffffffffffd
199+
zigzag_encoding 0xffffffff80000000 = 0x00000000ffffffff
200+
zigzag_encoding 0xffffffff80000001 = 0x00000000fffffffd
201+
zigzag_encoding 0xfffffffffffffffe = 0x0000000000000003
202+
zigzag_encoding 0xffffffffffffffff = 0x0000000000000001
203+
zigzag_encoding 0x0000000000000000 = 0x0000000000000000
204+
zigzag_encoding 0x0000000000000001 = 0x0000000000000002
205+
zigzag_encoding 0x0000000000000002 = 0x0000000000000004
206+
zigzag_encoding 0x0000000000000003 = 0x0000000000000006
207+
zigzag_encoding 0x000000007ffffffe = 0x00000000fffffffc
208+
zigzag_encoding 0x000000007fffffff = 0x00000000fffffffe
209+
zigzag_encoding 0x3ffffffffffffffe = 0x7ffffffffffffffc
210+
zigzag_encoding 0x3fffffffffffffff = 0x7ffffffffffffffe
211+
zigzag_encoding 0x7ffffffffffffffe = 0xfffffffffffffffc
212+
zigzag_encoding 0x7fffffffffffffff = 0xfffffffffffffffe
213+
zigzag_encoding_unboxed 0x0000000000000000 = 0x0000000000000000
214+
zigzag_encoding_unboxed 0x0000000000000001 = 0x0000000000000002
215+
zigzag_encoding_unboxed 0x4000000000000000 = 0x7fffffffffffffff
216+
zigzag_encoding_unboxed 0x4000000000000001 = 0x7ffffffffffffffd
217+
zigzag_encoding_unboxed 0x7fffffff80000000 = 0x00000000ffffffff
218+
zigzag_encoding_unboxed 0x7fffffff80000001 = 0x00000000fffffffd
219+
zigzag_encoding_unboxed 0x7ffffffffffffffe = 0x0000000000000003
220+
zigzag_encoding_unboxed 0x7fffffffffffffff = 0x0000000000000001
221+
zigzag_encoding_unboxed 0x0000000000000000 = 0x0000000000000000
222+
zigzag_encoding_unboxed 0x0000000000000001 = 0x0000000000000002
223+
zigzag_encoding_unboxed 0x0000000000000002 = 0x0000000000000004
224+
zigzag_encoding_unboxed 0x0000000000000003 = 0x0000000000000006
225+
zigzag_encoding_unboxed 0x000000007ffffffe = 0x00000000fffffffc
226+
zigzag_encoding_unboxed 0x000000007fffffff = 0x00000000fffffffe
227+
zigzag_encoding_unboxed 0x3ffffffffffffffe = 0x7ffffffffffffffc
228+
zigzag_encoding_unboxed 0x3fffffffffffffff = 0x7ffffffffffffffe
229+
zigzag_encoding_unboxed 0x7ffffffffffffffe = 0x0000000000000003
230+
zigzag_encoding_unboxed 0x7fffffffffffffff = 0x0000000000000001
205231
|}]

test/int_types_native_test.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let test_signed32 (type t) ~(create : Int32.t -> t) (module T : Test_lib.T with
2626

2727
let test_unsigned32 (type t) ~(create : Int32.t -> t) (module T : Test_lib.T with type t = t) =
2828
Printf.printf "Test %s\n%!" (T.name ());
29-
let values = [0l; 1l; 2l; 2147483647l] in
29+
let values = [0l; 1l; 2l; 1073741823l; Int32.pred Int32.max_int; Int32.max_int] in
3030
List.iter
3131
~f:(fun v -> Test_lib.test_encode ~proto_file (module T) (create v))
3232
values
@@ -122,4 +122,7 @@ let%expect_test _ =
122122
Test .int_types_native.UInt32
123123
i: 1
124124
i: 2
125-
i: 2147483647 |}]
125+
i: 1073741823
126+
i: 2147483646
127+
i: 2147483647
128+
|}]

0 commit comments

Comments
 (0)