Skip to content

Commit

Permalink
More unit tests for filtered iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
neonailol committed Jun 26, 2018
1 parent ffb030a commit d40d857
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 14 deletions.
4 changes: 2 additions & 2 deletions docs/nnl.rocks.kactoos.iterator/-filtered/-init-.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

# <init>

`Filtered(func: `[`KFunc`](../../nnl.rocks.kactoos/-k-func.md)`<`[`X`](index.md#X)`, `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`>, iterator: `[`Iterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/index.html)`<`[`X`](index.md#X)`>)`
`Filtered(func: `[`Func`](../../nnl.rocks.kactoos/-func/index.md)`<`[`X`](index.md#X)`, `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`>, iterator: `[`Iterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/index.html)`<`[`X`](index.md#X)`>)`
`Filtered(predicate: `[`KFunc`](../../nnl.rocks.kactoos/-k-func.md)`<`[`X`](index.md#X)`, `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`>, iterator: `[`Iterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/index.html)`<`[`X`](index.md#X)`>)`
`Filtered(predicate: `[`Func`](../../nnl.rocks.kactoos/-func/index.md)`<`[`X`](index.md#X)`, `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`>, iterator: `[`Iterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/index.html)`<`[`X`](index.md#X)`>)`
2 changes: 1 addition & 1 deletion docs/nnl.rocks.kactoos.iterator/-filtered/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ There is no thread-safety guarantee.

| Name | Summary |
|---|---|
| [&lt;init&gt;](-init-.md) | `Filtered(func: `[`KFunc`](../../nnl.rocks.kactoos/-k-func.md)`<`[`X`](index.md#X)`, `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`>, iterator: `[`Iterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/index.html)`<`[`X`](index.md#X)`>)`<br>`Filtered(func: `[`Func`](../../nnl.rocks.kactoos/-func/index.md)`<`[`X`](index.md#X)`, `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`>, iterator: `[`Iterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/index.html)`<`[`X`](index.md#X)`>)` |
| [&lt;init&gt;](-init-.md) | `Filtered(predicate: `[`KFunc`](../../nnl.rocks.kactoos/-k-func.md)`<`[`X`](index.md#X)`, `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`>, iterator: `[`Iterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/index.html)`<`[`X`](index.md#X)`>)`<br>`Filtered(predicate: `[`Func`](../../nnl.rocks.kactoos/-func/index.md)`<`[`X`](index.md#X)`, `[`Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`>, iterator: `[`Iterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/index.html)`<`[`X`](index.md#X)`>)` |

### Functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class FuncOf<in X : Any, out Y : Any>(
companion object {
operator fun <Y : Any> invoke(
result: Y
) = FuncOf({ _: Any -> result })
) = FuncOf { _: Any -> result }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class EmptyIterator<out T : Any> : Iterator<T> {

override fun hasNext(): Boolean = false

override fun next(): T = throw NoSuchElementException()
override fun next(): T = throw NoSuchElementException("This iterator is always empty")
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ import nnl.rocks.kactoos.internal.isEmpty
*/
class Filtered<out X : Any> private constructor(
private val iterator: Iterator<X>,
private val func: KFunc<X, Boolean>,
private val predicate: KFunc<X, Boolean>,
private val buffer: Temporary<X>
) : Iterator<X> {

constructor(
func: KFunc<X, Boolean>,
predicate: KFunc<X, Boolean>,
iterator: Iterator<X>
) : this(iterator, func, Temporary())
) : this(iterator, predicate, Temporary())

constructor(
func: Func<X, Boolean>,
predicate: Func<X, Boolean>,
iterator: Iterator<X>
) : this(iterator, { x -> func.apply(x) }, Temporary())
) : this(iterator, { x -> predicate.apply(x) }, Temporary())

override fun hasNext(): Boolean {
if (buffer.isEmpty()) {
while (iterator.hasNext()) {
val obj = iterator.next()
if (func(obj)) {
if (predicate(obj)) {
buffer.put(obj)
break
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nnl.rocks.kactoos.iterable

import nnl.rocks.kactoos.Func
import nnl.rocks.kactoos.iterator.IteratorOf
import nnl.rocks.kactoos.test.BehavesAsIterable
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package nnl.rocks.kactoos.iterator

import nnl.rocks.kactoos.func.FuncOf
import nnl.rocks.kactoos.test.BehavesAsIterator
import kotlin.test.*
import kotlin.test.Test

class FilteredTest {

@Test
fun filters() {
fun filtersUsingKFunc() {
BehavesAsIterator(
Filtered(
{ input -> input.length < 4 },
Expand All @@ -15,4 +16,48 @@ class FilteredTest {
arrayOf("red", "fox")
)
}

@Test
fun filtersUsingFunc() {
BehavesAsIterator(
Filtered(
FuncOf { input -> input.length < 4 },
IteratorOf("red", "lazy", "fox")
),
arrayOf("red", "fox")
)
}

@Test
fun filtersPredicateAlwaysFalse() {
BehavesAsIterator(
Filtered(
{ false },
IteratorOf("red", "lazy", "fox")
),
arrayOf()
)
}

@Test
fun filtersPredicateAlwaysTrue() {
BehavesAsIterator(
Filtered(
{ true },
IteratorOf("red", "lazy", "fox")
),
arrayOf("red", "lazy", "fox")
)
}

@Test
fun filtersEmptyIterator() {
BehavesAsIterator(
Filtered(
{ true },
EmptyIterator()
),
arrayOf()
)
}
}

0 comments on commit d40d857

Please sign in to comment.