Test the hexdump function.
[ocaml-bitstring.git] / tests / 15_extract_int.ml
1 (* Test functions which construct and extract fixed-length ints
2  * of various sizes.
3  * $Id$
4  *)
5
6 open Printf
7
8 open Bitstring
9
10 let () =
11   for i = 0 to 129 do
12     let zeroes = zeroes_bitstring i in
13     let bits = (
14       BITSTRING {
15         zeroes : i : bitstring;
16         true : 1;
17         2 : 2 : littleendian;
18         2 : 2 : bigendian;
19         2 : 2 : nativeendian;
20         3 : 3 : littleendian;
21         3 : 3 : bigendian;
22         3 : 3 : nativeendian;
23         0x5a : 8 : littleendian;
24         0x5a : 8 : bigendian;
25         0x5a : 8 : nativeendian;
26         0xa5a5 : 16 : littleendian;
27         0xa5a5 : 16 : bigendian;
28         0xa5a5 : 16 : nativeendian;
29         0xeeddcc : 24 : littleendian;
30         0xeeddcc : 24 : bigendian;
31         0xeeddcc : 24 : nativeendian;
32         0x48888888 : 31 : littleendian;
33         0x48888888 : 31 : bigendian;
34         0x48888888 : 31 : nativeendian;
35         0xaabbccdd_l : 32 : littleendian;
36         0xaabbccdd_l : 32 : bigendian;
37         0xaabbccdd_l : 32 : nativeendian;
38         0xaabbccddeeff_L : 48 : littleendian;
39         0xaabbccddeeff_L : 48 : bigendian;
40         0xaabbccddeeff_L : 48 : nativeendian;
41         0x0011aabbccddeeff_L : 64 : littleendian;
42         0x0011aabbccddeeff_L : 64 : bigendian;
43         0x0011aabbccddeeff_L : 64 : nativeendian
44       }
45     ) in
46     bitmatch bits with
47     | { _ : i : bitstring;
48         a : 1;
49         b0 : 2 : littleendian;
50         b1 : 2 : bigendian;
51         b2 : 2 : nativeendian;
52         c0 : 3 : littleendian;
53         c1 : 3 : bigendian;
54         c2 : 3 : nativeendian;
55         d0 : 8 : littleendian;
56         d1 : 8 : bigendian;
57         d2 : 8 : nativeendian;
58         e0 : 16 : littleendian;
59         e1 : 16 : bigendian;
60         e2 : 16 : nativeendian;
61         f0 : 24 : littleendian;
62         f1 : 24 : bigendian;
63         f2 : 24 : nativeendian;
64         g0 : 31 : littleendian;
65         g1 : 31 : bigendian;
66         g2 : 31 : nativeendian;
67         h0 : 32 : littleendian;
68         h1 : 32 : bigendian;
69         h2 : 32 : nativeendian;
70         j0 : 48 : littleendian;
71         j1 : 48 : bigendian;
72         j2 : 48 : nativeendian;
73         k0 : 64 : littleendian;
74         k1 : 64 : bigendian;
75         k2 : 64 : nativeendian
76       } ->
77         if a <> true
78           || b0 <> 2
79           || b1 <> 2
80           || b2 <> 2
81           || c0 <> 3
82           || c1 <> 3
83           || c2 <> 3
84           || d0 <> 0x5a
85           || d1 <> 0x5a
86           || d2 <> 0x5a
87           || e0 <> 0xa5a5
88           || e1 <> 0xa5a5
89           || e2 <> 0xa5a5
90           || f0 <> 0xeeddcc
91           || f1 <> 0xeeddcc
92           || f2 <> 0xeeddcc
93           || g0 <> 0x48888888
94           || g1 <> 0x48888888
95           || g2 <> 0x48888888
96           || h0 <> 0xaabbccdd_l
97           || h1 <> 0xaabbccdd_l
98           || h2 <> 0xaabbccdd_l
99           || j0 <> 0xaabbccddeeff_L
100           || j1 <> 0xaabbccddeeff_L
101           || j2 <> 0xaabbccddeeff_L
102           || k0 <> 0x0011aabbccddeeff_L
103           || k1 <> 0x0011aabbccddeeff_L
104           || k2 <> 0x0011aabbccddeeff_L
105         then (
106             eprintf "15_extract_int: match failed %b %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %ld %ld %ld %Ld %Ld %Ld %Ld %Ld %Ld\n"
107               a b0 b1 b2 c0 c1 c2 d0 d1 d2 e0 e1 e2 f0 f1 f2 g0 g1 g2 h0 h1 h2 j0 j1 j2 k0 k1 k2;
108             exit 1
109           )
110     | { _ } ->
111         failwith "15_extract_int"
112   done