summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPaolo Giarrusso <p.giarrusso@gmail.com>2012-08-20 19:34:43 +0200
committerPaolo Giarrusso <p.giarrusso@gmail.com>2012-08-22 20:39:27 +0200
commit53130d21434a67d6d9b443d531578a04eb9e510f (patch)
treeb4f39819fbf5b9b98bb28f837988cd2e9a9ebce8 /test
parent8e7f44c89b2e1c45c6273a4753f28e8dd0bb21b0 (diff)
downloadscala-53130d21434a67d6d9b443d531578a04eb9e510f.tar.gz
scala-53130d21434a67d6d9b443d531578a04eb9e510f.tar.bz2
scala-53130d21434a67d6d9b443d531578a04eb9e510f.zip
Make Stream.withFilter.{map,flatMap} run in constant stack space
The included test currently fails because `map` and `flatMap` do not run in constant stack space on a stream returned by `Stream.withFilter`, as I reported here: https://groups.google.com/d/msg/scala-language/WqJR38REXnk/saaSiDdmyqoJ Fix the problem and add a simple testcase. Note that the stack space consumed when producing an element of this stream is proportional to the number of elements failing the test before the next success. The stack space consumed to produce the stream itself is the space needed to produce the first element, that is, is proportional to the number of failures before the first success.
Diffstat (limited to 'test')
-rw-r--r--test/files/run/stream-stack-overflow-filter-map.check2
-rw-r--r--test/files/run/stream-stack-overflow-filter-map.scala14
2 files changed, 16 insertions, 0 deletions
diff --git a/test/files/run/stream-stack-overflow-filter-map.check b/test/files/run/stream-stack-overflow-filter-map.check
new file mode 100644
index 0000000000..dbf856668b
--- /dev/null
+++ b/test/files/run/stream-stack-overflow-filter-map.check
@@ -0,0 +1,2 @@
+Stream()
+Stream()
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..e2bd47bfca
--- /dev/null
+++ b/test/files/run/stream-stack-overflow-filter-map.scala
@@ -0,0 +1,14 @@
+object Test extends App {
+ //This runs fine.
+ val resFMap1 = (1 to 10000).toStream filter (_ => false) flatMap (Seq(_))
+ val resMap1 = (1 to 10000).toStream filter (_ => false) map (_ + 1)
+ assert(resMap1.isEmpty)
+ assert(resFMap1.isEmpty)
+ println(resMap1)
+ println(resFMap1)
+ //This will cause a stack overflow
+ val resFMap2 = (1 to 10000).toStream withFilter (_ => false) flatMap (Seq(_))
+ val resMap2 = (1 to 10000).toStream withFilter (_ => false) map (_ + 1)
+ assert(resMap1 == resMap2)
+ assert(resFMap1 == resFMap2)
+}