From b29f29c850caf31662d2a18a8c6709b6122dc3c1 Mon Sep 17 00:00:00 2001 From: michelou Date: Tue, 5 Oct 2004 15:19:21 +0000 Subject: - added more tests. --- test/files/jvm/serialization.check | 92 ++++++++++++------ test/files/jvm/serialization.scala | 186 ++++++++++++++++++++++++------------- 2 files changed, 186 insertions(+), 92 deletions(-) (limited to 'test') diff --git a/test/files/jvm/serialization.check b/test/files/jvm/serialization.check index b188e72e86..6e88d3942a 100644 --- a/test/files/jvm/serialization.check +++ b/test/files/jvm/serialization.check @@ -14,43 +14,75 @@ x4 = y4 = 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 +x = List((buffers,20),(layers,2),(title,3)) +y = List((buffers,20),(layers,2),(title,3)) +x equals y: true - y equals x: true -x2 = {buffers -> 20, layers -> 2, title -> 3} -y2 = {buffers -> 20, layers -> 2, title -> 3} -x2 equals y2: true - y2 equals x2: true +x = {buffers -> 20, layers -> 2, title -> 3} +y = {buffers -> 20, layers -> 2, title -> 3} +x equals y: true - y equals x: true -x3 = {1} -y3 = {1} -x3 equals y3: true - y3 equals x3: true +x = {1} +y = {1} +x equals y: true - y equals x: true -x1 = {title -> 3, buffers -> 20, layers -> 2} -y1 = {title -> 3, buffers -> 20, layers -> 2} -x1 equals y1: true - y1 equals x1: true +x = {5, 3} +y = {5, 3} +x equals y: true - y equals x: true -x2 = {0, 8, 9} -y2 = {0, 8, 9} -x2 equals y2: true - y2 equals x2: true +x = Queue(a,b,c) +y = Queue(a,b,c) +x equals y: true - y equals x: true -x1 = Tim -y1 = Tim -x1 equals y1: true - y1 equals x1: true +x = Seq(c, b, a) +y = Seq(c, b, a) +x equals y: true - y equals x: true -x2 = Bob -y2 = Bob -x2 equals y2: true - y2 equals x2: true +x = {title -> 3, buffers -> 20, layers -> 2} +y = {title -> 3, buffers -> 20, layers -> 2} +x equals y: true - y equals x: true -x1 = John -y1 = John -x1 equals y1: true - y1 equals x1: true +x = {0, 8, 9} +y = {0, 8, 9} +x equals y: true - y equals x: true -x2 = Bill -y2 = Bill -x2 equals y2: true - y2 equals x2: true +x = {title, buffers, layers} +y = {title, buffers, layers} +x equals y: true - y equals x: true -x3 = Paul -y3 = Paul -x3 equals y3: true - y3 equals x3: true +x = Seq(2, 3) +y = Seq(2, 3) +x equals y: false - y equals x: false + +x = Queue(20, 2, 3) +y = Queue(20, 2, 3) +x equals y: true - y equals x: true + +x = Stack(20, 2, 3) +y = Stack(20, 2, 3) +x equals y: true - y equals x: true + +x = title +y = title +x equals y: true - y equals x: true + +x = Tim +y = Tim +x equals y: true - y equals x: true + +x = Bob +y = Bob +x equals y: true - y equals x: true + +x = John +y = John +x equals y: true - y equals x: true + +x = Bill +y = Bill +x equals y: true - y equals x: true + +x = Paul +y = Paul +x equals y: true - y equals x: true diff --git a/test/files/jvm/serialization.scala b/test/files/jvm/serialization.scala index 2819295f9e..cd23e9955f 100644 --- a/test/files/jvm/serialization.scala +++ b/test/files/jvm/serialization.scala @@ -9,10 +9,19 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.System; +object EqualityTest { + def check[A,B](x: A, y: B): Unit = { + System.out.println("x = " + x); + System.out.println("y = " + y); + System.out.println("x equals y: " + (x equals y) + " - y equals x: " + (y equals x)); + System.out.println(); + } +} + //############################################################################ // Test classes in package "scala" -object Test1 { +object Test1_scala { private def arrayToString[A](arr: Array[A]): String = { List.fromArray(arr).mkString("Array[",",","]"); } @@ -61,16 +70,16 @@ object Test1 { catch { case e: Exception => e.printStackTrace(); - System.out.println("Error in Test1: " + e); + System.out.println("Error in Test1_scala: " + e); } } //############################################################################ // Test classes in package "scala.collection.immutable" -object Test2 { - import scala.collection.immutable.BitSet; - import scala.collection.immutable.ListMap; +object Test2_immutable { + import scala.collection.immutable.{BitSet,ListMap,ListSet,Queue,Stack, + TreeSet,TreeMap}; val x1 = List( Pair("buffers", 20), @@ -85,80 +94,147 @@ object Test2 { val x3 = new BitSet(4, Array(2), true); + val x4 = new ListSet[Int]().incl(3).incl(5); + + val x5 = new Queue("a", "b", "c"); + + val x6 = new Stack().push("a", "b", "c"); +/* !!! InvalidClassException !!! + val x7 = new TreeMap[Int, String] + 42 -> "FortyTwo"; + + val x8 = new TreeSet[Int]().incl(2).incl(0); +*/ 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.writeObject(x4); + out.writeObject(x5); + out.writeObject(x6); +/* + out.writeObject(x7); + out.writeObject(x8); +*/ 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]; + val y4 = in.readObject().asInstanceOf[ListSet[Int]]; + val y5 = in.readObject().asInstanceOf[Queue[String]]; + val y6 = in.readObject().asInstanceOf[Stack[String]]; +/* + val y7 = in.readObject().asInstanceOf[TreeMap[Int, String]]; + val y8 = in.readObject().asInstanceOf[TreeSet[Int]]; +*/ 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(); + EqualityTest.check(x1, y1); + EqualityTest.check(x2, y2); + EqualityTest.check(x3, y3); + EqualityTest.check(x4, y4); + EqualityTest.check(x5, y5); + EqualityTest.check(x6, y6); +/* + EqualityTest.check(x7, y7); + EqualityTest.check(x8, y8); +*/ dataFile.deleteOnExit() } catch { case e: Exception => - System.out.println("Error in Test2: " + e); + System.out.println("Error in Test2_immutable: " + e); } } //############################################################################ // Test classes in package "scala.collection.mutable" -object Test3 { - import scala.collection.mutable.BitSet; - import scala.collection.mutable.HashMap; +object Test3_mutable { + import scala.collection.mutable.{BitSet,HashMap,HashSet,LinkedList, + Queue,Stack}; val x1 = new HashMap[String, Int]; - x1 ++= Test2.x1; + x1 ++= Test2_immutable.x1; val x2 = new BitSet(); x2.set(0); x2.set(8); x2.set(9); + val x3 = new HashSet[String]; + x3 ++= Test2_immutable.x1.map(p => p._1); + + val x4 = new LinkedList[Int](2, null); + x4.append(new LinkedList(3, null)); + + val x5 = new Queue[Int]; + x5 ++= Test2_immutable.x1.map(p => p._2); + + val x6 = new Stack[Int]; + x6 ++= x5; + try { val dataFile = java.io.File.createTempFile("test3", ".ser"); val out = new ObjectOutputStream(new FileOutputStream(dataFile)); out.writeObject(x1); out.writeObject(x2); + out.writeObject(x3); + out.writeObject(x4); + out.writeObject(x5); + out.writeObject(x6); out.close(); val in = new ObjectInputStream(new FileInputStream(dataFile)); val y1 = in.readObject().asInstanceOf[HashMap[String, Int]]; val y2 = in.readObject().asInstanceOf[BitSet]; + val y3 = in.readObject().asInstanceOf[HashSet[String]]; + val y4 = in.readObject().asInstanceOf[LinkedList[Int]]; + val y5 = in.readObject().asInstanceOf[Queue[Int]]; + val y6 = in.readObject().asInstanceOf[Stack[Int]]; 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(); + EqualityTest.check(x1, y1); + EqualityTest.check(x2, y2); + EqualityTest.check(x3, y3); + EqualityTest.check(x4, y4); + EqualityTest.check(x5, y5); + EqualityTest.check(x6, y6); dataFile.deleteOnExit() } catch { case e: Exception => - System.out.println("Error in Test3: " + e); + System.out.println("Error in Test3_mutable: " + e); + } +} + +//############################################################################ +// Test classes in package "scala.xml" + +object Test4_xml { + import scala.xml.{Elem}; + + val x1 = title; + + try { + val dataFile = java.io.File.createTempFile("test4", ".ser"); + val out = new ObjectOutputStream(new FileOutputStream(dataFile)); + out.writeObject(x1); + out.close(); + + val in = new ObjectInputStream(new FileInputStream(dataFile)); + val y1 = in.readObject().asInstanceOf[Elem]; + in.close(); + + EqualityTest.check(x1, y1); + dataFile.deleteOnExit() + } + catch { + case e: Exception => + System.out.println("Error in Test4_xml: " + e); } } @@ -179,12 +255,12 @@ class Employee(_name: String) with java.io.Serializable { } object bob extends Employee("Bob"); -object Test4 { +object Test5 { val x1 = new Person("Tim"); val x2 = bob; try { - val dataFile = java.io.File.createTempFile("test4", ".ser"); + val dataFile = java.io.File.createTempFile("test5", ".ser"); val out = new ObjectOutputStream(new FileOutputStream(dataFile)); out.writeObject(x1); out.writeObject(x2); @@ -195,26 +271,20 @@ object Test4 { 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(); + EqualityTest.check(x1, y1); + EqualityTest.check(x2, y2); dataFile.deleteOnExit() } catch { case e: Exception => - System.out.println("Error in Test4: " + e); + System.out.println("Error in Test5: " + e); } } //############################################################################ // Test user-defined classes WITH nesting -object Test5 with java.io.Serializable { +object Test6 with java.io.Serializable { object bill extends Employee("Bill") { val x = paul; } @@ -226,7 +296,7 @@ object Test5 with java.io.Serializable { val x3 = paul; try { - val dataFile = java.io.File.createTempFile("test5", ".ser"); + val dataFile = java.io.File.createTempFile("test6", ".ser"); val out = new ObjectOutputStream(new FileOutputStream(dataFile)); out.writeObject(x1); out.writeObject(x2); @@ -239,23 +309,14 @@ object Test5 with java.io.Serializable { 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(); + EqualityTest.check(x1, y1); + EqualityTest.check(x2, y2); + EqualityTest.check(x3, y3); dataFile.deleteOnExit() } catch { case e: Exception => - System.out.println("Error in Test5: " + e); + System.out.println("Error in Test6: " + e); } } @@ -264,11 +325,12 @@ object Test5 with java.io.Serializable { object Test { def main(args: Array[String]): Unit = { - Test1; - Test2; - Test3; - Test4; - Test5 + Test1_scala; + Test2_immutable; + Test3_mutable; + Test4_xml; + Test5; + Test6 } } -- cgit v1.2.3