Skip to content

Commit

Permalink
Merge pull request #80 from mirage/fix-adler32-ocaml
Browse files Browse the repository at this point in the history
Add tests about adler32
  • Loading branch information
dinosaure committed Apr 11, 2023
2 parents 33efd01 + b80ff4b commit df4b880
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src-ocaml/gin_adler32.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ let _nmax = 5552
let digest : type a. get:(a -> int -> char) -> a -> int -> int -> t -> t =
fun ~get buf off len adler32 ->
let a =
ref Optint.(to_unsigned_int Infix.((adler32 lsr 16) land of_int 0xFFFF))
ref
Optint.(
to_unsigned_int Infix.((adler32 lsr 16) land of_unsigned_int 0xFFFF))
in
let b =
ref Optint.(to_unsigned_int Infix.(adler32 land of_unsigned_int 0xFFFF))
in
let b = ref Optint.(to_unsigned_int Infix.(adler32 land of_int 0xFFFF)) in
let l = ref len in
let o = ref off in
if len = 0
Expand Down Expand Up @@ -124,7 +128,7 @@ let digest : type a. get:(a -> int -> char) -> a -> int -> int -> t -> t =
b := !b mod _base ;
a := !a mod _base) ;
Optint.Infix.(
Optint.of_unsigned_int !b land (Optint.of_unsigned_int !a lsl 16)))
Optint.of_unsigned_int !b lor (Optint.of_unsigned_int !a lsl 16)))

let unsafe_digest_bytes a o l v = digest ~get:Bytes.unsafe_get a o l v
let digest_bytes a o l v = digest ~get:Bytes.get a o l v
Expand Down
17 changes: 17 additions & 0 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ let make ~name (module M : Checkseum.S) input expected =
let () =
Alcotest.run "checkseum"
[
( "adler32",
[
make ~name:"0" (module Checkseum.Adler32) "" 1l;
make ~name:"1" (module Checkseum.Adler32) "\x00" 65537l;
make ~name:"2"
(module Checkseum.Adler32)
"\xff\xff\xff\xff" 167379965l;
make ~name:"3" (module Checkseum.Adler32) "123456789" 0x91E01DEl;
make ~name:"4"
(module Checkseum.Adler32)
(String.concat ""
[
"\x9d\x02\x9d\x02\x90\xad\x14\x72\xb9\xb4\x44\x59\x5d\x21\x05\xb7";
"\x34\x4f\x64\xe9\xa8\x5a\x3e\xd9\x91\x1f\x44\x91\xc1\x5c";
])
0xBDE50CD1l;
] );
( "crc32c",
[
make ~name:"0" (module Checkseum.Crc32c) "" 0l;
Expand Down

0 comments on commit df4b880

Please sign in to comment.