* added unit test for TreeIterator and FilterIterator

This commit is contained in:
Reinhard Pointner 2008-12-27 11:37:23 +00:00
parent 2fc8bb7195
commit e5ee870b73
6 changed files with 146 additions and 16 deletions

View File

@ -39,7 +39,7 @@ public class AbstractNameSimilarityMetricTest {
// lower-case
matches.put("the a-team", "The A-Team");
return TestUtil.asParameters(matches.entrySet().toArray());
return TestUtil.asParameters(matches.entrySet());
}
private Entry<String, String> entry;

View File

@ -57,7 +57,7 @@ public class NumericSimilarityMetricTest {
@Parameters
public static Collection<Object[]> createParameters() {
return TestUtil.asParameters(matches.keySet().toArray());
return TestUtil.asParameters(matches.keySet());
}
private String normalizedName;
@ -83,8 +83,6 @@ public class NumericSimilarityMetricTest {
for (String comparisonValue : testdata) {
float similarity = metric.getSimilarity(value, comparisonValue);
// System.out.println(String.format("%s vs %s = %f", value, comparisonValue, similarity));
if (similarity > maxSimilarity) {
maxSimilarity = similarity;
mostSimilar = comparisonValue;

View File

@ -0,0 +1,61 @@
package net.sourceforge.tuned;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class FilterIteratorTest {
private List<String> list = new ArrayList<String>();
@Before
public void setUp() {
list.add("first one");
list.add("2");
list.add("third space");
list.add("four square");
list.add("5");
list.add("last order");
}
@Test
public void iterateAndRemove() {
Iterator<Integer> integers = new FilterIterator<String, Integer>(list) {
@Override
protected Integer filter(String sourceValue) {
if (sourceValue.matches("\\d+"))
return Integer.parseInt(sourceValue);
return null;
}
};
assertEquals(2, integers.next());
integers.remove();
assertEquals(5, integers.next());
integers.remove();
assertFalse(integers.hasNext());
// check if remove() worked
Iterator<String> strings = list.iterator();
assertEquals("first one", strings.next());
assertEquals("third space", strings.next());
assertEquals("four square", strings.next());
assertEquals("last order", strings.next());
assertFalse(strings.hasNext());
}
}

View File

@ -5,16 +5,15 @@ package net.sourceforge.tuned;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class TestUtil {
public static List<Object[]> asParameters(Object... parameterSet) {
public static List<Object[]> asParameters(Object... parameters) {
List<Object[]> list = new ArrayList<Object[]>();
for (Object parameter : parameterSet) {
for (Object parameter : parameters) {
list.add(new Object[] { parameter });
}
@ -22,14 +21,8 @@ public class TestUtil {
}
public static <T> List<T> asList(Iterator<T> iterator) {
List<T> list = new ArrayList<T>();
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
public static List<Object[]> asParameters(Collection<?> parameters) {
return asParameters(parameters.toArray());
}

View File

@ -0,0 +1,78 @@
package net.sourceforge.tuned;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class TreeIteratorTest {
private List<Object> tree;
@Before
public void setUp() throws Exception {
tree = new ArrayList<Object>();
tree.add("r1");
List<Object> branch = new ArrayList<Object>();
branch.add("b1");
branch.add("b2");
tree.add(branch);
tree.add("r2");
List<Object> treetop = new ArrayList<Object>();
treetop.add("t1");
treetop.add("t2");
treetop.add("t3");
List<Object> trunk = new ArrayList<Object>();
trunk.add(treetop);
tree.add(trunk);
}
@Test
public void iterate() {
TreeIterator<Object> treeIterator = new TreeIterator<Object>(tree) {
@SuppressWarnings("unchecked")
@Override
protected Iterator<Object> children(Object node) {
if (node instanceof Iterable)
return ((Iterable) node).iterator();
return null;
}
};
// check leafs (String) and nodes (Iterable)
assertTrue(treeIterator.next() instanceof Iterable); // root
assertEquals("r1", treeIterator.next());
assertTrue(treeIterator.next() instanceof Iterable); // branch
assertEquals("b1", treeIterator.next());
assertEquals("b2", treeIterator.next());
assertEquals("r2", treeIterator.next());
assertTrue(treeIterator.next() instanceof Iterable); // trunk
assertTrue(treeIterator.next() instanceof Iterable); // treetop
assertEquals("t1", treeIterator.next());
assertEquals("t2", treeIterator.next());
assertEquals("t3", treeIterator.next());
assertFalse(treeIterator.hasNext());
}
}

View File

@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses( { ByteBufferOutputStreamTest.class, PreferencesMapTest.class, PreferencesListTest.class })
@SuiteClasses( { ByteBufferOutputStreamTest.class, PreferencesMapTest.class, PreferencesListTest.class, TreeIteratorTest.class, FilterIteratorTest.class })
public class TunedTestSuite {
}