1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
(* Test the Bitstring.Buffer module and string_of_bitstring in
* nasty non-aligned corner cases.
* $Id: 30_bitbuffer.ml 142 2008-07-17 15:45:56Z richard.wm.jones $
*)
open Printf
let () =
Random.self_init ();
let str1 = "012345678" in
for offset = 0 to 65 do
for len = 1 to 65 do
let expected =
let strlen = (len+7) lsr 3 in
let expected = String.create strlen in
for i = 0 to strlen-1 do
expected.[i] <- Char.chr (Random.int 256)
done;
let last = Char.code expected.[strlen-1] in
let last = last land (0xff lsl (8 - (len land 7))) in
expected.[strlen-1] <- Char.chr last;
expected in
(* Create a random bitstring:
* +-------------+-------------------------------------------+
* | (random) | bits that we check (expected) |
* +-------------+-------------------------------------------+
* 0 offset offset+len
* <---------------- len bits --------------->
*)
let bits =
let bits = Bitstring.Buffer.create () in
Bitstring.Buffer.add_bits bits str1 offset;
Bitstring.Buffer.add_bits bits expected len;
Bitstring.Buffer.contents bits in
(* Create a sub bitstring corresponding to what we want to check. *)
let subbits =
let bits, bitoffset, bitlen = bits in
(bits, bitoffset+offset, bitlen-offset) in
assert (Bitstring.bitstring_length subbits = len);
(* Now try to read out the substring using string_of_bitstring. *)
let actual = Bitstring.string_of_bitstring subbits in
if actual <> expected then (
eprintf "MISMATCH between actual and expected, offset=%d, len=%d\n"
offset len;
eprintf "EXPECTED string:\n";
for i = 0 to String.length expected-1 do
eprintf " %02x" (Char.code expected.[i])
done;
eprintf "\nACTUAL string:\n";
for i = 0 to String.length actual-1 do
eprintf " %02x" (Char.code actual.[i])
done;
eprintf "\nBITS:\n";
Bitstring.hexdump_bitstring stderr bits;
eprintf "SUBBITS:\n";
Bitstring.hexdump_bitstring stderr subbits;
exit 1
);
done
done
|