diff --git a/test/clojure/core_test/abs.cljc b/test/clojure/core_test/abs.cljc index 57dbb5c3..319729e4 100644 --- a/test/clojure/core_test/abs.cljc +++ b/test/clojure/core_test/abs.cljc @@ -1,5 +1,6 @@ (ns clojure.core-test.abs (:require [clojure.test :as t :refer [deftest testing is are]] + [clojure.core-test.number-range :as r] [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) (when-var-exists clojure.core/abs @@ -8,7 +9,8 @@ (are [in ex] (= ex (abs in)) -1 1 1 1 - Long/MIN_VALUE Long/MIN_VALUE ; Special case! + (inc r/min-int) (- (inc r/min-int)) + #?@(:clj [r/min-int r/min-int]) ; fixed int 2's complement oddity -1.0 1.0 -0.0 0.0 ##-Inf ##Inf @@ -19,7 +21,8 @@ :default [-1/5 1/5])) (is (NaN? (abs ##NaN))) - (is (thrown? #?(:cljs :default :clj Exception) (abs nil)))) + #?(:cljs (is (zero? (abs nil))) + :default(is (thrown? Exception (abs nil))))) (testing "unboxed" (let [a 42 diff --git a/test/clojure/core_test/binding.cljc b/test/clojure/core_test/binding.cljc index 11b5fc04..873e743e 100644 --- a/test/clojure/core_test/binding.cljc +++ b/test/clojure/core_test/binding.cljc @@ -10,56 +10,60 @@ (defn test-fn [] *x*) (t/deftest test-binding - ;; base-case with no overrides - (t/is (= *x* :unset) "Unset is :unset") - (t/is (= (*f* 1) 2) "fn call") + ;; base-case with no overrides + (t/is (= *x* :unset) "Unset is :unset") + (t/is (= (*f* 1) 2) "fn call") - ;; common cases - (t/is (binding [*x* :set] (= *x* :set)) "Can bind dynamic var.") - (t/is (binding [*x* :set] (= (test-fn) :set)) "Binding for indirect reference.") - (t/is (binding [*x* nil] (= (test-fn) nil)) "Dynamic vars are nullable.") - (t/is (binding [*f* dec] (= (*f* 1) 0)) "Can bind functions.") + ;; common cases + (t/is (binding [*x* :set] (= *x* :set)) "Can bind dynamic var.") + (t/is (binding [*x* :set] (= (test-fn) :set)) "Binding for indirect reference.") + (t/is (binding [*x* nil] (= (test-fn) nil)) "Dynamic vars are nullable.") + (t/is (binding [*f* dec] (= (*f* 1) 0)) "Can bind functions.") - ;; infinite seqs - (binding [*x* (range)] - (t/is (= '(0 1 2 3) (take 4 (test-fn))) "Infinite range") - (t/is (= '(0 1 2 3) (take 4 (test-fn))) "Immutability")) + ;; infinite seqs + (binding [*x* (range)] + (t/is (= '(0 1 2 3) (take 4 (test-fn))) "Infinite range") + (t/is (= '(0 1 2 3) (take 4 (test-fn))) "Immutability")) - ;; Nested cases - (binding [*x* :first!] - (let [layer-1 (fn [] (test-fn))] - (binding [*x* :second!] - (t/is (= :second! (layer-1) (test-fn)) "Value is determined at call-site")))) - (binding [*y* *x*] - (t/is (= *y* :unset) "Dynamic reference is by value at binding.") - (binding [*x* :layer-2] - (t/is (= *y* :unset) "Dynamic reference does not update.")) - (binding [*y* *x* - *x* :set-later] - (t/is (= *y* :unset) "Bind vars are applied in sequence."))) - (let [f (fn [] (binding [*x* :inside-f] (test-fn)))] - (binding [*x* :outside-f] - (t/is (= (test-fn) :outside-f)) - (t/is (= (f) :inside-f) "Nested in func-call"))) - (binding [*y* (binding [*x* :bad] (test-fn))] - (t/is (= *y* :bad) "Binding in a binding vector")) + ;; Nested cases + (binding [*x* :first!] + (let [layer-1 (fn [] (test-fn))] + (binding [*x* :second!] + (t/is (= :second! (layer-1) (test-fn)) "Value is determined at call-site")))) + (binding [*y* *x*] + (t/is (= *y* :unset) "Dynamic reference is by value at binding.") + (binding [*x* :layer-2] + (t/is (= *y* :unset) "Dynamic reference does not update.")) + (binding [*y* *x* + *x* :set-later] + (t/is (= *y* :unset) "Bind vars are applied in sequence."))) + (let [f (fn [] (binding [*x* :inside-f] (test-fn)))] + (binding [*x* :outside-f] + (t/is (= (test-fn) :outside-f)) + (t/is (= (f) :inside-f) "Nested in func-call"))) + (binding [*y* (binding [*x* :bad] (test-fn))] + (t/is (= *y* :bad) "Binding in a binding vector")) - ;; Threading/future/delay cases - (let [f (delay (test-fn))] - (binding [*x* :here] - (t/is (= @f :here) "Delayed functions inherit there bindings when forced")) - (t/is (= @f :here) "And value persists outside binding expression")) - (let [f (future (test-fn))] - (binding [*x* :now-here] - (t/is (= @f :unset) "Thread context is separate from joining thread"))) - (binding [*x* :outer] - (let [f (future (test-fn))] - (binding [*x* :inner] - (t/is (= @f :outer) "Thread context preserves binding context.")))) - (binding [*x* :caller] - (let [f (future - (binding [*x* :callee] - (future (test-fn))))] - (binding [*x* :derefer] - (let [derefed-f @f] - (t/is (= :callee @derefed-f) "Binding in futures preserved."))))))) + ;; Threading/future/delay cases + (let [f (delay (test-fn))] + (binding [*x* :here] + (t/is (= @f :here) "Delayed functions inherit there bindings when forced")) + (t/is (= @f :here) "And value persists outside binding expression")) + + ;; CLJS doesn't have futures + #?@(:cljs [] + :default + [(let [f (future (test-fn))] + (binding [*x* :now-here] + (t/is (= @f :unset) "Thread context is separate from joining thread"))) + (binding [*x* :outer] + (let [f (future (test-fn))] + (binding [*x* :inner] + (t/is (= @f :outer) "Thread context preserves binding context.")))) + (binding [*x* :caller] + (let [f (future + (binding [*x* :callee] + (future (test-fn))))] + (binding [*x* :derefer] + (let [derefed-f @f] + (t/is (= :callee @derefed-f) "Binding in futures preserved.")))))]))) diff --git a/test/clojure/core_test/bit_and.cljc b/test/clojure/core_test/bit_and.cljc index 2b3e052e..ad9115f7 100644 --- a/test/clojure/core_test/bit_and.cljc +++ b/test/clojure/core_test/bit_and.cljc @@ -15,7 +15,7 @@ 8 8 0xff 0 r/all-ones-int 0 0 0 r/all-ones-int - r/all-ones-int r/all-ones-int r/all-ones-int + #?(:cljs -1 :default r/all-ones-int) r/all-ones-int r/all-ones-int 0 r/full-width-checker-pos 0 r/full-width-checker-pos r/full-width-checker-pos r/full-width-checker-pos r/full-width-checker-pos r/full-width-checker-pos r/all-ones-int diff --git a/test/clojure/core_test/bit_and_not.cljc b/test/clojure/core_test/bit_and_not.cljc index 86d0f28c..a828067f 100644 --- a/test/clojure/core_test/bit_and_not.cljc +++ b/test/clojure/core_test/bit_and_not.cljc @@ -15,7 +15,7 @@ 8 12 4 0xff 0xff 0 0x80 0xff 0x7f - r/all-ones-int r/all-ones-int 0 + #?(:cljs -1 :default r/all-ones-int) r/all-ones-int 0 0 0 r/all-ones-int 0 r/all-ones-int r/all-ones-int r/full-width-checker-pos r/full-width-checker-pos 0 diff --git a/test/clojure/core_test/bit_or.cljc b/test/clojure/core_test/bit_or.cljc index 5dd1af33..76d7ffa9 100644 --- a/test/clojure/core_test/bit_or.cljc +++ b/test/clojure/core_test/bit_or.cljc @@ -13,11 +13,11 @@ (are [ex a b] (= ex (bit-or a b)) 2r1101 2r1100 2r1001 1 1 0 - r/all-ones-int r/all-ones-int 0 - r/all-ones-int 0 r/all-ones-int - r/all-ones-int r/all-ones-int r/all-ones-int + #?(:cljs -1 :default r/all-ones-int) r/all-ones-int 0 + #?(:cljs -1 :default r/all-ones-int) 0 r/all-ones-int + #?(:cljs -1 :default r/all-ones-int) r/all-ones-int r/all-ones-int r/full-width-checker-pos r/full-width-checker-pos 0 r/full-width-checker-pos r/full-width-checker-pos r/full-width-checker-pos - r/all-ones-int r/full-width-checker-pos r/all-ones-int - r/all-ones-int r/full-width-checker-pos r/full-width-checker-neg))) + #?(:cljs -1 :default r/all-ones-int) r/full-width-checker-pos r/all-ones-int + #?(:cljs -1 :default r/all-ones-int) r/full-width-checker-pos r/full-width-checker-neg))) diff --git a/test/clojure/core_test/bit_set.cljc b/test/clojure/core_test/bit_set.cljc index 2357c571..9e23e69a 100644 --- a/test/clojure/core_test/bit_set.cljc +++ b/test/clojure/core_test/bit_set.cljc @@ -11,8 +11,8 @@ (are [ex a b] (= ex (bit-set a b)) 2r1111 2r1011 2 - -9223372036854775808 0 63 - 4294967296 0 32 + #?@(:cljs [] :default [-9223372036854775808 0 63]) + #?@(:cljs [] :default [4294967296 0 32]) 65536 0 16 256 0 8 16 0 4))) diff --git a/test/clojure/core_test/bit_test.cljc b/test/clojure/core_test/bit_test.cljc index 7bbe0c9c..da39edd4 100644 --- a/test/clojure/core_test/bit_test.cljc +++ b/test/clojure/core_test/bit_test.cljc @@ -5,9 +5,9 @@ (when-var-exists clojure.core/bit-test (deftest test-bit-test #?(:clj (is (thrown? Exception (bit-test nil 1))) - :cljs (is (bit-test nil 1))) + :cljs (is (= false (bit-test nil 1)))) #?(:clj (is (thrown? Exception (bit-test 1 nil))) - :cljs (is (bit-test 1 nil))) + :cljs (is (= true (bit-test 1 nil)))) (are [ex a b] (= ex (bit-test a b)) true 2r1001 0 diff --git a/test/clojure/core_test/bit_xor.cljc b/test/clojure/core_test/bit_xor.cljc index 3e67b2b9..70b4b53b 100644 --- a/test/clojure/core_test/bit_xor.cljc +++ b/test/clojure/core_test/bit_xor.cljc @@ -12,10 +12,10 @@ (are [ex a b] (= ex (bit-xor a b)) 2r0101 2r1100 2r1001 - r/all-ones-int r/all-ones-int 0 - r/all-ones-int 0 r/all-ones-int + #?(:cljs -1 :default r/all-ones-int) r/all-ones-int 0 + #?(:cljs -1 :default r/all-ones-int) 0 r/all-ones-int 0 r/all-ones-int r/all-ones-int r/full-width-checker-pos r/full-width-checker-pos 0 0 r/full-width-checker-pos r/full-width-checker-pos - r/full-width-checker-neg r/full-width-checker-pos r/all-ones-int - r/all-ones-int r/full-width-checker-pos r/full-width-checker-neg))) + #?(:cljs -1431655766 :default r/full-width-checker-neg) r/full-width-checker-pos r/all-ones-int + #?(:cljs -1 :default r/all-ones-int) r/full-width-checker-pos r/full-width-checker-neg))) diff --git a/test/clojure/core_test/byte.cljc b/test/clojure/core_test/byte.cljc index 0b16c888..bd8b4581 100644 --- a/test/clojure/core_test/byte.cljc +++ b/test/clojure/core_test/byte.cljc @@ -3,47 +3,65 @@ [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) (when-var-exists clojure.core/byte - (deftest test-byte - ;; There is no platform independent predicate to test for a - ;; byte (e.g., `byte?`). In ClojureJVM, it's an instance of - ;; `java.lang.Byte`, but there is no predicate for it. Here, we just - ;; test whether it's a fixed-length integer of some sort. - (is (int? (byte 0))) - #?@(:cljs [] - :default - [(is (instance? java.lang.Byte (byte 0)))]) + (deftest test-byte + ;; There is no platform independent predicate to test for a + ;; byte (e.g., `byte?`). In ClojureJVM, it's an instance of + ;; `java.lang.Byte`, but there is no predicate for it. Here, we just + ;; test whether it's a fixed-length integer of some sort. + (is (int? (byte 0))) + #?@(:cljs [] + :default + [(is (instance? java.lang.Byte (byte 0)))]) - ;; Check conversions and rounding from other numeric types - (are [expected x] (= expected (byte x)) - -128 -128 - 0 0 - 127 127 - 1 1N - 0 0N - -1 -1N - 1 1.0M - 0 0.0M - -1 -1.0M - 1 1.1 - -1 -1.1 - 1 1.9 - #?@(:cljs [] - :default - [1 3/2 - -1 -3/2 - 0 1/10 - 0 -1/10]) - 1 1.1M - -1 -1.1M) + ;; Check conversions and rounding from other numeric types + (are [expected x] (= expected (byte x)) + -128 -128 + 0 0 + 127 127 + 1 1N + 0 0N + -1 -1N + 1 1.0M + 0 0.0M + -1 -1.0M + ;; Clojurescript `byte` is a "dummy cast" which doesn't do + ;; anything (no-op). Thus, there is no conversion, no truncation + ;; of decimal values, etc. + #?@(:cljs + [1.1 1.1 + -1.1 -1.1 + 1.9 1.9 + 1.1 1.1M + -1.1 -1.1M] + :default + [1 1.1 + -1 -1.1 + 1 1.9 + 1 3/2 + -1 -3/2 + 0 1/10 + 0 -1/10 + 1 1.1M + -1 -1.1M])) - ;; `byte` throws outside the range of 127 ... -128. - (is (thrown? #?(:cljs :default :clj Exception) (byte -128.000001))) - (is (thrown? #?(:cljs :default :clj Exception) (byte -129))) - (is (thrown? #?(:cljs :default :clj Exception) (byte 128))) - (is (thrown? #?(:cljs :default :clj Exception) (byte 127.000001))) - - ;; Check handling of other types - (is (thrown? #?(:cljs :default :clj Exception) (byte "0"))) - (is (thrown? #?(:cljs :default :clj Exception) (byte :0))) - (is (thrown? #?(:cljs :default :clj Exception) (byte [0]))) - (is (thrown? #?(:cljs :default :clj Exception) (byte nil))))) + #?@(:cljs + [ ;; ClojureScript `byte` just returns its argument + (is (= -128.5 (byte -128.5))) + (is (= -129 (byte -129))) + (is (= 128(byte 128))) + (is (= 127.5 (byte 127.5))) + (is (= "0" (byte "0"))) + (is (= :0 (byte :0))) + (is (= [0] (byte [0]))) + (is (= nil (byte nil)))] + :default + [ ;; `byte` throws outside the range of 127 ... -128. + (is (thrown? Exception (byte -128.000001))) + (is (thrown? Exception (byte -129))) + (is (thrown? Exception (byte 128))) + (is (thrown? Exception (byte 127.000001))) + ;; Check handling of other types + (is (thrown? Exception (byte "0"))) + (is (thrown? Exception (byte :0))) + (is (thrown? Exception (byte [0]))) + (is (thrown? Exception (byte nil)))]))) diff --git a/test/clojure/core_test/char.cljc b/test/clojure/core_test/char.cljc index 31378958..57148d62 100644 --- a/test/clojure/core_test/char.cljc +++ b/test/clojure/core_test/char.cljc @@ -13,5 +13,5 @@ ;; TODO: Add Unicode tests ) - #?(:default (is (thrown? Exception (char -1)))) ; TODO: include CLJS test. CLJS returns string with just NULL byte + #?(:cljs nil :default (is (thrown? Exception (char -1)))) (is (thrown? #?(:cljs :default :default Exception) (char nil))))) diff --git a/test/clojure/core_test/char_qmark.cljc b/test/clojure/core_test/char_qmark.cljc index d3d3484f..cb2d4333 100644 --- a/test/clojure/core_test/char_qmark.cljc +++ b/test/clojure/core_test/char_qmark.cljc @@ -4,56 +4,60 @@ [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) (when-var-exists clojure.core/char? - (deftest test-char? - (are [expected x] (= expected (char? x)) - false 0 - false 1 - false -1 - false r/max-int - false r/min-int - false 0.0 - false 1.0 - false -1.0 - false (float 0.0) - false (float 1.0) - false (float -1.0) - false (double 0.0) - false (double 1.0) - false (double -1.0) - false r/max-double - false r/min-double - false ##Inf - false ##-Inf - false ##NaN - false 0N - false 1N - false -1N - #?@(:cljs [] - :default - [false 0/2 - false 1/2 - false -1/2]) - false 0.0M - false 1.0M - false -1.0M - false nil - false true - false false - false "a string" - false "0" - false "1" - false "-1" - false {:a :map} - false #{:a-set} - false [:a :vector] - false '(:a :list) - true \0 - true \1 - true \A - true \space - false :a-keyword - false :0 - false :1 - false :-1 - false 'a-sym)) - ) + (deftest test-char? + (are [expected x] (= expected (char? x)) + false 0 + false 1 + false -1 + false r/max-int + false r/min-int + false 0.0 + false 1.0 + false -1.0 + false (float 0.0) + false (float 1.0) + false (float -1.0) + false (double 0.0) + false (double 1.0) + false (double -1.0) + false r/max-double + false r/min-double + false ##Inf + false ##-Inf + false ##NaN + false 0N + false 1N + false -1N + #?@(:cljs [] ; cljs doesn't have ratios + :default + [false 0/2 + false 1/2 + false -1/2]) + false 0.0M + false 1.0M + false -1.0M + false nil + false true + false false + false "a string" + #?@(:cljs ; In cljs, chars are single char strings + [true "0" + true "1"] + :default + [false "0" + false "1"]) + false "-1" + false {:a :map} + false #{:a-set} + false [:a :vector] + false '(:a :list) + true \0 + true \1 + true \A + true \space + false :a-keyword + false :0 + false :1 + false :-1 + false 'a-sym)) + ) diff --git a/test/clojure/core_test/double_qmark.cljc b/test/clojure/core_test/double_qmark.cljc index 7c5a5763..5f0878b7 100644 --- a/test/clojure/core_test/double_qmark.cljc +++ b/test/clojure/core_test/double_qmark.cljc @@ -4,53 +4,79 @@ [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) (when-var-exists clojure.core/double? - (deftest test-double? - (are [expected x] (= expected (double? x)) - false 0 - false 1 - false -1 - false r/max-int - false r/min-int - true 0.0 - true 1.0 - true -1.0 - false (float 0.0) ; surprising since (float? (double 0.0)) = true - false (float 1.0) ; surprising since (float? (double 1.0)) = true - false (float -1.0) ; surprising since (float? (double -1.0)) = true - true (double 0.0) - true (double 1.0) - true (double -1.0) - true r/max-double - true r/min-double - true ##Inf - true ##-Inf - true ##NaN - false 0N - false 1N - false -1N - #?@(:cljs [] - :default - [false 0/2 - false 1/2 - false -1/2]) - false 0.0M - false 1.0M - false -1.0M - false nil - false true - false false - false "a string" - false "0" - false "1" - false "-1" - false {:a :map} - false #{:a-set} - false [:a :vector] - false '(:a :list) - false \0 - false \1 - false :a-keyword - false :0 - false :1 - false :-1 - false 'a-sym))) + (deftest test-double? + (are [expected x] (= expected (double? x)) + #?@(:cljs ; In CLJS, all numbers satisfy `double?` + [true 0 + true 1 + true -1 + true r/max-int + true r/min-int + true 0.0 + true 1.0 + true -1.0 + true (float 0.0) ; surprising since (float? (double 0.0)) = true + true (float 1.0) ; surprising since (float? (double 1.0)) = true + true (float -1.0) ; surprising since (float? (double -1.0)) = true + true (double 0.0) + true (double 1.0) + true (double -1.0) + true r/max-double + true r/min-double + true ##Inf + true ##-Inf + true ##NaN + true 0N + true 1N + true -1N + true 0.0M + true 1.0M + true -1.0M] + :default + [false 0 + false 1 + false -1 + false r/max-int + false r/min-int + true 0.0 + true 1.0 + true -1.0 + false (float 0.0) ; surprising since (float? (double 0.0)) = true + false (float 1.0) ; surprising since (float? (double 1.0)) = true + false (float -1.0) ; surprising since (float? (double -1.0)) = true + true (double 0.0) + true (double 1.0) + true (double -1.0) + true r/max-double + true r/min-double + true ##Inf + true ##-Inf + true ##NaN + false 0N + false 1N + false -1N + false 0.0M + false 1.0M + false -1.0M]) + #?@(:cljs [] ; CLJS doesn't have ratios + :default + [false 0/2 + false 1/2 + false -1/2]) false nil + false true + false false + false "a string" + false "0" + false "1" + false "-1" + false {:a :map} + false #{:a-set} + false [:a :vector] + false '(:a :list) + false \0 + false \1 + false :a-keyword + false :0 + false :1 + false :-1 + false 'a-sym))) diff --git a/test/clojure/core_test/float.cljc b/test/clojure/core_test/float.cljc index 9c4b6a5d..752259b3 100644 --- a/test/clojure/core_test/float.cljc +++ b/test/clojure/core_test/float.cljc @@ -12,13 +12,19 @@ (float 1.0) 1N (float 0.0) 0N (float -1.0) -1N + ;; The CLJS reader will read these values and convert to + ;; double. Since they are all clean conversions, they match (float 1.0) 12/12 (float 0.0) 0/12 (float -1.0) -12/12 (float 1.0) 1.0M (float 0.0) 0.0M (float -1.0) -1.0M - (float 0.0) r/min-double) + ;; Since CLJS numbers are all doubles, casting r/min-double to a + ;; float doesn't do anything, whereas in Clojure JVM it rounds + ;; down to zero. + #?@(:cljs [r/min-double r/min-double] + :default [(float 0.0) r/min-double])) (is (NaN? (float ##NaN))) #?@(:cljs diff --git a/test/clojure/core_test/float_qmark.cljc b/test/clojure/core_test/float_qmark.cljc index 8b178738..70c6031c 100644 --- a/test/clojure/core_test/float_qmark.cljc +++ b/test/clojure/core_test/float_qmark.cljc @@ -6,36 +6,63 @@ (when-var-exists clojure.core/float? (deftest test-float? (are [expected x] (= expected (float? x)) - false 0 - false 1 - false -1 - false r/max-int - false r/min-int - true 0.0 - true 1.0 - true -1.0 - true (float 0.0) - true (float 1.0) - true (float -1.0) - true (double 0.0) - true (double 1.0) - true (double -1.0) - true r/max-double - true r/min-double - true ##Inf - true ##-Inf - true ##NaN - false 0N - false 1N - false -1N - #?@(:cljs [] + #?@(:cljs ; In CLJS all numbers satisfy `float?` + [true 0 + true 1 + true -1 + true r/max-int + true r/min-int + true 0.0 + true 1.0 + true -1.0 + true (float 0.0) + true (float 1.0) + true (float -1.0) + true (double 0.0) + true (double 1.0) + true (double -1.0) + true r/max-double + true r/min-double + true ##Inf + true ##-Inf + true ##NaN + true 0N + true 1N + true -1N + true 0.0M + true 1.0M + true -1.0M] + :default + [false 0 + false 1 + false -1 + false r/max-int + false r/min-int + true 0.0 + true 1.0 + true -1.0 + true (float 0.0) + true (float 1.0) + true (float -1.0) + true (double 0.0) + true (double 1.0) + true (double -1.0) + true r/max-double + true r/min-double + true ##Inf + true ##-Inf + true ##NaN + false 0N + false 1N + false -1N + false 0.0M + false 1.0M + false -1.0M]) + #?@(:cljs [] ; CLJS doesn't have ratios :default [false 0/2 false 1/2 false -1/2]) - false 0.0M - false 1.0M - false -1.0M false nil false true false false diff --git a/test/clojure/core_test/int.cljc b/test/clojure/core_test/int.cljc index e5f0b14d..043de81b 100644 --- a/test/clojure/core_test/int.cljc +++ b/test/clojure/core_test/int.cljc @@ -3,49 +3,54 @@ [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) (when-var-exists clojure.core/int - (deftest test-int - ;; There is no platform independent predicate to test specifically - ;; for an int. While `int?` exists, it returns true for any - ;; fixed-range integer type (e.g., byte, short, int, or long). In - ;; ClojureJVM, it's an instance of `java.lang.Int`, but there is no - ;; predicate for it. Here, we just test whether it's a fixed-length - ;; integer of some sort. - (is (int? (int 0))) - #?@(:cljs [] + (deftest test-int + ;; There is no platform independent predicate to test specifically + ;; for an int. While `int?` exists, it returns true for any + ;; fixed-range integer type (e.g., byte, short, int, or long). In + ;; ClojureJVM, it's an instance of `java.lang.Integer`, but there is no + ;; predicate for it. Here, we just test whether it's a fixed-length + ;; integer of some sort. + (is (int? (int 0))) + #?(:cljs nil :default - [(is (instance? java.lang.Integer (int 0)))]) + (is (instance? java.lang.Integer (int 0)))) - ;; Check conversions and rounding from other numeric types - (are [expected x] (= expected (int x)) - -2147483648 -2147483648 - 0 0 - 2147483647 2147483647 - 1 1N - 0 0N - -1 -1N - 1 1.0M - 0 0.0M - -1 -1.0M - 1 1.1 - -1 -1.1 - 1 1.9 - #?@(:cljs [] - :default - [1 3/2 - -1 -3/2 - 0 1/10 - 0 -1/10]) - 1 1.1M - -1 -1.1M) + ;; Check conversions and rounding from other numeric types + (are [expected x] (= expected (int x)) + -2147483648 -2147483648 + 0 0 + 2147483647 2147483647 + 1 1N + 0 0N + -1 -1N + 1 1.0M + 0 0.0M + -1 -1.0M + 1 1.1 + -1 -1.1 + 1 1.9 + 1 1.1M + -1 -1.1M + #?@(:cljs [] ; The CLSJ compiler barfs on + ; these for some unknown + ; reason. Interestingly, they + ; work in a CLJS REPL. + :default + [1 3/2 + -1 -3/2 + 0 1/10 + 0 -1/10])) - ;; `int` throws outside the range of 32767 ... -32768. - (is (thrown? #?(:cljs :default :clj Exception) (int -2147483648.000001))) - (is (thrown? #?(:cljs :default :clj Exception) (int -2147483649))) - (is (thrown? #?(:cljs :default :clj Exception) (int 2147483648))) - (is (thrown? #?(:cljs :default :clj Exception) (int 2147483647.000001))) + #?@(:cljs [] + :default + [ ;; `int` throws outside the range of 32767 ... -32768. + (is (thrown? Exception (int -2147483648.000001))) + (is (thrown? Exception (int -2147483649))) + (is (thrown? Exception (int 2147483648))) + (is (thrown? Exception (int 2147483647.000001))) - ;; Check handling of other types - (is (thrown? #?(:cljs :default :clj Exception) (int "0"))) - (is (thrown? #?(:cljs :default :clj Exception) (int :0))) - (is (thrown? #?(:cljs :default :clj Exception) (int [0]))) - (is (thrown? #?(:cljs :default :clj Exception) (int nil))))) + ;; Check handling of other types + (is (thrown? Exception (int "0"))) + (is (thrown? Exception (int :0))) + (is (thrown? Exception (int [0]))) + (is (thrown? Exception (int nil)))])))