Any idea, why this fails? <https://pl.kotl.in/EW-w...
# announcements
t
Any idea, why this fails? https://pl.kotl.in/EW-w9FlLK it says the general contract of
Comparable
is violated but io cannot spot the violation
d
Your
-
can overflow. For example
Int.MIN_VALUE
is clearly smaller than
1
, so
compareTo
must return a negative number. Your method overflows and returns
Int.MAX_VALUE
, which is positive.
t
ahhh eagle eyed you 😄 thanks 🙂
d
What got me curious what that it didn't happen if I reduced the size of the list. But then it did happen even with the smaller size. So it must only happen for some numbers and then I looked into the edge cases 😄
t
makes totally sense. it's a shame i didn't had this thought although i realized it happens only seldomly with a smaller size 🙈
m
Yes that’s the cause, the subtraction trick only works for small numbers. The weird part is that I don’t have that exception in IntelliJ, but a test run shows the ordering is inconsistent. Your implementation should be corrected to:
Copy code
override fun compareTo(other: Data) = compareValues(value, other.value)
d
The weird part is that I don’t have that exception in IntelliJ
Run it a few more times... it'll happen (stupid new slack editor, mumble mumble)
🚫 1
t
the exeception is thrown by an implementation of
TimSort
in the JVm - maybe your runtime does use a different algorithm
m
nope it doesn’t happen, depends on the runtime, probably the one used by Kotlin Playground is different
d
TimSort is in OpenJDK, so I'd be very surprised if you have something else
Maybe they changed it in versions after 8 though
m
I’m using AdoptOpenJdk 11, looking at its source code right now and I can see where the exception is thrown, but it’s not happening for me. Maybe because I reduced the list size to 10
d
Yes, that makes the bug much less likely to materialize because you have a lot less elements
m
ok got it
found the point: is the array is smaller than 32 it will do a binary sort without merges
the exception is raised inside the private mergeLo/mergeHi methods…. oh well it was a nice trip inside the algorithm 🙂
s
just out of curiosity:
would this be rather an edge case or corner case? 🙂
t
sure - still it happens 😄
s
what you mean? 🙂
t
edgecases happen
s
oh i see
i wanted to see which term would be more appropriate, since one could also say that this is a corner case
I’d opt for corner case :D
d
Edge case = within operating parameters, but at their extreme values corner case = outside operating parameters so this is an edge case ;)
👍 2
🙂 1
s
well I think the terminology is a little bit fluent anyway
but I can accept this answer
corner would be at the contact region of two edges
so 🙂
I really don’t advocate for this terminology
if we talking about 2d