File: test_minmax.ml

package info (click to toggle)
janest-base 0.17.3-1
  • 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 (166 lines) | stat: -rw-r--r-- 4,501 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
open Import

module Size = struct
  type t =
    | Small
    | Large
end

let[@inline never] test (type t) m size =
  let (module T : Int.S with type t = t) = m in
  let fwd =
    match (size : Size.t) with
    | Small -> List.map ~f:T.of_int_exn [ 0; -20; -10; -1; 1; 2; 4; 100 ]
    | Large -> [ T.min_value; T.max_value; T.zero ]
  in
  let rev = List.rev fwd in
  List.iter2_exn fwd rev ~f:(fun a b ->
    print_s [%message "min" ~_:(a : T.t) ~_:(b : T.t) "=" ~_:(T.min a b : T.t)];
    print_s [%message "max" ~_:(a : T.t) ~_:(b : T.t) "=" ~_:(T.max a b : T.t)])
;;

let%expect_test "small values" =
  let test (module T : Int.S) =
    test (module T) Small;
    [%expect
      {|
      (min 0 100 = 0)
      (max 0 100 = 100)
      (min -20 4 = -20)
      (max -20 4 = 4)
      (min -10 2 = -10)
      (max -10 2 = 2)
      (min -1 1 = -1)
      (max -1 1 = 1)
      (min 1 -1 = -1)
      (max 1 -1 = 1)
      (min 2 -10 = -10)
      (max 2 -10 = 2)
      (min 4 -20 = -20)
      (max 4 -20 = 4)
      (min 100 0 = 0)
      (max 100 0 = 100)
      |}]
  in
  test (module Int);
  test (module Int64);
  test (module Int32);
  test (module Nativeint);
  [%expect {| |}]
;;

let%expect_test "fixed-size types" =
  test (module Int64) Large;
  [%expect
    {|
    (min -9_223_372_036_854_775_808 0 = -9_223_372_036_854_775_808)
    (max -9_223_372_036_854_775_808 0 = 0)
    (min
     9_223_372_036_854_775_807
     9_223_372_036_854_775_807
     =
     9_223_372_036_854_775_807)
    (max
     9_223_372_036_854_775_807
     9_223_372_036_854_775_807
     =
     9_223_372_036_854_775_807)
    (min 0 -9_223_372_036_854_775_808 = -9_223_372_036_854_775_808)
    (max 0 -9_223_372_036_854_775_808 = 0)
    |}];
  test (module Int32) Large;
  [%expect
    {|
    (min -2_147_483_648 0 = -2_147_483_648)
    (max -2_147_483_648 0 = 0)
    (min 2_147_483_647 2_147_483_647 = 2_147_483_647)
    (max 2_147_483_647 2_147_483_647 = 2_147_483_647)
    (min 0 -2_147_483_648 = -2_147_483_648)
    (max 0 -2_147_483_648 = 0)
    |}]
;;

let%expect_test ("64-bit platforms" [@tags "no-js", "64-bits-only"]) =
  test (module Int) Large;
  [%expect
    {|
    (min -4_611_686_018_427_387_904 0 = -4_611_686_018_427_387_904)
    (max -4_611_686_018_427_387_904 0 = 0)
    (min
     4_611_686_018_427_387_903
     4_611_686_018_427_387_903
     =
     4_611_686_018_427_387_903)
    (max
     4_611_686_018_427_387_903
     4_611_686_018_427_387_903
     =
     4_611_686_018_427_387_903)
    (min 0 -4_611_686_018_427_387_904 = -4_611_686_018_427_387_904)
    (max 0 -4_611_686_018_427_387_904 = 0)
    |}];
  test (module Nativeint) Large;
  [%expect
    {|
    (min -9_223_372_036_854_775_808 0 = -9_223_372_036_854_775_808)
    (max -9_223_372_036_854_775_808 0 = 0)
    (min
     9_223_372_036_854_775_807
     9_223_372_036_854_775_807
     =
     9_223_372_036_854_775_807)
    (max
     9_223_372_036_854_775_807
     9_223_372_036_854_775_807
     =
     9_223_372_036_854_775_807)
    (min 0 -9_223_372_036_854_775_808 = -9_223_372_036_854_775_808)
    (max 0 -9_223_372_036_854_775_808 = 0)
    |}]
;;

let%expect_test ("32-bit platforms" [@tags "no-js", "32-bits-only"]) =
  test (module Int) Large;
  [%expect
    {|
    (min -1_073_741_824 0 = -1_073_741_824)
    (max -1_073_741_824 0 = 0)
    (min 1_073_741_823 1_073_741_823 = 1_073_741_823)
    (max 1_073_741_823 1_073_741_823 = 1_073_741_823)
    (min 0 -1_073_741_824 = -1_073_741_824)
    (max 0 -1_073_741_824 = 0)
    |}];
  test (module Nativeint) Large;
  [%expect
    {|
    (min -2_147_483_648 0 = -2_147_483_648)
    (max -2_147_483_648 0 = 0)
    (min 2_147_483_647 2_147_483_647 = 2_147_483_647)
    (max 2_147_483_647 2_147_483_647 = 2_147_483_647)
    (min 0 -2_147_483_648 = -2_147_483_648)
    (max 0 -2_147_483_648 = 0)
    |}]
;;

let%expect_test ("js_of_ocaml platforms" [@tags "js-only"]) =
  test (module Int) Large;
  [%expect
    {|
    (min -2_147_483_648 0 = -2_147_483_648)
    (max -2_147_483_648 0 = 0)
    (min 2_147_483_647 2_147_483_647 = 2_147_483_647)
    (max 2_147_483_647 2_147_483_647 = 2_147_483_647)
    (min 0 -2_147_483_648 = -2_147_483_648)
    (max 0 -2_147_483_648 = 0)
    |}];
  test (module Nativeint) Large;
  [%expect
    {|
    (min -2_147_483_648 0 = -2_147_483_648)
    (max -2_147_483_648 0 = 0)
    (min 2_147_483_647 2_147_483_647 = 2_147_483_647)
    (max 2_147_483_647 2_147_483_647 = 2_147_483_647)
    (min 0 -2_147_483_648 = -2_147_483_648)
    (max 0 -2_147_483_648 = 0)
    |}]
;;