@Test public void testNext() { Function<String, String> next = null; Helpers.assertThrows(NullPointerException.class, () -> new Range<>("").next(next)); BiFunction<String, Integer, String> biNext = null; Helpers.assertThrows(NullPointerException.class, () -> new Range<>("").next(biNext)); }
@Test public void testUntil() { Range<Character> range = new Range<>('a').until('z'); assertThat(range.getTo(), is('z')); assertThat(range.isToIncluded(), is(false)); Helpers.assertThrows(NullPointerException.class, () -> new Range<>("").until(null)); }
@Test public void testIterator() { Iterator<Integer> itr = new Range<>(1).to(1).next(i -> i + 1).iterator(); assertThat(itr.next(), is(1)); assertThat(itr.hasNext(), is(false)); final Iterator<Integer> it = itr; Helpers.assertThrows(NoSuchElementException.class, () -> it.next()); itr = new Range<>(1).to(2).next(i -> i + 1).iterator(); assertThat(itr.next(), is(1)); assertThat(itr.next(), is(2)); assertThat(itr.hasNext(), is(false)); itr = new Range<>(1).next(i -> i + 1).iterator(); assertThat(itr.hasNext(), is(true)); for (int i = 0; i < 100; i++) { itr.next(); } assertThat(itr.next(), is(101)); assertThat(itr.hasNext(), is(true)); List<Integer> list = new ArrayList<>(); for (int d : new Range<>(1).next(i -> i + 1).until(5)) { list.add(d); } assertThat(list, equalTo(Arrays.asList(1, 2, 3, 4))); }
@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 testForEach() { List<Integer> list = new ArrayList<>(); new Range<>(1).to(64).next(i -> i + i).forEach(e -> list.add(e)); assertThat(list, equalTo(Arrays.asList(1, 2, 4, 8, 16, 32, 64))); list.clear(); new Range<>(1).until(64).next(i -> i + i).forEach(e -> list.add(e)); assertThat(list, equalTo(Arrays.asList(1, 2, 4, 8, 16, 32))); list.clear(); new Range<>(64).to(1).next(i -> i / 2).forEach(e -> list.add(e)); assertThat(list, equalTo(Arrays.asList(64, 32, 16, 8, 4, 2, 1))); list.clear(); new Range<>(1).to(1).next(i -> i + i).forEach(e -> list.add(e)); assertThat(list, equalTo(Arrays.asList(1))); list.clear(); new Range<>(1).until(1).next(i -> i + i).forEach(e -> list.add(e)); assertThat(list, equalTo(Collections.emptyList())); list.clear(); List<Integer> indices = new ArrayList<>(); new Range<>(1) .until(64) .next(i -> i + i) .forEach( (e, i) -> { list.add(e); indices.add(i); }); assertThat(list, equalTo(Arrays.asList(1, 2, 4, 8, 16, 32))); assertThat(indices, equalTo(Arrays.asList(0, 1, 2, 3, 4, 5))); list.clear(); new Range<>(1).to(720).next((c, i) -> c * (i + 2)).forEach(e -> list.add(e)); assertThat(list, equalTo(Arrays.asList(1, 2, 6, 24, 120, 720))); list.clear(); new Range<>(1).to(5).next(i -> i + 1).next((c, i) -> c * (i + 2)).forEach(e -> list.add(e)); assertThat(list, equalTo(Arrays.asList(1, 2, 3, 4, 5))); list.clear(); Helpers.assertThrows( NullPointerException.class, () -> new Range<>(1).to(10).forEach(e -> list.add(e))); }
@Test public void testFrom() { assertThat(new Range<>(1).from(2).getFrom(), is(2)); Helpers.assertThrows(NullPointerException.class, () -> new Range<>("").from(null)); }
@Test public void testSpliterator() { Helpers.assertThrows( IllegalArgumentException.class, () -> new Range<>(1).next(i -> i + 1).take(-1)); }