Skip to content

Commit

Permalink
Merge pull request #74 from NoahTheDuke/nb/fix-set-npe
Browse files Browse the repository at this point in the history
Fix NPE in ordered-set
  • Loading branch information
NoahTheDuke authored May 10, 2024
2 parents 8cc133d + 6977ad7 commit b4277db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
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 + (map #(.hashCode ^Object %) (.seq this))))
(reduce + (keep #(when % (.hashCode ^Object %)) (.seq this))))
(equals [this other]
(or (identical? this other)
(and (instance? Set other)
Expand Down
7 changes: 5 additions & 2 deletions test/flatland/ordered/set_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,14 @@
(is (= (hash m1) (hash m2)))
(is (= (.hashCode m1) (.hashCode m2)))
(is (= (hash (ordered-set)) (hash (hash-set))))
(is (= (.hashCode (ordered-set)) (.hashCode (hash-set))))))
(is (= (.hashCode (ordered-set)) (.hashCode (hash-set))))
(is (= (hash (ordered-set nil)) (hash (hash-set nil))))
(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-hash-code-npe
;; No assertions here; just check that it doesn't NPE
;; See: https://github.com/amalloy/ordered/issues/27
(are [contents] (.hashCode (ordered-set contents))
(are [contents] (.hashCode (apply ordered-set contents))
[nil]
[nil :a]))

0 comments on commit b4277db

Please sign in to comment.