From 84278e07acb2ce9d0ea9c9892b3bc0b7aa84b0a4 Mon Sep 17 00:00:00 2001 From: Aaron Bedra Date: Sat, 2 Feb 2013 14:30:54 -0600 Subject: [PATCH] Fixing set equality. The following: (= #{-1} #{(Integer. -1)}) was broken due to a bug in set equality. APersistentMap deals with things the proper way this patch mirrors the implementation direction of it by supplying different methods for equals and equiv. Signed-off-by: Aaron Bedra --- src/jvm/clojure/lang/APersistentSet.java | 51 +++++++++++++++++---------- test/clojure/test_clojure/data_structures.clj | 3 +- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/jvm/clojure/lang/APersistentSet.java b/src/jvm/clojure/lang/APersistentSet.java index 638b814..73723a4 100644 --- a/src/jvm/clojure/lang/APersistentSet.java +++ b/src/jvm/clojure/lang/APersistentSet.java @@ -51,30 +51,43 @@ public Object invoke(Object arg1) { } public boolean equals(Object obj){ - if(this == obj) return true; - if(!(obj instanceof Set)) - return false; - Set m = (Set) obj; + return setEquals(this, obj); +} - if(m.size() != count() || m.hashCode() != hashCode()) - return false; +static public boolean setEquals(IPersistentSet s1, Object obj) { + if(s1 == obj) return true; + if(!(obj instanceof Set)) + return false; + Set m = (Set) obj; - for(Object aM : m) - { - if(!contains(aM)) - return false; - } -// for(ISeq s = seq(); s != null; s = s.rest()) -// { -// if(!m.contains(s.first())) -// return false; -// } + if(m.size() != s1.count()) + return false; - return true; + for(Object aM : m) + { + if(!s1.contains(aM)) + return false; + } + + return true; } -public boolean equiv(Object o){ - return equals(o); +public boolean equiv(Object obj){ + if (!(obj instanceof Set)) + return false; + + Set m = (Set) obj; + + if (m.size() != size()) + return false; + + for(Object aM : m) + { + if(!contains(aM)) + return false; + } + + return true; } public int hashCode(){ diff --git a/test/clojure/test_clojure/data_structures.clj b/test/clojure/test_clojure/data_structures.clj index 3d913b3..0eaa12d 100644 --- a/test/clojure/test_clojure/data_structures.clj +++ b/test/clojure/test_clojure/data_structures.clj @@ -647,7 +647,8 @@ (hash-set nil 2) #{nil 2} (hash-set #{}) #{#{}} (hash-set 1 #{}) #{1 #{}} - (hash-set #{} 2) #{#{} 2} )) + (hash-set #{} 2) #{#{} 2} + (hash-set (Integer. -1)) (hash-set (Long. -1)))) (deftest test-sorted-set -- 1.8.1.2