Skip to content

Commit e9d601f

Browse files
author
Antoine Brunner
committed
Wrote tests for take and drop
1 parent 769cd8a commit e9d601f

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,27 +265,36 @@ object DynamicTuple {
265265
}
266266

267267
def dynamicTake[This <: Tuple, N <: Int](self: This, n: N): Take[This, N] = {
268+
if (n < 0) throw new IndexOutOfBoundsException(n.toString)
269+
val actualN = Math.min(n, self.size)
270+
268271
type Result = Take[This, N]
269272
val arr = (self: Any) match {
273+
case () => Array.empty[Object]
270274
case xxl: TupleXXL =>
271-
xxl.elems.asInstanceOf[Array[Object]].take(n)
275+
xxl.elems.asInstanceOf[Array[Object]].take(actualN)
272276
case _ =>
273-
val arr = new Array[Object](n)
274-
itToArray(self.asInstanceOf[Product].productIterator, n, arr, 0)
277+
val arr = new Array[Object](actualN)
278+
itToArray(self.asInstanceOf[Product].productIterator, actualN, arr, 0)
275279
arr
276280
}
277281
dynamicFromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result]
278282
}
279283

280284
def dynamicDrop[This <: Tuple, N <: Int](self: This, n: N): Drop[This, N] = {
285+
if (n < 0) throw new IndexOutOfBoundsException(n.toString)
286+
val size = self.size
287+
val actualN = Math.min(n, size)
288+
281289
type Result = Drop[This, N]
282290
val arr = (self: Any) match {
291+
case () => Array.empty[Object]
283292
case xxl: TupleXXL =>
284-
xxl.elems.asInstanceOf[Array[Object]].drop(n)
293+
xxl.elems.asInstanceOf[Array[Object]].drop(actualN)
285294
case _ =>
286-
val size = self.asInstanceOf[Product].productArity - n
287-
val arr = new Array[Object](size)
288-
itToArray(self.asInstanceOf[Product].productIterator.drop(n), size, arr, 0)
295+
val rem = size - actualN
296+
val arr = new Array[Object](rem)
297+
itToArray(self.asInstanceOf[Product].productIterator.drop(actualN), rem, arr, 0)
289298
arr
290299
}
291300
dynamicFromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result]
@@ -294,6 +303,7 @@ object DynamicTuple {
294303
def dynamicSplitAt[This <: Tuple, N <: Int](self: This, n: N): Split[This, N] = {
295304
type Result = Split[This, N]
296305
val (arr1, arr2) = (self: Any) match {
306+
case () => (Array.empty[Object], Array.empty[Object])
297307
case xxl: TupleXXL =>
298308
xxl.elems.asInstanceOf[Array[Object]].splitAt(n)
299309
case _ =>

tests/run/tuple-drop.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
()
2+
()
3+
()
4+
(1,2,3,4,5)
5+
(2,3,4,5)
6+
(4,5)
7+
()
8+
()
9+
(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
10+
(12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
11+
(21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
12+
(31,32,33,34,35)
13+
()
14+
()

tests/run/tuple-drop.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
object Test extends App {
3+
val emptyTuple: Tuple = ()
4+
val tuple: Tuple = ("1", "2", "3", "4", "5")
5+
val tupleXXL: Tuple = ("11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35")
6+
7+
println(emptyTuple.drop(0))
8+
println(emptyTuple.drop(1))
9+
println(emptyTuple.drop(10))
10+
11+
println(tuple.drop(0))
12+
println(tuple.drop(1))
13+
println(tuple.drop(3))
14+
println(tuple.drop(5))
15+
println(tuple.drop(10))
16+
17+
println(tupleXXL.drop(0))
18+
println(tupleXXL.drop(1))
19+
println(tupleXXL.drop(10))
20+
println(tupleXXL.drop(20))
21+
println(tupleXXL.drop(25))
22+
println(tupleXXL.drop(30))
23+
}

tests/run/tuple-take.check

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
()
2+
()
3+
()
4+
()
5+
()
6+
(1)
7+
(1,2,3)
8+
(1,2,3,4,5)
9+
(1,2,3,4,5)
10+
()
11+
(11)
12+
(11,12,13,14,15,16,17,18,19,20)
13+
(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30)
14+
(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
15+
(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)

tests/run/tuple-take.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
object Test extends App {
3+
val emptyTuple: Tuple = ()
4+
val tuple: Tuple = ("1", "2", "3", "4", "5")
5+
val tupleXXL: Tuple = ("11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35")
6+
7+
println(emptyTuple.take(0))
8+
println(emptyTuple.take(0))
9+
println(emptyTuple.take(1))
10+
println(emptyTuple.take(10))
11+
12+
println(tuple.take(0))
13+
println(tuple.take(1))
14+
println(tuple.take(3))
15+
println(tuple.take(5))
16+
println(tuple.take(10))
17+
18+
println(tupleXXL.take(0))
19+
println(tupleXXL.take(1))
20+
println(tupleXXL.take(10))
21+
println(tupleXXL.take(20))
22+
println(tupleXXL.take(25))
23+
println(tupleXXL.take(30))
24+
}

0 commit comments

Comments
 (0)