@@ -16,19 +16,27 @@ let test_cases =
16
16
ListLabels. concat_map
17
17
[pred bit; bit; succ bit; rand () ]
18
18
~f: (fun x -> [x; lognot x; sub max_int x; lognot (sub max_int x)]))
19
- |> List. concat |> List. sort_uniq compare
19
+ |> List. concat |> List. sort_uniq compare |> Array. of_list
20
20
21
21
let [@ inline never] check_result ~dividend ~divisor ~quotient ~remainder =
22
- assert (divisor <> 0n );
23
- assert (quotient = div dividend divisor);
24
- assert (remainder = rem dividend divisor);
25
- assert (dividend = add (mul quotient divisor) remainder);
26
- assert (
22
+ let [@ inline] sign x = if x < 0n then - 1 else if x = 0n then 0 else 1 in
23
+ let identity = dividend = add (mul quotient divisor) remainder in
24
+ let magnitude =
27
25
if divisor = min_int
28
26
then remainder <> min_int
29
- else abs remainder < abs divisor);
30
- let [@ inline] sign x = if x < 0n then - 1 else if x = 0n then 0 else 1 in
31
- assert (remainder = 0n || sign remainder = sign dividend)
27
+ else abs remainder < abs divisor
28
+ in
29
+ let sign = (remainder = 0n || sign remainder = sign dividend) in
30
+ if not (identity && magnitude && sign) then (
31
+ Printf. fprintf
32
+ stderr
33
+ " FAILED: dividend=%nd, divisor=%nd, quotient=%nd, remainder=%nd\n %!"
34
+ dividend
35
+ divisor
36
+ quotient
37
+ remainder;
38
+ exit 1 )
39
+
32
40
33
41
(* test that the order of side effects are preserved in all cases *)
34
42
@@ -88,9 +96,9 @@ let () =
88
96
let [@ inline always] check ~divisor =
89
97
let [@ inline always] quotient ~dividend = div dividend divisor in
90
98
let [@ inline always] remainder ~dividend = rem dividend divisor in
91
- ListLabels . iter test_cases ~f: (fun dividend ->
92
- check_result ~dividend ~divisor ~quotient: (quotient ~dividend )
93
- ~remainder: (remainder ~dividend ))
99
+ ArrayLabels . iter test_cases ~f: (fun dividend ->
100
+ check_result ~dividend ~divisor ~quotient: (quotient ~dividend )
101
+ ~remainder: (remainder ~dividend ))
94
102
95
103
(* manual tests *)
96
104
@@ -103,7 +111,7 @@ let () = check ~divisor:(-7n)
103
111
let () = check ~divisor: 65537n
104
112
let () = check ~divisor: (- 65537n )
105
113
106
- (* These tests are generated by a manual run of test_cases with seed
114
+ (* The following tests are generated by a manual run of test_cases with seed
107
115
[[| int_size |]] to test against a hard-coded list of dividends. *)
108
116
109
117
let () = check ~divisor: 0x1n
0 commit comments