Skip to content

Commit 79d8ede

Browse files
author
Antoine Brunner
committed
Add test for tuple split
1 parent dfb8033 commit 79d8ede

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,21 @@ object DynamicTuple {
301301
}
302302

303303
def dynamicSplitAt[This <: Tuple, N <: Int](self: This, n: N): Split[This, N] = {
304+
if (n < 0) throw new IndexOutOfBoundsException(n.toString)
305+
val size = self.size
306+
val actualN = Math.min(n, size)
307+
304308
type Result = Split[This, N]
305309
val (arr1, arr2) = (self: Any) match {
306310
case () => (Array.empty[Object], Array.empty[Object])
307311
case xxl: TupleXXL =>
308-
xxl.elems.asInstanceOf[Array[Object]].splitAt(n)
312+
xxl.elems.asInstanceOf[Array[Object]].splitAt(actualN)
309313
case _ =>
310-
val size = self.asInstanceOf[Product].productArity
311-
val arr1 = new Array[Object](n)
312-
val arr2 = new Array[Object](size - n)
314+
val arr1 = new Array[Object](actualN)
315+
val arr2 = new Array[Object](size - actualN)
313316
val it = self.asInstanceOf[Product].productIterator
314-
itToArray(it, n, arr1, 0)
315-
itToArray(it, size - n, arr2, 0)
317+
itToArray(it, actualN, arr1, 0)
318+
itToArray(it, size - actualN, arr2, 0)
316319
(arr1, arr2)
317320
}
318321
(

tests/run/tuple-split.check

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
((),())
2+
((),())
3+
((),())
4+
((),())
5+
((),(1,2,3,4,5))
6+
((1),(2,3,4,5))
7+
((1,2,3),(4,5))
8+
((1,2,3,4,5),())
9+
((1,2,3,4,5),())
10+
((),(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))
11+
((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))
12+
((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))
13+
((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))
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-split.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.splitAt(0))
8+
println(emptyTuple.splitAt(0))
9+
println(emptyTuple.splitAt(1))
10+
println(emptyTuple.splitAt(10))
11+
12+
println(tuple.splitAt(0))
13+
println(tuple.splitAt(1))
14+
println(tuple.splitAt(3))
15+
println(tuple.splitAt(5))
16+
println(tuple.splitAt(10))
17+
18+
println(tupleXXL.splitAt(0))
19+
println(tupleXXL.splitAt(1))
20+
println(tupleXXL.splitAt(10))
21+
println(tupleXXL.splitAt(20))
22+
println(tupleXXL.splitAt(25))
23+
println(tupleXXL.splitAt(30))
24+
}

0 commit comments

Comments
 (0)