-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from softwaremill/source-interleave
Add Source.interleave combinator
- Loading branch information
Showing
2 changed files
with
151 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
82 changes: 82 additions & 0 deletions
82
core/src/test/scala/ox/channels/SourceOpsInterleaveTest.scala
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,82 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsInterleaveTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Source.interleave" | ||
|
||
it should "interleave with an empty source" in scoped { | ||
val c1 = Source.fromValues(1, 2, 3) | ||
val c2 = Source.fromValues() | ||
|
||
val s1 = c1.interleave(c2) | ||
|
||
s1.toList shouldBe List(1, 2, 3) | ||
} | ||
|
||
it should "interleave two sources with default segment size" in scoped { | ||
val c1 = Source.fromValues(1, 3, 5) | ||
val c2 = Source.fromValues(2, 4, 6) | ||
|
||
val s = c1.interleave(c2) | ||
|
||
s.toList shouldBe List(1, 2, 3, 4, 5, 6) | ||
} | ||
|
||
it should "interleave two sources with default segment size and different lengths" in scoped { | ||
val c1 = Source.fromValues(1, 3, 5) | ||
val c2 = Source.fromValues(2, 4, 6, 8, 10, 12) | ||
|
||
val s = c1.interleave(c2) | ||
|
||
s.toList shouldBe List(1, 2, 3, 4, 5, 6, 8, 10, 12) | ||
} | ||
|
||
it should "interleave two sources with custom segment size" in scoped { | ||
val c1 = Source.fromValues(1, 2, 3, 4) | ||
val c2 = Source.fromValues(10, 20, 30, 40) | ||
|
||
val s = c1.interleave(c2, segmentSize = 2) | ||
|
||
s.toList shouldBe List(1, 2, 10, 20, 3, 4, 30, 40) | ||
} | ||
|
||
it should "interleave two sources with custom segment size and different lengths" in scoped { | ||
val c1 = Source.fromValues(1, 2, 3, 4, 5, 6, 7) | ||
val c2 = Source.fromValues(10, 20, 30, 40) | ||
|
||
val s = c1.interleave(c2, segmentSize = 2) | ||
|
||
s.toList shouldBe List(1, 2, 10, 20, 3, 4, 30, 40, 5, 6, 7) | ||
} | ||
|
||
it should "interleave two sources with different lengths and complete eagerly" in scoped { | ||
val c1 = Source.fromValues(1, 3, 5) | ||
val c2 = Source.fromValues(2, 4, 6, 8, 10, 12) | ||
|
||
val s = c1.interleave(c2, eagerComplete = true) | ||
|
||
s.toList shouldBe List(1, 2, 3, 4, 5, 6) | ||
} | ||
|
||
it should "when empty, interleave with a non-empty source and complete eagerly" in scoped { | ||
val c1 = Source.fromValues() | ||
val c2 = Source.fromValues(1, 2, 3) | ||
|
||
val s1 = c1.interleave(c2, eagerComplete = true) | ||
|
||
s1.toList shouldBe empty | ||
} | ||
|
||
it should "interleave with an empty source and complete eagerly" in scoped { | ||
val c1 = Source.fromValues(1, 2, 3) | ||
val c2 = Source.fromValues() | ||
|
||
val s1 = c1.interleave(c2, eagerComplete = true) | ||
|
||
s1.toList shouldBe List(1) | ||
} | ||
} |