File: 10_match_bits.ml

package info (click to toggle)
ocaml-bitstring 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,276 kB
  • ctags: 492
  • sloc: ml: 3,360; sh: 377; makefile: 324; ansic: 113
file content (86 lines) | stat: -rw-r--r-- 2,461 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(* Match random bits.
 * $Id: 10_match_bits.ml 142 2008-07-17 15:45:56Z richard.wm.jones $
 *)

open Printf

let rec range a b =
  if a <= b then
    a :: range (a+1) b
  else
    []

let () =
  Random.self_init ();

  for len = 0 to 999 do
    (* Create a random string of bits. *)
    let expected = List.map (fun _ -> Random.bool ()) (range 0 (len-1)) in

    let bits = Bitstring.Buffer.create () in
    List.iter (Bitstring.Buffer.add_bit bits) expected;
    let bits = Bitstring.Buffer.contents bits in

    (* Now read the bitstring in groups of 1, 2, 3 .. etc. bits.
     * In each case check the result against what we generated ('expected').
     *)
    let actual =
      let rec loop bits =
	bitmatch bits with
	| { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest
	| { _ } -> []
      in
      loop bits in
    if actual <> expected then
      failwith (sprintf "match bits: failed on 1 bit test, len = %d" len);

    let actual =
      let rec loop bits =
	bitmatch bits with
	| { b0 : 1; b1 : 1; rest : -1 : bitstring } -> b0 :: b1 :: loop rest
	| { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest
	| { _ } -> []
      in
      loop bits in
    if actual <> expected then
      failwith (sprintf "match bits: failed on 1 bit test, len = %d" len);

    let actual =
      let rec loop bits =
	bitmatch bits with
	| { b0 : 1; b1 : 1; b2 : 1;
	    rest : -1 : bitstring } -> b0 :: b1 :: b2 :: loop rest
	| { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest
	| { _ } -> []
      in
      loop bits in
    if actual <> expected then
      failwith (sprintf "match bits: failed on 1 bit test, len = %d" len);

    let actual =
      let rec loop bits =
	bitmatch bits with
	| { b0 : 1; b1 : 1; b2 : 1; b3 : 1;
	    rest : -1 : bitstring } -> b0 :: b1 :: b2 :: b3 :: loop rest
	| { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest
	| { _ } -> []
      in
      loop bits in
    if actual <> expected then
      failwith (sprintf "match bits: failed on 1 bit test, len = %d" len);

    let actual =
      let rec loop bits =
	bitmatch bits with
	| { b0 : 1; b1 : 1; b2 : 1; b3 : 1;
	    b4 : 1; b5 : 1; b6 : 1; b7 : 1;
	    b8 : 1;
	    rest : -1 : bitstring } ->
	    b0 :: b1 :: b2 :: b3 :: b4 :: b5 :: b6 :: b7 :: b8 :: loop rest
	| { b0 : 1; rest : -1 : bitstring } -> b0 :: loop rest
	| { _ } -> []
      in
      loop bits in
    if actual <> expected then
      failwith (sprintf "match bits: failed on 1 bit test, len = %d" len);
  done