From 604f8d41c972656529969376a1c35bb9025c2053 Mon Sep 17 00:00:00 2001 From: NthPortal Date: Thu, 12 Jul 2018 17:26:15 -0400 Subject: [PATCH] Fix lengthCompare for indexed types --- src/library/scala/collection/ArrayOps.scala | 2 +- src/library/scala/collection/IndexedSeq.scala | 2 +- src/library/scala/collection/StringOps.scala | 2 ++ .../scala/collection/immutable/Vector.scala | 2 -- .../scala/collection/IndexedSeqTest.scala | 18 ++++++++++++++++++ 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/library/scala/collection/ArrayOps.scala b/src/library/scala/collection/ArrayOps.scala index ab526698b817..e90eac6b76ac 100644 --- a/src/library/scala/collection/ArrayOps.scala +++ b/src/library/scala/collection/ArrayOps.scala @@ -221,7 +221,7 @@ final class ArrayOps[A](val xs: Array[A]) extends AnyVal { * x > 0 if this.length > len * }}} */ - def lengthCompare(len: Int): Int = xs.length - len + def lengthCompare(len: Int): Int = Integer.compare(xs.length, len) /** Selects an interval of elements. The returned array is made up * of all elements `x` which satisfy the invariant: diff --git a/src/library/scala/collection/IndexedSeq.scala b/src/library/scala/collection/IndexedSeq.scala index ed14a6ea7bc8..b69cff729760 100644 --- a/src/library/scala/collection/IndexedSeq.scala +++ b/src/library/scala/collection/IndexedSeq.scala @@ -52,7 +52,7 @@ trait IndexedSeqOps[+A, +CC[_], +C] extends Any with SeqOps[A, CC, C] { self => override def last: A = apply(length - 1) - override def lengthCompare(len: Int): Int = length - len + override def lengthCompare(len: Int): Int = Integer.compare(length, len) final override def knownSize: Int = length diff --git a/src/library/scala/collection/StringOps.scala b/src/library/scala/collection/StringOps.scala index 7434c0c38573..3e7f58ca63a6 100644 --- a/src/library/scala/collection/StringOps.scala +++ b/src/library/scala/collection/StringOps.scala @@ -155,6 +155,8 @@ final class StringOps(private val s: String) extends AnyVal { /** Get the char at the specified index. */ @`inline` def apply(i: Int): Char = s.charAt(i) + def lengthCompare(len: Int): Int = Integer.compare(s.length, len) + /** Builds a new collection by applying a function to all chars of this String. * * @param f the function to apply to each char. diff --git a/src/library/scala/collection/immutable/Vector.scala b/src/library/scala/collection/immutable/Vector.scala index 39d0c238ca25..7f810530c245 100644 --- a/src/library/scala/collection/immutable/Vector.scala +++ b/src/library/scala/collection/immutable/Vector.scala @@ -80,8 +80,6 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I def length: Int = endIndex - startIndex - override def lengthCompare(len: Int): Int = length - len - private[collection] def initIterator[B >: A](s: VectorIterator[B]): Unit = { s.initFrom(this) if (dirty) s.stabilize(focus) diff --git a/test/junit/scala/collection/IndexedSeqTest.scala b/test/junit/scala/collection/IndexedSeqTest.scala index 0671b9dd7e57..4f1927bf8f48 100644 --- a/test/junit/scala/collection/IndexedSeqTest.scala +++ b/test/junit/scala/collection/IndexedSeqTest.scala @@ -39,6 +39,12 @@ abstract class IndexedTest[T, E] { } } + /** check that lengthCompare compares values correctly */ + @Test def checkLengthCompare(): Unit = { + val test = underTest(size) + assert(lengthCompare(test, Int.MinValue) > 0) + } + /** * check simple equallity of the initial data. * More a test of the infra that we use in this est than a full test of equallity @@ -231,6 +237,8 @@ abstract class IndexedTest[T, E] { //the length of underTest def length(underTest: T): Int + def lengthCompare(underTest: T, len: Int): Int + //the value at index i of underTest def get(underTest: T, i: Int): E @@ -316,6 +324,8 @@ package IndexedTestImpl { val TYPE: Class[_]) extends IndexedTest[Array[E], E]{ override final def length(underTest: Array[E]) = underTest.length + override final def lengthCompare(underTest: Array[E], len: Int): Int = underTest.lengthCompare(len) + override def get(underTest: Array[E], i: Int) = underTest(i) override def slice(underTest: Array[E], from: Int, to: Int) = underTest.slice(from, to) @@ -347,6 +357,8 @@ package IndexedTestImpl { import mutable.ArraySeq override final def length(underTest: ArraySeq[E]) = underTest.length + override final def lengthCompare(underTest: ArraySeq[E], len: Int): Int = underTest.lengthCompare(len) + override def get(underTest: ArraySeq[E], i: Int) = underTest(i) override def slice(underTest: ArraySeq[E], from: Int, to: Int) = underTest.slice(from, to) @@ -377,6 +389,8 @@ package IndexedTestImpl { abstract class MutableIndexedSeqTest[T <: mutable.Seq[E], E] extends IndexedTest[T, E] with DataProvider[E]{ override final def length(underTest: T) = underTest.length + override final def lengthCompare(underTest: T, len: Int): Int = underTest.lengthCompare(len) + override def get(underTest: T, i: Int) = underTest(i) override def slice(underTest: T, from: Int, to: Int) = underTest.slice(from, to).asInstanceOf[T] @@ -407,6 +421,8 @@ package IndexedTestImpl { abstract class ImmutableIndexedSeqTest[T <: SeqOps[E, Seq, T], E] extends IndexedTest[T, E] with DataProvider[E] { override final def length(underTest: T) = underTest.length + override final def lengthCompare(underTest: T, len: Int): Int = underTest.lengthCompare(len) + override def get(underTest: T, i: Int) = underTest(i) override def slice(underTest: T, from: Int, to: Int) = underTest.slice(from, to) @@ -428,6 +444,8 @@ package IndexedTestImpl { abstract class StringOpsBaseTest extends IndexedTest[StringOps, Char] with DataProvider[Char] { override final def length(underTest: StringOps) = underTest.size + override final def lengthCompare(underTest: StringOps, len: Int): Int = underTest.lengthCompare(len) + override def get(underTest: StringOps, i: Int) = underTest(i) override def slice(underTest: StringOps, from: Int, to: Int) = underTest.slice(from, to)