Skip to content
Browse files

SI-7837 quickSort, along with Ordering[K], may result in stackoverflo…

…w because the code uses '==' instead of 'equiv'

== instead of equiv (from Ordering) was used by mistake.  Fixed.

Also created a test to make sure that == is not used by throwing an exception if it is (as suggested by Jason).
  • Loading branch information...
1 parent b2bf66a commit feebc7131c669b212e6a6ff73585a879baff2c48 @Ichoran Ichoran committed Dec 23, 2013
Showing with 31 additions and 2 deletions.
  1. +2 −2 src/library/scala/util/Sorting.scala
  2. +29 −0 test/junit/scala/collection/ArraySortingTest.scala
View
4 src/library/scala/util/Sorting.scala
@@ -141,14 +141,14 @@ object Sorting {
var done = false
while (!done) {
while (b <= c && x(b) <= v) {
- if (x(b) == v) {
+ if (x(b) equiv v) {
swap(a, b)
a += 1
}
b += 1
}
while (c >= b && x(c) >= v) {
- if (x(c) == v) {
+ if (x(c) equiv v) {
swap(c, d)
d -= 1
}
View
29 test/junit/scala/collection/ArraySortingTest.scala
@@ -0,0 +1,29 @@
+package scala.collection.mutable
+
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+
+/* Tests various maps by making sure they all agree on the same answers. */
+@RunWith(classOf[JUnit4])
+class ArraySortingTest {
+
+ class CantSortMe(val i: Int) {
+ override def equals(a: Any) = throw new IllegalArgumentException("I cannot be equalled!")
+ }
+
+ object CanOrder extends Ordering[CantSortMe] {
+ def compare(a: CantSortMe, b: CantSortMe) = a.i compare b.i
+ }
+
+ // Tests SI-7837
+ @Test
+ def sortByTest() {
+ val test = Array(1,2,3,4,1,3,5,7,1,4,8,1,1,1,1)
+ val cant = test.map(i => new CantSortMe(i))
+ java.util.Arrays.sort(test)
+ scala.util.Sorting.quickSort(cant)(CanOrder)
+ assert( test(6) == 1 )
+ assert( (test,cant).zipped.forall(_ == _.i) )
+ }
+}

0 comments on commit feebc71

Please sign in to comment.
Something went wrong with that request. Please try again.