Skip to content

Commit a228be1

Browse files
committed
sped up division tests
1 parent 9f18f0a commit a228be1

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

testsuite/tests/basic/division_by_constant.ml

+21-13
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,27 @@ let test_cases =
1616
ListLabels.concat_map
1717
[pred bit; bit; succ bit; rand ()]
1818
~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
2020

2121
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 =
2725
if divisor = min_int
2826
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+
3240

3341
(* test that the order of side effects are preserved in all cases *)
3442

@@ -88,9 +96,9 @@ let () =
8896
let[@inline always] check ~divisor =
8997
let[@inline always] quotient ~dividend = div dividend divisor in
9098
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))
94102

95103
(* manual tests *)
96104

@@ -103,7 +111,7 @@ let () = check ~divisor:(-7n)
103111
let () = check ~divisor:65537n
104112
let () = check ~divisor:(-65537n)
105113

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
107115
[[| int_size |]] to test against a hard-coded list of dividends. *)
108116

109117
let () = check ~divisor:0x1n

0 commit comments

Comments
 (0)