Added integer matching test.
authorRichard W.M. Jones <rich@annexia.org>
Sun, 18 May 2008 21:06:15 +0000 (21:06 +0000)
committerRichard W.M. Jones <rich@annexia.org>
Sun, 18 May 2008 21:06:15 +0000 (21:06 +0000)
tests/11_match_ints.ml [new file with mode: 0644]

diff --git a/tests/11_match_ints.ml b/tests/11_match_ints.ml
new file mode 100644 (file)
index 0000000..c2a2b8b
--- /dev/null
@@ -0,0 +1,50 @@
+(* Match random bits with integers.
+ * $Id$
+ *)
+
+open Printf
+
+let rec range a b =
+  if a <= b then
+    a :: range (a+1) b
+  else
+    []
+
+let () =
+  Random.self_init ();
+
+  for len = 1 to 99 do
+    for bitlen = 1 to 63 do
+      (* Create a random string of ints. *)
+      let expected =
+       List.map (fun _ ->
+                   Random.int64 (Int64.sub (Int64.shift_left 1L bitlen) 1L))
+         (range 0 (len-1)) in
+
+      let bits = Bitmatch.Buffer.create () in
+      List.iter (fun i ->
+                  Bitmatch.construct_int64_be_unsigned bits i bitlen
+                    (Failure "constructing string"))
+       expected;
+      let bits = Bitmatch.Buffer.contents bits in
+
+      (* Now read the bitstring as integers.
+       * In each case check the result against what we generated ('expected').
+       *)
+      let actual =
+       let rec loop bits =
+         bitmatch bits with
+         | { i : bitlen; rest : -1 : bitstring } ->
+             if Bitmatch.bitstring_length rest > 0 then
+               i :: loop rest
+             else
+               [i]
+         | { _ } ->
+             failwith (sprintf "loop failed with len = %d, bitlen = %d"
+                         len bitlen)
+       in
+       loop bits in
+      if actual <> expected then
+       failwith (sprintf "match bits: failed on 1 bit test, len = %d" len)
+    done
+  done