summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPaolo Giarrusso <p.giarrusso@gmail.com>2012-08-22 16:39:40 +0200
committerPaolo Giarrusso <p.giarrusso@gmail.com>2012-08-22 20:39:38 +0200
commit33cdba506b959b37bc46d73957f7f5f796416bce (patch)
treefdcd4137f6c9ad9cb3c861b3f13857ea7c29403c /test
parent15ed4f044f3e518f627b918df77a961bcef2f55f (diff)
downloadscala-33cdba506b959b37bc46d73957f7f5f796416bce.tar.gz
scala-33cdba506b959b37bc46d73957f7f5f796416bce.tar.bz2
scala-33cdba506b959b37bc46d73957f7f5f796416bce.zip
Improve test for Stream.withFilter.{map,flatMap}
Test a wider range of functionality.
Diffstat (limited to 'test')
-rw-r--r--test/files/run/stream-stack-overflow-filter-map.scala50
1 files changed, 40 insertions, 10 deletions
diff --git a/test/files/run/stream-stack-overflow-filter-map.scala b/test/files/run/stream-stack-overflow-filter-map.scala
index 8210518d95..c6e8781d0a 100644
--- a/test/files/run/stream-stack-overflow-filter-map.scala
+++ b/test/files/run/stream-stack-overflow-filter-map.scala
@@ -1,12 +1,42 @@
+import collection.generic.{FilterMonadic, CanBuildFrom}
+
object Test extends App {
- 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)
-
- //This risks causing 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)
+ 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) {
+ testStream((1 to j).toStream)
+ }
}