|
1 | 1 | (ns clojure.core-test.num |
2 | | - (:require [clojure.test :as t :refer [deftest is]] |
3 | | - [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 2 | + (:require [clojure.test :as t :refer [are deftest is testing]] |
| 3 | + [clojure.core-test.portability |
| 4 | + #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 5 | + |
| 6 | +(defn f []) |
| 7 | + |
| 8 | +#?(:bb |
| 9 | + nil |
| 10 | + |
| 11 | + :clj |
| 12 | + (do |
| 13 | + (definterface IChecker |
| 14 | + (isLong [^long l]) |
| 15 | + (isLong [^Object l]) |
| 16 | + |
| 17 | + (isDouble [^double d]) |
| 18 | + (isDouble [^Object d])) |
| 19 | + |
| 20 | + (deftype Checker [] |
| 21 | + IChecker |
| 22 | + (isLong [_this ^long _l] true) |
| 23 | + (isLong [_this ^Object _l] false) |
| 24 | + (isDouble [_this ^double _d] true) |
| 25 | + (isDouble [_this ^Object _d] false)))) |
4 | 26 |
|
5 | 27 | (when-var-exists num |
6 | 28 | (deftest test-num |
7 | | - #?@(:cljs [] |
8 | | - :clj |
9 | | - ;; The compiler should pass `x` as a primitive to `num`. |
10 | | - [(let [x 1.0] |
11 | | - (is (instance? java.lang.Double (num x)))) |
12 | | - (let [x 1] |
13 | | - (is (instance? java.lang.Long (num x)))) |
14 | | - ;; `BigInt` and `BigDecimal` are always boxed and `num` just returns them as-is. |
15 | | - (is (instance? clojure.lang.BigInt (num 1N))) |
16 | | - (is (instance? java.math.BigDecimal (num 1.0M)))] |
17 | | - :cljr |
18 | | - ;; The compiler should pass `x` as a primitive to `num`. |
19 | | - [(let [x 1.0] |
20 | | - (is (instance? System.Double (num x)))) |
21 | | - (let [x 1] |
22 | | - (is (instance? System.Int64 (num x)))) |
23 | | - ;; `BigInt` and `BigDecimal` are always boxed and `num` just returns them as-is. |
24 | | - (is (instance? clojure.lang.BigInt (num 1N))) |
25 | | - (is (instance? clojure.lang.BigDecimal (num 1.0M)))] |
26 | | - :default (is false "TODO test num")))) |
| 29 | + (testing "positive cases" |
| 30 | + #?@(:bb |
| 31 | + [(is (NaN? (num ##NaN))) |
| 32 | + (are [n] (and (= n (num n)) |
| 33 | + (= (type n) (type (num n)))) |
| 34 | + 0 |
| 35 | + 0.1 |
| 36 | + 1/2 |
| 37 | + 1N |
| 38 | + 1.0M |
| 39 | + (short 1) |
| 40 | + (byte 1) |
| 41 | + (int 1) |
| 42 | + (long 1) |
| 43 | + (float 1.0) |
| 44 | + (double 1.0) |
| 45 | + nil |
| 46 | + ##Inf)] |
| 47 | + |
| 48 | + :cljs |
| 49 | + [] |
| 50 | + |
| 51 | + :clj |
| 52 | + [(testing "longs" |
| 53 | + (let [l (long 1) |
| 54 | + L (num l) |
| 55 | + checker (Checker.)] |
| 56 | + (is (.isLong checker l)) |
| 57 | + (is (false? (.isLong checker L))))) |
| 58 | + (testing "doubles" |
| 59 | + (let [d (double 1.0) |
| 60 | + D (num d) |
| 61 | + checker (Checker.)] |
| 62 | + (is (.isDouble checker d)) |
| 63 | + (is (false? (.isDouble checker D))))) |
| 64 | + ;; `BigInt` and `BigDecimal` are always boxed and `num` just returns them as-is. |
| 65 | + (is (instance? clojure.lang.BigInt (num 1N))) |
| 66 | + (is (instance? java.math.BigDecimal (num 1.0M)))] |
| 67 | + |
| 68 | + :cljr [(is (NaN? (num ##NaN))) |
| 69 | + (is (= (byte 1) (num (byte 1)))) |
| 70 | + (is (= System.UInt64 (type (num (byte 1))))) |
| 71 | + (are [n] (and (= n (num n)) |
| 72 | + (= (type (num n)) System.Int64)) |
| 73 | + (short 1) |
| 74 | + (int 1)) |
| 75 | + (are [n] (and (= n (num n)) |
| 76 | + (= (type n) (type (num n)))) |
| 77 | + 0 |
| 78 | + 0.1 |
| 79 | + 1/2 |
| 80 | + 1N |
| 81 | + 1.0M |
| 82 | + (long 1) |
| 83 | + (float 1.0) |
| 84 | + (double 1.0) |
| 85 | + nil |
| 86 | + ##Inf)] |
| 87 | + |
| 88 | + ;; By default assume that other platforms are no-ops for numeric inputs |
| 89 | + :default [(is (NaN? (num ##NaN))) |
| 90 | + (are [n] (and (= n (num n)) |
| 91 | + (= (type n) (type (num n)))) |
| 92 | + 0 |
| 93 | + 0.1 |
| 94 | + 1/2 |
| 95 | + 1N |
| 96 | + 1.0M |
| 97 | + (short 1) |
| 98 | + (byte 1) |
| 99 | + (int 1) |
| 100 | + (long 1) |
| 101 | + (float 1.0) |
| 102 | + (double 1.0) |
| 103 | + nil |
| 104 | + ##Inf)]) |
| 105 | + (testing "exceptions thrown" |
| 106 | + ;; [[num]] is *almost* a true no-op in `cljr`, equivalent to [[identity]], |
| 107 | + ;; except that it will upcast to System.Int64/System.UInt64 |
| 108 | + #?@(:cljs |
| 109 | + [] |
| 110 | + |
| 111 | + :cljr |
| 112 | + [(are [x] (and (= x (num x)) |
| 113 | + (= (type x) (type (num x)))) |
| 114 | + f |
| 115 | + {} |
| 116 | + #{} |
| 117 | + [] |
| 118 | + '() |
| 119 | + \1 |
| 120 | + \a |
| 121 | + "" |
| 122 | + "1" |
| 123 | + 'a |
| 124 | + #"") |
| 125 | + (is (fn? (num (fn []))))] |
| 126 | + |
| 127 | + :default |
| 128 | + [(are [x] (thrown? Exception (num x)) |
| 129 | + (fn []) |
| 130 | + f |
| 131 | + {} |
| 132 | + #{} |
| 133 | + [] |
| 134 | + '() |
| 135 | + \1 |
| 136 | + \a |
| 137 | + "" |
| 138 | + "1" |
| 139 | + 'a |
| 140 | + #"")]))))) |
0 commit comments