Renaming bitmatch -> bitstring.
[ocaml-bitstring.git] / tests / 62_offset_padding.ml
1 (* Test computed offsets when original_off <> 0.
2  * $Id$
3  *)
4
5 open Printf
6 open Bitstring
7
8 let make_bits p i n j m k = (
9   let pad0 = ones_bitstring p in
10   let pad1 = ones_bitstring (n-8) in
11   let pad2 = ones_bitstring (m-n-8) in
12   BITSTRING {
13     pad0 : p : bitstring;            (* will be skipped below *)
14     i : 8;
15     pad1 : n-8 : bitstring;
16     j : 8;                           (* this should be at offset(n) *)
17     pad2 : m-n-8 : bitstring;
18     k : 8                            (* this should be at offset(m) *)
19   }
20 )
21
22 let test_bits bits p i n j m k =
23   (* Skip the 'p' padding bits so the match starts at a non-zero offset. *)
24   let bits = dropbits p bits in
25
26   bitmatch bits with
27   | { i' : 8;
28       j' : 8 : offset(n);
29       k' : 8 : offset(m) } when i = i' && j = j' && k = k' -> () (* ok *)
30   | { _ } ->
31       failwith (sprintf "62_offset_padding: test_bits: failed %d %d %d %d %d %d"
32                   p i n j m k)
33
34 let () =
35   for p = 1 to 4 do
36     for n = 8 to 128 do
37       for m = n+8 to 256 do
38         List.iter (fun (i,j,k) -> test_bits (make_bits p i n j m k) p i n j m k)
39           [0x55, 0xaa, 0x33; 0x33, 0xaa, 0x55; 0x12, 0x34, 0x56]
40       done;
41     done;
42   done