Skip to content

Commit

Permalink
Merge pull request #75 from NoahTheDuke/nb/fix-set-npe
Browse files Browse the repository at this point in the history
Fast-follow to #74 Fix NPE in ordered sets
  • Loading branch information
NoahTheDuke authored May 10, 2024
2 parents b4277db + 05952c3 commit 574e470
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/flatland/ordered/set.clj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
(toString [this]
(str "#{" (clojure.string/join " " (map str this)) "}"))
(hashCode [this]
(reduce + (keep #(when % (.hashCode ^Object %)) (.seq this))))
(reduce + (keep #(when (some? %) (.hashCode ^Object %)) (.seq this))))
(equals [this other]
(or (identical? this other)
(and (instance? Set other)
Expand Down
5 changes: 5 additions & 0 deletions test/flatland/ordered/set_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@
(is (= (.hashCode (ordered-set nil)) (.hashCode (hash-set nil))))
(is (= (.hashCode (ordered-set nil :a {:b nil})) (.hashCode (hash-set nil :a {:b nil}))))))

(deftest nil-and-false-hashes
(is (not= (.hashCode (ordered-set nil)) (.hashCode (hash-set false))))
(is (not= (.hashCode (ordered-set false)) (.hashCode (hash-set nil))))
(is (= (.hashCode (ordered-set false nil)) (.hashCode (hash-set nil false)))))

(deftest nil-hash-code-npe
;; No assertions here; just check that it doesn't NPE
;; See: https://github.com/amalloy/ordered/issues/27
Expand Down

0 comments on commit 574e470

Please sign in to comment.