Skip to content

Add tests for - and / #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions test/clojure/core_test/minus.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
(ns clojure.core-test.minus
(:require [clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.number-range :as r]))

(deftest common
(are [expected x y] (= expected (- x y))
;; longs
0 0 0
1 1 0
-1 -1 0
0 1 1
-1 0 1
-2 -1 1
2 1 -1
1 0 -1
0 -1 -1
1 2 1
5 6 1
5 10 5
-1 1 2

;; doubles
0.0 0.0 0.0
1.0 1.0 0.0
-1.0 -1.0 0.0
0.0 1.0 1.0
-1.0 0.0 1.0
-2.0 -1.0 1.0
2.0 1.0 -1.0
1.0 0.0 -1.0
0.0 -1.0 -1.0
1.0 2.0 1.0
5.0 6.0 1.0
5.0 10.0 5.0
-1.0 1.0 2.0

;; BigInts
0N 0N 0N
1N 1N 0N
-1N -1N 0N
0N 1N 1N
-1N 0N 1N
-2N -1N 1N
2N 1N -1N
1N 0N -1N
0N -1N -1N
1N 2N 1N
5N 6N 1N
5N 10N 5N
-1N 1N 2N

;; BigDecimals
0.0M 0.0M 0.0M
1.0M 1.0M 0.0M
-1.0M -1.0M 0.0M
0.0M 1.0M 1.0M
-1.0M 0.0M 1.0M
-2.0M -1.0M 1.0M
2.0M 1.0M -1.0M
1.0M 0.0M -1.0M
0.0M -1.0M -1.0M
1.0M 2.0M 1.0M
5.0M 6.0M 1.0M
5.0M 10.0M 5.0M
-1.0M 1.0M 2.0M)

;; Single arg
(is (= -3 (- 3)))
(is (= 3 (- -3)))

;; Multi-arg
(is (= -45 (- 0 1 2 3 4 5 6 7 8 9)))

(is (thrown? Exception (- nil 1)))
(is (thrown? Exception (- 1 nil)))
(is (thrown? Exception (- nil 1N)))
(is (thrown? Exception (- 1N nil)))
(is (thrown? Exception (- nil 1.0)))
(is (thrown? Exception (- 1.0 nil)))
(is (thrown? Exception (- nil 1.0M)))
(is (thrown? Exception (- 1.0M nil)))

#?@(:cljs nil
:default
[(is (thrown? Exception (- r/min-int 1)))
(is (thrown? Exception (- r/max-int -1)))]))


(deftest rationals
(are [expected x y] (= expected (- x y))
1/2 1 1/2
1/3 1 2/3
1/4 1 3/4
1/5 1 4/5
1/6 1 5/6
1/7 1 6/7
1/8 1 7/8
1/9 1 8/9

1 1/2 -1/2
1 1/3 -2/3
1 1/4 -3/4
1 1/5 -4/5
1 1/6 -5/6
1 1/7 -6/7
1 1/8 -7/8
1 1/9 -8/9

1 3/2 1/2
1 5/3 2/3
1 7/4 3/4
1 9/5 4/5
1 11/6 5/6
1 13/7 6/7
1 15/8 7/8
1 17/9 8/9

3/2 2 1/2
4/3 2 2/3

;; Be careful here because floating point rounding can bite us.
;; This case is pretty safe.
1.0 1.5 1/2)

;; Single arg
(is (= -1/2 (- 1/2)))
(is (= 1/2 (- -1/2)))

;; Multi arg
(is (= -2089/2520 (- 1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9)))

(is (thrown? Exception (- nil 1/2)))
(is (thrown? Exception (- 1/2 nil)))

#?@(:cljs nil
:default
[(is (- r/max-int -1/2)) ; test that these don't throw
(is (- r/min-int 1/2))
(is (= (- r/max-double) (- (- r/max-double) 1/2))) ; should silently round
(is (= r/max-double (- r/max-double -1/2)))
(is (= -0.5 (- r/min-double 1/2)))
(is (= 0.5 (- r/min-double -1/2)))
(is (instance? clojure.lang.Ratio (- 0 1/3)))
(is (instance? clojure.lang.Ratio (- 0N 1/3)))
(is (instance? clojure.lang.Ratio (- 1 1/3)))
(is (instance? clojure.lang.Ratio (- 1N 1/3)))
;; Note that we use `double?` here because JVM Clojure uses
;; java.lang.Double instead of clojure.lang.Double and we'd
;; like to keep this test as generic as possible.
(is (double? (- 0.0 1/3)))
(is (double? (- 1.0 1/3)))]))

(deftest inf-nan
(are [expected x y] (= expected (- x y))
##Inf 1 ##-Inf
##Inf 1N ##-Inf
##Inf 1.0 ##-Inf
##Inf 1/2 ##-Inf
##-Inf 1 ##Inf
##-Inf 1N ##Inf
##-Inf 1.0 ##Inf
##-Inf 1/2 ##Inf

##-Inf ##-Inf 1
##-Inf ##-Inf 1N
##-Inf ##-Inf 1.0
##-Inf ##-Inf 1/2
##Inf ##Inf 1
##Inf ##Inf 1N
##Inf ##Inf 1.0
##Inf ##Inf 1/2)

(are [x y] (and (NaN? (- x y))
(NaN? (- y x)))
##-Inf ##-Inf
1 ##NaN
1N ##NaN
1.0 ##NaN
1/2 ##NaN
##Inf ##NaN
##-Inf ##NaN
##NaN ##NaN)

;; Single arg
(is (= ##-Inf (- ##Inf)))
(is (= ##Inf (- ##-Inf)))
(is (NaN? (- ##NaN)))

(is (thrown? Exception (- ##NaN nil)))
(is (thrown? Exception (- ##Inf nil))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both of these, it'd be good to have a test case for the variadic arity. i.e. (apply - (range 20))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll add.

204 changes: 204 additions & 0 deletions test/clojure/core_test/slash.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
(ns clojure.core-test.slash
(:require [clojure.test :as t :refer [deftest testing is are]]))

(deftest common
(are [expected x y] (= expected (/ x y))
2 2 1
1 2 2
3 15 5
5 15 3

2 2N 1N
1 2N 2N
3 15N 5N
5 15N 3N

2 2 1N
1 2 2N
3 15 5N
5 15 3N

2 2N 1
1 2N 2
3 15N 5
5 15N 3

2.0 2.0 1.0
1.0 2.0 2.0
3.0 15.0 5.0
5.0 15.0 3.0
7.5 15.0 2.0

2.0 2 1.0
1.0 2 2.0
3.0 15 5.0
5.0 15 3.0
7.5 15 2.0

2.0 2.0 1
1.0 2.0 2
3.0 15.0 5
5.0 15.0 3
7.5 15.0 2

2.0 2.0M 1.0 ; Unexpected downcast to double
1.0 2.0M 2.0
3.0 15.0M 5.0
5.0 15.0M 3.0
7.5 15.0M 2.0

2.0 2.0 1.0M ; Unexpected downcast to double
1.0 2.0 2.0M
3.0 15.0 5.0M
5.0 15.0 3.0M
7.5 15.0 2.0M

2.0M 2.0M 1.0M
1.0M 2.0M 2.0M
3.0M 15.0M 5.0M
5.0M 15.0M 3.0M
7.5M 15.0M 2.0M

2.0M 2 1.0M
1.0M 2 2.0M
3.0M 15 5.0M
5.0M 15 3.0M
7.5M 15 2.0M

2.0M 2.0M 1
1.0M 2.0M 2
3.0M 15.0M 5
5.0M 15.0M 3
7.5M 15.0M 2

2.0 2N 1.0
1.0 2N 2.0
3.0 15N 5.0
5.0 15N 3.0
7.5 15N 2.0

2.0 2.0 1N
1.0 2.0 2N
3.0 15.0 5N
5.0 15.0 3N
7.5 15.0 2N

2.0M 2N 1.0M
1.0M 2N 2.0M
3.0M 15N 5.0M
5.0M 15N 3.0M
7.5M 15N 2.0M

2.0M 2.0M 1N
1.0M 2.0M 2N
3.0M 15.0M 5N
5.0M 15.0M 3N
7.5M 15.0M 2N

;; test signs
-1 1 -1
-1 -1 1
1 -1 -1
-1.0 1.0 -1.0
-1.0 -1.0 1.0
1.0 -1.0 -1.0
-1N 1N -1N
-1N -1N 1N
1N -1N -1N
-1.0M 1.0M -1.0M
-1.0M -1.0M 1.0M
1.0M -1.0M -1.0M)

;; Single arg
(is (= 1/2 (/ 2)))
(is (= 0.5 (/ 2.0)))

;; Multi arg
(is (= 1/362880 (/ 1 2 3 4 5 6 7 8 9)))

(is (thrown? Exception (/ 1 0)))
(is (thrown? Exception (/ nil 1)))
(is (thrown? Exception (/ 1 nil))))

(deftest rationals
(are [expected x y] (= expected (/ x y))
10 10 1
5 10 2
10/3 10 3
1 2 2
4 2 1/2
1/4 1/2 2
4.0 2.0 1/2
0.25 1/2 2.0
4M 2.0M 1/2
0.25M 1/2 2.0M)

;; Single arg
(is (= 2N (/ 1/2)))
(is (= 3N (/ 1/3)))

;; Multi arg
(is (= 362880N (/ 1/1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9)))

(is (thrown? Exception (/ 1/2 nil)))
(is (thrown? Exception (/ nil 1/2))))

(deftest inf-nan
(are [expected x y] (= expected (/ x y))
##Inf ##Inf 1
##-Inf ##-Inf 1
0.0 1 ##Inf ; Note conversion to double
0.0 1 ##-Inf
0.0 -1 ##Inf
0.0 -1 ##-Inf
##Inf ##Inf 0 ; Surprisingly, these down't throw
##-Inf ##-Inf 0

##Inf ##Inf 1N
##-Inf ##-Inf 1N
0.0 1N ##Inf ; Note conversion to double
0.0 1N ##-Inf
0.0 -1N ##Inf
0.0 -1N ##-Inf
##Inf ##Inf 0N ; Surprisingly, these down't throw
##-Inf ##-Inf 0N

##Inf ##Inf 1.0
##-Inf ##-Inf 1.0
0.0 1.0 ##Inf
0.0 1.0 ##-Inf
0.0 -1.0 ##Inf
0.0 -1.0 ##-Inf
##Inf ##Inf 0.0 ; Surprisingly, these down't throw
##-Inf ##-Inf 0.0

##Inf ##Inf 1.0M
##-Inf ##-Inf 1.0M
0.0 1.0M ##Inf ; Note conversion back double
0.0 1.0M ##-Inf
0.0 -1.0M ##Inf
0.0 -1.0M ##-Inf
##Inf ##Inf 0.0M ; Surprisingly, these down't throw
##-Inf ##-Inf 0.0M

0.0 1/2 ##Inf
0.0 -1/2 ##Inf
0.0 1/2 ##-Inf
0.0 -1/2 ##-Inf)

;; These all result in ##NaN, but we can't test for that with `=`.
(are [x y] (NaN? (/ x y))
##NaN 0 ; Note that this doesn't throw
0 ##NaN
##NaN 0N
0N ##NaN
##NaN 1.0
1.0 ##NaN
##NaN 1.0M
1.0M ##NaN
##NaN 1/2
1/2 ##NaN
##Inf ##Inf
##Inf ##-Inf
##-Inf ##Inf
##-Inf ##-Inf))
Loading