summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRui Gonçalves <ruippeixotog@gmail.com>2015-12-06 19:25:00 +0000
committerRui Gonçalves <ruippeixotog@gmail.com>2015-12-06 19:25:00 +0000
commitc17b18e173e07f421f9171272d94d84908526609 (patch)
tree3008e9e5be2acc44a6ffc7e56e84fe91fbcb0cb8 /test
parent5e99f82159842c5e2b60a1427e129b0b720a0d38 (diff)
downloadscala-c17b18e173e07f421f9171272d94d84908526609.tar.gz
scala-c17b18e173e07f421f9171272d94d84908526609.tar.bz2
scala-c17b18e173e07f421f9171272d94d84908526609.zip
SI-9581 Fix overflow on Vector take and drop methods
Fixes the index/length comparison in `Vector#take` and `Vector#drop` so that they handle all possible integer values. Given the collection's invariants `startIndex >= endIndex` and `0 >= startIndex, endIndex`, it is sufficient to change the arithmetic in the comparison as done in this commit to avoid overflows. As cases when `n <= 0` are handled beforehand, `endIndex - n` cannot overflow, contrary to `startIndex + n`. If without the danger of overflows the condition yields true, on the other hand, `startIndex + n` cannot overflow as it is smaller than `endIndex` (as the previous formulation of the condition shows).
Diffstat (limited to 'test')
-rw-r--r--test/junit/scala/collection/immutable/VectorTest.scala30
1 files changed, 30 insertions, 0 deletions
diff --git a/test/junit/scala/collection/immutable/VectorTest.scala b/test/junit/scala/collection/immutable/VectorTest.scala
new file mode 100644
index 0000000000..69f74872d0
--- /dev/null
+++ b/test/junit/scala/collection/immutable/VectorTest.scala
@@ -0,0 +1,30 @@
+package scala.collection.immutable
+
+import org.junit.Assert._
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+
+@RunWith(classOf[JUnit4])
+class VectorTest {
+
+ @Test
+ def hasCorrectDropAndTakeMethods() {
+ val v = Vector(0) ++ Vector(1 to 64: _*)
+
+ assertEquals(Vector(0, 1), v take 2)
+ assertEquals(Vector(63, 64), v takeRight 2)
+ assertEquals(Vector(2 to 64: _*), v drop 2)
+ assertEquals(Vector(0 to 62: _*), v dropRight 2)
+
+ assertEquals(v, v take Int.MaxValue)
+ assertEquals(v, v takeRight Int.MaxValue)
+ assertEquals(Vector.empty[Int], v drop Int.MaxValue)
+ assertEquals(Vector.empty[Int], v dropRight Int.MaxValue)
+
+ assertEquals(Vector.empty[Int], v take Int.MinValue)
+ assertEquals(Vector.empty[Int], v takeRight Int.MinValue)
+ assertEquals(v, v drop Int.MinValue)
+ assertEquals(v, v dropRight Int.MinValue)
+ }
+}