Skip to content

Commit eeb0925

Browse files
Merge pull request #7633 from brunnerant/tuple-tests
Add tests for the current tuple API
2 parents 4adc435 + dbd777d commit eeb0925

File tree

11 files changed

+116
-16
lines changed

11 files changed

+116
-16
lines changed

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,12 @@ object DynamicTuple {
257257
).asInstanceOf[Zip[This, T2]]
258258
}
259259

260-
def dynamicMap[This <: Tuple, F[_]](self: This, f: [t] => t => F[t]): Map[This, F] =
261-
Tuple.fromArray(self.asInstanceOf[Product].productIterator.map(f(_)).toArray) // TODO use toIArray of Object to avoid double/triple array copy
262-
.asInstanceOf[Map[This, F]]
260+
def dynamicMap[This <: Tuple, F[_]](self: This, f: [t] => t => F[t]): Map[This, F] = (self: Any) match {
261+
case self: Unit => ().asInstanceOf[Map[This, F]]
262+
case _ =>
263+
Tuple.fromArray(self.asInstanceOf[Product].productIterator.map(f(_)).toArray) // TODO use toIArray of Object to avoid double/triple array copy
264+
.asInstanceOf[Map[This, F]]
265+
}
263266

264267
def consIterator(head: Any, tail: Tuple): Iterator[Any] =
265268
Iterator.single(head) ++ tail.asInstanceOf[Product].productIterator

tests/run/tuple-concat.check

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
tuple1 ++ emptyTuple = (e1,e2,e3)
2-
emptyTuple ++ tuple1 = (e1,e2,e3)
3-
tuple2 ++ emptyTuple = (e4,e5,e6)
4-
emptyTuple ++ tuple2 = (e4,e5,e6)
5-
tuple1 ++ tuple2 = (e1,e2,e3,e4,e5,e6)
1+
()
2+
(1,2,3,4,5)
3+
(1,2,3,4,5)
4+
(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)
5+
(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+
(1,2,3,4,5,6,7,8,9,10)
7+
(1,2,3,4,5,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)
8+
(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,1,2,3,4,5)
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,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60)

tests/run/tuple-concat.scala

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11

22
object Test extends App {
33
val emptyTuple: Tuple = ()
4-
val tuple1: Tuple = "e1" *: "e2" *: "e3" *: ()
5-
val tuple2: Tuple = "e4" *: "e5" *: "e6" *: ()
6-
val result: Tuple = "e1" *: "e2" *: "e3" *: "e4" *: "e5" *: "e6" *: ()
4+
val tuple1: Tuple = ("1", "2", "3", "4", "5")
5+
val tuple2: Tuple = ("6", "7", "8", "9", "10")
6+
val tupleXXL1: 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")
7+
val tupleXXL2: Tuple = ("36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60")
78

8-
println("tuple1 ++ emptyTuple = " + (tuple1 ++ emptyTuple))
9-
println("emptyTuple ++ tuple1 = " + (emptyTuple ++ tuple1))
10-
println("tuple2 ++ emptyTuple = " + (tuple2 ++ emptyTuple))
11-
println("emptyTuple ++ tuple2 = " + (emptyTuple ++ tuple2))
12-
println("tuple1 ++ tuple2 = " + (tuple1 ++ tuple2))
9+
// All possible combinations of concatenating two tuples
10+
println(emptyTuple ++ emptyTuple)
11+
println(emptyTuple ++ tuple1)
12+
println(tuple1 ++ emptyTuple)
13+
println(tupleXXL1 ++ emptyTuple)
14+
println(emptyTuple ++ tupleXXL1)
15+
println(tuple1 ++ tuple2)
16+
println(tuple1 ++ tupleXXL1)
17+
println(tupleXXL1 ++ tuple1)
18+
println(tupleXXL1 ++ tupleXXL2)
19+
20+
// Concatenation with an empty tuple should be a no-op
1321
assert((tuple1 ++ emptyTuple).asInstanceOf[AnyRef] eq tuple1.asInstanceOf[AnyRef])
22+
assert((tupleXXL1 ++ emptyTuple).asInstanceOf[AnyRef] eq tupleXXL1.asInstanceOf[AnyRef])
1423
assert((emptyTuple ++tuple1).asInstanceOf[AnyRef] eq tuple1.asInstanceOf[AnyRef])
24+
assert((emptyTuple ++ tupleXXL1).asInstanceOf[AnyRef] eq tupleXXL1.asInstanceOf[AnyRef])
1525
}

tests/run/tuple-cons.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(head)
2+
(head,1,2,3,4,5)
3+
(head,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)
4+
(head,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57)

tests/run/tuple-cons.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
val tuple22: Tuple = ("36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57")
7+
8+
// Test all possible combinations of making
9+
println("head" *: emptyTuple)
10+
println("head" *: tuple)
11+
println("head" *: tupleXXL)
12+
println("head" *: tuple22)
13+
}

tests/run/tuple-map.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
()
2+
(2,3,4,5,6)
3+
(21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45)

tests/run/tuple-map.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
type Id[X] = X
8+
val f: [t] => t => Id[t] = [t] => (x: t) => {
9+
val str = x.asInstanceOf[String]
10+
str.updated(0, (str(0) + 1).toChar).asInstanceOf[t]
11+
}
12+
13+
// Test all possible combinations of making
14+
println(emptyTuple.map(f))
15+
println(tuple.map(f))
16+
println(tupleXXL.map(f))
17+
}

tests/run/tuple-tail.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
()
2+
(2,3,4,5)
3+
(12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
4+
(37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58)

tests/run/tuple-tail.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
object Test extends App {
3+
val singletonTuple: NonEmptyTuple = Tuple1("59")
4+
val tuple: NonEmptyTuple = ("1", "2", "3", "4", "5")
5+
val tupleXXL: NonEmptyTuple = ("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+
val tuple23: NonEmptyTuple = ("36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58")
7+
8+
// Test all possible combinations of making
9+
println(singletonTuple.tail)
10+
println(tuple.tail)
11+
println(tupleXXL.tail)
12+
println(tuple23.tail)
13+
}

tests/run/tuple-zip.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
()
2+
()
3+
()
4+
()
5+
()
6+
((1,6),(2,7),(3,8),(4,9),(5,10))
7+
((1,11),(2,12),(3,13),(4,14),(5,15))
8+
((11,1),(12,2),(13,3),(14,4),(15,5))
9+
((11,36),(12,37),(13,38),(14,39),(15,40),(16,41),(17,42),(18,43),(19,44),(20,45),(21,46),(22,47),(23,48),(24,49),(25,50),(26,51),(27,52),(28,53),(29,54),(30,55),(31,56),(32,57),(33,58),(34,59),(35,60))

tests/run/tuple-zip.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
object Test extends App {
3+
val emptyTuple: Tuple = ()
4+
val tuple1: Tuple = ("1", "2", "3", "4", "5")
5+
val tuple2: Tuple = ("6", "7", "8", "9", "10")
6+
val tupleXXL1: 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")
7+
val tupleXXL2: Tuple = ("36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60")
8+
9+
// All possible combinations of zipping two tuples
10+
println(emptyTuple zip emptyTuple)
11+
println(emptyTuple zip tuple1)
12+
println(tuple1 zip emptyTuple)
13+
println(tupleXXL1 zip emptyTuple)
14+
println(emptyTuple zip tupleXXL1)
15+
println(tuple1 zip tuple2)
16+
println(tuple1 zip tupleXXL1)
17+
println(tupleXXL1 zip tuple1)
18+
println(tupleXXL1 zip tupleXXL2)
19+
}
20+

0 commit comments

Comments
 (0)