-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
FlowOps: sample operator (#254)
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ox.flow | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class FlowOpsFilterTest extends AnyFlatSpec with Matchers: | ||
behavior of "filter" | ||
|
||
it should "not filter anything from the empty flow" in: | ||
val c = Flow.empty[Int] | ||
val s = c.filter(_ % 2 == 0) | ||
s.runToList() shouldBe List.empty | ||
|
||
it should "filter out everything if no element meets 'f'" in: | ||
val c = Flow.fromValues(1 to 10: _*) | ||
val s = c.filter(_ => false) | ||
s.runToList() shouldBe List.empty | ||
|
||
it should "not filter anything if all the elements meet 'f'" in: | ||
val c = Flow.fromValues(1 to 10: _*) | ||
val s = c.filter(_ => true) | ||
s.runToList() shouldBe (1 to 10) | ||
|
||
it should "filter out elements that don't meet 'f'" in: | ||
val c = Flow.fromValues(1 to 10: _*) | ||
val s = c.filter(_ % 2 == 0) | ||
s.runToList() shouldBe (2 to 10 by 2) | ||
|
||
end FlowOpsFilterTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ox.flow | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class FlowOpsSampleTest extends AnyFlatSpec with Matchers: | ||
behavior of "sample" | ||
|
||
it should "not sample anything from an empty flow" in: | ||
val c = Flow.empty[Int] | ||
val s = c.sample(5) | ||
s.runToList() shouldBe List.empty | ||
|
||
it should "not sample anything when 'n == 0'" in: | ||
val c = Flow.fromValues(1 to 10: _*) | ||
val s = c.sample(0) | ||
s.runToList() shouldBe List.empty | ||
|
||
it should "sample every element of the flow when 'n == 1'" in: | ||
val c = Flow.fromValues(1 to 10: _*) | ||
val n = 1 | ||
val s = c.sample(n) | ||
s.runToList() shouldBe (n to 10 by n) | ||
|
||
it should "sample every nth element of the flow" in: | ||
val c = Flow.fromValues(1 to 10: _*) | ||
val n = 3 | ||
val s = c.sample(n) | ||
s.runToList() shouldBe (n to 10 by n) | ||
|
||
end FlowOpsSampleTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ox.flow | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class FlowOpsScanTest extends AnyFlatSpec with Matchers: | ||
behavior of "scan" | ||
|
||
it should "scan the empty flow" in: | ||
val flow: Flow[Int] = Flow.empty | ||
val scannedFlow = flow.scan(0)((acc, el) => acc + el) | ||
scannedFlow.runToList() shouldBe List(0) | ||
|
||
it should "scan a flow of summed Int" in: | ||
val flow = Flow.fromValues(1 to 10: _*) | ||
val scannedFlow = flow.scan(0)((acc, el) => acc + el) | ||
scannedFlow.runToList() shouldBe List(0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55) | ||
|
||
it should "scan a flow of multiplied Int" in: | ||
val flow = Flow.fromValues(1 to 10: _*) | ||
val scannedFlow = flow.scan(1)((acc, el) => acc * el) | ||
scannedFlow.runToList() shouldBe List(1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800) | ||
|
||
it should "scan a flow of concatenated String" in: | ||
val flow = Flow.fromValues("f", "l", "o", "w") | ||
val scannedFlow = flow.scan("my")((acc, el) => acc + el) | ||
scannedFlow.runToList() shouldBe List("my", "myf", "myfl", "myflo", "myflow") | ||
|
||
end FlowOpsScanTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ox.flow | ||
|
||
import org.scalatest.concurrent.Eventually | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class FlowOpsZipWithIndexTest extends AnyFlatSpec with Matchers with Eventually: | ||
behavior of "zipWithIndex" | ||
|
||
it should "not zip anything from an empty flow" in: | ||
val c = Flow.empty[Int] | ||
val s = c.zipWithIndex | ||
s.runToList() shouldBe List.empty | ||
|
||
it should "zip flow with index" in: | ||
val c = Flow.fromValues(1 to 5: _*) | ||
val s = c.zipWithIndex | ||
s.runToList() shouldBe List((1, 0), (2, 1), (3, 2), (4, 3), (5, 4)) | ||
|
||
end FlowOpsZipWithIndexTest |