tonsky / persistent-sorted-set

Fast B-tree based persistent sorted set for Clojure/Script

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong equality with a custom comparator

darkleaf opened this issue · comments

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

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