Skip to content

Commit

Permalink
StreamExTest: testSegmentLength fixed (naive approach was incorrect when
Browse files Browse the repository at this point in the history
the longest segment is prefix or suffix)
  • Loading branch information
amaembo committed Aug 19, 2016
1 parent 3f408cf commit 569ac6b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/test/java/one/util/streamex/StreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1362,13 +1362,12 @@ private long segmentLength(IntStreamEx source, IntPredicate predicate) {

@Test
public void testSegmentLength() {
withRandom(r -> {
int[] input = IntStreamEx.of(r, 1000, -10, 100).toArray();
Consumer<int[]> test = input -> {
// get maximal count of consecutive positive numbers
long res = segmentLength(IntStreamEx.of(input), x -> x > 0);
long resParallel = segmentLength(IntStreamEx.of(input).parallel(), x -> x > 0);
long expected = 0;
long cur = 0;
long cur = input[0] > 0 ? 1 : 0;
for (int i = 0; i < input.length - 1; i++) {
if (input[i] > 0 && input[i + 1] > 0)
cur++;
Expand All @@ -1378,9 +1377,14 @@ public void testSegmentLength() {
cur = 1;
}
}
if(cur > expected)
expected = cur;
assertEquals(expected, res);
assertEquals(expected, resParallel);
});
};
withRandom(r -> repeat(100, n -> test.accept(IntStreamEx.of(r, 1000, -10, 100).toArray())));
test.accept(new int[] { 1, 2, 3, -1 });
test.accept(new int[] { -1, 1, 2, -1, 1, 2, 3 });
}

private static final class SeqList extends AbstractList<Integer> {
Expand Down

0 comments on commit 569ac6b

Please sign in to comment.