Skip to content

Commit

Permalink
Equivalence does not blow up when given odd input
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiesardo committed Aug 28, 2015
1 parent 7d98aff commit e60a866
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Current
- Equivalence checks for instance of Map first

## 1.2.2
- Move map and set functions to linked.core
Expand Down
6 changes: 4 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
:url "http://github.com/frankiesardo/linked"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:profiles {:dev {:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-2371"]]
:profiles {:dev {:dependencies [[org.clojure/clojure "1.7.0"
:scope "provided"]
[org.clojure/clojurescript "1.7.58"
:scope "provided"]]
:plugins [[com.keminglabs/cljx "0.6.0"
:exclusions [org.clojure/clojure]]
[lein-cljsbuild "1.0.5"]
Expand Down
3 changes: 2 additions & 1 deletion src/linked/map.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
(empty [_]
(with-meta empty-linked-map (meta delegate-map)))
(equiv [this o]
(and (= (.count this) (count o))
(and (instance? Map o)
(= (.count this) (count o))
(every? (fn [[k v]]
(= v (get o k)))
(.seq this))))
Expand Down
57 changes: 27 additions & 30 deletions src/linked/set.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,37 @@
(LinkedSet. (assoc linked-map o nil))))
(empty [_]
empty-linked-set)
(equiv [this o]
(and (= (.count this) (count o))
(every? (fn [e] (contains? o e))
(.seq this))))

Seqable
(seq [_]
(when-let [s (seq linked-map)] (map key s)))

Reversible
(rseq [_]
(when-let [s (rseq linked-map)] (map key s)))

IFn
(invoke [this k]
(get this k))

IObj
(meta [this]
(.meta ^IObj linked-map))
(withMeta [this m]
(LinkedSet. (.withMeta ^IObj linked-map m)))

Object
(toString [this]
(str "[" (string/join " " (map str this)) "]"))
(hashCode [this]
(reduce + (map hash (.seq this))))
(equals [this other]
(equiv [this other]
(or (identical? this other)
(and (instance? Set other)
(let [^Set s other]
(and (= (.size this) (.size s))
(every? #(.contains s %) (.seq this))))))))
(every? #(.contains s %) (.seq this)))))))
Seqable
(seq [_]
(when-let [s (seq linked-map)] (map key s)))

Reversible
(rseq [_]
(when-let [s (rseq linked-map)] (map key s)))

IFn
(invoke [this k]
(get this k))

IObj
(meta [this]
(.meta ^IObj linked-map))
(withMeta [this m]
(LinkedSet. (.withMeta ^IObj linked-map m)))

Object
(toString [this]
(str "[" (string/join " " (map str this)) "]"))
(hashCode [this]
(reduce + (map hash (.seq this))))
(equals [this other]
(.equiv this other)))

#+clj
(defmethod print-method LinkedSet [o ^java.io.Writer w]
Expand Down
5 changes: 4 additions & 1 deletion test/linked/map_test.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
(is (= other-way unsorted))))
(testing "Hash code sanity"
(is (integer? (hash one-item)))
(is (= (hash {1 2}) (hash one-item))))))
(is (= (hash {1 2}) (hash one-item))))
(testing "Does not blow up when give something different"
(is (not= one-item 'baz))
(is (not= 'baz one-item)))))

(deftest ordering
(let [values [[:first 10]
Expand Down
9 changes: 7 additions & 2 deletions test/linked/set_test.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
unsorted #{1 2 3 4}]
(is (= one-way other-way))
(is (= one-way unsorted))
(is (= other-way unsorted))))))
(is (= other-way unsorted))))
(testing "Does not blow up when given something random"
(is (not= one-item 'baz))
(is (not= 'baz one-item)))))

(deftest ordering
(let [values [[:first 10]
Expand Down Expand Up @@ -78,7 +81,9 @@
(testing "Ordered disj"
(is (= #{:a 1 2 3} (disj s :b :c))))
(testing "meta support"
(is (= {'a 'b} (meta (with-meta s {'a 'b})))))))
(is (= {'a 'b} (meta (with-meta s {'a 'b})))))
(testing "cons yields a list with element prepended"
(is (= '(:a :a 1 :b 2 :c 3) (cons :a s))))))

(deftest object-features
(let [s (linked/set 'a 1 :b 2)]
Expand Down

0 comments on commit e60a866

Please sign in to comment.