|
| 1 | +import collection.generic.{FilterMonadic, CanBuildFrom} |
| 2 | + |
1 | 3 | object Test extends App {
|
2 |
| - val resFMap1 = (1 to 10000).toStream filter (_ => false) flatMap (Seq(_)) |
3 |
| - val resMap1 = (1 to 10000).toStream filter (_ => false) map (_ + 1) |
4 |
| - assert(resMap1.isEmpty) |
5 |
| - assert(resFMap1.isEmpty) |
6 |
| - |
7 |
| - //This risks causing a stack overflow |
8 |
| - val resFMap2 = (1 to 10000).toStream withFilter (_ => false) flatMap (Seq(_)) |
9 |
| - val resMap2 = (1 to 10000).toStream withFilter (_ => false) map (_ + 1) |
10 |
| - assert(resMap1 == resMap2) |
11 |
| - assert(resFMap1 == resFMap2) |
| 4 | + def mapSucc[Repr, That](s: FilterMonadic[Int, Repr])(implicit cbf: CanBuildFrom[Repr, Int, That]) = s map (_ + 1) |
| 5 | + def flatMapId[T, Repr, That](s: FilterMonadic[T, Repr])(implicit cbf: CanBuildFrom[Repr, T, That]) = s flatMap (Seq(_)) |
| 6 | + |
| 7 | + def testStreamPred(s: Stream[Int])(p: Int => Boolean) { |
| 8 | + val res1 = s withFilter p |
| 9 | + val res2 = s filter p |
| 10 | + |
| 11 | + val expected = s.toSeq filter p |
| 12 | + |
| 13 | + val fMapped1 = flatMapId(res1) |
| 14 | + val fMapped2 = flatMapId(res2) |
| 15 | + assert(fMapped1 == fMapped2) |
| 16 | + assert(fMapped1.toSeq == expected) |
| 17 | + |
| 18 | + val mapped1 = mapSucc(res1) |
| 19 | + val mapped2 = mapSucc(res2) |
| 20 | + assert(mapped1 == mapped2) |
| 21 | + assert(mapped1.toSeq == (expected map (_ + 1))) |
| 22 | + |
| 23 | + assert((res1 map identity).toSeq == res2.toSeq) |
| 24 | + } |
| 25 | + |
| 26 | + def testStream(s: Stream[Int]) { |
| 27 | + testStreamPred(s)(_ => false) |
| 28 | + testStreamPred(s)(_ => true) |
| 29 | + testStreamPred(s)(_ % 2 == 0) |
| 30 | + testStreamPred(s)(_ % 3 == 0) |
| 31 | + } |
| 32 | + |
| 33 | + //Reduced version of the test case - either invocation used to cause a stack |
| 34 | + //overflow before commit 80b3f433e5536d086806fa108ccdfacf10719cc2. |
| 35 | + val resFMap = (1 to 10000).toStream withFilter (_ => false) flatMap (Seq(_)) |
| 36 | + val resMap = (1 to 10000).toStream withFilter (_ => false) map (_ + 1) |
| 37 | + |
| 38 | + //Complete test case for withFilter + map/flatMap, as requested by @axel22. |
| 39 | + for (j <- (0 to 3) :+ 10000) { |
| 40 | + testStream((1 to j).toStream) |
| 41 | + } |
12 | 42 | }
|
0 commit comments