-
Notifications
You must be signed in to change notification settings - Fork 25
Add tests for quot, rem, and mod
#34
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)))) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)))) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)))) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?returnstruefor a fixed-width integer, whereasinteger?returns true for all integers, whether fixed with or big. So, I figured this would be slightly more portable than a direct class check usinginstance?.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.