|
| 1 | +(ns clojure.core-test.reduce |
| 2 | + (:require |
| 3 | + [clojure.test :as t :refer [deftest testing is are]] |
| 4 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]) |
| 5 | + #?(:clj (:import (clojure.lang IReduce)))) |
| 6 | + |
| 7 | +(def interop |
| 8 | + {:int-new (fn [x] |
| 9 | + (#?(:clj Integer. |
| 10 | + :cljs js/Number.) x)) |
| 11 | + |
| 12 | + :Integer #?(:clj Integer/TYPE |
| 13 | + :cljs js/Number) |
| 14 | + |
| 15 | + :Long #?(:clj Long/TYPE |
| 16 | + :cljs js/Number) |
| 17 | + |
| 18 | + :Float #?(:clj Long/TYPE |
| 19 | + :cljs js/Number) |
| 20 | + |
| 21 | + :Double #?(:clj Double/TYPE |
| 22 | + :cljs js/Number) |
| 23 | + |
| 24 | + :Boolean #?(:clj Boolean/TYPE |
| 25 | + :cljs js/Boolean)}) |
| 26 | + |
| 27 | + |
| 28 | +(when-var-exists clojure.core/reduce |
| 29 | + (deftest test-reduce |
| 30 | + (testing "common" |
| 31 | + (is (nil? (reduce nil nil nil))) |
| 32 | + (is (thrown? #?(:clj Exception |
| 33 | + :cljs js/Error) (reduce nil nil))) |
| 34 | + (is (= 6 (reduce + 0 [1 2 3])))) |
| 35 | + |
| 36 | + (testing "val is not supplied" |
| 37 | + (is (= 3 (reduce (fn [a b] |
| 38 | + (+ a b)) |
| 39 | + [1 2]))) |
| 40 | + |
| 41 | + (testing "empty coll" |
| 42 | + (is (= 1 (reduce (fn [] 1) [])))) |
| 43 | + |
| 44 | + (testing "coll with 1 item" |
| 45 | + (is (= 1 (reduce (fn [] |
| 46 | + (is false) |
| 47 | + (throw (ex-info "should not get here" {}))) |
| 48 | + [1]))))) |
| 49 | + |
| 50 | + (testing "val is supplied, empty coll" |
| 51 | + (is (= 1 (reduce (fn [] |
| 52 | + (is false) |
| 53 | + (throw (ex-info "should not get here" {}))) |
| 54 | + 1 |
| 55 | + [])))) |
| 56 | + |
| 57 | + (testing "reduction by type" |
| 58 | + (let [int-new (interop :int-new) |
| 59 | + char-new (interop :char-new) |
| 60 | + byte-new (interop :byte-new) |
| 61 | + arange (range 1 100) ;; enough to cross nodes |
| 62 | + avec (into [] arange) |
| 63 | + alist (into () arange) |
| 64 | + obj-array (into-array arange) |
| 65 | + int-array (into-array (:Integer interop) (map #(int-new (int %)) arange)) |
| 66 | + long-array (into-array (:Long interop) arange) |
| 67 | + float-array (into-array (:Float interop) arange) |
| 68 | + double-array (into-array (:Double interop) arange) |
| 69 | + all-true (into-array (:Boolean interop) (repeat 10 true))] |
| 70 | + |
| 71 | + (testing "val is not supplied" |
| 72 | + (is (== 4950 |
| 73 | + (reduce + arange) |
| 74 | + (reduce + avec) |
| 75 | + #?(:clj (.reduce ^IReduce avec +)) |
| 76 | + (reduce + alist) |
| 77 | + (reduce + obj-array) |
| 78 | + (reduce + int-array) |
| 79 | + (reduce + long-array) |
| 80 | + (reduce + float-array) |
| 81 | + (reduce + double-array)))) |
| 82 | + |
| 83 | + (testing "val is supplied" |
| 84 | + (is (== 4951 |
| 85 | + (reduce + 1 arange) |
| 86 | + (reduce + 1 avec) |
| 87 | + #?(:clj (.reduce ^IReduce avec + 1)) |
| 88 | + (reduce + 1 alist) |
| 89 | + (reduce + 1 obj-array) |
| 90 | + (reduce + 1 int-array) |
| 91 | + (reduce + 1 long-array) |
| 92 | + (reduce + 1 float-array) |
| 93 | + (reduce + 1 double-array)))) |
| 94 | + |
| 95 | + (is (= true |
| 96 | + (reduce #(and %1 %2) all-true) |
| 97 | + (reduce #(and %1 %2) true all-true))))))) |
0 commit comments