aboutsummaryrefslogtreecommitdiff
path: root/tests/run/generic/Test.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2017-01-31 14:46:24 +1100
committerMartin Odersky <odersky@gmail.com>2017-02-08 19:35:58 +1100
commitfe09e0d8fe68e0b48d5e864e1de12ae5ee86077d (patch)
tree67279b95ae5e7a2f2ebd2418d3a4b12002f107ae /tests/run/generic/Test.scala
parentbc06d17ebfd7c8d6003dffe925c3cf9ebca6b1b9 (diff)
downloaddotty-fe09e0d8fe68e0b48d5e864e1de12ae5ee86077d.tar.gz
dotty-fe09e0d8fe68e0b48d5e864e1de12ae5ee86077d.tar.bz2
dotty-fe09e0d8fe68e0b48d5e864e1de12ae5ee86077d.zip
ADT and Serialization test
The test exercises all the improvements made in previous commits of this branch.
Diffstat (limited to 'tests/run/generic/Test.scala')
-rw-r--r--tests/run/generic/Test.scala58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/run/generic/Test.scala b/tests/run/generic/Test.scala
new file mode 100644
index 000000000..1431d1185
--- /dev/null
+++ b/tests/run/generic/Test.scala
@@ -0,0 +1,58 @@
+import generic._
+import Tree._
+import List._
+import java.io._
+import Shapes._
+
+object Test {
+ import Serialization._
+
+ private var lCount, tCount = 0
+
+// ------- Code that will eventually be produced by macros -------------
+
+ implicit def ListSerializable[Elem](implicit es: Serializable[Elem]): Serializable[List[Elem]] = {
+ implicit lazy val lsElem: Serializable[List[Elem]] = {
+ lCount += 1 // test code to verify we create bounded number of Serializables
+ RecSerializable[List[Elem], List.Shape[Elem]]
+ }
+ lsElem
+ }
+
+ implicit def TreeSerializable[R]: Serializable[Tree[R]] = {
+ implicit lazy val tR: Serializable[Tree[R]] = {
+ tCount += 1 // test code to verify we create bounded number of Serializables
+ RecSerializable[Tree[R], Tree.Shape[R]]
+ }
+ tR
+ }
+ implicit lazy val tsInt: Serializable[Tree[Int]] = TreeSerializable[Int]
+ implicit lazy val tsBoolean: Serializable[Tree[Boolean]] = TreeSerializable[Boolean]
+
+// ------- Test code --------------------------------------------------------
+
+ /** Serialize data, then deserialize it back and check that it is the same. */
+ def sds[D](data: D)(implicit ser: Serializable[D]) = {
+ val outBytes = new ByteArrayOutputStream
+ val out = new DataOutputStream(outBytes)
+ ser.write(data, out)
+ out.flush()
+ val inBytes = new ByteArrayInputStream(outBytes.toByteArray)
+ val in = new DataInputStream(inBytes)
+ val result = ser.read(in)
+ assert(data == result, s"$data != $result")
+ }
+
+ val data1 =
+ Cons(1, Cons(2, Cons(3, Nil)))
+
+ val data2 =
+ If(IsZero(Pred(Succ(Zero))), Succ(Succ(Zero)), Pred(Pred(Zero)))
+
+ def main(args: Array[String]) = {
+ sds(data1)
+ assert(lCount == 1, lCount)
+ sds(data2)
+ assert(tCount == 2, tCount)
+ }
+} \ No newline at end of file