File: test_int.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 (300 lines) | stat: -rw-r--r-- 7,774 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
open! Import
open! Int

let%expect_test ("hash coherence" [@tags "64-bits-only"]) =
  check_int_hash_coherence [%here] (module Int);
  [%expect {| |}]
;;

let%expect_test "[max_value_30_bits]" =
  print_s [%sexp (max_value_30_bits : t)];
  [%expect {| 1_073_741_823 |}]
;;

let%expect_test "of_string_opt" =
  print_s [%sexp (of_string_opt "1" : int option)];
  [%expect "(1)"];
  print_s [%sexp (of_string_opt "1a" : int option)];
  [%expect "()"];
  print_s [%sexp (of_string_opt "99999999999999999999999999999" : int option)];
  [%expect "()"]
;;

let%expect_test "to_string_hum" =
  let test_roundtrip int =
    require_equal [%here] (module Int) (of_string (to_string_hum int)) int
  in
  quickcheck_m
    [%here]
    (module struct
      type t = int [@@deriving quickcheck, sexp_of]
    end)
    ~examples:[ Int.min_value; Int.max_value ]
    ~f:test_roundtrip
;;

let%expect_test "hex" =
  let test x =
    let n = Or_error.try_with (fun () -> Int.Hex.of_string x) in
    print_s [%message (n : int Or_error.t)]
  in
  test "0x1c5f";
  [%expect {| (n (Ok 7_263)) |}];
  test "0x1c5f NON-HEX-GARBAGE";
  [%expect
    {|
    (n (
      Error (
        Failure
        "Base.Int.Hex.of_string: invalid input \"0x1c5f NON-HEX-GARBAGE\"")))
    |}]
;;

let%test_module "Hex" =
  (module struct
    let f (i, s_hum) =
      let s = String.filter s_hum ~f:(fun c -> not (Char.equal c '_')) in
      let sexp_hum = Sexp.Atom s_hum in
      let sexp = Sexp.Atom s in
      [%test_result: Sexp.t] ~message:"sexp_of_t" ~expect:sexp (Hex.sexp_of_t i);
      [%test_result: int] ~message:"t_of_sexp" ~expect:i (Hex.t_of_sexp sexp);
      [%test_result: int] ~message:"t_of_sexp[human]" ~expect:i (Hex.t_of_sexp sexp_hum);
      [%test_result: string] ~message:"to_string" ~expect:s (Hex.to_string i);
      [%test_result: string] ~message:"to_string_hum" ~expect:s_hum (Hex.to_string_hum i);
      [%test_result: int] ~message:"of_string" ~expect:i (Hex.of_string s);
      [%test_result: int] ~message:"of_string[human]" ~expect:i (Hex.of_string s_hum)
    ;;

    let%test_unit _ =
      List.iter
        ~f
        [ 0, "0x0"
        ; 1, "0x1"
        ; 2, "0x2"
        ; 5, "0x5"
        ; 10, "0xa"
        ; 16, "0x10"
        ; 254, "0xfe"
        ; 65_535, "0xffff"
        ; 65_536, "0x1_0000"
        ; 1_000_000, "0xf_4240"
        ; -1, "-0x1"
        ; -2, "-0x2"
        ; -1_000_000, "-0xf_4240"
        ; ( max_value
          , match num_bits with
            | 31 -> "0x3fff_ffff"
            | 32 -> "0x7fff_ffff"
            | 63 -> "0x3fff_ffff_ffff_ffff"
            | _ -> assert false )
        ; ( min_value
          , match num_bits with
            | 31 -> "-0x4000_0000"
            | 32 -> "-0x8000_0000"
            | 63 -> "-0x4000_0000_0000_0000"
            | _ -> assert false )
        ]
    ;;

    let%test_unit _ = [%test_result: int] (Hex.of_string "0XA") ~expect:10

    let%test_unit _ =
      match Option.try_with (fun () -> Hex.of_string "0") with
      | None -> ()
      | Some _ -> failwith "Hex must always have a 0x prefix."
    ;;

    let%test_unit _ =
      match Option.try_with (fun () -> Hex.of_string "0x_0") with
      | None -> ()
      | Some _ -> failwith "Hex may not have '_' before the first digit."
    ;;
  end)
;;

let%expect_test "binary" =
  quickcheck_m
    [%here]
    (module struct
      type t = int [@@deriving quickcheck, sexp_of]
    end)
    ~f:(fun (t : t) -> ignore (Binary.to_string t : string))
;;

let test_binary i =
  Binary.to_string_hum i |> print_endline;
  Binary.to_string i |> print_endline;
  print_s [%sexp (i : Binary.t)]
;;

let%expect_test "binary" =
  test_binary 0b0;
  [%expect {|
    0b0
    0b0
    0b0
    |}];
  test_binary 0b01;
  [%expect {|
    0b1
    0b1
    0b1
    |}];
  test_binary 0b100;
  [%expect {|
    0b100
    0b100
    0b100
    |}];
  test_binary 0b101;
  [%expect {|
    0b101
    0b101
    0b101
    |}];
  test_binary 0b10_1010_1010_1010;
  [%expect {|
    0b10_1010_1010_1010
    0b10101010101010
    0b10_1010_1010_1010
    |}];
  test_binary 0b11_1111_0000_0000;
  [%expect {|
    0b11_1111_0000_0000
    0b11111100000000
    0b11_1111_0000_0000
    |}];
  test_binary 19;
  [%expect {|
    0b1_0011
    0b10011
    0b1_0011
    |}];
  test_binary 0;
  [%expect {|
    0b0
    0b0
    0b0
    |}]
;;

let%expect_test ("63-bit cases" [@tags "64-bits-only"]) =
  test_binary (-1);
  [%expect
    {|
    0b111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
    0b111111111111111111111111111111111111111111111111111111111111111
    0b111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
    |}];
  test_binary max_value;
  [%expect
    {|
    0b11_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
    0b11111111111111111111111111111111111111111111111111111111111111
    0b11_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
    |}];
  test_binary min_value;
  [%expect
    {|
    0b100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000
    0b100000000000000000000000000000000000000000000000000000000000000
    0b100_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000
    |}]
;;

let%expect_test ("32-bit cases" [@tags "js-only"]) =
  test_binary (-1);
  [%expect
    {|
    0b1111_1111_1111_1111_1111_1111_1111_1111
    0b11111111111111111111111111111111
    0b1111_1111_1111_1111_1111_1111_1111_1111
    |}];
  test_binary max_value;
  [%expect
    {|
    0b111_1111_1111_1111_1111_1111_1111_1111
    0b1111111111111111111111111111111
    0b111_1111_1111_1111_1111_1111_1111_1111
    |}];
  test_binary min_value;
  [%expect
    {|
    0b1000_0000_0000_0000_0000_0000_0000_0000
    0b10000000000000000000000000000000
    0b1000_0000_0000_0000_0000_0000_0000_0000
    |}]
;;

let%test _ = neg 5 + 5 = 0
let%test _ = pow min_value 1 = min_value
let%test _ = pow max_value 1 = max_value

let%test "comparisons" =
  let original_compare (x : int) y = Stdlib.compare x y in
  let valid_compare x y =
    let result = compare x y in
    let expect = original_compare x y in
    assert (Bool.( = ) (result < 0) (expect < 0));
    assert (Bool.( = ) (result > 0) (expect > 0));
    assert (Bool.( = ) (result = 0) (expect = 0));
    assert (result = expect)
  in
  valid_compare min_value min_value;
  valid_compare min_value (-1);
  valid_compare (-1) min_value;
  valid_compare min_value 0;
  valid_compare 0 min_value;
  valid_compare max_value (-1);
  valid_compare (-1) max_value;
  valid_compare max_value min_value;
  valid_compare max_value max_value;
  true
;;

let test f x = test_conversion ~to_string:Int.Hex.to_string_hum f x
let numbers = [ 0x10_20; 0x11_22_33; 0x11_22_33_1F; 0x11_22_33_44 ]

let%expect_test "bswap16" =
  List.iter numbers ~f:(test bswap16);
  [%expect
    {|
    0x1020 --> 0x2010
    0x11_2233 --> 0x3322
    0x1122_331f --> 0x1f33
    0x1122_3344 --> 0x4433
    |}]
;;

let%expect_test "% and /%" =
  quickcheck_m
    [%here]
    (module struct
      type t =
        int
        * (int
          [@quickcheck.generator Base_quickcheck.Generator.small_strictly_positive_int])
      [@@deriving quickcheck, sexp_of]
    end)
    ~f:(fun (a, b) ->
      let r = a % b in
      let q = a /% b in
      require [%here] (r >= 0);
      require_equal [%here] (module Int) a ((q * b) + r))
;;

include (
  struct
    (** Various functors whose type-correctness ensures desired relationships between
      interfaces. *)

    (* O contained in S *)
    module _ (M : S) : module type of M.O = M

    (* O contained in S_unbounded *)
    module _ (M : S_unbounded) : module type of M.O = M

    (* S_unbounded in S *)
    module _ (M : S) : S_unbounded = M
  end :
    sig end)