Skip to content
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
85 changes: 85 additions & 0 deletions test/clojure/core_test/mod.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
(ns clojure.core-test.mod
(:require [clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability :as p]))

(deftest test-mod
(are [type-pred expected x y] (let [r (mod x y)]
(and (type-pred r)
(= expected r)))
int? 1 10 3
int? 2 -10 3
int? -1 -10 -3
int? -2 10 -3

p/big-int? 1N 10 3N
p/big-int? 2N -10 3N
p/big-int? -1N -10 -3N
p/big-int? -2N 10 -3N

p/big-int? 1N 10N 3
p/big-int? 2N -10N 3
p/big-int? -1N -10N -3
p/big-int? -2N 10N -3

p/big-int? 1N 10N 3N
p/big-int? 2N -10N 3N
p/big-int? -1N -10N -3N
p/big-int? -2N 10N -3N

double? 1.0 10 3.0
double? 2.0 -10 3.0
double? -1.0 -10 -3.0
double? -2.0 10 -3.0
double? 1.0 10.0 3
double? 2.0 -10.0 3
double? -1.0 -10.0 -3
double? -2.0 10.0 -3
double? 1.0 10.0 3.0
double? 2.0 -10.0 3.0
double? -1.0 -10.0 -3.0
double? -2.0 10.0 -3.0

decimal? 1.0M 10 3.0M
decimal? 2.0M -10 3.0M
decimal? -1.0M -10 -3.0M
decimal? -2.0M 10 -3.0M
decimal? 1.0M 10.0M 3
decimal? 2.0M -10.0M 3
decimal? -1.0M -10.0M -3
decimal? -2.0M 10.0M -3
decimal? 1.0M 10.0M 3.0M
decimal? 2.0M -10.0M 3.0M
decimal? -1.0M -10.0M -3.0M
decimal? -2.0M 10.0M -3.0M

;; Unexpectedly downconverts result to double, rather than BigDecimal
double? 1.0 10.0M 3.0
double? 2.0 -10.0M 3.0
double? -1.0 -10.0M -3.0
double? -2.0 10.0M -3.0
double? 1.0 10.0 3.0M
double? 2.0 -10.0 3.0M
double? -1.0 -10.0 -3.0M
double? -2.0 10.0 -3.0M

p/big-int? 0N 3 1/2
ratio? 1/3 3 4/3
ratio? 7/2 37/2 15
p/big-int? 0N 3 -1/2
p/big-int? -1N 3 -4/3
ratio? -23/2 37/2 -15
p/big-int? 0N -3 1/2
p/big-int? 1N -3 4/3
ratio? 23/2 -37/2 15
p/big-int? 0N -3 -1/2
ratio? -1/3 -3 -4/3
ratio? -7/2 -37/2 -15)

(is (thrown? Exception (mod 10 0)))
(is (thrown? Exception (mod ##Inf 1))) ; surprising since (/ ##Inf 1) = ##Inf
(is (NaN? (mod 1 ##Inf)))
(is (thrown? Exception (mod ##-Inf 1))) ; surprising since (/ ##-Inf 1) = ##-Inf
(is (NaN? (mod 1 ##-Inf)))
(is (thrown? Exception (mod ##NaN 1)))
(is (thrown? Exception (mod 1 ##NaN)))
(is (thrown? Exception (mod ##NaN 1))))
5 changes: 5 additions & 0 deletions test/clojure/core_test/portability.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns clojure.core-test.portability)

(defn big-int? [n]
(and (integer? n)
(not (int? n))))
Comment on lines +3 to +5
Copy link
Member

Choose a reason for hiding this comment

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

We probably want a reader conditional here, right? If CLJ, use instance?, otherwise, use the generic check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, but that makes it very specific to JVM. The doc strings say that int? returns true for a fixed-width integer, whereas integer? returns true for all integers, whether fixed with or big. So, I figured this would be slightly more portable than a direct class check using instance?.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, I hear you. I was just wondering about the odds that there's an integer among Clojure dialects which isn't int? and which isn't a big int. 🤔 Maybe not worth considering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that and decided we could cross that bridge later. It's easy enough to rewrite at that time.

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

(deftest test-quot
(are [type-pred expected x y] (let [r (quot x y)]
(and (type-pred r)
(= expected r)))
int? 3 10 3
int? -3 -10 3
int? 3 -10 -3
int? -3 10 -3

p/big-int? 3N 10 3N
p/big-int? -3N -10 3N
p/big-int? 3N -10 -3N
p/big-int? -3N 10 -3N
p/big-int? 3N 10N 3
p/big-int? -3N -10N 3
p/big-int? 3N -10N -3
p/big-int? -3N 10N -3
p/big-int? 3N 10N 3N
p/big-int? -3N -10N 3N
p/big-int? 3N -10N -3N
p/big-int? -3N 10N -3N

double? 3.0 10 3.0
double? -3.0 -10 3.0
double? 3.0 -10 -3.0
double? -3.0 10 -3.0
double? 3.0 10.0 3
double? -3.0 -10.0 3
double? 3.0 -10.0 -3
double? -3.0 10.0 -3
double? 3.0 10.0 3.0
double? -3.0 -10.0 3.0
double? 3.0 -10.0 -3.0
double? -3.0 10.0 -3.0

decimal? 3.0M 10 3.0M
decimal? -3.0M -10 3.0M
decimal? 3.0M -10 -3.0M
decimal? -3.0M 10 -3.0M
decimal? 3.0M 10.0M 3
decimal? -3.0M -10.0M 3
decimal? 3.0M -10.0M -3
decimal? -3.0M 10.0M -3
decimal? 3.0M 10.0M 3.0M
decimal? -3.0M -10.0M 3.0M
decimal? 3.0M -10.0M -3.0M
decimal? -3.0M 10.0M -3.0M

;; Unexpectedly downconverts result to double, rather than BigDecimal
double? 3.0 10.0M 3.0
double? -3.0 -10.0M 3.0
double? 3.0 -10.0M -3.0
double? -3.0 10.0M -3.0
double? 3.0 10.0 3.0M
double? -3.0 -10.0 3.0M
double? 3.0 -10.0 -3.0M
double? -3.0 10.0 -3.0M

;; Anything with a ratio seems to convert to BigInt
p/big-int? 6N 3 1/2
p/big-int? 2N 3 4/3
p/big-int? 1N 37/2 15
p/big-int? -6N 3 -1/2
p/big-int? -2N 3 -4/3
p/big-int? -1N 37/2 -15
p/big-int? -6N -3 1/2
p/big-int? -2N -3 4/3
p/big-int? -1N -37/2 15
p/big-int? 6N -3 -1/2
p/big-int? 2N -3 -4/3
p/big-int? 1N -37/2 -15

double? 0.0 1 ##Inf
double? 0.0 1 ##-Inf)

(is (thrown? Exception (quot 10 0)))
(is (thrown? Exception (quot ##Inf 1))) ; surprising since (/ ##Inf 1) = ##Inf
(is (thrown? Exception (quot ##-Inf 1))) ; surprising since (/ ##-Inf 1) = ##-Inf
(is (thrown? Exception (quot ##NaN 1)))
(is (thrown? Exception (quot 1 ##NaN)))
(is (thrown? Exception (quot ##NaN 1))))

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

(deftest test-rem
(are [type-pred expected x y] (let [r (rem x y)]
(and (type-pred r)
(= expected r)))
int? 1 10 3
int? -1 -10 3
int? -1 -10 -3
int? 1 10 -3

p/big-int? 1N 10 3N
p/big-int? -1N -10 3N
p/big-int? -1N -10 -3N
p/big-int? 1N 10 -3N
p/big-int? 1N 10N 3
p/big-int? -1N -10N 3
p/big-int? -1N -10N -3
p/big-int? 1N 10N -3
p/big-int? 1N 10N 3N
p/big-int? -1N -10N 3N
p/big-int? -1N -10N -3N
p/big-int? 1N 10N -3N

double? 1.0 10 3.0
double? -1.0 -10 3.0
double? -1.0 -10 -3.0
double? 1.0 10 -3.0
double? 1.0 10.0 3
double? -1.0 -10.0 3
double? -1.0 -10.0 -3
double? 1.0 10.0 -3
double? 1.0 10.0 3.0
double? -1.0 -10.0 3.0
double? -1.0 -10.0 -3.0
double? 1.0 10.0 -3.0

decimal? 1.0M 10 3.0M
decimal? -1.0M -10 3.0M
decimal? -1.0M -10 -3.0M
decimal? 1.0M 10 -3.0M
decimal? 1.0M 10.0M 3
decimal? -1.0M -10.0M 3
decimal? -1.0M -10.0M -3
decimal? 1.0M 10.0M -3
decimal? 1.0M 10.0M 3.0M
decimal? -1.0M -10.0M 3.0M
decimal? -1.0M -10.0M -3.0M
decimal? 1.0M 10.0M -3.0M

;; Unexpectedly downconverts result to double, rather than BigDecimal
double? 1.0 10.0M 3.0
double? -1.0 -10.0M 3.0
double? -1.0 -10.0M -3.0
double? 1.0 10.0M -3.0
double? 1.0 10.0 3.0M
double? -1.0 -10.0 3.0M
double? -1.0 -10.0 -3.0M
double? 1.0 10.0 -3.0M

p/big-int? 0N 3 1/2
ratio? 1/3 3 4/3
ratio? 7/2 37/2 15
p/big-int? 0N 3 -1/2
ratio? 1/3 3 -4/3
ratio? 7/2 37/2 -15
p/big-int? 0N -3 1/2
ratio? -1/3 -3 4/3
ratio? -7/2 -37/2 15
p/big-int? 0N -3 -1/2
ratio? -1/3 -3 -4/3
ratio? -7/2 -37/2 -15)

(is (thrown? Exception (rem 10 0)))
(is (thrown? Exception (rem ##Inf 1))) ; surprising since (/ ##Inf 1) = ##Inf
(is (NaN? (rem 1 ##Inf)))
(is (thrown? Exception (rem ##-Inf 1))) ; surprising since (/ ##-Inf 1) = ##-Inf
(is (NaN? (rem 1 ##-Inf)))
(is (thrown? Exception (rem ##NaN 1)))
(is (thrown? Exception (rem 1 ##NaN)))
(is (thrown? Exception (rem ##NaN 1))))
Loading