Add tests for compare, load, etc.
authorRichard W.M. Jones <rich@annexia.org>
Wed, 27 Aug 2008 11:28:18 +0000 (11:28 +0000)
committerRichard W.M. Jones <rich@annexia.org>
Wed, 27 Aug 2008 11:28:18 +0000 (11:28 +0000)
tests/31_bitstring_concat.ml [new file with mode: 0644]
tests/32_bitstring_compare.ml [new file with mode: 0644]
tests/35_load_from_file.ml [new file with mode: 0644]

diff --git a/tests/31_bitstring_concat.ml b/tests/31_bitstring_concat.ml
new file mode 100644 (file)
index 0000000..cd58c73
--- /dev/null
@@ -0,0 +1,27 @@
+(* Test concat and the bit get functions.
+ * $Id$
+ *)
+
+let () =
+  for i = 0 to 33 do
+    for j = 0 to 33 do
+      for k = 0 to 33 do
+       let bits =
+         Bitstring.concat [
+           Bitstring.ones_bitstring i;
+           Bitstring.zeroes_bitstring j;
+           Bitstring.ones_bitstring k
+         ] in
+       assert (Bitstring.bitstring_length bits = i+j+k);
+       for n = 0 to i-1 do
+         assert (Bitstring.is_set bits n)
+       done;
+       for n = i to i+j-1 do
+         assert (Bitstring.is_clear bits n)
+       done;
+       for n = i+j to i+j+k-1 do
+         assert (Bitstring.is_set bits n)
+       done
+      done
+    done
+  done
diff --git a/tests/32_bitstring_compare.ml b/tests/32_bitstring_compare.ml
new file mode 100644 (file)
index 0000000..4f06e6d
--- /dev/null
@@ -0,0 +1,60 @@
+(* Compare bitstrings.
+ * $Id$
+ *)
+
+open Printf
+
+let sgn = function
+  | 0 -> 0
+  | i when i > 0 -> 1
+  | _ -> -1
+
+let () =
+  for i = 0 to 33 do
+    for j = 0 to 33 do
+      let bits1 = Bitstring.ones_bitstring i
+      and bits2 = Bitstring.ones_bitstring j in
+      let r = Bitstring.compare bits1 bits2 in
+      if sgn r <> sgn (compare i j) then (
+       eprintf "ones compare failed %d %d %d\n" i j r;
+       exit 1
+      )
+    done
+ done;
+ for i = 0 to 33 do
+   for j = 0 to 33 do
+     let bits1 = Bitstring.zeroes_bitstring i
+     and bits2 = Bitstring.zeroes_bitstring j in
+     let r = Bitstring.compare bits1 bits2 in
+     if sgn r <> sgn (compare i j) then (
+       eprintf "zeroes compare failed %d %d %d\n" i j r;
+       exit 1
+     )
+   done
+ done;
+ for i = 0 to 33 do
+   for j = 0 to 33 do
+     let bits1 = Bitstring.make_bitstring i '\x55'
+     and bits2 = Bitstring.make_bitstring j '\x55' in
+     let r = Bitstring.compare bits1 bits2 in
+     if sgn r <> sgn (compare i j) then (
+       eprintf "x55 compare failed %d %d %d\n" i j r;
+       exit 1
+     )
+   done
+ done;
+ for i = 0 to 33 do
+   for j = 0 to 33 do
+     let bits1 = Bitstring.make_bitstring i '\x55' in
+     let bits2 = Bitstring.make_bitstring i '\x55' in
+     let bits2 = Bitstring.concat [Bitstring.zeroes_bitstring j; bits2] in
+     assert (Bitstring.bitstring_length bits2 = j+i);
+     let bits2 = Bitstring.dropbits j bits2 in
+     assert (Bitstring.bitstring_length bits2 = i);
+     let r = Bitstring.compare bits1 bits2 in
+     if r <> 0 then (
+       eprintf "x55 non-aligned compare failed %d %d %d\n" i j r;
+       exit 1
+     )
+   done
+ done
diff --git a/tests/35_load_from_file.ml b/tests/35_load_from_file.ml
new file mode 100644 (file)
index 0000000..849d2f1
--- /dev/null
@@ -0,0 +1,56 @@
+(* Test the various functions to load bitstrings from files.
+ * $Id$
+ *)
+
+open Printf
+open Bitstring
+
+let () =
+  let bits1 =
+    let b1 = make_bitstring 800 '\x5a' in
+    let b2 = make_bitstring 400 '\x88' in (
+      BITSTRING {
+       b1 : 800 : bitstring;
+       b2 : 400 : bitstring
+      }
+    ) in
+  let bits2 = (
+    let b = make_bitstring 800 '\xaa' in
+    BITSTRING {
+      b : 800 : bitstring
+    }
+  ) in
+  let bits = concat [bits1; bits2] in
+  let filename, chan =
+    Filename.open_temp_file ~mode:[Open_binary] "bitstring_test" ".tmp" in
+  bitstring_to_chan bits chan;
+  close_out chan;
+
+  let bits' = bitstring_of_file filename in
+  assert (equals bits bits');
+
+  let chan = open_in filename in
+  let bits' = bitstring_of_chan chan in
+  close_in chan;
+  assert (equals bits bits');
+
+  let chan = open_in filename in
+  let bits' = bitstring_of_chan_max chan 150 in
+  assert (equals bits1 bits');
+  let bits' = bitstring_of_chan_max chan 100 in
+  assert (equals bits2 bits');
+  close_in chan;
+
+  let fd = Unix.openfile filename [Unix.O_RDONLY] 0 in
+  let bits' = bitstring_of_file_descr fd in
+  Unix.close fd;
+  assert (equals bits bits');
+
+  let fd = Unix.openfile filename [Unix.O_RDONLY] 0 in
+  let bits' = bitstring_of_file_descr_max fd 150 in
+  assert (equals bits1 bits');
+  let bits' = bitstring_of_file_descr_max fd 100 in
+  assert (equals bits2 bits');
+  Unix.close fd;
+
+  Unix.unlink filename