Add signed int extract and construction functions, and test.
[ocaml-bitstring.git] / t13_signed_byte_create.ml
1 let a n =
2   let n' = 1 lsl (pred n) in
3      Array.to_list (Array.init n' (fun i -> -(n'-i), n'+i)) @
4       Array.to_list (Array.init (n' lsl 1) (fun i -> i,i));;
5
6 let t s i =
7     List.fold_left 
8       (fun ok (n,c) -> s n =  String.make 1 (Char.chr (c lsl (8-i))) && ok )
9       true
10       (a i);;
11
12 let ok = fst (List.fold_left (fun (ok,i) s ->
13                     t s i && ok, succ i) (true, 2)
14   [
15     (fun i -> Bitstring.string_of_bitstring (BITSTRING { i : 2 : signed }));
16     (fun i -> Bitstring.string_of_bitstring (BITSTRING { i : 3 : signed }));
17     (fun i -> Bitstring.string_of_bitstring (BITSTRING { i : 4 : signed }));
18     (fun i -> Bitstring.string_of_bitstring (BITSTRING { i : 5 : signed }));
19     (fun i -> Bitstring.string_of_bitstring (BITSTRING { i : 6 : signed }));
20     (fun i -> Bitstring.string_of_bitstring (BITSTRING { i : 7 : signed }));
21     (fun i -> Bitstring.string_of_bitstring (BITSTRING { i : 8 : signed }));
22   ])
23
24 in
25 if not ok then
26   failwith("t13_signed_byte_create: failed")