File: BitstringQualifierTest.ml

package info (click to toggle)
ocaml-bitstring 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 908 kB
  • sloc: ml: 3,817; ansic: 136; makefile: 3
file content (64 lines) | stat: -rw-r--r-- 2,013 bytes parent folder | download
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
(*
 * Copyright (c) 2016 Xavier R. Guérin <xguerin@users.noreply.github.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

open OUnit2
open Bitstring

(*
 * Test of the map() qualifier
 *)

let map_test context =
  let source = [%bitstring {| 1 : 16 ; 2 : 16 |}] in
  match%bitstring source with
  | {| value0 : 16 : map (fun v -> v + 1)
     ; value1 : 16 : map (fun v -> Some v)
    |} ->
    assert_equal value0 2;
    begin match value1 with
      | Some v -> assert_equal v 2
      | _      -> assert_bool "Invalid map result" false
    end
  | {| _ |} -> assert_bool "Invalid pattern" false

(*
 * Test of the save_offset_to() qualifier
 *)

let save_offset_test context =
  let source = [%bitstring {| 1 : 3 ; 2 : 7; 5 : 4; "abc" : -1 : string |}] in
  match%bitstring source with
  | {| _      :  3 : save_offset_to (off0)
     ; _      :  7 : save_offset_to (off1)
     ; _      :  4 : save_offset_to (off2)
     ; "abc"  : 24 : save_offset_to (off3), string
    |} ->
    assert_equal off0 0;
    assert_equal off1 3;
    assert_equal off2 10;
    assert_equal off3 14
  | {| _ |} -> assert_bool "Invalid pattern" false

(*
 * Test suite definition
 *)

let suite = "BitstringQualifierTest" >::: [
    "map"             >:: map_test;
    "save_offset_to"  >:: save_offset_test
  ]

let () = run_test_tt_main suite