summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJosh Suereth <Joshua.Suereth@gmail.com>2012-08-23 00:52:16 -0700
committerJosh Suereth <Joshua.Suereth@gmail.com>2012-08-23 00:52:16 -0700
commitde87eec36407316873de3eba94d620553ed1ecb8 (patch)
treed2162dee1e45c6274e56623033b7dfc89f9c5a5a /test
parent9a18152c5db3d9246e01c53b8e7eae24cbec0872 (diff)
parente3bb6bac5bf94d81b0c1d97eebce7f5952bbbc61 (diff)
downloadscala-de87eec36407316873de3eba94d620553ed1ecb8.tar.gz
scala-de87eec36407316873de3eba94d620553ed1ecb8.tar.bz2
scala-de87eec36407316873de3eba94d620553ed1ecb8.zip
Merge pull request #1181 from Blaisorblade/topic/2.9.x-stream-const-space
Backport "Make Stream.withFilter.{map,flatMap} run in constant stack space" against 2.9.x
Diffstat (limited to 'test')
-rw-r--r--test/files/run/stream-stack-overflow-filter-map.scala44
1 files changed, 44 insertions, 0 deletions
diff --git a/test/files/run/stream-stack-overflow-filter-map.scala b/test/files/run/stream-stack-overflow-filter-map.scala
new file mode 100644
index 0000000000..f3a9dd49cb
--- /dev/null
+++ b/test/files/run/stream-stack-overflow-filter-map.scala
@@ -0,0 +1,44 @@
+import collection.generic.{FilterMonadic, CanBuildFrom}
+
+object Test extends App {
+ def mapSucc[Repr, That](s: FilterMonadic[Int, Repr])(implicit cbf: CanBuildFrom[Repr, Int, That]) = s map (_ + 1)
+ def flatMapId[T, Repr, That](s: FilterMonadic[T, Repr])(implicit cbf: CanBuildFrom[Repr, T, That]) = s flatMap (Seq(_))
+
+ def testStreamPred(s: Stream[Int])(p: Int => Boolean) {
+ val res1 = s withFilter p
+ val res2 = s filter p
+
+ val expected = s.toSeq filter p
+
+ val fMapped1 = flatMapId(res1)
+ val fMapped2 = flatMapId(res2)
+ assert(fMapped1 == fMapped2)
+ assert(fMapped1.toSeq == expected)
+
+ val mapped1 = mapSucc(res1)
+ val mapped2 = mapSucc(res2)
+ assert(mapped1 == mapped2)
+ assert(mapped1.toSeq == (expected map (_ + 1)))
+
+ assert((res1 map identity).toSeq == res2.toSeq)
+ }
+
+ def testStream(s: Stream[Int]) {
+ testStreamPred(s)(_ => false)
+ testStreamPred(s)(_ => true)
+ testStreamPred(s)(_ % 2 == 0)
+ testStreamPred(s)(_ % 3 == 0)
+ }
+
+ //Reduced version of the test case - either invocation used to cause a stack
+ //overflow before commit 80b3f433e5536d086806fa108ccdfacf10719cc2.
+ val resFMap = (1 to 10000).toStream withFilter (_ => false) flatMap (Seq(_))
+ val resMap = (1 to 10000).toStream withFilter (_ => false) map (_ + 1)
+
+ //Complete test case for withFilter + map/flatMap, as requested by @axel22.
+ for (j <- (0 to 3) :+ 10000) {
+ val stream = (1 to j).toStream
+ assert(stream.toSeq == (1 to j).toSeq)
+ testStream(stream)
+ }
+}