|
1 | 1 | (ns clojure.core-test.byte
|
2 |
| - (:require [clojure.test :as t :refer [deftest testing is are]] |
3 |
| - [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 2 | + (:require [clojure.test :as t :refer [deftest is are]] |
| 3 | + [clojure.core-test.portability |
| 4 | + #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
4 | 5 |
|
5 | 6 | (when-var-exists clojure.core/byte
|
6 | 7 | (deftest test-byte
|
|
10 | 11 | ;; test whether it's a fixed-length integer of some sort.
|
11 | 12 | (is (int? (byte 0)))
|
12 | 13 | #?(:clj (is (instance? java.lang.Byte (byte 0)))
|
13 |
| - :cljr (is (instance? System.Byte (byte 0)))) |
| 14 | + :cljr (is (instance? System.Byte (byte 0)))) |
14 | 15 |
|
15 | 16 | ;; Check conversions and rounding from other numeric types
|
| 17 | + ;; In ClojureCLR, Byte is unsigned, so we have to wipe all tests |
| 18 | + ;; of negative values |
16 | 19 | (are [expected x] (= expected (byte x))
|
17 |
| - -128 -128 |
| 20 | + #?@(:cljr [] :default [-128 -128]) |
18 | 21 | 0 0
|
19 | 22 | 127 127
|
20 | 23 | 1 1N
|
21 | 24 | 0 0N
|
22 |
| - -1 -1N |
| 25 | + #?@(:cljr [] :default [-1 -1N]) |
23 | 26 | 1 1.0M
|
24 | 27 | 0 0.0M
|
25 |
| - -1 -1.0M |
| 28 | + #?@(:cljr [] :default [-1 -1.0M]) |
26 | 29 | ;; Clojurescript `byte` is a "dummy cast" which doesn't do
|
27 | 30 | ;; anything (no-op). Thus, there is no conversion, no truncation
|
28 | 31 | ;; of decimal values, etc.
|
|
34 | 37 | -1.1 -1.1M]
|
35 | 38 | :default
|
36 | 39 | [1 1.1
|
37 |
| - -1 -1.1 |
| 40 | + #?@(:cljr [] :default [-1 -1.1]) |
38 | 41 | 1 1.9
|
39 | 42 | 1 3/2
|
40 |
| - -1 -3/2 |
| 43 | + #?@(:cljr [] :default [-1 -3/2]) |
41 | 44 | 0 1/10
|
42 |
| - 0 -1/10 |
| 45 | + #?@(:cljr [] :default [0 -1/10]) |
43 | 46 | 1 1.1M
|
44 |
| - -1 -1.1M])) |
| 47 | + #?@(:cljr [] :default [-1 -1.1M])])) |
45 | 48 |
|
46 | 49 | #?@(:cljs
|
47 | 50 | [ ;; ClojureScript `byte` just returns its argument
|
|
53 | 56 | (is (= :0 (byte :0)))
|
54 | 57 | (is (= [0] (byte [0])))
|
55 | 58 | (is (= nil (byte nil)))]
|
56 |
| - :bb [] ;; byte constructions goes via boxed argument |
| 59 | + :cljr |
| 60 | + [ ;; `byte` throws outside the range of 127 ... -128. |
| 61 | + (is (thrown? Exception (byte -128.000001))) |
| 62 | + (is (thrown? Exception (byte -129))) |
| 63 | + (is (= 128 (byte 128))) |
| 64 | + (is (= 127(byte 127.000001))) |
| 65 | + ;; Check handling of other types |
| 66 | + (is (= 0 (byte "0"))) |
| 67 | + (is (thrown? Exception (byte :0))) |
| 68 | + (is (thrown? Exception (byte [0]))) |
| 69 | + (is (thrown? Exception (byte nil)))] |
| 70 | + :bb [] ;; byte constructions goes via boxed argument |
57 | 71 | :default
|
58 | 72 | [ ;; `byte` throws outside the range of 127 ... -128.
|
59 |
| - (is (thrown? #?(:clj Exception :cljr Exception) (byte -128.000001))) |
60 |
| - (is (thrown? #?(:clj Exception :cljr Exception) (byte -129))) |
61 |
| - (is (thrown? #?(:clj Exception :cljr Exception) (byte 128))) |
62 |
| - (is (thrown? #?(:clj Exception :cljr Exception) (byte 127.000001))) |
| 73 | + (is (thrown? Exception (byte -128.000001))) |
| 74 | + (is (thrown? Exception (byte -129))) |
| 75 | + (is (thrown? Exception (byte 128))) |
| 76 | + (is (thrown? Exception (byte 127.000001))) |
63 | 77 | ;; Check handling of other types
|
64 |
| - (is (thrown? #?(:clj Exception :cljr Exception) (byte "0"))) |
65 |
| - (is (thrown? #?(:clj Exception :cljr Exception) (byte :0))) |
66 |
| - (is (thrown? #?(:clj Exception :cljr Exception) (byte [0]))) |
67 |
| - (is (thrown? #?(:clj Exception :cljr Exception) (byte nil)))]))) |
| 78 | + (is (thrown? Exception (byte "0"))) |
| 79 | + (is (thrown? Exception (byte :0))) |
| 80 | + (is (thrown? Exception (byte [0]))) |
| 81 | + (is (thrown? Exception (byte nil)))]))) |
0 commit comments