Skip to content

Add tests for the current tuple API #7633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/src/scala/runtime/DynamicTuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,12 @@ object DynamicTuple {
).asInstanceOf[Zip[This, T2]]
}

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

def consIterator(head: Any, tail: Tuple): Iterator[Any] =
Iterator.single(head) ++ tail.asInstanceOf[Product].productIterator
Expand Down
14 changes: 9 additions & 5 deletions tests/run/tuple-concat.check
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
tuple1 ++ emptyTuple = (e1,e2,e3)
emptyTuple ++ tuple1 = (e1,e2,e3)
tuple2 ++ emptyTuple = (e4,e5,e6)
emptyTuple ++ tuple2 = (e4,e5,e6)
tuple1 ++ tuple2 = (e1,e2,e3,e4,e5,e6)
()
(1,2,3,4,5)
(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)
(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,6,7,8,9,10)
(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)
(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)
(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)
26 changes: 18 additions & 8 deletions tests/run/tuple-concat.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@

object Test extends App {
val emptyTuple: Tuple = ()
val tuple1: Tuple = "e1" *: "e2" *: "e3" *: ()
val tuple2: Tuple = "e4" *: "e5" *: "e6" *: ()
val result: Tuple = "e1" *: "e2" *: "e3" *: "e4" *: "e5" *: "e6" *: ()
val tuple1: Tuple = ("1", "2", "3", "4", "5")
val tuple2: Tuple = ("6", "7", "8", "9", "10")
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")
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")

println("tuple1 ++ emptyTuple = " + (tuple1 ++ emptyTuple))
println("emptyTuple ++ tuple1 = " + (emptyTuple ++ tuple1))
println("tuple2 ++ emptyTuple = " + (tuple2 ++ emptyTuple))
println("emptyTuple ++ tuple2 = " + (emptyTuple ++ tuple2))
println("tuple1 ++ tuple2 = " + (tuple1 ++ tuple2))
// All possible combinations of concatenating two tuples
println(emptyTuple ++ emptyTuple)
println(emptyTuple ++ tuple1)
println(tuple1 ++ emptyTuple)
println(tupleXXL1 ++ emptyTuple)
println(emptyTuple ++ tupleXXL1)
println(tuple1 ++ tuple2)
println(tuple1 ++ tupleXXL1)
println(tupleXXL1 ++ tuple1)
println(tupleXXL1 ++ tupleXXL2)

// Concatenation with an empty tuple should be a no-op
assert((tuple1 ++ emptyTuple).asInstanceOf[AnyRef] eq tuple1.asInstanceOf[AnyRef])
assert((tupleXXL1 ++ emptyTuple).asInstanceOf[AnyRef] eq tupleXXL1.asInstanceOf[AnyRef])
assert((emptyTuple ++tuple1).asInstanceOf[AnyRef] eq tuple1.asInstanceOf[AnyRef])
assert((emptyTuple ++ tupleXXL1).asInstanceOf[AnyRef] eq tupleXXL1.asInstanceOf[AnyRef])
}
4 changes: 4 additions & 0 deletions tests/run/tuple-cons.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(head)
(head,1,2,3,4,5)
(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)
(head,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57)
13 changes: 13 additions & 0 deletions tests/run/tuple-cons.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

object Test extends App {
val emptyTuple: Tuple = ()
val tuple: Tuple = ("1", "2", "3", "4", "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")
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")

// Test all possible combinations of making
println("head" *: emptyTuple)
println("head" *: tuple)
println("head" *: tupleXXL)
println("head" *: tuple22)
}
3 changes: 3 additions & 0 deletions tests/run/tuple-map.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
()
(2,3,4,5,6)
(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)
17 changes: 17 additions & 0 deletions tests/run/tuple-map.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

object Test extends App {
val emptyTuple: Tuple = ()
val tuple: Tuple = ("1", "2", "3", "4", "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")

type Id[X] = X
val f: [t] => t => Id[t] = [t] => (x: t) => {
val str = x.asInstanceOf[String]
str.updated(0, (str(0) + 1).toChar).asInstanceOf[t]
}

// Test all possible combinations of making
println(emptyTuple.map(f))
println(tuple.map(f))
println(tupleXXL.map(f))
}
4 changes: 4 additions & 0 deletions tests/run/tuple-tail.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
()
(2,3,4,5)
(12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)
(37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58)
13 changes: 13 additions & 0 deletions tests/run/tuple-tail.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

object Test extends App {
val singletonTuple: NonEmptyTuple = Tuple1("59")
val tuple: NonEmptyTuple = ("1", "2", "3", "4", "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")
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")

// Test all possible combinations of making
println(singletonTuple.tail)
println(tuple.tail)
println(tupleXXL.tail)
println(tuple23.tail)
}
9 changes: 9 additions & 0 deletions tests/run/tuple-zip.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
()
()
()
()
()
((1,6),(2,7),(3,8),(4,9),(5,10))
((1,11),(2,12),(3,13),(4,14),(5,15))
((11,1),(12,2),(13,3),(14,4),(15,5))
((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))
20 changes: 20 additions & 0 deletions tests/run/tuple-zip.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

object Test extends App {
val emptyTuple: Tuple = ()
val tuple1: Tuple = ("1", "2", "3", "4", "5")
val tuple2: Tuple = ("6", "7", "8", "9", "10")
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")
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")

// All possible combinations of zipping two tuples
println(emptyTuple zip emptyTuple)
println(emptyTuple zip tuple1)
println(tuple1 zip emptyTuple)
println(tupleXXL1 zip emptyTuple)
println(emptyTuple zip tupleXXL1)
println(tuple1 zip tuple2)
println(tuple1 zip tupleXXL1)
println(tupleXXL1 zip tuple1)
println(tupleXXL1 zip tupleXXL2)
}