Skip to content

Wrong equality with a custom comparator #3

Description

@darkleaf

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions