Skip to content
Browse files

CLJ-1335: Update hash for empty PersistList and LazySeq

Signed-off-by: Stuart Halloway <stu@cognitect.com>
  • Loading branch information...
1 parent 7e87659 commit 1833145f4d885da8bbebc1acb5fb9c943c58bfed @puredanger puredanger committed with Stuart Halloway
View
4 src/jvm/clojure/lang/LazySeq.java
@@ -108,10 +108,6 @@ public int hashCode(){
}
public int hasheq(){
- ISeq s = seq();
- if(s == null)
- return 1;
-// return Util.hasheq(seq());
return Murmur3.hashOrdered(this);
}
View
7 src/jvm/clojure/lang/PersistentList.java
@@ -126,12 +126,17 @@ public Object reduce(IFn f, Object start) {
}
- static class EmptyList extends Obj implements IPersistentList, List, ISeq, Counted{
+ static class EmptyList extends Obj implements IPersistentList, List, ISeq, Counted, IHashEq{
+ static final int hasheq = Murmur3.hashOrdered(Collections.EMPTY_LIST);
public int hashCode(){
return 1;
}
+ public int hasheq(){
+ return hasheq;
+ }
+
public boolean equals(Object o) {
return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null;
}
View
67 test/clojure/test_clojure/data_structures.clj
@@ -13,7 +13,8 @@
(:use clojure.test
[clojure.test.generative :exclude (is)])
(:require [clojure.test-clojure.generators :as cgen]
- [clojure.data.generators :as gen]))
+ [clojure.data.generators :as gen]
+ [clojure.string :as string]))
;; *** Helper functions ***
@@ -1009,3 +1010,67 @@
{:a 2 :b -2} (assoc {} :b -2 :a 2))
(is (thrown? IllegalArgumentException (assoc [] 0 5 1)))
(is (thrown? IllegalArgumentException (assoc {} :b -2 :a))))
+
+(defn is-same-collection [a b]
+ (let [msg (format "(class a)=%s (class b)=%s a=%s b=%s"
+ (.getName (class a)) (.getName (class b)) a b)]
+ (is (= (count a) (count b) (.size a) (.size b)) msg)
+ (is (= a b) msg)
+ (is (= b a) msg)
+ (is (.equals ^Object a b) msg)
+ (is (.equals ^Object b a) msg)
+ (is (= (hash a) (hash b)) msg)
+ (is (= (.hashCode ^Object a) (.hashCode ^Object b)) msg)))
+
+(deftest ordered-collection-equality-test
+ (let [empty-colls [ []
+ '()
+ (lazy-seq)
+ clojure.lang.PersistentQueue/EMPTY
+ (vector-of :long) ]]
+ (doseq [c1 empty-colls, c2 empty-colls]
+ (is-same-collection c1 c2)))
+ (let [colls1 [ [-3 :a "7th"]
+ '(-3 :a "7th")
+ (lazy-seq (cons -3
+ (lazy-seq (cons :a
+ (lazy-seq (cons "7th" nil))))))
+ (into clojure.lang.PersistentQueue/EMPTY
+ [-3 :a "7th"]) ]]
+ (doseq [c1 colls1, c2 colls1]
+ (is-same-collection c1 c2)))
+ (is-same-collection [-3 1 7] (vector-of :long -3 1 7)))
+
+(defn case-indendent-string-cmp [s1 s2]
+ (compare (string/lower-case s1) (string/lower-case s2)))
+
+(deftest set-equality-test
+ (let [empty-sets [ #{}
+ (hash-set)
+ (sorted-set)
+ (sorted-set-by case-indendent-string-cmp) ]]
+ (doseq [s1 empty-sets, s2 empty-sets]
+ (is-same-collection s1 s2)))
+ (let [sets1 [ #{"Banana" "apple" "7th"}
+ (hash-set "Banana" "apple" "7th")
+ (sorted-set "Banana" "apple" "7th")
+ (sorted-set-by case-indendent-string-cmp "Banana" "apple" "7th") ]]
+ (doseq [s1 sets1, s2 sets1]
+ (is-same-collection s1 s2))))
+
+(deftest map-equality-test
+ (let [empty-maps [ {}
+ (hash-map)
+ (array-map)
+ (sorted-map)
+ (sorted-map-by case-indendent-string-cmp) ]]
+ (doseq [m1 empty-maps, m2 empty-maps]
+ (is-same-collection m1 m2)))
+ (let [maps1 [ {"Banana" "like", "apple" "love", "7th" "indifferent"}
+ (hash-map "Banana" "like", "apple" "love", "7th" "indifferent")
+ (array-map "Banana" "like", "apple" "love", "7th" "indifferent")
+ (sorted-map "Banana" "like", "apple" "love", "7th" "indifferent")
+ (sorted-map-by case-indendent-string-cmp
+ "Banana" "like", "apple" "love", "7th" "indifferent") ]]
+ (doseq [m1 maps1, m2 maps1]
+ (is-same-collection m1 m2))))

0 comments on commit 1833145

Please sign in to comment.
Something went wrong with that request. Please try again.