diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/files/jvm/serialization.check | 56 | ||||
-rw-r--r-- | test/files/jvm/serialization.scala | 275 |
2 files changed, 331 insertions, 0 deletions
diff --git a/test/files/jvm/serialization.check b/test/files/jvm/serialization.check new file mode 100644 index 0000000000..b188e72e86 --- /dev/null +++ b/test/files/jvm/serialization.check @@ -0,0 +1,56 @@ +x1 = List() +y1 = List() +x1 eq y1: true - y1 eq x1: true + +x2 = None +y2 = None +x2 eq y2: true - y2 eq x2: true + +x3 = Array[1,2,3] +y3 = Array[1,2,3] +arrayEquals(x3, y3): true + +x4 = <na> +y4 = <na> +x4(2): 4 - y4(2): 4 + +x1 = List((buffers,20),(layers,2),(title,3)) +y1 = List((buffers,20),(layers,2),(title,3)) +x1 equals y1: true - y1 equals x1: true + +x2 = {buffers -> 20, layers -> 2, title -> 3} +y2 = {buffers -> 20, layers -> 2, title -> 3} +x2 equals y2: true - y2 equals x2: true + +x3 = {1} +y3 = {1} +x3 equals y3: true - y3 equals x3: true + +x1 = {title -> 3, buffers -> 20, layers -> 2} +y1 = {title -> 3, buffers -> 20, layers -> 2} +x1 equals y1: true - y1 equals x1: true + +x2 = {0, 8, 9} +y2 = {0, 8, 9} +x2 equals y2: true - y2 equals x2: true + +x1 = Tim +y1 = Tim +x1 equals y1: true - y1 equals x1: true + +x2 = Bob +y2 = Bob +x2 equals y2: true - y2 equals x2: true + +x1 = John +y1 = John +x1 equals y1: true - y1 equals x1: true + +x2 = Bill +y2 = Bill +x2 equals y2: true - y2 equals x2: true + +x3 = Paul +y3 = Paul +x3 equals y3: true - y3 equals x3: true + diff --git a/test/files/jvm/serialization.scala b/test/files/jvm/serialization.scala new file mode 100644 index 0000000000..2819295f9e --- /dev/null +++ b/test/files/jvm/serialization.scala @@ -0,0 +1,275 @@ +//############################################################################ +// Serialization +//############################################################################ +// $Id$ + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.lang.System; + +//############################################################################ +// Test classes in package "scala" + +object Test1 { + private def arrayToString[A](arr: Array[A]): String = { + List.fromArray(arr).mkString("Array[",",","]"); + } + private def arrayEquals[A,B](a1: Array[A], a2: Array[B]) = + (a1.length == a2.length) && + (Iterator.range(0, a1.length) forall { i => a1(i) == a2(i) }); + val x1 = Nil; + val x2 = None; + val x3 = Array(1, 2, 3); + val x4 = x: Int => 2 * x; + + try { + val dataFile = java.io.File.createTempFile("test1", ".ser"); + val out = new ObjectOutputStream(new FileOutputStream(dataFile)); + out.writeObject(x1); + out.writeObject(x2); + out.writeObject(x3); + out.writeObject(x4); + out.close(); + + val in = new ObjectInputStream(new FileInputStream(dataFile)); + val y1 = in.readObject().asInstanceOf[Nil.type]; + val y2 = in.readObject().asInstanceOf[Option[All]]; + val y3 = in.readObject().asInstanceOf[Array[Int]]; + val y4 = in.readObject().asInstanceOf[Int => Int]; + in.close(); + + System.out.println("x1 = " + x1); + System.out.println("y1 = " + y1); + System.out.println("x1 eq y1: " + (x1 eq y1) + " - y1 eq x1: " + (y1 eq x1)); + System.out.println(); + System.out.println("x2 = " + x2); + System.out.println("y2 = " + y2); + System.out.println("x2 eq y2: " + (x2 eq y2) + " - y2 eq x2: " + (y2 eq x2)); + System.out.println(); + System.out.println("x3 = " + arrayToString(x3)); + System.out.println("y3 = " + arrayToString(y3)); + System.out.println("arrayEquals(x3, y3): " + arrayEquals(x3, y3)); + System.out.println(); + System.out.println("x4 = <na>"); + System.out.println("y4 = <na>"); + System.out.println("x4(2): " + x4(2) + " - y4(2): " + y4(2)); + System.out.println(); + dataFile.deleteOnExit() + } + catch { + case e: Exception => + e.printStackTrace(); + System.out.println("Error in Test1: " + e); + } +} + +//############################################################################ +// Test classes in package "scala.collection.immutable" + +object Test2 { + import scala.collection.immutable.BitSet; + import scala.collection.immutable.ListMap; + + val x1 = List( + Pair("buffers", 20), + Pair("layers", 2), + Pair("title", 3) + ); + + val x2 = new ListMap[String, Int] + .incl(Pair("buffers", 20)) + .incl(Pair("layers", 2)) + .incl(Pair("title", 3)); + + val x3 = new BitSet(4, Array(2), true); + + try { + val dataFile = java.io.File.createTempFile("test2", ".ser"); + val out = new ObjectOutputStream(new FileOutputStream(dataFile)); + out.writeObject(x1); + out.writeObject(x2); + out.writeObject(x3); + out.close(); + + val in = new ObjectInputStream(new FileInputStream(dataFile)); + val y1 = in.readObject().asInstanceOf[List[Pair[String,Int]]]; + val y2 = in.readObject().asInstanceOf[ListMap[String, Int]]; + val y3 = in.readObject().asInstanceOf[BitSet]; + in.close(); + + System.out.println("x1 = " + x1); + System.out.println("y1 = " + y1); + System.out.println("x1 equals y1: " + (x1 equals y1) + " - y1 equals x1: " + (y1 equals x1)); + System.out.println(); + System.out.println("x2 = " + x2); + System.out.println("y2 = " + y2); + System.out.println("x2 equals y2: " + (x2 equals y2) + " - y2 equals x2: " + (y2 equals x2)); + System.out.println(); + System.out.println("x3 = " + x3); + System.out.println("y3 = " + y3); + System.out.println("x3 equals y3: " + (x3 equals y3) + " - y3 equals x3: " + (y3 equals x3)); + System.out.println(); + dataFile.deleteOnExit() + } + catch { + case e: Exception => + System.out.println("Error in Test2: " + e); + } +} + +//############################################################################ +// Test classes in package "scala.collection.mutable" + +object Test3 { + import scala.collection.mutable.BitSet; + import scala.collection.mutable.HashMap; + + val x1 = new HashMap[String, Int]; + x1 ++= Test2.x1; + + val x2 = new BitSet(); + x2.set(0); + x2.set(8); + x2.set(9); + + try { + val dataFile = java.io.File.createTempFile("test3", ".ser"); + val out = new ObjectOutputStream(new FileOutputStream(dataFile)); + out.writeObject(x1); + out.writeObject(x2); + out.close(); + + val in = new ObjectInputStream(new FileInputStream(dataFile)); + val y1 = in.readObject().asInstanceOf[HashMap[String, Int]]; + val y2 = in.readObject().asInstanceOf[BitSet]; + in.close(); + + System.out.println("x1 = " + x1); + System.out.println("y1 = " + y1); + System.out.println("x1 equals y1: " + (x1 equals y1) + " - y1 equals x1: " + (y1 equals x1)); + System.out.println(); + System.out.println("x2 = " + x2); + System.out.println("y2 = " + y2); + System.out.println("x2 equals y2: " + (x2 equals y2) + " - y2 equals x2: " + (y2 equals x2)); + System.out.println(); + dataFile.deleteOnExit() + } + catch { + case e: Exception => + System.out.println("Error in Test3: " + e); + } +} + +//############################################################################ +// Test user-defined classes WITHOUT nesting + +class Person(_name: String) with java.io.Serializable { + private var name = _name; + override def toString() = name; + override def equals(that: Any): Boolean = + that.isInstanceOf[Person] && + (name == that.asInstanceOf[Person].name); +} + +class Employee(_name: String) with java.io.Serializable { + private var name = _name; + override def toString() = name; +} +object bob extends Employee("Bob"); + +object Test4 { + val x1 = new Person("Tim"); + val x2 = bob; + + try { + val dataFile = java.io.File.createTempFile("test4", ".ser"); + val out = new ObjectOutputStream(new FileOutputStream(dataFile)); + out.writeObject(x1); + out.writeObject(x2); + out.close(); + + val in = new ObjectInputStream(new FileInputStream(dataFile)); + val y1 = in.readObject().asInstanceOf[Person]; + val y2 = in.readObject().asInstanceOf[Employee]; + in.close(); + + System.out.println("x1 = " + x1); + System.out.println("y1 = " + y1); + System.out.println("x1 equals y1: " + (x1 equals y1) + " - y1 equals x1: " + (y1 equals x1)); + System.out.println(); + System.out.println("x2 = " + x2); + System.out.println("y2 = " + y2); + System.out.println("x2 equals y2: " + (x2 equals y2) + " - y2 equals x2: " + (y2 equals x2)); + System.out.println(); + dataFile.deleteOnExit() + } + catch { + case e: Exception => + System.out.println("Error in Test4: " + e); + } +} + +//############################################################################ +// Test user-defined classes WITH nesting + +object Test5 with java.io.Serializable { + object bill extends Employee("Bill") { + val x = paul; + } + object paul extends Person("Paul") { + val x = 4; // bill; => StackOverflowException !!! + } + val x1 = new Person("John"); + val x2 = bill; + val x3 = paul; + + try { + val dataFile = java.io.File.createTempFile("test5", ".ser"); + val out = new ObjectOutputStream(new FileOutputStream(dataFile)); + out.writeObject(x1); + out.writeObject(x2); + out.writeObject(x3); + out.close(); + + val in = new ObjectInputStream(new FileInputStream(dataFile)); + val y1 = in.readObject().asInstanceOf[Person]; + val y2 = in.readObject().asInstanceOf[Employee]; + val y3 = in.readObject().asInstanceOf[Person]; + in.close(); + + System.out.println("x1 = " + x1); + System.out.println("y1 = " + y1); + System.out.println("x1 equals y1: " + (x1 equals y1) + " - y1 equals x1: " + (y1 equals x1)); + System.out.println(); + System.out.println("x2 = " + x2); + System.out.println("y2 = " + y2); + System.out.println("x2 equals y2: " + (x2 equals y2) + " - y2 equals x2: " + (y2 equals x2)); + System.out.println(); + System.out.println("x3 = " + x3); + System.out.println("y3 = " + y3); + System.out.println("x3 equals y3: " + (x3 equals y3) + " - y3 equals x3: " + (y3 equals x3)); + System.out.println(); + dataFile.deleteOnExit() + } + catch { + case e: Exception => + System.out.println("Error in Test5: " + e); + } +} + +//############################################################################ +// Test code + +object Test { + def main(args: Array[String]): Unit = { + Test1; + Test2; + Test3; + Test4; + Test5 + } +} + +//############################################################################ |