|
| 1 | +(ns clojure.core-test.subvec |
| 2 | + (:require [clojure.test :refer [deftest testing is are]] |
| 3 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 4 | + |
| 5 | +(when-var-exists subvec |
| 6 | + (deftest test-subvec |
| 7 | + |
| 8 | + (testing "subvec vec start" |
| 9 | + (are [expected vec start] (= expected (subvec vec start)) |
| 10 | + [2 3 4] [0 1 2 3 4] 2 |
| 11 | + [1 2 3 4] [0 1 2 3 4] 1 |
| 12 | + [] [1 2 3 4 5] 5 |
| 13 | + [] [] 0)) |
| 14 | + |
| 15 | + (testing "subvec vec start end" |
| 16 | + (are [expected vec start end] (= expected (subvec vec start end)) |
| 17 | + [2 3] [0 1 2 3 4] 2 4 |
| 18 | + [1 2 3 4] [0 1 2 3 4] 1 5 |
| 19 | + [] [1 2 3 4 5] 2 2 |
| 20 | + [] [] 0 0)) |
| 21 | + |
| 22 | + (testing "borderline indices" |
| 23 | + (testing "NaN and floats" |
| 24 | + (are [expected vec start end] (= expected (subvec vec start end)) |
| 25 | + [] [0 1 2] ##NaN ##NaN |
| 26 | + [0 1 2] [0 1 2] ##NaN 3 |
| 27 | + [] [0 1 2] 0 ##NaN |
| 28 | + [0 1 2] [0 1 2] -0 3 |
| 29 | + [2] [0 1 2] 2.72 3.14)) |
| 30 | + (testing "ratios" |
| 31 | + #?(:cljs "cljs doesn't have ratio" |
| 32 | + :default (is (= [0] (subvec [0 1 2] 1/2 4/3))))) |
| 33 | + (testing "cljs-only casts to number" |
| 34 | + #?(:cljs |
| 35 | + (are [expected vec start end] (= expected (subvec vec start end)) |
| 36 | + [0 1] [0 1 2] :a 2 |
| 37 | + [] [0 1 2] 0 :b |
| 38 | + [] [0 1 2] 'c 'd |
| 39 | + [0 1 2] [0 1 2] "a" 3 |
| 40 | + [] [0 1 2] [] {} |
| 41 | + [0] [0 1 2] false true |
| 42 | + [0 1 2 3] [0 1 2 3] ##-Inf 4 |
| 43 | + [] [0 1 2 3] 0 ##Inf)))) |
| 44 | + |
| 45 | + (testing "bad shapes" |
| 46 | + (testing "out-of-bounds" |
| 47 | + (are [vec start end] (thrown? #?(:cljs js/Error :default Exception) (subvec vec start end)) |
| 48 | + [0 1 2 3] -1 3 |
| 49 | + [0 1 2 3] 1 5 |
| 50 | + [0 1 2 3] 3 2 |
| 51 | + [] 0 1)) |
| 52 | + (testing "nil args" |
| 53 | + (are [vec start end] (thrown? #?(:cljs js/Error :default Exception) (subvec vec start end)) |
| 54 | + nil 0 0 |
| 55 | + [] nil 0 |
| 56 | + [0 1 2] 1 nil)) |
| 57 | + (testing "not a vector" |
| 58 | + (are [vec start end] (thrown? #?(:cljs js/Error :default Exception) (subvec vec start end)) |
| 59 | + '(0 1 2) 0 2 |
| 60 | + #{0 1 2} 0 2 |
| 61 | + {:a 0 :b 1} 0 2 |
| 62 | + (range 3) 0 2 |
| 63 | + "012" 0 2 |
| 64 | + (transient [0 1 2]) 0 2)) |
| 65 | + (testing "indices that cannot be cast to numbers" |
| 66 | + #?(:cljs "cljs actually can actually cast these to numbers" |
| 67 | + :default (are [vec start end] (thrown? Exception (subvec vec start end)) |
| 68 | + [0 1 2] :a 2 |
| 69 | + [0 1 2] 1 :b |
| 70 | + [0 1 2] 'c 'd |
| 71 | + [0 1 2] "a" "b" |
| 72 | + [0 1 2] [] {} |
| 73 | + [0 1 2 3] ##-Inf 4 |
| 74 | + [0 1 2 3] 0 ##Inf)))))) |
0 commit comments