Skip to content

Commit 2c1eb3e

Browse files
authored
Merge pull request #34 from dgr/dgr-quot-rem-mod
Add tests for `quot`, `rem`, and `mod`
2 parents eed030a + be93c0b commit 2c1eb3e

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

test/clojure/core_test/mod.cljc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
(ns clojure.core-test.mod
2+
(:require [clojure.test :as t :refer [deftest testing is are]]
3+
[clojure.core-test.portability :as p]))
4+
5+
(deftest test-mod
6+
(are [type-pred expected x y] (let [r (mod x y)]
7+
(and (type-pred r)
8+
(= expected r)))
9+
int? 1 10 3
10+
int? 2 -10 3
11+
int? -1 -10 -3
12+
int? -2 10 -3
13+
14+
p/big-int? 1N 10 3N
15+
p/big-int? 2N -10 3N
16+
p/big-int? -1N -10 -3N
17+
p/big-int? -2N 10 -3N
18+
19+
p/big-int? 1N 10N 3
20+
p/big-int? 2N -10N 3
21+
p/big-int? -1N -10N -3
22+
p/big-int? -2N 10N -3
23+
24+
p/big-int? 1N 10N 3N
25+
p/big-int? 2N -10N 3N
26+
p/big-int? -1N -10N -3N
27+
p/big-int? -2N 10N -3N
28+
29+
double? 1.0 10 3.0
30+
double? 2.0 -10 3.0
31+
double? -1.0 -10 -3.0
32+
double? -2.0 10 -3.0
33+
double? 1.0 10.0 3
34+
double? 2.0 -10.0 3
35+
double? -1.0 -10.0 -3
36+
double? -2.0 10.0 -3
37+
double? 1.0 10.0 3.0
38+
double? 2.0 -10.0 3.0
39+
double? -1.0 -10.0 -3.0
40+
double? -2.0 10.0 -3.0
41+
42+
decimal? 1.0M 10 3.0M
43+
decimal? 2.0M -10 3.0M
44+
decimal? -1.0M -10 -3.0M
45+
decimal? -2.0M 10 -3.0M
46+
decimal? 1.0M 10.0M 3
47+
decimal? 2.0M -10.0M 3
48+
decimal? -1.0M -10.0M -3
49+
decimal? -2.0M 10.0M -3
50+
decimal? 1.0M 10.0M 3.0M
51+
decimal? 2.0M -10.0M 3.0M
52+
decimal? -1.0M -10.0M -3.0M
53+
decimal? -2.0M 10.0M -3.0M
54+
55+
;; Unexpectedly downconverts result to double, rather than BigDecimal
56+
double? 1.0 10.0M 3.0
57+
double? 2.0 -10.0M 3.0
58+
double? -1.0 -10.0M -3.0
59+
double? -2.0 10.0M -3.0
60+
double? 1.0 10.0 3.0M
61+
double? 2.0 -10.0 3.0M
62+
double? -1.0 -10.0 -3.0M
63+
double? -2.0 10.0 -3.0M
64+
65+
p/big-int? 0N 3 1/2
66+
ratio? 1/3 3 4/3
67+
ratio? 7/2 37/2 15
68+
p/big-int? 0N 3 -1/2
69+
p/big-int? -1N 3 -4/3
70+
ratio? -23/2 37/2 -15
71+
p/big-int? 0N -3 1/2
72+
p/big-int? 1N -3 4/3
73+
ratio? 23/2 -37/2 15
74+
p/big-int? 0N -3 -1/2
75+
ratio? -1/3 -3 -4/3
76+
ratio? -7/2 -37/2 -15)
77+
78+
(is (thrown? Exception (mod 10 0)))
79+
(is (thrown? Exception (mod ##Inf 1))) ; surprising since (/ ##Inf 1) = ##Inf
80+
(is (NaN? (mod 1 ##Inf)))
81+
(is (thrown? Exception (mod ##-Inf 1))) ; surprising since (/ ##-Inf 1) = ##-Inf
82+
(is (NaN? (mod 1 ##-Inf)))
83+
(is (thrown? Exception (mod ##NaN 1)))
84+
(is (thrown? Exception (mod 1 ##NaN)))
85+
(is (thrown? Exception (mod ##NaN 1))))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns clojure.core-test.portability)
2+
3+
(defn big-int? [n]
4+
(and (integer? n)
5+
(not (int? n))))

test/clojure/core_test/quot.cljc

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
(ns clojure.core-test.quot
2+
(:require [clojure.test :as t :refer [deftest testing is are]]
3+
[clojure.core-test.portability :as p]))
4+
5+
(deftest test-quot
6+
(are [type-pred expected x y] (let [r (quot x y)]
7+
(and (type-pred r)
8+
(= expected r)))
9+
int? 3 10 3
10+
int? -3 -10 3
11+
int? 3 -10 -3
12+
int? -3 10 -3
13+
14+
p/big-int? 3N 10 3N
15+
p/big-int? -3N -10 3N
16+
p/big-int? 3N -10 -3N
17+
p/big-int? -3N 10 -3N
18+
p/big-int? 3N 10N 3
19+
p/big-int? -3N -10N 3
20+
p/big-int? 3N -10N -3
21+
p/big-int? -3N 10N -3
22+
p/big-int? 3N 10N 3N
23+
p/big-int? -3N -10N 3N
24+
p/big-int? 3N -10N -3N
25+
p/big-int? -3N 10N -3N
26+
27+
double? 3.0 10 3.0
28+
double? -3.0 -10 3.0
29+
double? 3.0 -10 -3.0
30+
double? -3.0 10 -3.0
31+
double? 3.0 10.0 3
32+
double? -3.0 -10.0 3
33+
double? 3.0 -10.0 -3
34+
double? -3.0 10.0 -3
35+
double? 3.0 10.0 3.0
36+
double? -3.0 -10.0 3.0
37+
double? 3.0 -10.0 -3.0
38+
double? -3.0 10.0 -3.0
39+
40+
decimal? 3.0M 10 3.0M
41+
decimal? -3.0M -10 3.0M
42+
decimal? 3.0M -10 -3.0M
43+
decimal? -3.0M 10 -3.0M
44+
decimal? 3.0M 10.0M 3
45+
decimal? -3.0M -10.0M 3
46+
decimal? 3.0M -10.0M -3
47+
decimal? -3.0M 10.0M -3
48+
decimal? 3.0M 10.0M 3.0M
49+
decimal? -3.0M -10.0M 3.0M
50+
decimal? 3.0M -10.0M -3.0M
51+
decimal? -3.0M 10.0M -3.0M
52+
53+
;; Unexpectedly downconverts result to double, rather than BigDecimal
54+
double? 3.0 10.0M 3.0
55+
double? -3.0 -10.0M 3.0
56+
double? 3.0 -10.0M -3.0
57+
double? -3.0 10.0M -3.0
58+
double? 3.0 10.0 3.0M
59+
double? -3.0 -10.0 3.0M
60+
double? 3.0 -10.0 -3.0M
61+
double? -3.0 10.0 -3.0M
62+
63+
;; Anything with a ratio seems to convert to BigInt
64+
p/big-int? 6N 3 1/2
65+
p/big-int? 2N 3 4/3
66+
p/big-int? 1N 37/2 15
67+
p/big-int? -6N 3 -1/2
68+
p/big-int? -2N 3 -4/3
69+
p/big-int? -1N 37/2 -15
70+
p/big-int? -6N -3 1/2
71+
p/big-int? -2N -3 4/3
72+
p/big-int? -1N -37/2 15
73+
p/big-int? 6N -3 -1/2
74+
p/big-int? 2N -3 -4/3
75+
p/big-int? 1N -37/2 -15
76+
77+
double? 0.0 1 ##Inf
78+
double? 0.0 1 ##-Inf)
79+
80+
(is (thrown? Exception (quot 10 0)))
81+
(is (thrown? Exception (quot ##Inf 1))) ; surprising since (/ ##Inf 1) = ##Inf
82+
(is (thrown? Exception (quot ##-Inf 1))) ; surprising since (/ ##-Inf 1) = ##-Inf
83+
(is (thrown? Exception (quot ##NaN 1)))
84+
(is (thrown? Exception (quot 1 ##NaN)))
85+
(is (thrown? Exception (quot ##NaN 1))))
86+

test/clojure/core_test/rem.cljc

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
(ns clojure.core-test.rem
2+
(:require [clojure.test :as t :refer [deftest testing is are]]
3+
[clojure.core-test.portability :as p]))
4+
5+
(deftest test-rem
6+
(are [type-pred expected x y] (let [r (rem x y)]
7+
(and (type-pred r)
8+
(= expected r)))
9+
int? 1 10 3
10+
int? -1 -10 3
11+
int? -1 -10 -3
12+
int? 1 10 -3
13+
14+
p/big-int? 1N 10 3N
15+
p/big-int? -1N -10 3N
16+
p/big-int? -1N -10 -3N
17+
p/big-int? 1N 10 -3N
18+
p/big-int? 1N 10N 3
19+
p/big-int? -1N -10N 3
20+
p/big-int? -1N -10N -3
21+
p/big-int? 1N 10N -3
22+
p/big-int? 1N 10N 3N
23+
p/big-int? -1N -10N 3N
24+
p/big-int? -1N -10N -3N
25+
p/big-int? 1N 10N -3N
26+
27+
double? 1.0 10 3.0
28+
double? -1.0 -10 3.0
29+
double? -1.0 -10 -3.0
30+
double? 1.0 10 -3.0
31+
double? 1.0 10.0 3
32+
double? -1.0 -10.0 3
33+
double? -1.0 -10.0 -3
34+
double? 1.0 10.0 -3
35+
double? 1.0 10.0 3.0
36+
double? -1.0 -10.0 3.0
37+
double? -1.0 -10.0 -3.0
38+
double? 1.0 10.0 -3.0
39+
40+
decimal? 1.0M 10 3.0M
41+
decimal? -1.0M -10 3.0M
42+
decimal? -1.0M -10 -3.0M
43+
decimal? 1.0M 10 -3.0M
44+
decimal? 1.0M 10.0M 3
45+
decimal? -1.0M -10.0M 3
46+
decimal? -1.0M -10.0M -3
47+
decimal? 1.0M 10.0M -3
48+
decimal? 1.0M 10.0M 3.0M
49+
decimal? -1.0M -10.0M 3.0M
50+
decimal? -1.0M -10.0M -3.0M
51+
decimal? 1.0M 10.0M -3.0M
52+
53+
;; Unexpectedly downconverts result to double, rather than BigDecimal
54+
double? 1.0 10.0M 3.0
55+
double? -1.0 -10.0M 3.0
56+
double? -1.0 -10.0M -3.0
57+
double? 1.0 10.0M -3.0
58+
double? 1.0 10.0 3.0M
59+
double? -1.0 -10.0 3.0M
60+
double? -1.0 -10.0 -3.0M
61+
double? 1.0 10.0 -3.0M
62+
63+
p/big-int? 0N 3 1/2
64+
ratio? 1/3 3 4/3
65+
ratio? 7/2 37/2 15
66+
p/big-int? 0N 3 -1/2
67+
ratio? 1/3 3 -4/3
68+
ratio? 7/2 37/2 -15
69+
p/big-int? 0N -3 1/2
70+
ratio? -1/3 -3 4/3
71+
ratio? -7/2 -37/2 15
72+
p/big-int? 0N -3 -1/2
73+
ratio? -1/3 -3 -4/3
74+
ratio? -7/2 -37/2 -15)
75+
76+
(is (thrown? Exception (rem 10 0)))
77+
(is (thrown? Exception (rem ##Inf 1))) ; surprising since (/ ##Inf 1) = ##Inf
78+
(is (NaN? (rem 1 ##Inf)))
79+
(is (thrown? Exception (rem ##-Inf 1))) ; surprising since (/ ##-Inf 1) = ##-Inf
80+
(is (NaN? (rem 1 ##-Inf)))
81+
(is (thrown? Exception (rem ##NaN 1)))
82+
(is (thrown? Exception (rem 1 ##NaN)))
83+
(is (thrown? Exception (rem ##NaN 1))))

0 commit comments

Comments
 (0)