1 (* Test the Bitstring.Buffer module and string_of_bitstring in
2 * nasty non-aligned corner cases.
11 let str1 = "012345678" in
13 for offset = 0 to 65 do
16 let strlen = (len+7) lsr 3 in
17 let expected = String.create strlen in
18 for i = 0 to strlen-1 do
19 expected.[i] <- Char.chr (Random.int 256)
21 let last = Char.code expected.[strlen-1] in
22 let last = last land (0xff lsl (8 - (len land 7))) in
23 expected.[strlen-1] <- Char.chr last;
26 (* Create a random bitstring:
27 * +-------------+-------------------------------------------+
28 * | (random) | bits that we check (expected) |
29 * +-------------+-------------------------------------------+
31 * <---------------- len bits --------------->
34 let bits = Bitstring.Buffer.create () in
35 Bitstring.Buffer.add_bits bits str1 offset;
36 Bitstring.Buffer.add_bits bits expected len;
37 Bitstring.Buffer.contents bits in
39 (* Create a sub bitstring corresponding to what we want to check. *)
41 let bits, bitoffset, bitlen = bits in
42 (bits, bitoffset+offset, bitlen-offset) in
44 assert (Bitstring.bitstring_length subbits = len);
46 (* Now try to read out the substring using string_of_bitstring. *)
47 let actual = Bitstring.string_of_bitstring subbits in
48 if actual <> expected then (
49 eprintf "MISMATCH between actual and expected, offset=%d, len=%d\n"
51 eprintf "EXPECTED string:\n";
52 for i = 0 to String.length expected-1 do
53 eprintf " %02x" (Char.code expected.[i])
55 eprintf "\nACTUAL string:\n";
56 for i = 0 to String.length actual-1 do
57 eprintf " %02x" (Char.code actual.[i])
60 Bitstring.hexdump_bitstring stderr bits;
62 Bitstring.hexdump_bitstring stderr subbits;