From: Richard W.M. Jones Date: Sun, 18 May 2008 20:54:08 +0000 (+0000) Subject: Bit-matching test. X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=de6efed668136104f0597834c9345fde03d98c02;p=ocaml-bitstring.git Bit-matching test. --- diff --git a/tests/01_load.ml b/tests/01_load.ml index cbc2858..c4160ab 100644 --- a/tests/01_load.ml +++ b/tests/01_load.ml @@ -1,4 +1,4 @@ -(* Just check that the extension and library load. +(* Just check that the extension and library load without error. * $Id$ *) diff --git a/tests/02_run.ml b/tests/02_run.ml new file mode 100644 index 0000000..8cc06c5 --- /dev/null +++ b/tests/02_run.ml @@ -0,0 +1,7 @@ +(* Just check that we can run some functions from the library. + * $Id$ + *) + +let () = + let bits = Bitmatch.create_bitstring 16 in + ignore (Bitmatch.string_of_bitstring bits) diff --git a/tests/10_match_bits.ml b/tests/10_match_bits.ml new file mode 100644 index 0000000..90a7a08 --- /dev/null +++ b/tests/10_match_bits.ml @@ -0,0 +1,86 @@ +(* Match random bits. + * $Id$ + *) + +open Printf + +let rec range a b = + if a <= b then + a :: range (a+1) b + else + [] + +let () = + Random.self_init (); + + for len = 0 to 999 do + (* Create a random string of bits. *) + let expected = List.map (fun _ -> Random.bool ()) (range 0 (len-1)) in + + let bits = Bitmatch.Buffer.create () in + List.iter (Bitmatch.Buffer.add_bit bits) expected; + let bits = Bitmatch.Buffer.contents bits in + + (* Now read the bitstring in groups of 1, 2, 3 .. etc. bits. + * In each case check the result against what we generated ('expected'). + *) + let actual = + let rec loop bits = + bitmatch bits with + | { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest + | { _ } -> [] + in + loop bits in + if actual <> expected then + failwith (sprintf "match bits: failed on 1 bit test, len = %d" len); + + let actual = + let rec loop bits = + bitmatch bits with + | { b0 : 1; b1 : 1; rest : -1 : bitstring } -> b0 :: b1 :: loop rest + | { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest + | { _ } -> [] + in + loop bits in + if actual <> expected then + failwith (sprintf "match bits: failed on 1 bit test, len = %d" len); + + let actual = + let rec loop bits = + bitmatch bits with + | { b0 : 1; b1 : 1; b2 : 1; + rest : -1 : bitstring } -> b0 :: b1 :: b2 :: loop rest + | { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest + | { _ } -> [] + in + loop bits in + if actual <> expected then + failwith (sprintf "match bits: failed on 1 bit test, len = %d" len); + + let actual = + let rec loop bits = + bitmatch bits with + | { b0 : 1; b1 : 1; b2 : 1; b3 : 1; + rest : -1 : bitstring } -> b0 :: b1 :: b2 :: b3 :: loop rest + | { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest + | { _ } -> [] + in + loop bits in + if actual <> expected then + failwith (sprintf "match bits: failed on 1 bit test, len = %d" len); + + let actual = + let rec loop bits = + bitmatch bits with + | { b0 : 1; b1 : 1; b2 : 1; b3 : 1; + b4 : 1; b5 : 1; b6 : 1; b7 : 1; + b8 : 1; + rest : -1 : bitstring } -> + b0 :: b1 :: b2 :: b3 :: b4 :: b5 :: b6 :: b7 :: b8 :: loop rest + | { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest + | { _ } -> [] + in + loop bits in + if actual <> expected then + failwith (sprintf "match bits: failed on 1 bit test, len = %d" len); + done