Skip to content

Commit 185066a

Browse files
committed
scalafmt
1 parent 590c6f1 commit 185066a

File tree

2 files changed

+59
-54
lines changed

2 files changed

+59
-54
lines changed

compat/src/main/scala-2.11_2.12/scala/collection/compat/immutable/LazyList.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,11 +1508,11 @@ object LazyList extends SeqFactory[LazyList] {
15081508

15091509
private[this] def readObject(in: ObjectInputStream): Unit = {
15101510
in.defaultReadObject()
1511-
val init = new mutable.ListBuffer[A]
1511+
val init = new mutable.ListBuffer[A]
15121512
var initRead = false
15131513
while (!initRead) in.readObject match {
15141514
case SerializeEnd => initRead = true
1515-
case a => init += a.asInstanceOf[A]
1515+
case a => init += a.asInstanceOf[A]
15161516
}
15171517
val tail = in.readObject().asInstanceOf[LazyList[A]]
15181518
// scala/scala#10118: caution that no code path can evaluate `tail.state`

compat/src/test/scala/test/scala/collection/LazyListTest.scala

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,73 +22,78 @@ import scala.collection.mutable.{Builder, ListBuffer}
2222
import scala.util.Try
2323

2424
class LazyListTest {
25-
25+
2626
@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._
3630

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+
}
4137

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+
}
4342

44-
val l = LazyList.from(10)
43+
def serializeDeserialize[T <: AnyRef](obj: T) = deserialize(serialize(obj)).asInstanceOf[T]
4544

46-
val ld1 = serializeDeserialize(l)
47-
assertEquals(l.take(10).toList, ld1.take(10).toList)
45+
val l = LazyList.from(10)
4846

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)
6549

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)
6853

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 })
7156

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+
}
7368

74-
u.head
75-
assertEquals(LazyListTest.serializationForceCount, 1)
69+
// to update this test, comment-out `LazyList.writeReplace` and run `printDiff`
70+
// printDiff()
7671

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)
8076

81-
val ud1 = deserialize(data).asInstanceOf[LazyList[Int]]
77+
assertEquals(LazyListTest.serializationForceCount, 0)
8278

83-
// this check failed before scala/scala#10118, deserialization triggered evaluation
84-
assertEquals(LazyListTest.serializationForceCount, 1)
79+
u.head
80+
assertEquals(LazyListTest.serializationForceCount, 1)
8581

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 })
8885

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+
}
9297

9398
@Test
9499
def t6727_and_t6440_and_8627(): Unit = {

0 commit comments

Comments
 (0)