Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
105 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
48 changes: 48 additions & 0 deletions
48
stream/src/test/java/com/annimon/stream/streamtests/FindIndexed.java
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,48 @@ | ||
package com.annimon.stream.streamtests; | ||
|
||
import com.annimon.stream.IntPair; | ||
import com.annimon.stream.Optional; | ||
import com.annimon.stream.Stream; | ||
import com.annimon.stream.function.IndexedPredicate; | ||
import org.junit.Test; | ||
import static com.annimon.stream.test.hamcrest.OptionalMatcher.isEmpty; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public final class FindIndexed { | ||
|
||
@Test | ||
public void testFindIndexed() { | ||
IntPair<Integer> result = Stream.rangeClosed(1, 10) | ||
.findIndexed(sumEquals(7)) | ||
.get(); | ||
assertThat(result.getFirst(), is(3)); | ||
assertThat(result.getSecond(), is(4)); | ||
} | ||
|
||
@Test | ||
public void testFindIndexedWithStartAndStep() { | ||
IntPair<Integer> result = Stream.of(1, 11, 22, 12, 40) | ||
.findIndexed(0, 10, sumEquals(42)) | ||
.get(); | ||
assertThat(result.getFirst(), is(20)); | ||
assertThat(result.getSecond(), is(22)); | ||
} | ||
|
||
@Test | ||
public void testFindIndexedNoMatch() { | ||
Optional<IntPair<Integer>> result = Stream.range(0, 10) | ||
.findIndexed(sumEquals(42)); | ||
assertThat(result, isEmpty()); | ||
} | ||
|
||
|
||
private static IndexedPredicate<Integer> sumEquals(final int sum) { | ||
return new IndexedPredicate<Integer>() { | ||
@Override | ||
public boolean test(int index, Integer value) { | ||
return index + value == sum; | ||
} | ||
}; | ||
} | ||
} |