Skip to content

Commit 71c9336

Browse files
author
dnolen
committed
more moving things around, test nses for hashing, seqs & collections
1 parent 9f1c1be commit 71c9336

File tree

9 files changed

+965
-913
lines changed

9 files changed

+965
-913
lines changed

src/test/cljs/cljs/collections_test.cljs

Lines changed: 613 additions & 0 deletions
Large diffs are not rendered by default.

src/test/cljs/cljs/core_test.cljs

Lines changed: 0 additions & 912 deletions
Large diffs are not rendered by default.

src/test/cljs/cljs/destructuring_test.cljs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,35 @@
145145
(is (= [1 2 [3 4]]
146146
(apply destructure-1216 [1 2 3 4])))
147147
(is (= (destructure-1216 1 2 3 4)[1 2 [3 4]]
148-
(apply destructure-1216 [1 2 3 4])))))
148+
(apply destructure-1216 [1 2 3 4])))))
149+
150+
(defprotocol CLJS-1600-IFoo
151+
(foo-fn [_ {:keys [a b] :as x}]))
152+
153+
(defrecord CLJS-1600-Foo []
154+
CLJS-1600-IFoo
155+
(foo-fn [_ {:keys [a b] :as args}]
156+
args))
157+
158+
(deftest test-cljs-1600
159+
(let [foo (reify
160+
CLJS-1600-IFoo
161+
(foo-fn [_ {:keys [a b] :as args}]
162+
args))]
163+
(is (= (foo-fn (->CLJS-1600-Foo) {:a 1 :b 2})
164+
{:a 1 :b 2}))
165+
(is (= (foo-fn foo {:a 1 :b 2})
166+
{:a 1 :b 2})))
167+
;; test that the destructuring works
168+
(let [foo (reify
169+
CLJS-1600-IFoo
170+
(foo-fn [_ {:keys [a b] :as args}]
171+
{:a a :b b}))]
172+
(is (= (foo-fn foo {:a 1 :b 2})
173+
{:a 1 :b 2})))
174+
(let [foo (reify
175+
CLJS-1600-IFoo
176+
(foo-fn [_ {:keys [a b c] :or {c 3}}]
177+
{:c c}))]
178+
(is (= (foo-fn foo {:a 1 :b 2})
179+
{:c 3}))))

src/test/cljs/cljs/hashing_test.cljs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
9+
(ns cljs.hashing-test
10+
(:refer-clojure :exclude [iter])
11+
(:require [cljs.test :refer-macros [deftest testing is]]
12+
[clojure.string :as s]
13+
[clojure.set :as set]))
14+
15+
(deftest test-hash-null
16+
(is (zero? (hash (aget (js-obj) "foo")))))
17+
18+
;; hashing bug in many JS runtimes CLJ-118
19+
(deftest test-clj-118
20+
(let [g #{(conj #{:2} :alt)}
21+
h #{#{:2 :alt}}]
22+
(is (= g h)))
23+
(is (= (hash {:a 1 :b 2})
24+
(hash {:b 2 :a 1})))
25+
(is (= (hash (hash-map :a 1 :b 2))
26+
(hash (hash-map :b 2 :a 1))))
27+
(is (= (hash {:start 133 :end 134})
28+
(hash (apply hash-map [:start 133 :end 134]))))
29+
(is (= (hash :a)
30+
(hash (keyword "a")))))
31+
32+
(deftest test-962-empty-literal-hashes
33+
(testing "CLJS-962: empty literals should produce collections with correct hash codes"
34+
(let [l ()
35+
v []
36+
s #{}
37+
m {}]
38+
(is (== (hash l) (hash v) (hash-ordered-coll ())))
39+
(is (== (hash s) (hash m) (hash-unordered-coll #{})))))
40+
(testing "CLJS-962: EMPTY collections should have correct hash codes"
41+
(let [l (.-EMPTY List)
42+
pv (.-EMPTY PersistentVector)
43+
phs (.-EMPTY PersistentHashSet)
44+
pts (.-EMPTY PersistentTreeSet)
45+
pam (.-EMPTY PersistentArrayMap)
46+
phm (.-EMPTY PersistentHashMap)
47+
ptm (.-EMPTY PersistentTreeMap)]
48+
(is (== (hash l) (hash pv) (hash-ordered-coll ())))
49+
(is (apply == (hash-unordered-coll #{}) (map hash [phs pts pam phm ptm]))))))
50+
51+
(deftest test-uuid-compile-and-runtime-hash
52+
(is (= (hash (.toString #uuid "0d1f9029-40fc-4728-8bdd-9862172d4370"))
53+
(hash (.toString (UUID. "0d1f9029-40fc-4728-8bdd-9862172d4370" nil))))))
54+
55+
(deftest test-cljs-1524
56+
(let [x0 []
57+
x1 (conj x0 1)
58+
x2 (conj x1 2)
59+
x3 (remove #{1} x2)
60+
x4 (remove #{2} x3)
61+
x5 (conj x4 3)
62+
x6 (conj x5 4)
63+
x7 (conj x6 5)]
64+
(is (not (== (hash x0) (hash x1) (hash x2) (hash x3) (hash x4)
65+
(hash x5) (hash x6) (hash x7))))))
66+
67+
(deftest test-nil-hashing-cljs-1649
68+
(is (zero? (hash-string nil)))
69+
(is (not (zero? (hash-string "null")))))
70+
71+
(deftest test-cljs-1779
72+
(is (= (hash (keyword 'app "foo"))
73+
(hash (keyword "app" "foo")))))
74+
75+
(deftest test-mumur-support
76+
(testing "Testing murmur support"
77+
;; int-rotate-left
78+
(is (== (int-rotate-left (bit-or 0x87654321 0) 4) (bit-or 0x76543218 0)))
79+
(is (== (int-rotate-left (bit-or 0x87654321 0) 8) (bit-or 0x65432187 0)))
80+
(is (== (int-rotate-left (bit-or 0x80000000 0) 1) 0x1))
81+
(is (== (int-rotate-left (bit-or 0x78123456 0) 4) (bit-or 0x81234567 0)))
82+
(is (== (int-rotate-left (bit-or 0xffffffff 0) 4) (bit-or 0xffffffff 0)))
83+
84+
;; imul
85+
(is (== (imul 3 3) 9))
86+
(is (== (imul -1 8) -8))
87+
(is (== (imul -2 -2) 4))
88+
(is (== (imul 0xffffffff 5) -5))
89+
(is (== (imul 0xfffffffe 5) -10))
90+
))

src/test/cljs/cljs/new_new_test.cljs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,8 @@
116116
(deftest test-get-basis
117117
(is (= (.getBasis TypeBasis) '[a b]))
118118
(is (= (.getBasis RecordBasis) '[c d e])))
119+
120+
(deftype PositionalFactoryTest [x])
121+
122+
(deftest test-515
123+
(is (== 1 (.-x (->PositionalFactoryTest 1)))))

src/test/cljs/cljs/primitives_test.cljs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,3 +928,26 @@
928928
(catch js/Error e true)))
929929
(is (try (do (take-nth nil [1 2 3]) false)
930930
(catch js/Error e true)))))
931+
932+
(deftest test-496
933+
(is (= (char 65) \A))
934+
(is (= (char \A) \A)))
935+
936+
(deftest test-717
937+
(testing "Testing CLJS-717, JS literals"
938+
(is (array? #js [1 2 3]))
939+
(is (= (alength #js [1 2 3]) 3))
940+
(is (= (seq #js [1 2 3]) (seq [1 2 3])))
941+
(is (= (set (js-keys #js {:foo "bar" :baz "woz"})) #{"foo" "baz"}))
942+
(is (= (aget #js {:foo "bar"} "foo") "bar"))
943+
(is (= (aget #js {"foo" "bar"} "foo") "bar"))
944+
(is (array? (aget #js {"foo" #js [1 2 3]} "foo")))
945+
(is (= (seq (aget #js {"foo" #js [1 2 3]} "foo")) '(1 2 3)))))
946+
947+
(deftest test-1556
948+
(testing "Testing CLJS-1556, JS object literal code emission, beginning of statement"
949+
;; Really testing that this evaluates properly
950+
(is (= 1 (do #js {:a 1}
951+
1)))
952+
(is (= 1 (aget #js {:a 1} "a")))
953+
(is (= 1 (.-a #js {:a 1})))))

src/test/cljs/cljs/seqs_test.cljs

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software.
8+
9+
(ns cljs.seqs-test
10+
(:refer-clojure :exclude [iter])
11+
(:require [cljs.test :refer-macros [deftest testing is]]
12+
[clojure.string :as s]
13+
[clojure.set :as set]))
14+
15+
(deftest test-sequential-equality
16+
(testing "Testing ISequential equality"
17+
(is (= (list 3 2 1) [3 2 1]))
18+
(is (= [3 2 1] (seq (array 3 2 1))))))
19+
20+
(deftest test-seq-operations
21+
(testing "Testing basic seq operations"
22+
(is (= () (rest nil)))
23+
(is (= nil (seq (array))))
24+
(is (= nil (seq "")))
25+
(is (= nil (seq [])))
26+
(is (= nil (seq {})))
27+
(is (= () (rest ())))
28+
(is (= () (rest [1])))
29+
(is (= () (rest (array 1))))))
30+
31+
(deftest test-empy-and-seq
32+
(testing "Testing empty & seq"
33+
(is (nil? (empty nil)))
34+
(let [e-lazy-seq (empty (with-meta (lazy-seq (cons :a nil)) {:b :c}))]
35+
(testing "lazy seq"
36+
(is (seq? e-lazy-seq))
37+
(is (empty? e-lazy-seq))
38+
(is (= {:b :c} (meta e-lazy-seq)))))
39+
(let [e-list (empty '^{:b :c} (1 2 3))]
40+
(testing "list"
41+
(is (seq? e-list))
42+
(is (empty? e-list))
43+
(is (= {:b :c} (meta e-list)))))
44+
(let [e-elist (empty '^{:b :c} ())]
45+
(testing "empty list with metadata"
46+
(is (seq? e-elist))
47+
(is (empty? e-elist))
48+
(is (= :c (get (meta e-elist) :b)))))
49+
(let [e-cons (empty (with-meta (cons :a nil) {:b :c}))]
50+
(testing "cons"
51+
(is (seq? e-cons))
52+
(is (empty? e-cons))
53+
(is (= {:b :c} (meta e-cons)))))
54+
(let [e-vec (empty ^{:b :c} [:a :d :g])]
55+
(testing "vector"
56+
(is (vector? e-vec))
57+
(is (empty? e-vec))
58+
(is (= {:b :c} (meta e-vec)))))
59+
(let [e-omap (empty ^{:b :c} {:a :d :g :h})]
60+
(testing "map"
61+
(is (map? e-omap))
62+
(is (empty? e-omap))
63+
(is (= {:b :c} (meta e-omap)))))
64+
(let [e-hmap (empty ^{:b :c} {[1 2] :d :g :h})]
65+
(testing "map with complex keys"
66+
(is (map? e-hmap))
67+
(is (empty? e-hmap))
68+
(is (= {:b :c} (meta e-hmap)))))
69+
(let [smap (with-meta (sorted-map-by (comp - compare) 2 :a 1 :b 5 :c) {:b :c})
70+
e-smap (empty smap)]
71+
(testing "sorted-map-by"
72+
(is (map? e-smap))
73+
(is (empty? e-smap))
74+
(is (= {:b :c} (meta e-smap)))
75+
(is (identical? (-comparator smap) (-comparator e-smap)))
76+
(is (= [[5 :c] [2 :a] [1 :b]] (seq (assoc e-smap 2 :a 1 :b 5 :c))))))
77+
(let [sset (with-meta (sorted-set-by (comp - compare) 5 1 2) {:b :c})
78+
e-sset (empty sset)]
79+
(testing "sorted-set-by"
80+
(is (set? e-sset))
81+
(is (empty? e-sset))
82+
(is (= {:b :c} (meta e-sset)))
83+
(is (identical? (-comparator sset) (-comparator e-sset)))
84+
(is (= [5 2 1] (seq (conj e-sset 5 1 2))))))
85+
(let [e-queue (empty (with-meta (.-EMPTY PersistentQueue) {:b :c}))]
86+
(testing "queue"
87+
(is (identical? (type e-queue) PersistentQueue))
88+
(is (empty? e-queue))
89+
(is (= {:b :c} (meta e-queue)))))))
90+
91+
(deftest test-distinct
92+
(testing "Testing distinct? & distinct"
93+
(is (distinct? 1 2 3))
94+
(is (not (distinct? 1 2 3 1)))
95+
(is (= (distinct ()) ()))
96+
(is (= (distinct '(1)) '(1)))
97+
(is (= (distinct '(1 2 3 1 1 1)) '(1 2 3)))
98+
(is (= (distinct [1 1 1 2]) '(1 2)))
99+
(is (= (distinct [1 2 1 2]) '(1 2)))
100+
(is (= (distinct "a") ["a"]))
101+
(is (= (distinct "abcabab") ["a" "b" "c"]))
102+
(is (= (distinct ["abc" "abc"]) ["abc"]))
103+
(is (= (distinct [nil nil]) [nil]))
104+
(is (= (distinct [0.0 0.0]) [0.0]))
105+
(is (= (distinct ['sym 'sym]) '[sym]))
106+
(is (= (distinct [:kw :kw]) [:kw]))
107+
(is (= (distinct [42 42]) [42]))
108+
(is (= (distinct [[] []]) [[]]))
109+
(is (= (distinct ['(1 2) '(1 2)]) '[(1 2)]))
110+
(is (= (distinct [() ()]) [()]))
111+
(is (= (distinct [[1 2] [1 2]]) [[1 2]]))
112+
(is (= (distinct [{:a 1 :b 2} {:a 1 :b 2}]) [{:a 1 :b 2}]))
113+
(is (= (distinct [{} {}]) [{}]))
114+
(is (= (distinct [#{1 2} #{1 2}]) [#{1 2}]))
115+
(is (= (distinct [#{} #{}]) [#{}]))))
116+
117+
(deftest test-rearrange-sequential
118+
(testing "Test rearranging sequential collections"
119+
(is (= [1 2 3 4 5] (sort [5 3 1 4 2])))
120+
(is (= [1 2 3 4 5] (sort < [5 3 1 4 2])))
121+
(is (= [5 4 3 2 1] (sort > [5 3 1 4 2])))
122+
(is (= ["a" [ 1 2] "foo"] (sort-by count ["foo" "a" [1 2]])))
123+
(is (= ["foo" [1 2] "a"] (sort-by count > ["foo" "a" [1 2]])))
124+
(let [coll [1 2 3 4 5 6 7 8 9 10]
125+
;; while it is technically possible for this test to fail with a false negative,
126+
;; it's _extraordinarily_ unlikely.
127+
shuffles (filter #(not= coll %) (take 100 (iterate shuffle coll)))]
128+
(is (not (empty? shuffles))))
129+
))
130+
131+
(deftest test-ISequential-indexOf
132+
(testing "Testing JS .indexOf in ISequential types"
133+
;; PersistentVector
134+
(is (= (.indexOf [] 2) -1))
135+
(is (= (.indexOf [] 2 3) -1))
136+
(is (= (.indexOf [1 2 3 4 5] 2) 1))
137+
(is (= (.indexOf [1 2 3 4 5] 6) -1))
138+
(is (= (.indexOf [1 2 3 4 5] -1) -1))
139+
(is (= (.indexOf [1 2 "x" 4 5 "a"] "a") 5))
140+
(is (= (.indexOf [1 2 3 4 5] 1 2) -1))
141+
(is (= (.indexOf [1 2 3 4 5] 2 2) -1))
142+
(is (= (.indexOf [1 2 3 1 5] 1 2) 3))
143+
(is (= (.indexOf [1 2 3 4 5] 2) 1))
144+
(is (= (.indexOf '(1 2 3 4 5) 2) 1))
145+
(is (= (.indexOf (list 1 2 3) 3) 2))
146+
(is (= (.indexOf (lazy-seq [1 2 3 4 5]) 3)) 2)
147+
(is (= (.indexOf (sequence (map inc) '(0 1 2 3 4)) 5) 4))))
148+
149+
(deftest test-ISequential-lastIndexOf
150+
(testing "Testing JS .lastIndexOf in ISequential types"
151+
;; PersistentVector
152+
(is (= (.lastIndexOf [] 2) -1))
153+
(is (= (.lastIndexOf [] 2 3) -1))
154+
(is (= (.lastIndexOf [1 2 3 4 5] 2) 1))
155+
(is (= (.lastIndexOf [1 2 3 1 5] 1) 3))
156+
(is (= (.lastIndexOf [1 2 3 1 5] 1 3) 3))
157+
(is (= (.lastIndexOf [1 2 3 1 5] 1 2) 0))
158+
(is (= (.lastIndexOf [1 2 3 1] 1 0) 0))
159+
(is (= (.lastIndexOf [1 2 3 4 5] 3 100) 2))
160+
(is (= (.lastIndexOf [1 1 1 1 1] 1) 4))
161+
(is (= (.lastIndexOf [1 1 1 1 1] 1 6) 4))
162+
(is (= (.lastIndexOf [1 2 1 1 1] 2) 1))
163+
(is (= (.lastIndexOf [1 2 3 4 5] 3 -100) -1))
164+
(is (= (.lastIndexOf [1 2 3 4 5] 3 -2) 2))
165+
(is (= (.lastIndexOf '(1 2 1 4 5) 1) 2))
166+
(is (= (.lastIndexOf (list 1 2 3 1 5) 1) 3))
167+
(is (= (.lastIndexOf (lazy-seq [1 2 1 4 5]) 1)) 2)
168+
(is (= (.lastIndexOf (sequence (map inc) '(0 1 0 3 4)) 1) 2))))
169+
170+
(deftest test-chunked
171+
(let [r (range 64)
172+
v (into [] r)]
173+
(testing "Testing Chunked Seqs"
174+
(is (= (hash (seq v)) (hash (seq v))))
175+
(is (= 6 (reduce + (array-chunk (array 1 2 3)))))
176+
(is (instance? ChunkedSeq (seq v)))
177+
(is (= r (seq v)))
178+
(is (= (map inc r) (map inc v)))
179+
(is (= (filter even? r) (filter even? v)))
180+
(is (= (filter odd? r) (filter odd? v)))
181+
(is (= (concat r r r) (concat v v v)))
182+
(is (satisfies? IReduce (seq v)))
183+
(is (== 2010 (reduce + (nnext (nnext (seq v))))))
184+
(is (== 2020 (reduce + 10 (nnext (nnext (seq v)))))))))
185+
186+
(deftest test-778
187+
(testing "Testing CLJS-778, -rest, -next RSeq"
188+
(is (= (-rest (rseq [0])) ()))
189+
(is (nil? (-next (rseq [0]))))
190+
(is (= (set (rseq [0])) #{0}))))

src/test/cljs/cljs/test_runner.cljs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
[cljs.destructuring-test]
55
[cljs.new-new-test]
66
[cljs.printing-test]
7+
[cljs.seqs-test]
8+
[cljs.collections-test]
9+
[cljs.hashing-test]
710
[cljs.core-test :as core-test]
811
[cljs.reader-test]
912
[cljs.binding-test]
@@ -30,6 +33,9 @@
3033
'cljs.destructuring-test
3134
'cljs.new-new-test
3235
'cljs.printing-test
36+
'cljs.seqs-test
37+
'cljs.collections-test
38+
'cljs.hashing-test
3339
'cljs.core-test
3440
'cljs.reader-test
3541
'clojure.string-test

src/test/self/self_parity/test.cljs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@
247247
[cljs.destructuring-test]
248248
[cljs.new-new-test]
249249
[cljs.printing-test]
250+
[cljs.seqs-test]
251+
[cljs.collections-test]
252+
[cljs.hashing-test]
250253
[cljs.core-test :as core-test]
251254
[cljs.reader-test]
252255
[cljs.binding-test]
@@ -274,6 +277,9 @@
274277
'cljs.destructuring-test
275278
'cljs.new-new-test
276279
'cljs.printing-test
280+
'cljs.seqs-test
281+
'cljs.collections-test
282+
'cljs.hashing-test
277283
'cljs.core-test
278284
'cljs.reader-test
279285
'clojure.string-test

0 commit comments

Comments
 (0)