@@ -8,12 +8,79 @@ import org.junit.Assert._
88
99import scala .annotation .unused
1010import scala .collection .mutable .{Builder , ListBuffer }
11- import scala .tools .testkit .AssertUtil
11+ import scala .tools .testkit .{ AssertUtil , ReflectUtil }
1212import scala .util .Try
1313
1414@ RunWith (classOf [JUnit4 ])
1515class LazyListTest {
1616
17+ @ Test
18+ def serialization (): Unit = {
19+ import java .io ._
20+
21+ def serialize (obj : AnyRef ): Array [Byte ] = {
22+ val buffer = new ByteArrayOutputStream
23+ val out = new ObjectOutputStream (buffer)
24+ out.writeObject(obj)
25+ buffer.toByteArray
26+ }
27+
28+ def deserialize (a : Array [Byte ]): AnyRef = {
29+ val in = new ObjectInputStream (new ByteArrayInputStream (a))
30+ in.readObject
31+ }
32+
33+ def serializeDeserialize [T <: AnyRef ](obj : T ) = deserialize(serialize(obj)).asInstanceOf [T ]
34+
35+ val l = LazyList .from(10 )
36+
37+ val ld1 = serializeDeserialize(l)
38+ assertEquals(l.take(10 ).toList, ld1.take(10 ).toList)
39+
40+ l.tail.head
41+ val ld2 = serializeDeserialize(l)
42+ assertEquals(l.take(10 ).toList, ld2.take(10 ).toList)
43+
44+ LazyListTest .serializationForceCount = 0
45+ val u = LazyList .from(10 ).map(x => { LazyListTest .serializationForceCount += 1 ; x })
46+
47+ @ unused def printDiff (): Unit = {
48+ val a = serialize(u)
49+ ReflectUtil .getFieldAccessible[LazyList [_]](" scala$collection$immutable$LazyList$$stateEvaluated" ).setBoolean(u, true )
50+ val b = serialize(u)
51+ val i = a.zip(b).indexWhere(p => p._1 != p._2)
52+ println(" difference: " )
53+ println(s " val from = ${a.slice(i - 10 , i + 10 ).mkString(" List[Byte](" , " , " , " )" )}" )
54+ println(s " val to = ${b.slice(i - 10 , i + 10 ).mkString(" List[Byte](" , " , " , " )" )}" )
55+ }
56+
57+ // to update this test, comment-out `LazyList.writeReplace` and run `printDiff`
58+ // printDiff()
59+
60+ val from = List [Byte ](83 , 116 , 97 , 116 , 101 , 59 , 120 , 112 , 0 , 0 , 0 , 115 , 114 , 0 , 33 , 106 , 97 , 118 , 97 , 46 )
61+ val to = List [Byte ](83 , 116 , 97 , 116 , 101 , 59 , 120 , 112 , 0 , 0 , 1 , 115 , 114 , 0 , 33 , 106 , 97 , 118 , 97 , 46 )
62+
63+ assertEquals(LazyListTest .serializationForceCount, 0 )
64+
65+ u.head
66+ assertEquals(LazyListTest .serializationForceCount, 1 )
67+
68+ val data = serialize(u)
69+ var i = data.indexOfSlice(from)
70+ to.foreach(x => {data(i) = x; i += 1 })
71+
72+ val ud1 = deserialize(data).asInstanceOf [LazyList [Int ]]
73+
74+ // this check failed before scala/scala#10118, deserialization triggered evaluation
75+ assertEquals(LazyListTest .serializationForceCount, 1 )
76+
77+ ud1.tail.head
78+ assertEquals(LazyListTest .serializationForceCount, 2 )
79+
80+ u.tail.head
81+ assertEquals(LazyListTest .serializationForceCount, 3 )
82+ }
83+
1784 @ Test
1885 def t6727_and_t6440_and_8627 (): Unit = {
1986 assertTrue(LazyList .continually(()).filter(_ => true ).take(2 ) == Seq ((), ()))
@@ -378,3 +445,7 @@ class LazyListTest {
378445 assertEquals(1 , count)
379446 }
380447}
448+
449+ object LazyListTest {
450+ var serializationForceCount = 0
451+ }
0 commit comments