@Test public void testTake() { assertThat(new Range<>(1).next(i -> i + 1).take(0), equalTo(Seqs.newMutableSeq())); assertThat(new Range<>(1).next(i -> i + 1).take(5), equalTo(Seqs.newMutableSeq(1, 2, 3, 4, 5))); assertThat(new Range<>(1).next(i -> i + 1).to(3).take(5), equalTo(Seqs.newMutableSeq(1, 2, 3))); Helpers.assertThrows(UnsupportedOperationException.class, () -> new Range<>(1).spliterator()); }
@Test public void testToSeq() { assertThat(new Range<>(1).to(1).next(i -> i + 1).toSeq(), is(equalTo(Seqs.newSeq(1)))); assertThat(new Range<>(1).until(1).next(i -> i + 1).toSeq(), is(equalTo(Seqs.newSeq()))); assertThat( new Range<>('a').to('e').next(c -> (char) (c + 1)).toSeq(), is(equalTo(Seqs.newSeq('a', 'b', 'c', 'd', 'e')))); assertThat(new Range<>(6).until(0).next(i -> i - 2).toSeq(), is(equalTo(Seqs.newSeq(6, 4, 2)))); }
@Test public void testTakeWhile() { assertThat( new Range<>(1).next(i -> i + 1).takeWhile(e -> e <= 5), equalTo(Seqs.newMutableSeq(1, 2, 3, 4, 5))); assertThat( new Range<>(1).next(i -> i + 1).takeWhile(e -> e < 1), equalTo(Seqs.newMutableSeq())); assertThat( new Range<>(1).next(i -> i + 1).until(1).takeWhile(e -> e <= 5), equalTo(Seqs.newMutableSeq())); assertThat( new Range<>(1).next(i -> i + 1).takeWhile((e, i) -> i < 5), equalTo(Seqs.newMutableSeq(1, 2, 3, 4, 5))); assertThat( new Range<>(1).next(i -> i + 1).takeWhile((e, i) -> i < 0), equalTo(Seqs.newMutableSeq())); assertThat( new Range<>(1).next(i -> i + 1).until(1).takeWhile((e, i) -> i < 5), equalTo(Seqs.newMutableSeq())); List<Integer> indices = new ArrayList<>(); assertThat( new Range<>(1) .next(i -> i + i) .takeWhile( (e, i) -> { indices.add(i); return e < 64; }), equalTo(Seqs.newMutableSeq(1, 2, 4, 8, 16, 32))); assertThat(indices, equalTo(Arrays.asList(0, 1, 2, 3, 4, 5, 6))); }
/** Converts the child list to a sequence. Replaces singleton with the token they contain. */ private void generate() { Iterable<Pattern> children = (Iterable<Pattern>) this.children; if (children == null) { this.seq = null; } else { this.seq = Seqs.from(children) .map( new Unary() { public Object apply(Object o) throws InvocationError { CommonTree t = (CommonTree) o; if (t.getChildCount() == 0 && t.getToken() != null) { return wrap(t.getToken()); } else { return t; } } }); } this.generated = true; }
@Test public void testToMutableSeq() { assertThat( new Range<>(1).to(5).next(i -> i + 1).toMutableSeq(), is(equalTo(Seqs.newMutableSeq(1, 2, 3, 4, 5)))); }