@@ -125,4 +125,86 @@ class LazyListGCTest {
125125 def tapEach_takeRight_headOption_allowsGC (): Unit = {
126126 assertLazyListOpAllowsGC(_.tapEach(_).takeRight(2 ).headOption, _ => ())
127127 }
128+
129+ @ Test
130+ def serialization (): Unit =
131+ if (scala.util.Properties .releaseVersion.exists(_.startsWith(" 2.12" ))) {
132+ import java .io ._
133+
134+ def serialize (obj : AnyRef ): Array [Byte ] = {
135+ val buffer = new ByteArrayOutputStream
136+ val out = new ObjectOutputStream (buffer)
137+ out.writeObject(obj)
138+ buffer.toByteArray
139+ }
140+
141+ def deserialize (a : Array [Byte ]): AnyRef = {
142+ val in = new ObjectInputStream (new ByteArrayInputStream (a))
143+ in.readObject
144+ }
145+
146+ def serializeDeserialize [T <: AnyRef ](obj : T ) = deserialize(serialize(obj)).asInstanceOf [T ]
147+
148+ val l = LazyList .from(10 )
149+
150+ val ld1 = serializeDeserialize(l)
151+ assertEquals(l.take(10 ).toList, ld1.take(10 ).toList)
152+
153+ l.tail.head
154+ val ld2 = serializeDeserialize(l)
155+ assertEquals(l.take(10 ).toList, ld2.take(10 ).toList)
156+
157+ LazyListGCTest .serializationForceCount = 0
158+ val u = LazyList
159+ .from(10 )
160+ .map(x => {
161+ LazyListGCTest .serializationForceCount += 1 ; x
162+ })
163+
164+ def printDiff (): Unit = {
165+ val a = serialize(u)
166+ classOf [LazyList [_]]
167+ .getDeclaredField(" scala$collection$compat$immutable$LazyList$$stateEvaluated" )
168+ .setBoolean(u, true )
169+ val b = serialize(u)
170+ val i = a.zip(b).indexWhere(p => p._1 != p._2)
171+ println(" difference: " )
172+ println(s " val from = ${a.slice(i - 10 , i + 10 ).mkString(" List[Byte](" , " , " , " )" )}" )
173+ println(s " val to = ${b.slice(i - 10 , i + 10 ).mkString(" List[Byte](" , " , " , " )" )}" )
174+ }
175+
176+ // to update this test, comment-out `LazyList.writeReplace` and run `printDiff`
177+ // printDiff()
178+
179+ val from = List [Byte ](83 , 116 , 97 , 116 , 101 , 59 , 120 , 112 , 0 , 0 , 0 , 115 , 114 , 0 , 33 , 106 , 97 ,
180+ 118 , 97 , 46 )
181+ val to = List [Byte ](83 , 116 , 97 , 116 , 101 , 59 , 120 , 112 , 0 , 0 , 1 , 115 , 114 , 0 , 33 , 106 , 97 ,
182+ 118 , 97 , 46 )
183+
184+ assertEquals(LazyListGCTest .serializationForceCount, 0 )
185+
186+ u.head
187+ assertEquals(LazyListGCTest .serializationForceCount, 1 )
188+
189+ val data = serialize(u)
190+ var i = data.indexOfSlice(from)
191+ to.foreach(x => {
192+ data(i) = x; i += 1
193+ })
194+
195+ val ud1 = deserialize(data).asInstanceOf [LazyList [Int ]]
196+
197+ // this check failed before scala/scala#10118, deserialization triggered evaluation
198+ assertEquals(LazyListGCTest .serializationForceCount, 1 )
199+
200+ ud1.tail.head
201+ assertEquals(LazyListGCTest .serializationForceCount, 2 )
202+
203+ u.tail.head
204+ assertEquals(LazyListGCTest .serializationForceCount, 3 )
205+ }
206+ }
207+
208+ object LazyListGCTest {
209+ var serializationForceCount = 0
128210}
0 commit comments