From e5ee870b7364e313732226670f021841f4b52e3c Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 27 Dec 2008 11:37:23 +0000 Subject: [PATCH] * added unit test for TreeIterator and FilterIterator --- .../AbstractNameSimilarityMetricTest.java | 2 +- .../metric/NumericSimilarityMetricTest.java | 4 +- .../sourceforge/tuned/FilterIteratorTest.java | 61 +++++++++++++++ test/net/sourceforge/tuned/TestUtil.java | 15 +--- .../sourceforge/tuned/TreeIteratorTest.java | 78 +++++++++++++++++++ .../net/sourceforge/tuned/TunedTestSuite.java | 2 +- 6 files changed, 146 insertions(+), 16 deletions(-) create mode 100644 test/net/sourceforge/tuned/FilterIteratorTest.java create mode 100644 test/net/sourceforge/tuned/TreeIteratorTest.java diff --git a/test/net/sourceforge/filebot/ui/panel/rename/metric/AbstractNameSimilarityMetricTest.java b/test/net/sourceforge/filebot/ui/panel/rename/metric/AbstractNameSimilarityMetricTest.java index 669d5894..33670f8c 100644 --- a/test/net/sourceforge/filebot/ui/panel/rename/metric/AbstractNameSimilarityMetricTest.java +++ b/test/net/sourceforge/filebot/ui/panel/rename/metric/AbstractNameSimilarityMetricTest.java @@ -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 entry; diff --git a/test/net/sourceforge/filebot/ui/panel/rename/metric/NumericSimilarityMetricTest.java b/test/net/sourceforge/filebot/ui/panel/rename/metric/NumericSimilarityMetricTest.java index 0f2b17ed..c9251421 100644 --- a/test/net/sourceforge/filebot/ui/panel/rename/metric/NumericSimilarityMetricTest.java +++ b/test/net/sourceforge/filebot/ui/panel/rename/metric/NumericSimilarityMetricTest.java @@ -57,7 +57,7 @@ public class NumericSimilarityMetricTest { @Parameters public static Collection 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; diff --git a/test/net/sourceforge/tuned/FilterIteratorTest.java b/test/net/sourceforge/tuned/FilterIteratorTest.java new file mode 100644 index 00000000..6eb74d3d --- /dev/null +++ b/test/net/sourceforge/tuned/FilterIteratorTest.java @@ -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 list = new ArrayList(); + + + @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 integers = new FilterIterator(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 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()); + } +} diff --git a/test/net/sourceforge/tuned/TestUtil.java b/test/net/sourceforge/tuned/TestUtil.java index 69bb6397..4823fb06 100644 --- a/test/net/sourceforge/tuned/TestUtil.java +++ b/test/net/sourceforge/tuned/TestUtil.java @@ -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 asParameters(Object... parameterSet) { + public static List asParameters(Object... parameters) { List list = new ArrayList(); - for (Object parameter : parameterSet) { + for (Object parameter : parameters) { list.add(new Object[] { parameter }); } @@ -22,14 +21,8 @@ public class TestUtil { } - public static List asList(Iterator iterator) { - List list = new ArrayList(); - - while (iterator.hasNext()) { - list.add(iterator.next()); - } - - return list; + public static List asParameters(Collection parameters) { + return asParameters(parameters.toArray()); } diff --git a/test/net/sourceforge/tuned/TreeIteratorTest.java b/test/net/sourceforge/tuned/TreeIteratorTest.java new file mode 100644 index 00000000..cc5b0271 --- /dev/null +++ b/test/net/sourceforge/tuned/TreeIteratorTest.java @@ -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 tree; + + + @Before + public void setUp() throws Exception { + tree = new ArrayList(); + + tree.add("r1"); + + List branch = new ArrayList(); + branch.add("b1"); + branch.add("b2"); + + tree.add(branch); + + tree.add("r2"); + + List treetop = new ArrayList(); + treetop.add("t1"); + treetop.add("t2"); + treetop.add("t3"); + + List trunk = new ArrayList(); + trunk.add(treetop); + + tree.add(trunk); + } + + + @Test + public void iterate() { + TreeIterator treeIterator = new TreeIterator(tree) { + + @SuppressWarnings("unchecked") + @Override + protected Iterator 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()); + } +} diff --git a/test/net/sourceforge/tuned/TunedTestSuite.java b/test/net/sourceforge/tuned/TunedTestSuite.java index 5efa1efa..4ae981f4 100644 --- a/test/net/sourceforge/tuned/TunedTestSuite.java +++ b/test/net/sourceforge/tuned/TunedTestSuite.java @@ -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 { }