summaryrefslogtreecommitdiff
path: root/testsuite/tests/utils/overflow_detection.ml
blob: c9d984b55bf442013f53661aaab703d3bfa81867 (plain)
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
(* TEST
 include config;
 include testing;
 binary_modules = "config build_path_prefix_map misc identifiable numbers";
 bytecode;
*)

let print_int i =
  if i = max_int then
    "max_int"
  else if i = min_int then
    "min_int"
  else
    Int.to_string i

let test_no_overflow_add a b =
  Printf.printf "Misc.no_overflow_add %s %s = %b\n"
    (print_int a)
    (print_int b)
    (Misc.no_overflow_add a b)

let test_no_overflow_sub a b =
  Printf.printf "Misc.no_overflow_sub %s %s = %b\n"
    (print_int a)
    (print_int b)
    (Misc.no_overflow_sub a b)

let test_no_overflow_mul a b =
  Printf.printf "Misc.no_overflow_mul %s %s = %b\n"
    (print_int a)
    (print_int b)
    (Misc.no_overflow_mul a b)

let cartesian_product l1 l2 =
  List.concat
    (l1 |> List.map (fun v1 ->
     l2 |> List.map (fun v2 ->
     (v1, v2))))

let () =
  let ints = [ 0; 1; 2; max_int; -1; -2; min_int ] in
  let int_pairs = cartesian_product ints ints in
  int_pairs |> List.iter (fun (a, b) -> test_no_overflow_add a b);
  int_pairs |> List.iter (fun (a, b) -> test_no_overflow_sub a b);
  int_pairs |> List.iter (fun (a, b) -> test_no_overflow_mul a b)