Skip to content

Commit d30098c

Browse files
committed
Merge pull request scala#4362 from ms-tg/SI-9134
SI-9134 Verify Stream#withFilter#map lazy in tail
2 parents 6a6b95d + 05f46dc commit d30098c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/junit/scala/collection/immutable/StreamTest.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,29 @@ class StreamTest {
8080
assertTrue( wf.map(identity).length == 5 ) // success instead of NPE
8181
}
8282

83+
/** Test helper to verify that the given Stream operation is properly lazy in the tail */
84+
def assertStreamOpLazyInTail(op: (=> Stream[Int]) => Stream[Int], expectedEvaluated: List[Int]): Unit = {
85+
// mutable state to record every strict evaluation
86+
var evaluated: List[Int] = Nil
87+
88+
def trackEffectsOnNaturals: Stream[Int] = {
89+
def loop(i: Int): Stream[Int] = { evaluated ++= List(i); i #:: loop(i + 1) }
90+
loop(1)
91+
}
92+
93+
// call op on a stream which records every strict evaluation
94+
val result = op(trackEffectsOnNaturals)
95+
96+
assertTrue( evaluated == expectedEvaluated )
97+
}
98+
99+
@Test // SI-9134
100+
def filter_map_properly_lazy_in_tail: Unit = {
101+
assertStreamOpLazyInTail(_.filter(_ % 2 == 0).map(identity), List(1, 2))
102+
}
103+
104+
@Test // SI-9134
105+
def withFilter_map_properly_lazy_in_tail: Unit = {
106+
assertStreamOpLazyInTail(_.withFilter(_ % 2 == 0).map(identity), List(1, 2))
107+
}
83108
}

0 commit comments

Comments
 (0)