@Override public Iterator<InType> iterator() { return CombiningIterator.create(it.iterator(), comparator, fn); } }
public static <InType> CombiningIterator<InType> create( Iterator<InType> it, Comparator<InType> comparator, BinaryFn<InType, InType, InType> fn ) { return new CombiningIterator<InType>(it, comparator, fn); }
@Override public InType next() { if (!hasNext()) { throw new NoSuchElementException(); } InType res = null; while (hasNext()) { if (res == null) { res = fn.apply(it.next(), null); continue; } if (comparator.compare(res, it.peek()) == 0) { res = fn.apply(res, it.next()); } else { break; } } return res; }
@Test(expected = NoSuchElementException.class) public void testExceptionInNext() { boolean expected = false; EasyMock.expect(peekIterator.hasNext()).andReturn(expected); EasyMock.replay(peekIterator); testingIterator.next(); EasyMock.verify(peekIterator); }
@Test(expected = UnsupportedOperationException.class) public void testRemove() { testingIterator.remove(); } }
@Test public void testHasNext() { boolean expected = true; EasyMock.expect(peekIterator.hasNext()).andReturn(expected); EasyMock.replay(peekIterator); boolean actual = testingIterator.hasNext(); EasyMock.verify(peekIterator); Assert.assertEquals("The hasNext function is broken", expected, actual); }
@Test public void testFalseBranchNext() { boolean expected = true; EasyMock.expect(peekIterator.hasNext()).andReturn(expected); expected = false; EasyMock.expect(peekIterator.hasNext()).andReturn(expected); EasyMock.replay(peekIterator); Object res = testingIterator.next(); EasyMock.verify(peekIterator); Assert.assertNull("Should be null", res); }
@Override public InType next() { if (!hasNext()) { throw new NoSuchElementException(); } InType res = null; while (hasNext()) { if (res == null) { res = fn.apply(it.next(), null); continue; } if (comparator.compare(res, it.peek()) == 0) { res = fn.apply(res, it.next()); } else { break; } } return res; }
@Before public void setUp() { peekIterator = EasyMock.createMock(PeekingIterator.class); comparator = EasyMock.createMock(Comparator.class); binaryFn = EasyMock.createMock(BinaryFn.class); testingIterator = CombiningIterator.create(peekIterator, comparator, binaryFn); }
public static <InType> CombiningIterator<InType> create( Iterator<InType> it, Comparator<InType> comparator, BinaryFn<InType, InType, InType> fn ) { return new CombiningIterator<InType>(it, comparator, fn); }
@Test public void testNext() { boolean expected = true; EasyMock.expect(peekIterator.hasNext()).andReturn(expected).times(4); String defaultString = "S1"; String resString = "S2"; EasyMock.expect(peekIterator.next()).andReturn(defaultString); EasyMock.expect(binaryFn.apply(EasyMock.eq(defaultString), EasyMock.isNull())) .andReturn(resString); EasyMock.expect(peekIterator.next()).andReturn(defaultString); EasyMock.expect(comparator.compare(EasyMock.eq(resString), EasyMock.eq(defaultString))) .andReturn(0); EasyMock.expect(peekIterator.next()).andReturn(defaultString); EasyMock.expect(binaryFn.apply(EasyMock.eq(resString), EasyMock.eq(defaultString))) .andReturn(resString); EasyMock.expect(comparator.compare(EasyMock.eq(resString), EasyMock.eq(defaultString))) .andReturn(1); EasyMock.replay(peekIterator); EasyMock.replay(binaryFn); EasyMock.replay(comparator); String actual = testingIterator.next(); Assert.assertEquals(resString, actual); EasyMock.verify(peekIterator); EasyMock.verify(comparator); EasyMock.verify(binaryFn); }
@Override public Iterator<InType> iterator() { return CombiningIterator.create(it.iterator(), comparator, fn); } }