File: test_maybe_bound.ml

package info (click to toggle)
janest-base 0.17.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,632 kB
  • sloc: ml: 48,653; ansic: 281; javascript: 126; makefile: 14
file content (123 lines) | stat: -rw-r--r-- 3,161 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
open! Import
open! Maybe_bound

let%test_unit "bounds_crossed" =
  let a, b, c, d = Incl 1, Excl 1, Incl 3, Excl 3 in
  let cases =
    [ a, a, false
    ; a, b, false
    ; a, c, false
    ; a, d, false
    ; b, a, false
    ; b, b, false
    ; b, c, false
    ; b, d, false
    ; c, a, true
    ; c, b, true
    ; c, c, false
    ; c, d, false
    ; d, a, true
    ; d, b, true
    ; d, c, false
    ; d, d, false
    ]
  in
  List.iter cases ~f:(fun (lower, upper, expect) ->
    let actual = bounds_crossed ~lower ~upper ~compare in
    assert ([%compare.equal: bool] expect actual))
;;

let%test_module "is_lower_bound" =
  (module struct
    let compare = Int.compare
    let%test _ = is_lower_bound Unbounded ~of_:Int.min_value ~compare
    let%test _ = not (is_lower_bound (Incl 2) ~of_:1 ~compare)
    let%test _ = is_lower_bound (Incl 2) ~of_:2 ~compare
    let%test _ = is_lower_bound (Incl 2) ~of_:3 ~compare
    let%test _ = not (is_lower_bound (Excl 2) ~of_:1 ~compare)
    let%test _ = not (is_lower_bound (Excl 2) ~of_:2 ~compare)
    let%test _ = is_lower_bound (Excl 2) ~of_:3 ~compare
  end)
;;

let%test_module "is_upper_bound" =
  (module struct
    let compare = Int.compare
    let%test _ = is_upper_bound Unbounded ~of_:Int.max_value ~compare
    let%test _ = is_upper_bound (Incl 2) ~of_:1 ~compare
    let%test _ = is_upper_bound (Incl 2) ~of_:2 ~compare
    let%test _ = not (is_upper_bound (Incl 2) ~of_:3 ~compare)
    let%test _ = is_upper_bound (Excl 2) ~of_:1 ~compare
    let%test _ = not (is_upper_bound (Excl 2) ~of_:2 ~compare)
    let%test _ = not (is_upper_bound (Excl 2) ~of_:3 ~compare)
  end)
;;

let%test_module "check_range" =
  (module struct
    let compare = Int.compare

    let tests (lower, upper) cases =
      List.iter cases ~f:(fun (n, comparison) ->
        [%test_result: interval_comparison]
          ~expect:comparison
          (compare_to_interval_exn n ~lower ~upper ~compare);
        [%test_result: bool]
          ~expect:
            (match comparison with
             | In_range -> true
             | _ -> false)
          (interval_contains_exn n ~lower ~upper ~compare))
    ;;

    let%test_unit _ =
      tests
        (Unbounded, Unbounded)
        [ Int.min_value, In_range; 0, In_range; Int.max_value, In_range ]
    ;;

    let%test_unit _ =
      tests
        (Incl 2, Incl 4)
        [ 1, Below_lower_bound
        ; 2, In_range
        ; 3, In_range
        ; 4, In_range
        ; 5, Above_upper_bound
        ]
    ;;

    let%test_unit _ =
      tests
        (Incl 2, Excl 4)
        [ 1, Below_lower_bound
        ; 2, In_range
        ; 3, In_range
        ; 4, Above_upper_bound
        ; 5, Above_upper_bound
        ]
    ;;

    let%test_unit _ =
      tests
        (Excl 2, Incl 4)
        [ 1, Below_lower_bound
        ; 2, Below_lower_bound
        ; 3, In_range
        ; 4, In_range
        ; 5, Above_upper_bound
        ]
    ;;

    let%test_unit _ =
      tests
        (Excl 2, Excl 4)
        [ 1, Below_lower_bound
        ; 2, Below_lower_bound
        ; 3, In_range
        ; 4, Above_upper_bound
        ; 5, Above_upper_bound
        ]
    ;;
  end)
;;