Test for offsets
[ocaml-bitstring.git] / tests / 60_simple_offset.ml
1 (* Simple offset test
2  * $Id: 50_named_pattern.ml 86 2008-06-13 15:56:04Z richard.wm.jones $
3  *)
4
5 open Printf
6 open Bitmatch
7
8 let make_bits i n j m k = (
9   let pad1 = ones_bitstring (n-8) in
10   let pad2 = ones_bitstring (m-n-8) in
11   BITSTRING {
12     i : 8;
13     pad1 : n-8 : bitstring;
14     j : 8;                           (* this should be at offset(n) *)
15     pad2 : m-n-8 : bitstring;
16     k : 8                            (* this should be at offset(m) *)
17   }
18 )
19
20 let test_bits bits i n j m k =
21   bitmatch bits with
22   | { i' : 8;
23       j' : 8 : offset(n);
24       k' : 8 : offset(m) } when i = i' && j = j' && k = k' -> () (* ok *)
25   | { _ } ->
26       failwith (sprintf "60_simple_offset: test_bits: failed %d %d %d %d %d"
27                   i n j m k)
28
29 let () =
30   for n = 8 to 128 do
31     for m = n+8 to 256 do
32       List.iter (fun (i,j,k) -> test_bits (make_bits i n j m k) i n j m k)
33         [0x55, 0xaa, 0x33; 0x33, 0xaa, 0x55; 0x12, 0x34, 0x56]
34     done;
35   done