From 58375d932adab9889cbf373b08051f520ff46b69 Mon Sep 17 00:00:00 2001 From: Iulian Dragos Date: Tue, 13 Dec 2005 16:19:05 +0000 Subject: Updated to new syntax. --- test-nsc/files/jvm/serialization.check | 100 +++++++++ test-nsc/files/jvm/serialization.scala | 316 +++++++++++++++++++++++++++ test-nsc/files/pos/expressions-current.scala | 5 +- test-nsc/files/pos/traits.scala | 3 +- test-nsc/files/run/Course-2002-02.scala | 26 +-- test-nsc/files/run/Course-2002-05.scala | 16 +- test-nsc/files/run/Course-2002-06.scala | 3 +- test-nsc/files/run/Course-2002-08.scala | 8 +- 8 files changed, 443 insertions(+), 34 deletions(-) create mode 100644 test-nsc/files/jvm/serialization.check create mode 100644 test-nsc/files/jvm/serialization.scala (limited to 'test-nsc/files') diff --git a/test-nsc/files/jvm/serialization.check b/test-nsc/files/jvm/serialization.check new file mode 100644 index 0000000000..c2728a5401 --- /dev/null +++ b/test-nsc/files/jvm/serialization.check @@ -0,0 +1,100 @@ +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 = +y4 = +x4(2): 4 - y4(2): 4 + +x = List((buffers,20),(layers,2),(title,3)) +y = List((buffers,20),(layers,2),(title,3)) +x equals y: true - y equals x: true + +x = {buffers -> 20, layers -> 2, title -> 3} +y = {buffers -> 20, layers -> 2, title -> 3} +x equals y: true - y equals x: true + +x = {1} +y = {1} +x equals y: true - y equals x: true + +x = {5, 3} +y = {5, 3} +x equals y: true - y equals x: true + +x = Queue(a,b,c) +y = Queue(a,b,c) +x equals y: true - y equals x: true + +x = Stack(c, b, a) +y = Stack(c, b, a) +x equals y: true - y equals x: true + +x = {42 -> FortyTwo} +y = {42 -> FortyTwo} +x equals y: true - y equals x: true + +x = {0, 2} +y = {0, 2} +x equals y: true - y equals x: true + +x = {title -> 3, buffers -> 20, layers -> 2} +y = {title -> 3, buffers -> 20, layers -> 2} +x equals y: true - y equals x: true + +x = {0, 8, 9} +y = {0, 8, 9} +x equals y: true - y equals x: true + +x = {title, buffers, layers} +y = {title, buffers, layers} +x equals y: true - y equals x: true + +x = LinkedList(2, 3) +y = LinkedList(2, 3) +x equals y: true - y equals x: true + +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 =
Last NameFirst Name
Tom20
Bob22
James19
+y =
Last NameFirst Name
Tom20
Bob22
James19
+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-nsc/files/jvm/serialization.scala b/test-nsc/files/jvm/serialization.scala new file mode 100644 index 0000000000..f55af0d46f --- /dev/null +++ b/test-nsc/files/jvm/serialization.scala @@ -0,0 +1,316 @@ +//############################################################################ +// Serialization +//############################################################################ +// $Id$ + +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(); + } +} + +object Serialize { + def write[A](o: A): Array[Byte] = { // throws Exception + val ba = new java.io.ByteArrayOutputStream(512); + val out = new java.io.ObjectOutputStream(ba); + out.writeObject(o); + out.close(); + ba.toByteArray() + } + def read[A](buffer: Array[Byte]): A = { // throws Exception + val in = + new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(buffer)); + in.readObject().asInstanceOf[A] + } +} + +//############################################################################ +// Test classes in package "scala" + +[serializable] +object Test1_scala { + 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 y1: List[All] = Serialize.read(Serialize.write(x1)); + val y2: Option[All] = Serialize.read(Serialize.write(x2)); + val y3: Array[Int] = Serialize.read(Serialize.write(x3)); + val y4: Function[Int, Int] = Serialize.read(Serialize.write(x4)); + + 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 = "); + System.out.println("y4 = "); + System.out.println("x4(2): " + x4(2) + " - y4(2): " + y4(2)); + System.out.println(); + } + catch { + case e: Exception => + e.printStackTrace(); + System.out.println("Error in Test1_scala: " + e); + } +} + +//############################################################################ +// Test classes in package "scala.collection.immutable" +[serializable] +object Test2_immutable { + import scala.collection.immutable.{BitSet,ListMap,ListSet,Queue,Stack, + TreeSet,TreeMap}; + + 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); + + val x4 = new ListSet[Int]().incl(3).incl(5); + + val x5 = new Queue("a", "b", "c"); + + val x6 = new Stack().push("a", "b", "c"); + + val x7 = new TreeMap[Int, String] + 42 -> "FortyTwo"; + + val x8 = new TreeSet[Int]().incl(2).incl(0); + + try { + val y1: List[Pair[String, Int]] = Serialize.read(Serialize.write(x1)); + val y2: ListMap[String, Int] = Serialize.read(Serialize.write(x2)); + val y3: BitSet = Serialize.read(Serialize.write(x3)); + val y4: ListSet[Int] = Serialize.read(Serialize.write(x4)); + val y5: Queue[String] = Serialize.read(Serialize.write(x5)); + val y6: Stack[String] = Serialize.read(Serialize.write(x6)); + val y7: TreeMap[Int, String] = Serialize.read(Serialize.write(x7)); + val y8: TreeSet[Int] = Serialize.read(Serialize.write(x8)); + + 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); + } + catch { + case e: Exception => + System.out.println("Error in Test2_immutable: " + e); + } +} + +//############################################################################ +// Test classes in package "scala.collection.mutable" + +object Test3_mutable { + import scala.collection.mutable.{BitSet,HashMap,HashSet,LinkedList, + Queue,Stack}; + + val x1 = new HashMap[String, Int]; + 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 y1: HashMap[String, Int] = Serialize.read(Serialize.write(x1)); + val y2: BitSet = Serialize.read(Serialize.write(x2)); + val y3: HashSet[String] = Serialize.read(Serialize.write(x3)); + val y4: LinkedList[Int] = Serialize.read(Serialize.write(x4)); + val y5: Queue[Int] = Serialize.read(Serialize.write(x5)); + val y6: Stack[Int] = Serialize.read(Serialize.write(x6)); + + EqualityTest.check(x1, y1); + EqualityTest.check(x2, y2); + EqualityTest.check(x3, y3); + EqualityTest.check(x4, y4); + EqualityTest.check(x5, y5); + EqualityTest.check(x6, y6); + } + catch { + case e: Exception => + System.out.println("Error in Test3_mutable: " + e); + } +} + +//############################################################################ +// Test classes in package "scala.xml" + +object Test4_xml { + import scala.xml.{Elem}; + + val x1 = title; + + case class Person(name: String, age: Int); + + class AddressBook(a: Person*) { + private val people: List[Person] = a.toList; + def toXHTML = + + + + + + { for (val p <- people) yield + + + + } +
Last NameFirst Name
{ p.name } { p.age.toString() }
; + } + + val people = new AddressBook( + Person("Tom", 20), + Person("Bob", 22), + Person("James", 19)); + + val x2 = + + + { people.toXHTML } + + ; + + try { + val y1: scala.xml.Elem = Serialize.read(Serialize.write(x1)); + val y2: scala.xml.Elem = Serialize.read(Serialize.write(x2)); + + EqualityTest.check(x1, y1); + EqualityTest.check(x2, y2); + } + catch { + case e: Exception => + System.out.println("Error in Test4_xml: " + e); + } +} + +//############################################################################ +// Test user-defined classes WITHOUT nesting + +[serializable] +class Person(_name: String) { + private var name = _name; + override def toString() = name; + override def equals(that: Any): Boolean = + that.isInstanceOf[Person] && + (name == that.asInstanceOf[Person].name); +} + +[serializable] +class Employee(_name: String) { + private var name = _name; + override def toString() = name; +} +[serializable] +object bob extends Employee("Bob"); + +object Test5 { + val x1 = new Person("Tim"); + val x2 = bob; + + try { + val y1: Person = Serialize.read(Serialize.write(x1)); + val y2: Employee = Serialize.read(Serialize.write(x2)); + + EqualityTest.check(x1, y1); + EqualityTest.check(x2, y2); + } + catch { + case e: Exception => + System.out.println("Error in Test5: " + e); + } +} + +//############################################################################ +// Test user-defined classes WITH nesting + +[serializable] +object Test6 { + [serializable] + object bill extends Employee("Bill") { + val x = paul; + } + [serializable] + object paul extends Person("Paul") { + val x = 4; // bill; => StackOverflowException !!! + } + val x1 = new Person("John"); + val x2 = bill; + val x3 = paul; + + try { + val y1: Person = Serialize.read(Serialize.write(x1)); + val y2: Employee = Serialize.read(Serialize.write(x2)); + val y3: Person = Serialize.read(Serialize.write(x3)); + + EqualityTest.check(x1, y1); + EqualityTest.check(x2, y2); + EqualityTest.check(x3, y3); + } + catch { + case e: Exception => + System.out.println("Error in Test6: " + e); + } +} + +//############################################################################ +// Test code + +object Test { + def main(args: Array[String]): Unit = { + Test1_scala; + Test2_immutable; + Test3_mutable; + Test4_xml; + Test5; + Test6 + } +} + +//############################################################################ + diff --git a/test-nsc/files/pos/expressions-current.scala b/test-nsc/files/pos/expressions-current.scala index b343dbf68b..f3def81931 100755 --- a/test-nsc/files/pos/expressions-current.scala +++ b/test-nsc/files/pos/expressions-current.scala @@ -39,9 +39,10 @@ abstract class Lang2 extends Lang { class Show2(result: Ref[String]): visitor extends Visitor2 { def caseNum(n: int) = result.elem = n.toString(); def casePlus(l: Exp, r: Exp) = - result.elem = + result.elem = ( "(" + { l.visit(this); result.elem } + - "+" + { r.visit(this); result.elem }+ ")"; + "+" + { r.visit(this); result.elem }+ ")" + ) } } diff --git a/test-nsc/files/pos/traits.scala b/test-nsc/files/pos/traits.scala index 5fdf4b342e..605395c5a6 100755 --- a/test-nsc/files/pos/traits.scala +++ b/test-nsc/files/pos/traits.scala @@ -19,10 +19,11 @@ object Test { } trait BorderedColoredShape extends Shape with Bordered with Colored { override def equals(other: Any) = other match { - case that: BorderedColoredShape => + case that: BorderedColoredShape => ( super.equals(that) && super[Bordered].equals(that) && super[Colored].equals(that) + ) case _ => false } } diff --git a/test-nsc/files/run/Course-2002-02.scala b/test-nsc/files/run/Course-2002-02.scala index 00f43501f4..a86ffebaf5 100755 --- a/test-nsc/files/run/Course-2002-02.scala +++ b/test-nsc/files/run/Course-2002-02.scala @@ -17,16 +17,13 @@ object M0 { object M1 { def cube(x: Int): Double = x * x * x; - def sumInts(a: Int, b: Int): Double = - if (a > b) 0 + def sumInts(a: Int, b: Int): Double = if (a > b) 0 else a + sumInts(a + 1, b); - def sumCubes(a: Int, b: Int): Double = - if (a > b) 0 + def sumCubes(a: Int, b: Int): Double = if (a > b) 0 else cube(a) + sumCubes(a + 1, b); - def sumReciprocals(a: Int, b: Int): Double = - if (a > b) 0 + def sumReciprocals(a: Int, b: Int): Double = if (a > b) 0 else 1.0/a + sumReciprocals(a + 1, b); def sumPi(n: Int): Double = { @@ -198,17 +195,14 @@ object M8 { //############################################################################ object M9 { - def accumulate[t](combiner: (t, t) => t, nullValue: t, f: Int => t, next: Int => Int) - (a: Int, b: Int): t = - if (a > b) nullValue + def accumulate[t](combiner: (t, t) => t, nullValue: t, f: Int => t, + next: Int => Int)(a: Int, b: Int): t = if (a > b) nullValue else combiner(f(a), accumulate(combiner, nullValue, f, next)(next(a), b)); def inc(x: Int) = x + 1; - def sum(f: Int => Double) = - accumulate((x: Double, y: Double) => x + y, 0d, f, inc); - def product(f: Int => Double) = - accumulate((x: Double, y: Double) => x * y, 1d, f, inc); + def sum(f: Int => Double) = accumulate((x: Double, y: Double) => x + y, 0d, f, inc); + def product(f: Int => Double) = accumulate((x: Double, y: Double) => x * y, 1d, f, inc); def sumInts = sum(x => x); def sumCubes = sum(x => x * x * x); @@ -323,8 +317,7 @@ object MC { //############################################################################ object MD { - def reduce(op: (Double,Double) => Double, zero:Double) - (f: Int => Double)(a: Int,b: Int): Double = { + def reduce(op: (Double,Double) => Double, zero:Double)(f: Int => Double)(a: Int,b: Int): Double = { def iter(a: Int, result: Double): Double = { if (a > b) result else iter(a + 1, op(result, f(a))) @@ -367,8 +360,7 @@ object MD { //############################################################################ object ME { - def reduce(op: (Double,Double) => Double, zero:Double) - (f: Int => Double)(a: Int,b: Int): Double = { + def reduce(op: (Double,Double) => Double, zero:Double)(f: Int => Double)(a: Int,b: Int): Double = { def iter(a: Int, result: Double): Double = { if (a > b) result else iter(a + 1, op(result, f(a))) diff --git a/test-nsc/files/run/Course-2002-05.scala b/test-nsc/files/run/Course-2002-05.scala index 6f40f7f99e..c761f88f5d 100755 --- a/test-nsc/files/run/Course-2002-05.scala +++ b/test-nsc/files/run/Course-2002-05.scala @@ -130,14 +130,14 @@ object M3 { else { def isSafe(column: int, placement: Placement): boolean = placement forall { - pos => pos._2 != column - && abs(pos._2 - column) != row - pos._1 + pos => (pos._2 != column && + abs(pos._2 - column) != row - pos._1) } def adjoinRow(placement: Placement): List[Placement] = range(1, n) - filter (column => isSafe(column, placement)) - map (column => Pair(row, column) :: placement); + .filter (column => isSafe(column, placement)) + .map (column => Pair(row, column) :: placement); placeQueens(row - 1) flatMap adjoinRow } @@ -173,10 +173,10 @@ object M4 { List(List()) else { def isSafe(col: Int, p: Placement, delta: Int): Boolean = - p.isEmpty || - (col != p.head && - abs(col - p.head) != delta && - isSafe(col, p.tail, delta + 1)); + (p.isEmpty || + (col != p.head && + abs(col - p.head) != delta && + isSafe(col, p.tail, delta + 1))); for ( val placement <- placeQueens(row - 1); diff --git a/test-nsc/files/run/Course-2002-06.scala b/test-nsc/files/run/Course-2002-06.scala index 8ce003333f..76bd581a73 100644 --- a/test-nsc/files/run/Course-2002-06.scala +++ b/test-nsc/files/run/Course-2002-06.scala @@ -155,8 +155,7 @@ object M0 { /** Transform the frame in which the painter is to be drawn, hence * changing the appearance of the painter */ - def transformPainter(origin: Vector, newX: Vector, newY: Vector) - (painter: Painter): Painter = { + def transformPainter(origin: Vector, newX: Vector, newY: Vector)(painter: Painter): Painter = { frame: Frame => { val newOrigin = frame.coordMap(origin); val newFrame = new Frame(newOrigin, diff --git a/test-nsc/files/run/Course-2002-08.scala b/test-nsc/files/run/Course-2002-08.scala index 7e06001732..a0d679d3d1 100755 --- a/test-nsc/files/run/Course-2002-08.scala +++ b/test-nsc/files/run/Course-2002-08.scala @@ -342,8 +342,8 @@ object M5 { halfAdder(ain, bin, sout, cout); def result = - (if (sout.getSignal) 1 else 0) + - (if (cout.getSignal) 2 else 0); + ((if (sout.getSignal) 1 else 0) + + (if (cout.getSignal) 2 else 0)); def test(a: Int, b: Int) = { ain setSignal (if (a == 0) false else true); @@ -372,8 +372,8 @@ object M5 { fullAdder(ain, bin, cin, sout, cout); def result = - (if (sout.getSignal) 1 else 0) + - (if (cout.getSignal) 2 else 0); + ((if (sout.getSignal) 1 else 0) + + (if (cout.getSignal) 2 else 0)); def test(a: Int, b: Int, c: Int) = { ain setSignal (if (a == 0) false else true); -- cgit v1.2.3