@@ -22,73 +22,78 @@ import scala.collection.mutable.{Builder, ListBuffer}
22
22
import scala .util .Try
23
23
24
24
class LazyListTest {
25
-
25
+
26
26
@ Test
27
- def serialization (): Unit = if (scala.util.Properties .releaseVersion.exists(_.startsWith(" 2.12" ))) {
28
- import java .io ._
29
-
30
- def serialize (obj : AnyRef ): Array [Byte ] = {
31
- val buffer = new ByteArrayOutputStream
32
- val out = new ObjectOutputStream (buffer)
33
- out.writeObject(obj)
34
- buffer.toByteArray
35
- }
27
+ def serialization (): Unit =
28
+ if (scala.util.Properties .releaseVersion.exists(_.startsWith(" 2.12" ))) {
29
+ import java .io ._
36
30
37
- def deserialize (a : Array [Byte ]): AnyRef = {
38
- val in = new ObjectInputStream (new ByteArrayInputStream (a))
39
- in.readObject
40
- }
31
+ def serialize (obj : AnyRef ): Array [Byte ] = {
32
+ val buffer = new ByteArrayOutputStream
33
+ val out = new ObjectOutputStream (buffer)
34
+ out.writeObject(obj)
35
+ buffer.toByteArray
36
+ }
41
37
42
- def serializeDeserialize [T <: AnyRef ](obj : T ) = deserialize(serialize(obj)).asInstanceOf [T ]
38
+ def deserialize (a : Array [Byte ]): AnyRef = {
39
+ val in = new ObjectInputStream (new ByteArrayInputStream (a))
40
+ in.readObject
41
+ }
43
42
44
- val l = LazyList .from( 10 )
43
+ def serializeDeserialize [ T <: AnyRef ]( obj : T ) = deserialize(serialize(obj)). asInstanceOf [ T ]
45
44
46
- val ld1 = serializeDeserialize(l)
47
- assertEquals(l.take(10 ).toList, ld1.take(10 ).toList)
45
+ val l = LazyList .from(10 )
48
46
49
- l.tail.head
50
- val ld2 = serializeDeserialize(l)
51
- assertEquals(l.take(10 ).toList, ld2.take(10 ).toList)
52
-
53
- LazyListTest .serializationForceCount = 0
54
- val u = LazyList .from(10 ).map(x => { LazyListTest .serializationForceCount += 1 ; x })
55
-
56
- def printDiff (): Unit = {
57
- val a = serialize(u)
58
- classOf [LazyList [_]].getDeclaredField(" scala$collection$compat$immutable$LazyList$$stateEvaluated" ).setBoolean(u, true )
59
- val b = serialize(u)
60
- val i = a.zip(b).indexWhere(p => p._1 != p._2)
61
- println(" difference: " )
62
- println(s " val from = ${a.slice(i - 10 , i + 10 ).mkString(" List[Byte](" , " , " , " )" )}" )
63
- println(s " val to = ${b.slice(i - 10 , i + 10 ).mkString(" List[Byte](" , " , " , " )" )}" )
64
- }
47
+ val ld1 = serializeDeserialize(l)
48
+ assertEquals(l.take(10 ).toList, ld1.take(10 ).toList)
65
49
66
- // to update this test, comment-out `LazyList.writeReplace` and run `printDiff`
67
- // printDiff()
50
+ l.tail.head
51
+ val ld2 = serializeDeserialize(l)
52
+ assertEquals(l.take(10 ).toList, ld2.take(10 ).toList)
68
53
69
- val from = List [ Byte ]( 83 , 116 , 97 , 116 , 101 , 59 , 120 , 112 , 0 , 0 , 0 , 115 , 114 , 0 , 33 , 106 , 97 , 118 , 97 , 46 )
70
- val to = List [ Byte ]( 83 , 116 , 97 , 116 , 101 , 59 , 120 , 112 , 0 , 0 , 1 , 115 , 114 , 0 , 33 , 106 , 97 , 118 , 97 , 46 )
54
+ LazyListTest .serializationForceCount = 0
55
+ val u = LazyList .from( 10 ).map(x => { LazyListTest .serializationForceCount += 1 ; x } )
71
56
72
- assertEquals(LazyListTest .serializationForceCount, 0 )
57
+ def printDiff (): Unit = {
58
+ val a = serialize(u)
59
+ classOf [LazyList [_]]
60
+ .getDeclaredField(" scala$collection$compat$immutable$LazyList$$stateEvaluated" )
61
+ .setBoolean(u, true )
62
+ val b = serialize(u)
63
+ val i = a.zip(b).indexWhere(p => p._1 != p._2)
64
+ println(" difference: " )
65
+ println(s " val from = ${a.slice(i - 10 , i + 10 ).mkString(" List[Byte](" , " , " , " )" )}" )
66
+ println(s " val to = ${b.slice(i - 10 , i + 10 ).mkString(" List[Byte](" , " , " , " )" )}" )
67
+ }
73
68
74
- u.head
75
- assertEquals( LazyListTest .serializationForceCount, 1 )
69
+ // to update this test, comment-out `LazyList.writeReplace` and run `printDiff`
70
+ // printDiff( )
76
71
77
- val data = serialize(u)
78
- var i = data.indexOfSlice(from)
79
- to.foreach(x => {data(i) = x; i += 1 })
72
+ val from = List [Byte ](83 , 116 , 97 , 116 , 101 , 59 , 120 , 112 , 0 , 0 , 0 , 115 , 114 , 0 , 33 , 106 , 97 ,
73
+ 118 , 97 , 46 )
74
+ val to = List [Byte ](83 , 116 , 97 , 116 , 101 , 59 , 120 , 112 , 0 , 0 , 1 , 115 , 114 , 0 , 33 , 106 , 97 ,
75
+ 118 , 97 , 46 )
80
76
81
- val ud1 = deserialize(data). asInstanceOf [ LazyList [ Int ]]
77
+ assertEquals( LazyListTest .serializationForceCount, 0 )
82
78
83
- // this check failed before scala/scala#10118, deserialization triggered evaluation
84
- assertEquals(LazyListTest .serializationForceCount, 1 )
79
+ u.head
80
+ assertEquals(LazyListTest .serializationForceCount, 1 )
85
81
86
- ud1.tail.head
87
- assertEquals(LazyListTest .serializationForceCount, 2 )
82
+ val data = serialize(u)
83
+ var i = data.indexOfSlice(from)
84
+ to.foreach(x => { data(i) = x; i += 1 })
88
85
89
- u.tail.head
90
- assertEquals(LazyListTest .serializationForceCount, 3 )
91
- }
86
+ val ud1 = deserialize(data).asInstanceOf [LazyList [Int ]]
87
+
88
+ // this check failed before scala/scala#10118, deserialization triggered evaluation
89
+ assertEquals(LazyListTest .serializationForceCount, 1 )
90
+
91
+ ud1.tail.head
92
+ assertEquals(LazyListTest .serializationForceCount, 2 )
93
+
94
+ u.tail.head
95
+ assertEquals(LazyListTest .serializationForceCount, 3 )
96
+ }
92
97
93
98
@ Test
94
99
def t6727_and_t6440_and_8627 (): Unit = {
0 commit comments