Skip to content

Commit 17e14e8

Browse files
committed
Update expect tests with large signed numbers for zigzag encoding/decoding
1 parent 27b7eda commit 17e14e8

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

src/ocaml_protoc_plugin/deserialize.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,31 @@ let deserialize_fast: type constr a. (constr, a) compound_list -> constr -> Read
326326
let extension_ranges = extension_ranges spec in
327327
let values = make_values spec in
328328
fun reader -> deserialize_fast extension_ranges values constr reader
329+
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);
335+
in
336+
List.iter ~f:test [0L; -1L; 1L; -2L; 2L; 2147483647L; -2147483648L; Int64.max_int; Int64.min_int; ];
337+
[%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
356+
|}]

src/ocaml_protoc_plugin/serialize.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,5 @@ let%expect_test "zigzag encoding" =
201201
zigzag_encoding(9223372036854775807L) = -2L
202202
zigzag_encoding_unboxed(-1) = 1
203203
zigzag_encoding(-9223372036854775808L) = -1L
204-
zigzag_encoding_unboxed(0) = 0 |}]
204+
zigzag_encoding_unboxed(0) = 0
205+
|}]

test/int_types_native_test.ml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let proto_file = "int_types_native.proto"
55

66
let test_signed64 (type t) ~(create : Int64.t -> t) (module T : Test_lib.T with type t = t) =
77
Printf.printf "Test %s\n%!" (T.name ());
8-
let values = [-1073741823L; -2L; -1L; 0L; 1L; 2L; 1073741823L] in
8+
let values = [Int64.min_int; Int64.succ Int64.min_int; -1073741823L; -2L; -1L; 0L; 1L; 2L; 1073741823L; Int64.pred Int64.max_int; Int64.max_int] in
99
List.iter
1010
~f:(fun v -> Test_lib.test_encode ~proto_file (module T) (create v))
1111
values
@@ -19,7 +19,7 @@ let test_unsigned64 (type t) ~(create : Int64.t -> t) (module T : Test_lib.T wit
1919

2020
let test_signed32 (type t) ~(create : Int32.t -> t) (module T : Test_lib.T with type t = t) =
2121
Printf.printf "Test %s\n%!" (T.name ());
22-
let values = [-1073741823l; -2l; -1l; 0l; 1l; 2l; 1073741823l] in
22+
let values = [Int32.min_int; Int32.succ Int32.min_int; -1073741823l; -2l; -1l; 0l; 1l; 2l; 1073741823l; Int32.pred Int32.max_int; Int32.max_int] in
2323
List.iter
2424
~f:(fun v -> Test_lib.test_encode ~proto_file (module T) (create v))
2525
values
@@ -37,38 +37,53 @@ let%expect_test _ =
3737
test_signed64 ~create (module T);
3838
[%expect {|
3939
Test .int_types_native.SInt64
40+
i: -9223372036854775808
41+
i: -9223372036854775807
4042
i: -1073741823
4143
i: -2
4244
i: -1
4345
i: 1
4446
i: 2
45-
i: 1073741823 |}]
47+
i: 1073741823
48+
i: 9223372036854775806
49+
i: 9223372036854775807
50+
|}]
4651

4752
let%expect_test _ =
4853
let module T = Int_types_native.SInt32 in
4954
let create i = i in
5055
test_signed32 ~create (module T);
5156
[%expect {|
5257
Test .int_types_native.SInt32
58+
i: -2147483648
59+
i: -2147483647
5360
i: -1073741823
5461
i: -2
5562
i: -1
5663
i: 1
5764
i: 2
58-
i: 1073741823 |}]
65+
i: 1073741823
66+
i: 2147483646
67+
i: 2147483647
68+
|}]
5969

6070
let%expect_test _ =
6171
let module T = Int_types_native.Int64 in
6272
let create i = i in
6373
test_signed64 ~create (module T);
6474
[%expect {|
6575
Test .int_types_native.Int64
76+
i: -9223372036854775808
77+
i: -9223372036854775807
6678
i: -1073741823
6779
i: -2
6880
i: -1
6981
i: 1
7082
i: 2
71-
i: 1073741823 |}]
83+
i: 1073741823
84+
i: 9223372036854775806
85+
i: 9223372036854775807
86+
|}]
7287

7388
let%expect_test _ =
7489
let module T = Int_types_native.Int32 in
@@ -77,12 +92,17 @@ let%expect_test _ =
7792
[%expect
7893
{|
7994
Test .int_types_native.Int32
95+
i: -2147483648
96+
i: -2147483647
8097
i: -1073741823
8198
i: -2
8299
i: -1
83100
i: 1
84101
i: 2
85-
i: 1073741823 |}]
102+
i: 1073741823
103+
i: 2147483646
104+
i: 2147483647
105+
|}]
86106

87107
let%expect_test _ =
88108
let module T = Int_types_native.UInt64 in

0 commit comments

Comments
 (0)