tonsky / tonsky/persistent-sorted-set

Wrong equality with a custom comparator

Open
#3 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Clojure
Stars
90
Forks
20
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the equality failures with pss/sorted-set-by and pss/from-sequential, then compare their behavior with Clojure's sorted-set-by. Use the supplied test.check hash-cmp property to identify additional failing data; done means equal sets built with the same custom comparator compare equal and the generated cases pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure, java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.