summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@typesafe.com>2012-11-21 13:27:43 -0800
committerAdriaan Moors <adriaan.moors@typesafe.com>2012-11-21 13:27:43 -0800
commit889ceade520ae5d2d1485edf2826696fa91a0e91 (patch)
treed49195c14a87d4bfe46a3e6f438757148a2efcab /test
parent1cfb36317834f9bca0c3ce94e92590f7b4ace3b7 (diff)
parentbc3dda2b0222d3b7cf3db491728b98f9b6110856 (diff)
downloadscala-889ceade520ae5d2d1485edf2826696fa91a0e91.tar.gz
scala-889ceade520ae5d2d1485edf2826696fa91a0e91.tar.bz2
scala-889ceade520ae5d2d1485edf2826696fa91a0e91.zip
Merge pull request #1570 from retronym/ticket/6448
SI-6448 Collecting the spoils of PartialFun#runWith
Diffstat (limited to 'test')
-rw-r--r--test/files/run/t6448.check32
-rw-r--r--test/files/run/t6448.scala61
2 files changed, 93 insertions, 0 deletions
diff --git a/test/files/run/t6448.check b/test/files/run/t6448.check
new file mode 100644
index 0000000000..9401568319
--- /dev/null
+++ b/test/files/run/t6448.check
@@ -0,0 +1,32 @@
+
+=List.collect=
+f(1)
+f(2)
+List(1)
+
+=List.collectFirst=
+f(1)
+Some(1)
+
+=Option.collect=
+f(1)
+Some(1)
+
+=Option.collect=
+f(2)
+None
+
+=Stream.collect=
+f(1)
+f(2)
+List(1)
+
+=Stream.collectFirst=
+f(1)
+Some(1)
+
+=ParVector.collect=
+(ParVector(1),2)
+
+=ParArray.collect=
+(ParArray(1),2)
diff --git a/test/files/run/t6448.scala b/test/files/run/t6448.scala
new file mode 100644
index 0000000000..4d1528e500
--- /dev/null
+++ b/test/files/run/t6448.scala
@@ -0,0 +1,61 @@
+// Tests to show that various `collect` functions avoid calling
+// both `PartialFunction#isDefinedAt` and `PartialFunction#apply`.
+//
+object Test {
+ def f(i: Int) = { println("f(" + i + ")"); true }
+ class Counter {
+ var count = 0
+ def apply(i: Int) = synchronized {count += 1; true}
+ }
+
+ def testing(label: String)(body: => Any) {
+ println(s"\n=$label=")
+ println(body)
+ }
+
+ def main(args: Array[String]) {
+ testing("List.collect")(List(1, 2) collect { case x if f(x) && x < 2 => x})
+ testing("List.collectFirst")(List(1, 2) collectFirst { case x if f(x) && x < 2 => x})
+ testing("Option.collect")(Some(1) collect { case x if f(x) && x < 2 => x})
+ testing("Option.collect")(Some(2) collect { case x if f(x) && x < 2 => x})
+ testing("Stream.collect")((Stream(1, 2).collect { case x if f(x) && x < 2 => x}).toList)
+ testing("Stream.collectFirst")(Stream.continually(1) collectFirst { case x if f(x) && x < 2 => x})
+
+ import collection.parallel.ParIterable
+ import collection.parallel.immutable.ParVector
+ import collection.parallel.mutable.ParArray
+ testing("ParVector.collect") {
+ val counter = new Counter()
+ (ParVector(1, 2) collect { case x if counter(x) && x < 2 => x}, counter.synchronized(counter.count))
+ }
+
+ testing("ParArray.collect") {
+ val counter = new Counter()
+ (ParArray(1, 2) collect { case x if counter(x) && x < 2 => x}, counter.synchronized(counter.count))
+ }
+
+ object PendingTests {
+ testing("Iterator.collect")((Iterator(1, 2) collect { case x if f(x) && x < 2 => x}).toList)
+
+ testing("List.view.collect")((List(1, 2).view collect { case x if f(x) && x < 2 => x}).force)
+
+ // This would do the trick in Future.collect, but I haven't added this yet as there is a tradeoff
+ // with extra allocations to consider.
+ //
+ // pf.lift(v) match {
+ // case Some(x) => p success x
+ // case None => fail(v)
+ // }
+ testing("Future.collect") {
+ import concurrent.ExecutionContext.Implicits.global
+ import concurrent.Await
+ import concurrent.duration.Duration
+ val result = concurrent.future(1) collect { case x if f(x) => x}
+ Await.result(result, Duration.Inf)
+ }
+
+ // TODO Future.{onSuccess, onFailure, recoverWith, andThen}
+ }
+
+ }
+}