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
7 changes: 5 additions & 2 deletions test/clojure/core_test/abs.cljc
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
102 changes: 53 additions & 49 deletions test/clojure/core_test/binding.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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.")))))])))
2 changes: 1 addition & 1 deletion test/clojure/core_test/bit_and.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/clojure/core_test/bit_and_not.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/clojure/core_test/bit_or.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

4 changes: 2 additions & 2 deletions test/clojure/core_test/bit_set.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
4 changes: 2 additions & 2 deletions test/clojure/core_test/bit_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/clojure/core_test/bit_xor.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
102 changes: 60 additions & 42 deletions test/clojure/core_test/byte.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))])))
2 changes: 1 addition & 1 deletion test/clojure/core_test/char.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))
Loading
Loading