Skip to content

Commit 59cbdba

Browse files
authored
Merge pull request #27 from dgr/dgr-minus-slash
Add tests for - and /
2 parents 5a73a7a + f286244 commit 59cbdba

File tree

2 files changed

+394
-0
lines changed

2 files changed

+394
-0
lines changed

test/clojure/core_test/minus.cljc

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
(ns clojure.core-test.minus
2+
(:require [clojure.test :as t :refer [deftest testing is are]]
3+
[clojure.core-test.number-range :as r]))
4+
5+
(deftest common
6+
(are [expected x y] (= expected (- x y))
7+
;; longs
8+
0 0 0
9+
1 1 0
10+
-1 -1 0
11+
0 1 1
12+
-1 0 1
13+
-2 -1 1
14+
2 1 -1
15+
1 0 -1
16+
0 -1 -1
17+
1 2 1
18+
5 6 1
19+
5 10 5
20+
-1 1 2
21+
22+
;; doubles
23+
0.0 0.0 0.0
24+
1.0 1.0 0.0
25+
-1.0 -1.0 0.0
26+
0.0 1.0 1.0
27+
-1.0 0.0 1.0
28+
-2.0 -1.0 1.0
29+
2.0 1.0 -1.0
30+
1.0 0.0 -1.0
31+
0.0 -1.0 -1.0
32+
1.0 2.0 1.0
33+
5.0 6.0 1.0
34+
5.0 10.0 5.0
35+
-1.0 1.0 2.0
36+
37+
;; BigInts
38+
0N 0N 0N
39+
1N 1N 0N
40+
-1N -1N 0N
41+
0N 1N 1N
42+
-1N 0N 1N
43+
-2N -1N 1N
44+
2N 1N -1N
45+
1N 0N -1N
46+
0N -1N -1N
47+
1N 2N 1N
48+
5N 6N 1N
49+
5N 10N 5N
50+
-1N 1N 2N
51+
52+
;; BigDecimals
53+
0.0M 0.0M 0.0M
54+
1.0M 1.0M 0.0M
55+
-1.0M -1.0M 0.0M
56+
0.0M 1.0M 1.0M
57+
-1.0M 0.0M 1.0M
58+
-2.0M -1.0M 1.0M
59+
2.0M 1.0M -1.0M
60+
1.0M 0.0M -1.0M
61+
0.0M -1.0M -1.0M
62+
1.0M 2.0M 1.0M
63+
5.0M 6.0M 1.0M
64+
5.0M 10.0M 5.0M
65+
-1.0M 1.0M 2.0M)
66+
67+
;; Single arg
68+
(is (= -3 (- 3)))
69+
(is (= 3 (- -3)))
70+
71+
;; Multi-arg
72+
(is (= -45 (- 0 1 2 3 4 5 6 7 8 9)))
73+
74+
(is (thrown? Exception (- nil 1)))
75+
(is (thrown? Exception (- 1 nil)))
76+
(is (thrown? Exception (- nil 1N)))
77+
(is (thrown? Exception (- 1N nil)))
78+
(is (thrown? Exception (- nil 1.0)))
79+
(is (thrown? Exception (- 1.0 nil)))
80+
(is (thrown? Exception (- nil 1.0M)))
81+
(is (thrown? Exception (- 1.0M nil)))
82+
83+
#?@(:cljs nil
84+
:default
85+
[(is (thrown? Exception (- r/min-int 1)))
86+
(is (thrown? Exception (- r/max-int -1)))]))
87+
88+
89+
(deftest rationals
90+
(are [expected x y] (= expected (- x y))
91+
1/2 1 1/2
92+
1/3 1 2/3
93+
1/4 1 3/4
94+
1/5 1 4/5
95+
1/6 1 5/6
96+
1/7 1 6/7
97+
1/8 1 7/8
98+
1/9 1 8/9
99+
100+
1 1/2 -1/2
101+
1 1/3 -2/3
102+
1 1/4 -3/4
103+
1 1/5 -4/5
104+
1 1/6 -5/6
105+
1 1/7 -6/7
106+
1 1/8 -7/8
107+
1 1/9 -8/9
108+
109+
1 3/2 1/2
110+
1 5/3 2/3
111+
1 7/4 3/4
112+
1 9/5 4/5
113+
1 11/6 5/6
114+
1 13/7 6/7
115+
1 15/8 7/8
116+
1 17/9 8/9
117+
118+
3/2 2 1/2
119+
4/3 2 2/3
120+
121+
;; Be careful here because floating point rounding can bite us.
122+
;; This case is pretty safe.
123+
1.0 1.5 1/2)
124+
125+
;; Single arg
126+
(is (= -1/2 (- 1/2)))
127+
(is (= 1/2 (- -1/2)))
128+
129+
;; Multi arg
130+
(is (= -2089/2520 (- 1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9)))
131+
132+
(is (thrown? Exception (- nil 1/2)))
133+
(is (thrown? Exception (- 1/2 nil)))
134+
135+
#?@(:cljs nil
136+
:default
137+
[(is (- r/max-int -1/2)) ; test that these don't throw
138+
(is (- r/min-int 1/2))
139+
(is (= (- r/max-double) (- (- r/max-double) 1/2))) ; should silently round
140+
(is (= r/max-double (- r/max-double -1/2)))
141+
(is (= -0.5 (- r/min-double 1/2)))
142+
(is (= 0.5 (- r/min-double -1/2)))
143+
(is (instance? clojure.lang.Ratio (- 0 1/3)))
144+
(is (instance? clojure.lang.Ratio (- 0N 1/3)))
145+
(is (instance? clojure.lang.Ratio (- 1 1/3)))
146+
(is (instance? clojure.lang.Ratio (- 1N 1/3)))
147+
;; Note that we use `double?` here because JVM Clojure uses
148+
;; java.lang.Double instead of clojure.lang.Double and we'd
149+
;; like to keep this test as generic as possible.
150+
(is (double? (- 0.0 1/3)))
151+
(is (double? (- 1.0 1/3)))]))
152+
153+
(deftest inf-nan
154+
(are [expected x y] (= expected (- x y))
155+
##Inf 1 ##-Inf
156+
##Inf 1N ##-Inf
157+
##Inf 1.0 ##-Inf
158+
##Inf 1/2 ##-Inf
159+
##-Inf 1 ##Inf
160+
##-Inf 1N ##Inf
161+
##-Inf 1.0 ##Inf
162+
##-Inf 1/2 ##Inf
163+
164+
##-Inf ##-Inf 1
165+
##-Inf ##-Inf 1N
166+
##-Inf ##-Inf 1.0
167+
##-Inf ##-Inf 1/2
168+
##Inf ##Inf 1
169+
##Inf ##Inf 1N
170+
##Inf ##Inf 1.0
171+
##Inf ##Inf 1/2)
172+
173+
(are [x y] (and (NaN? (- x y))
174+
(NaN? (- y x)))
175+
##-Inf ##-Inf
176+
1 ##NaN
177+
1N ##NaN
178+
1.0 ##NaN
179+
1/2 ##NaN
180+
##Inf ##NaN
181+
##-Inf ##NaN
182+
##NaN ##NaN)
183+
184+
;; Single arg
185+
(is (= ##-Inf (- ##Inf)))
186+
(is (= ##Inf (- ##-Inf)))
187+
(is (NaN? (- ##NaN)))
188+
189+
(is (thrown? Exception (- ##NaN nil)))
190+
(is (thrown? Exception (- ##Inf nil))))

test/clojure/core_test/slash.cljc

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
(ns clojure.core-test.slash
2+
(:require [clojure.test :as t :refer [deftest testing is are]]))
3+
4+
(deftest common
5+
(are [expected x y] (= expected (/ x y))
6+
2 2 1
7+
1 2 2
8+
3 15 5
9+
5 15 3
10+
11+
2 2N 1N
12+
1 2N 2N
13+
3 15N 5N
14+
5 15N 3N
15+
16+
2 2 1N
17+
1 2 2N
18+
3 15 5N
19+
5 15 3N
20+
21+
2 2N 1
22+
1 2N 2
23+
3 15N 5
24+
5 15N 3
25+
26+
2.0 2.0 1.0
27+
1.0 2.0 2.0
28+
3.0 15.0 5.0
29+
5.0 15.0 3.0
30+
7.5 15.0 2.0
31+
32+
2.0 2 1.0
33+
1.0 2 2.0
34+
3.0 15 5.0
35+
5.0 15 3.0
36+
7.5 15 2.0
37+
38+
2.0 2.0 1
39+
1.0 2.0 2
40+
3.0 15.0 5
41+
5.0 15.0 3
42+
7.5 15.0 2
43+
44+
2.0 2.0M 1.0 ; Unexpected downcast to double
45+
1.0 2.0M 2.0
46+
3.0 15.0M 5.0
47+
5.0 15.0M 3.0
48+
7.5 15.0M 2.0
49+
50+
2.0 2.0 1.0M ; Unexpected downcast to double
51+
1.0 2.0 2.0M
52+
3.0 15.0 5.0M
53+
5.0 15.0 3.0M
54+
7.5 15.0 2.0M
55+
56+
2.0M 2.0M 1.0M
57+
1.0M 2.0M 2.0M
58+
3.0M 15.0M 5.0M
59+
5.0M 15.0M 3.0M
60+
7.5M 15.0M 2.0M
61+
62+
2.0M 2 1.0M
63+
1.0M 2 2.0M
64+
3.0M 15 5.0M
65+
5.0M 15 3.0M
66+
7.5M 15 2.0M
67+
68+
2.0M 2.0M 1
69+
1.0M 2.0M 2
70+
3.0M 15.0M 5
71+
5.0M 15.0M 3
72+
7.5M 15.0M 2
73+
74+
2.0 2N 1.0
75+
1.0 2N 2.0
76+
3.0 15N 5.0
77+
5.0 15N 3.0
78+
7.5 15N 2.0
79+
80+
2.0 2.0 1N
81+
1.0 2.0 2N
82+
3.0 15.0 5N
83+
5.0 15.0 3N
84+
7.5 15.0 2N
85+
86+
2.0M 2N 1.0M
87+
1.0M 2N 2.0M
88+
3.0M 15N 5.0M
89+
5.0M 15N 3.0M
90+
7.5M 15N 2.0M
91+
92+
2.0M 2.0M 1N
93+
1.0M 2.0M 2N
94+
3.0M 15.0M 5N
95+
5.0M 15.0M 3N
96+
7.5M 15.0M 2N
97+
98+
;; test signs
99+
-1 1 -1
100+
-1 -1 1
101+
1 -1 -1
102+
-1.0 1.0 -1.0
103+
-1.0 -1.0 1.0
104+
1.0 -1.0 -1.0
105+
-1N 1N -1N
106+
-1N -1N 1N
107+
1N -1N -1N
108+
-1.0M 1.0M -1.0M
109+
-1.0M -1.0M 1.0M
110+
1.0M -1.0M -1.0M)
111+
112+
;; Single arg
113+
(is (= 1/2 (/ 2)))
114+
(is (= 0.5 (/ 2.0)))
115+
116+
;; Multi arg
117+
(is (= 1/362880 (/ 1 2 3 4 5 6 7 8 9)))
118+
119+
(is (thrown? Exception (/ 1 0)))
120+
(is (thrown? Exception (/ nil 1)))
121+
(is (thrown? Exception (/ 1 nil))))
122+
123+
(deftest rationals
124+
(are [expected x y] (= expected (/ x y))
125+
10 10 1
126+
5 10 2
127+
10/3 10 3
128+
1 2 2
129+
4 2 1/2
130+
1/4 1/2 2
131+
4.0 2.0 1/2
132+
0.25 1/2 2.0
133+
4M 2.0M 1/2
134+
0.25M 1/2 2.0M)
135+
136+
;; Single arg
137+
(is (= 2N (/ 1/2)))
138+
(is (= 3N (/ 1/3)))
139+
140+
;; Multi arg
141+
(is (= 362880N (/ 1/1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9)))
142+
143+
(is (thrown? Exception (/ 1/2 nil)))
144+
(is (thrown? Exception (/ nil 1/2))))
145+
146+
(deftest inf-nan
147+
(are [expected x y] (= expected (/ x y))
148+
##Inf ##Inf 1
149+
##-Inf ##-Inf 1
150+
0.0 1 ##Inf ; Note conversion to double
151+
0.0 1 ##-Inf
152+
0.0 -1 ##Inf
153+
0.0 -1 ##-Inf
154+
##Inf ##Inf 0 ; Surprisingly, these down't throw
155+
##-Inf ##-Inf 0
156+
157+
##Inf ##Inf 1N
158+
##-Inf ##-Inf 1N
159+
0.0 1N ##Inf ; Note conversion to double
160+
0.0 1N ##-Inf
161+
0.0 -1N ##Inf
162+
0.0 -1N ##-Inf
163+
##Inf ##Inf 0N ; Surprisingly, these down't throw
164+
##-Inf ##-Inf 0N
165+
166+
##Inf ##Inf 1.0
167+
##-Inf ##-Inf 1.0
168+
0.0 1.0 ##Inf
169+
0.0 1.0 ##-Inf
170+
0.0 -1.0 ##Inf
171+
0.0 -1.0 ##-Inf
172+
##Inf ##Inf 0.0 ; Surprisingly, these down't throw
173+
##-Inf ##-Inf 0.0
174+
175+
##Inf ##Inf 1.0M
176+
##-Inf ##-Inf 1.0M
177+
0.0 1.0M ##Inf ; Note conversion back double
178+
0.0 1.0M ##-Inf
179+
0.0 -1.0M ##Inf
180+
0.0 -1.0M ##-Inf
181+
##Inf ##Inf 0.0M ; Surprisingly, these down't throw
182+
##-Inf ##-Inf 0.0M
183+
184+
0.0 1/2 ##Inf
185+
0.0 -1/2 ##Inf
186+
0.0 1/2 ##-Inf
187+
0.0 -1/2 ##-Inf)
188+
189+
;; These all result in ##NaN, but we can't test for that with `=`.
190+
(are [x y] (NaN? (/ x y))
191+
##NaN 0 ; Note that this doesn't throw
192+
0 ##NaN
193+
##NaN 0N
194+
0N ##NaN
195+
##NaN 1.0
196+
1.0 ##NaN
197+
##NaN 1.0M
198+
1.0M ##NaN
199+
##NaN 1/2
200+
1/2 ##NaN
201+
##Inf ##Inf
202+
##Inf ##-Inf
203+
##-Inf ##Inf
204+
##-Inf ##-Inf))

0 commit comments

Comments
 (0)