com.aliasi.test.unit.util.IteratorsTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aliasi-lingpipe Show documentation
Show all versions of aliasi-lingpipe Show documentation
This is the original Lingpipe:
http://alias-i.com/lingpipe/web/download.html
There were not made any changes to the source code.
package com.aliasi.test.unit.util;
import com.aliasi.util.Iterators;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertNotNull;
import static com.aliasi.test.unit.Asserts.assertEqualsIterations;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class IteratorsTest {
@Test
public void testOne() {
List xs = Arrays.asList(new String[0]);
assertEqualsIterations(xs.iterator(),
new TrueIterator(xs.iterator()));
assertEqualsIterations(xs.iterator(),
new FalseIterator(xs.iterator()));
List ys = Arrays.asList(new String[] { "a", "b", "c" });
assertEqualsIterations(ys.iterator(),
new TrueIterator(ys.iterator()));
assertEqualsIterations(xs.iterator(),
new FalseIterator(ys.iterator()));
List zs1 = Arrays.asList(new String[] { "a", "b", "c" });
List zs1a = Arrays.asList(new String[] { "b", "c" });
assertEqualsIterations(zs1a.iterator(),
new RemoveIterator(zs1.iterator(),"a"));
List zs1b = Arrays.asList(new String[] { "a", "c" });
assertEqualsIterations(zs1b.iterator(),
new RemoveIterator(zs1.iterator(),"b"));
List zs1c = Arrays.asList(new String[] { "a", "b" });
assertEqualsIterations(zs1c.iterator(),
new RemoveIterator(zs1.iterator(),"c"));
}
@Test(expected=UnsupportedOperationException.class)
public void testTwo() {
List xs = Arrays.asList(new String[] { "a", "b" });
TrueIterator it = new TrueIterator(xs.iterator());
assertNotNull(it.next());
it.remove();
}
@Test(expected=NoSuchElementException.class)
public void testExcThreeA() {
List xs = Arrays.asList(new String[] { });
TrueIterator it = new TrueIterator(xs.iterator());
assertFalse(it.hasNext());
it.next();
}
@Test(expected=NoSuchElementException.class)
public void testExcThreeB() {
List ys = Arrays.asList(new String[] { "a", "b" });
TrueIterator it2 = new TrueIterator(ys.iterator());
assertNotNull(it2.next());
assertNotNull(it2.next());
assertFalse(it2.hasNext());
it2.next();
}
static class TrueIterator extends Iterators.Filter {
public TrueIterator(Iterator it) {
super(it);
}
@Override
public boolean accept(Object x) {
return true;
}
}
static class FalseIterator extends Iterators.Filter {
public FalseIterator(Iterator it) {
super(it);
}
@Override
public boolean accept(Object x) {
return false;
}
}
static class RemoveIterator extends Iterators.Filter {
private final String mX;
public RemoveIterator(Iterator it, String x) {
super(it);
mX = x;
}
@Override
public boolean accept(Object x) {
return !x.equals(mX);
}
}
@Test
public void testBufferedOne() {
List xs = Arrays.asList(new String[0]);
Iterator it = new ListBufferedIterator(xs.iterator());
assertEqualsIterations(xs.iterator(),it);
List ys = Arrays.asList(new String[] { "a" });
Iterator it2 = new ListBufferedIterator(ys.iterator());
assertEqualsIterations(ys.iterator(),it2);
}
@Test(expected=UnsupportedOperationException.class)
public void testBufferedTwo() {
List xs = Arrays.asList(new String[0]);
Iterator it = new ListBufferedIterator(xs.iterator());
it.remove();
}
@Test(expected=NoSuchElementException.class)
public void testBufferedThreeExcA() {
List xs = Arrays.asList(new String[0]);
Iterator it = new ListBufferedIterator(xs.iterator());
it.next();
}
@Test(expected=NoSuchElementException.class)
public void testBufferedThreeExcB() {
List ys = Arrays.asList(new String[] { "a", "b", "c" });
Iterator it2 = new ListBufferedIterator(ys.iterator());
assertNotNull(it2.next());
assertNotNull(it2.next());
assertNotNull(it2.next());
it2.next();
}
public static class ListBufferedIterator extends Iterators.Buffered {
private final Iterator mIterator;
public ListBufferedIterator(Iterator iterator) {
mIterator = iterator;
}
@Override
public Object bufferNext() {
if (!mIterator.hasNext()) return null;
while (mIterator.hasNext()) {
Object next = mIterator.next();
if (next != null) return next;
}
return null;
}
}
@Test(expected=IllegalStateException.class)
public void testSIOne() {
HashSet
© 2015 - 2025 Weber Informatics LLC | Privacy Policy