summaryrefslogtreecommitdiff
path: root/test/files/jvm/serialization.scala
blob: 2819295f9e145ae6c5006ee833d7f45ad1049fea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
  }
}

//############################################################################