-
Notifications
You must be signed in to change notification settings - Fork 27
Add tests for sort-by
#817
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
Conversation
jeaye
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Dave!
| (is (= (seq [{:a 1 :b 10} | ||
| {:a 2 :b 9} | ||
| {:a 3 :b 8} | ||
| {:a 4 :b 7}]) | ||
| (sort-by :a < simple-vec-maps))) | ||
| (is (= (seq [{:a 4 :b 7} | ||
| {:a 3 :b 8} | ||
| {:a 2 :b 9} | ||
| {:a 1 :b 10}]) | ||
| (sort-by :a > simple-vec-maps))) | ||
|
|
||
| ;; Use `compare` | ||
| (is (= (seq [{:a 4 :b 7} | ||
| {:a 3 :b 8} | ||
| {:a 2 :b 9} | ||
| {:a 1 :b 10}]) | ||
| (sort-by :b compare simple-vec-maps))) | ||
| ;; Reverse `compare` | ||
| (is (= (seq [{:a 1 :b 10} | ||
| {:a 2 :b 9} | ||
| {:a 3 :b 8} | ||
| {:a 4 :b 7}]) | ||
| (sort-by :b #(compare %2 %1) simple-vec-maps))) | ||
| (is (= (seq [{:a 4 :b 7} | ||
| {:a 3 :b 8} | ||
| {:a 2 :b 9} | ||
| {:a 1 :b 10}]) | ||
| (sort-by :b < simple-vec-maps))) | ||
| (is (= (seq [{:a 1 :b 10} | ||
| {:a 2 :b 9} | ||
| {:a 3 :b 8} | ||
| {:a 4 :b 7}]) | ||
| (sort-by :b > simple-vec-maps)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these testing any unique functionality that the compare + reverse compare cases don't cover? Looks to me like over-testing which we can remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could probably cut them out. In a sense, they are testing the same thing. It's a fuzzy line between testing sort-by and the key-fn and compare functions themselves. That said (sort-by :kw > ...) is going to be pretty common. The reverse comparison should be equivalent to that if > is implemented correctly. Happy to cut all of that stuff out if you think it's redundant. I always try to be a little bit "belt-and-suspenders" when I write tests, but not overly so.
|
@E-A-Griffin Do you have a working CLR setup to see what's up with these failures before we ping David? |
|
I suspect the issue with the CLR tests is that |
|
Yeah I can check tonight |
|
I just looked at things more closely. It's not related to comparison. For whatever reason, |
|
Tested locally and reproduced the same results |
|
ClojureCLR source: (defn sort-by
"Returns a sorted sequence of the items in coll, where the sort
order is determined by comparing (keyfn item). If no comparator is
supplied, uses compare. comparator must implement
java.util.Comparator. Guaranteed to be stable: equal elements will
not be reordered. If coll is a Java array, it will be modified. To
avoid this, sort a copy of the array."
{:added "1.0"
:static true}
([keyfn coll]
(sort-by keyfn compare coll))
([keyfn comp coll]
(sort (fn [x y] (comp (keyfn x) (keyfn y))) coll)))Clojure (defn sort-by
"Returns a sorted sequence of the items in coll, where the sort
order is determined by comparing (keyfn item). If no comparator is
supplied, uses compare. comparator must implement
java.util.Comparator. Guaranteed to be stable: equal elements will
not be reordered. If coll is a Java array, it will be modified. To
avoid this, sort a copy of the array."
{:added "1.0"
:static true}
([keyfn coll]
(sort-by keyfn compare coll))
([keyfn ^java.util.Comparator comp coll]
(sort (fn [x y] (. comp (compare (keyfn x) (keyfn y)))) coll))) |
|
Need to verify but I think the issue is hence user=> (sort-by :a #(if (> %1 %2) 1 (if (< %1 %2) -1 0)) simple-vec-maps)
({:a 1, :b 10} {:a 2, :b 9} {:a 3, :b 8} {:a 4, :b 7})working as expected and also hence @dgr 's point about |
|
Nice digging, folks. If this is looking like a bug, we can ping David to loop him in. |
|
Note that in those failing tests, I'm passing |
|
It's a bug. Dates back to 16 years ago. (defn sb
([keyfn coll]
(sb keyfn compare coll))
([keyfn ^IComparable comp coll]
(sort (fn [x y] (. comp (Compare (keyfn x) (keyfn y)))) coll)))resulting in I think I'll be putting out another alpha this week, so I can drop this in there. |
|
Thank you, David! That's great to hear. |
|
@jeaye could you manually retrigger tests for this? I want to see if my merged in PR fixes some or all of the issues here |
Dave will need to update the branch first. |
|
You just want me to add a trivial change of some sort? |
|
Yep |
|
Tweaked. CI is running... |
|
Looks like CI was successful. |
|
Thank you! |
Closes #493