diff options
Diffstat (limited to 'src/test/java/bjc')
| -rw-r--r-- | src/test/java/bjc/TestUtils.java | 133 | ||||
| -rw-r--r-- | src/test/java/bjc/data/BooleanToggleTest.java | 38 | ||||
| -rw-r--r-- | src/test/java/bjc/data/CircularIteratorTest.java | 49 | ||||
| -rw-r--r-- | src/test/java/bjc/data/QueuedIteratorTest.java | 74 | ||||
| -rw-r--r-- | src/test/java/bjc/esodata/StackTest.java | 152 | ||||
| -rw-r--r-- | src/test/java/bjc/esodata/ThresholdSetTest.java | 80 |
6 files changed, 526 insertions, 0 deletions
diff --git a/src/test/java/bjc/TestUtils.java b/src/test/java/bjc/TestUtils.java new file mode 100644 index 0000000..a8cbf43 --- /dev/null +++ b/src/test/java/bjc/TestUtils.java @@ -0,0 +1,133 @@ +package bjc; + +import java.util.*; + +import static org.junit.Assert.*; + +/** + * Utility methods for doing testing + * + * @author Ben Culkin + */ +public class TestUtils { + /** + * Assert an iterator provides a particular sequence of values. + * + * @param src + * The iterator to pull values from. + * @param vals + * The values to expect from the iterator. + */ + @SafeVarargs + public static <T> void assertIteratorEquals(Iterator<T> src, T... vals) { + for (int i = 0; i < vals.length; i++) { + if (src.hasNext()) { + assertEquals(vals[i], src.next()); + } else { + String msg = String.format("not enough values: got %d, wanted %d", + i, vals.length); + + assertTrue(msg, false); + } + } + } + + /** + * Assert an iterator provides a particular sequence of values. + * + * @param src + * The iterator to pull values from. + * @param hasMore + * The expected value of hasNext for the iterator. + * @param vals + * The values to expect from the iterator. + */ + @SafeVarargs + public static <T> void assertIteratorEquals(boolean hasMore, Iterator<T> src, T... vals) { + /* + * @NOTE + * + * Even though it's awkward, the boolean has to come first. + * Otherwise, there are cases where the compiler will get + * confused as to what the right value for T is, and be unable + * to pick an overload. + */ + assertIteratorEquals(src, vals); + + assertEquals("iterator not exhausted", hasMore, src.hasNext()); + } + + /** + * Assert an iterator provides a particular sequence of values. + * + * @param src + * The iterator to pull values from. + * @param vals + * The values to expect from the iterator. + */ + @SafeVarargs + public static <T> void assertIteratorSet(Iterator<T> src, T... vals) { + Set<T> s1 = new HashSet<>(); + Set<T> s2 = new HashSet<>(); + + for (int i = 0; i < vals.length; i++) { + if (src.hasNext()) { + s1.add(vals[i]); + s2.add(src.next()); + } else { + String msg = String.format("not enough values: got %d, wanted %d", + i, vals.length); + + assertTrue(msg, false); + } + } + + assertEquals(s1, s2); + } + + /** + * Assert an iterator provides a particular sequence of values. + * + * @param src + * The iterator to pull values from. + * @param hasMore + * The expected value of hasNext for the iterator. + * @param vals + * The values to expect from the iterator. + */ + @SafeVarargs + public static <T> void assertIteratorSet(boolean hasMore, Iterator<T> src, T... vals) { + /* + * @NOTE + * + * Even though it's awkward, the boolean has to come first. + * Otherwise, there are cases where the compiler will get + * confused as to what the right value for T is, and be unable + * to pick an overload. + */ + assertIteratorSet(src, vals); + + assertEquals("iterator not exhausted", hasMore, src.hasNext()); + } + + /** + * Assert that a list contains a certain set of values. + * + * @param src + * The list to read values from. + * + * @param exps + * The values to expect in the list. + */ + @SafeVarargs + public static <T> void assertListEquals(List<T> src, T... exps) { + assertEquals(exps.length, src.size()); + + int i = 0; + for (T act : src) { + T exp = exps[i++]; + + assertEquals(exp, act); + } + } +} diff --git a/src/test/java/bjc/data/BooleanToggleTest.java b/src/test/java/bjc/data/BooleanToggleTest.java new file mode 100644 index 0000000..b3d33eb --- /dev/null +++ b/src/test/java/bjc/data/BooleanToggleTest.java @@ -0,0 +1,38 @@ +package bjc.data; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import bjc.data.BooleanToggle; + +/** + * Test for boolean toggles. + * @author bjculkin + * + */ +public class BooleanToggleTest { + + /** + * Test that boolean toggles work right. + */ + @Test + public void test() { + BooleanToggle tog = new BooleanToggle(); + + // Check initial value is false. + assertEquals(false, tog.peek()); + // Check that 'get' returns the old value + assertEquals(false, tog.get()); + // Check that 'get' swaps the value + assertEquals(true, tog.peek()); + // Check that we can round-trip back. + assertEquals(true, tog.get()); + assertEquals(false, tog.peek()); + + tog.set(true); + + // Check set works + assertEquals(true, tog.peek()); + } +} diff --git a/src/test/java/bjc/data/CircularIteratorTest.java b/src/test/java/bjc/data/CircularIteratorTest.java new file mode 100644 index 0000000..82a08c6 --- /dev/null +++ b/src/test/java/bjc/data/CircularIteratorTest.java @@ -0,0 +1,49 @@ +package bjc.data; + +import static bjc.TestUtils.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +import bjc.data.CircularIterator; +/** + * Test for circular iterators., + * + * @author bjculkin + * + */ +public class CircularIteratorTest { + /** + * Test regular repetition of the entire iterator. + */ + @Test + public void testRegular() { + List<String> lst = Arrays.asList("a", "b", "c"); + + CircularIterator<String> itr = new CircularIterator<>(lst); + + // Check we get initial values correctly, and have more remaining + assertIteratorEquals(true, itr, "a", "b", "c"); + + // Check we repeat correctly, and can still repeat + assertIteratorEquals(true, itr, "a", "b", "c"); + } + + /** + * Test that the last element repeats correctly. + */ + @Test + public void testRepLast() { + List<String> lst = Arrays.asList("a", "b", "c"); + + CircularIterator<String> itr = new CircularIterator<>(lst, false); + + // Check we get initial values correctly, and have more remaining + assertIteratorEquals(true, itr, "a", "b", "c"); + + // Check we repeat correctly, and can still repeat + assertIteratorEquals(true, itr, "c", "c", "c"); + } +} diff --git a/src/test/java/bjc/data/QueuedIteratorTest.java b/src/test/java/bjc/data/QueuedIteratorTest.java new file mode 100644 index 0000000..69f37b8 --- /dev/null +++ b/src/test/java/bjc/data/QueuedIteratorTest.java @@ -0,0 +1,74 @@ +package bjc.data; + +import static java.util.Arrays.asList; + +import org.junit.Test; + +import bjc.data.QueuedIterator; + +import static bjc.TestUtils.*; +import static bjc.data.QueuedIterator.queued; + +/** + * Test of QueuedIterator. + * + * @author bjculkin + * + */ +public class QueuedIteratorTest { + + /** + * Test of functionality. + */ + @Test + public void test() { + assertIteratorEquals(false, queued()); + + assertIteratorEquals(false, queued(1, 2, 3), 1, 2, 3); + assertIteratorEquals(false, queued(asList(1, 2, 3), asList(3, 2, 1)), 1, 2, 3, 3, 2, 1); + + } + + /** + * Test of before() method. + */ + @Test + public void testBefore() { + QueuedIterator<Integer> itr = queued(1, 2, 3); + + assertIteratorEquals(true, itr, 1, 2); + + itr.before(1, 2, 3); + + assertIteratorEquals(false, itr, 1, 2, 3, 3); + } + + /** + * Test of after() method. + */ + @Test + public void testAfter() { + QueuedIterator<Integer> itr = queued(1, 2, 3); + + assertIteratorEquals(true, itr, 1, 2); + + itr.after(1, 2, 3); + + assertIteratorEquals(false, itr, 3, 1, 2, 3); + } + + /** + * Test of last() method. + */ + @Test + public void testLast() { + QueuedIterator<Integer> itr = queued(1, 2, 3); + + assertIteratorEquals(true, itr, 1, 2); + + itr.after(4); + itr.last(1, 2, 3); + + assertIteratorEquals(false, itr, 3, 4, 1, 2, 3); + } +} diff --git a/src/test/java/bjc/esodata/StackTest.java b/src/test/java/bjc/esodata/StackTest.java new file mode 100644 index 0000000..568e6ea --- /dev/null +++ b/src/test/java/bjc/esodata/StackTest.java @@ -0,0 +1,152 @@ +package bjc.esodata; + +import org.junit.Test; + +import bjc.TestUtils; + +import java.util.*; + +import static bjc.TestUtils.*; + +import static org.junit.Assert.*; + +/** + * Tests of Stack. + * + * @author Ben Culkin + */ +public class StackTest { + @Test + public void testBasic() { + Stack<String> st = new SimpleStack<>(); + + assertEquals(0, st.size()); + + st.push("a"); + assertEquals(1, st.size()); + + assertEquals("a", st.top()); + assertEquals(1, st.size()); + + assertEquals("a", st.pop()); + assertEquals(0, st.size()); + assertTrue(st.isEmpty()); + } + + @Test + public void testSpaghetti() { + Stack<String> st = new SimpleStack<>(); + + st.pushAll("a", "b", "c"); + + Stack<String> spst1 = st.spaghettify(); + Stack<String> spst2 = st.spaghettify(); + + // st should be [a, b, c] + // spst1 should be [[a, b, c]] + // spst2 should be [[a, b, c]] + + assertEquals("c", st.top()); + assertEquals("c", spst1.top()); + assertEquals("c", spst2.top()); + + assertEquals(3, st.size()); + assertEquals(3, spst1.size()); + assertEquals(3, spst2.size()); + + st.push("d"); + spst1.push("e"); + spst2.push("f"); + + // st should be [a, b, c, d] + // spst1 should be [[a, b, c, d], e] + // spst2 should be [[a, b, c, d], f] + + assertEquals("d", st.top()); + assertEquals("e", spst1.top()); + assertEquals("f", spst2.top()); + + assertEquals(4, st.size()); + assertEquals(5, spst1.size()); + assertEquals(5, spst2.size()); + + spst1.pop(); + + // st should be [a, b, c] + // spst1 should be [[a, b, c]] + // spst2 should be [[a, b, c], f] + + assertEquals("d", spst1.pop()); + assertEquals("c", st.top()); + + assertEquals(3, st.size()); + assertEquals(3, spst1.size()); + assertEquals(4, spst2.size()); + } + + @Test + public void testBasicComb() { + Stack<String> st = new SimpleStack<>(); + + st.pushAll("a", "b", "c", "d"); + + st.drop(); + + // stack should be [a, b, c] + + assertEquals("c", st.top()); + assertEquals(3, st.size()); + + st.drop(2); + + // stack should be [a] + + assertEquals("a", st.top()); + assertEquals(1, st.size()); + + st.pushAll("b", "c", "d"); + + st.nip(); + st.nip(); + + // stack should be [a, d] + assertEquals("d", st.top()); + assertEquals(2, st.size()); + + st.pop(); + + assertEquals("a", st.top()); + assertEquals(1, st.size()); + + st.multidup(1, 1); + + // stack should be [a, a] + assertEquals("a", st.top()); + assertEquals(2, st.size()); + + st.pushAll("b", "c"); + + st.multidup(3, 1); + + // stack should be [a, a, b, c, a, b, c] + assertEquals("c", st.top()); + assertEquals(7, st.size()); + + st.pop(); + assertEquals("b", st.pop()); + assertEquals("a", st.pop()); + assertEquals("c", st.top()); + + // stack should be [a, a, b, c] + + st.dup(); + assertEquals("c", st.top()); + assertEquals(5, st.size()); + + // stack should be [a, a, b, c, c] + assertEquals("c", st.pop()); + assertEquals(4, st.size()); + + // stack should be [a, a, b, c] + } +} diff --git a/src/test/java/bjc/esodata/ThresholdSetTest.java b/src/test/java/bjc/esodata/ThresholdSetTest.java new file mode 100644 index 0000000..6995ad8 --- /dev/null +++ b/src/test/java/bjc/esodata/ThresholdSetTest.java @@ -0,0 +1,80 @@ +package bjc.esodata; + +import org.junit.Test; + +import bjc.TestUtils; + +import java.util.*; + +import static bjc.TestUtils.*; + +import static bjc.esodata.ThresholdSet.*; + +import static org.junit.Assert.*; + +/** + * Tests for ThresholdSet + * + * @author Ben Culkin. + */ +public class ThresholdSetTest { + @Test + public void testAdd() { + ThresholdSet<String> thst = TS("a", "b"); + + assertIteratorSet(false, thst.setView().iterator(), "a", "b"); + assertEquals(thst.setSize(), 2); + } + + @Test + public void testAddMulti() { + ThresholdSet<String> thst = TS("a", "b", "a"); + + assertIteratorSet(false, thst.setView().iterator(), "b"); + assertEquals(thst.setSize(), 1); + } + + @Test + public void testAddMulti2() { + ThresholdSet<String> thst = TS("a", "b"); + thst.add("a"); + + assertIteratorSet(false, thst.setView().iterator(), "b"); + assertEquals(thst.setSize(), 1); + } + + @Test + public void testContains() { + ThresholdSet<String> thst = TS("1", "2", "2", "x", "z"); + + int[] exps = new int[] {1, 2, -1}; + assertArrayEquals(exps, thst.containsKeys("1", "2", "y")); + } + + @Test + public void testRemoveMulti() { + ThresholdSet<String> thst = TS("a", "a", "b"); + + thst.remove("a"); + + assertIteratorSet(false, thst.setView().iterator(), "a", "b"); + assertEquals(2, thst.setSize()); + + thst.remove("a"); + + assertIteratorSet(false, thst.setView().iterator(), "b"); + assertEquals(1, thst.setSize()); + } + + @Test + public void testSetTransparency() { + ThresholdSet<String> thst = TS("a", "b", "c"); + + thst.setView().add("b"); + assertIteratorSet(false, thst.setView().iterator(), "a", "c"); + assertEquals(2, thst.setView().size()); + + thst.setView().remove("c"); + assertIteratorSet(false, thst.setView().iterator(), "a"); + } +} |
