Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong equality with a custom comparator #3

Open
darkleaf opened this issue Jan 22, 2021 · 1 comment
Open

Wrong equality with a custom comparator #3

darkleaf opened this issue Jan 22, 2021 · 1 comment

Comments

@darkleaf
Copy link

darkleaf commented Jan 22, 2021

Clojure 1.10.0, Java 11.0.8
master branch

(let [cmp (fn [a b] (- (hash a) (hash b)))]
  (= (pss/sorted-set-by cmp 1 -2 0 5)
     (pss/sorted-set-by cmp 1 -2 0 5)))
;; false

(let [cmp (fn [a b] (- (hash a) (hash b)))]
  (= (sorted-set-by cmp 1 -2 0 5)
     (sorted-set-by cmp 1 -2 0 5)))
;; true     

You can find other wrong data by using test.check:

(ns me.tonsky.persistent-sorted-set.generative
  (:require
   [clojure.test.check :as tc]
   [clojure.test.check.generators :as gen]
   [clojure.test.check.properties :as prop]
   [clojure.test.check.clojure-test :refer [defspec]]
   [me.tonsky.persistent-sorted-set :as pss]))

(defspec hash-cmp
  1000
  (let [cmp     (fn [a b] (- (hash a) (hash b)))]
    (prop/for-all [initial (gen/not-empty (gen/list gen/small-integer))]
                  (let [a (pss/from-sequential cmp initial)
                        b (pss/from-sequential cmp initial)]
                    (comment
                      (println)
                      (prn a)
                      (prn b)
                      (prn (= a b)))
                    
                    (= a b)))))

For strings:

(let [cmp (fn [a b] (- (hash a) (hash b)))]
  (= (pss/sorted-set-by cmp "" "9" "T10" "0")
     (pss/sorted-set-by cmp "" "9" "T10" "0")))
;; false

(let [cmp (fn [a b] (- (hash a) (hash b)))]
  (= (sorted-set-by cmp "" "9" "T10" "0")
     (sorted-set-by cmp "" "9" "T10" "0")))
;; true
@darkleaf
Copy link
Author

but probably it's not a bug:

(let [cmp (fn [a b] (- (hash a) (hash b)))]
  (= (pss/sorted-set-by cmp 0 -1 2 -2)
     (pss/sorted-set-by cmp 0 -1 2 -2)))
;; true

(let [cmp (fn [a b] (- (hash a) (hash b)))]
  (= (sorted-set-by cmp 0 -1 2 -2)
     (sorted-set-by cmp 0 -1 2 -2)))
;; false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant