From 629b6bc7444005915983ae8d86c89c4ae215729e Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 26 Jul 2022 20:35:01 -0400 Subject: Restructure a bit --- src/test/java/bjc/TestUtils.java | 141 ------------ src/test/java/bjc/data/ArrayIteratorTest.java | 33 --- src/test/java/bjc/data/BooleanToggleTest.java | 52 ----- src/test/java/bjc/data/CircularIteratorTest.java | 57 ----- src/test/java/bjc/data/EitherTest.java | 35 --- src/test/java/bjc/data/QueuedIteratorTest.java | 73 ------- src/test/java/bjc/esodata/MinMaxListTest.java | 52 ----- src/test/java/bjc/esodata/NestListTest.java | 119 ----------- src/test/java/bjc/esodata/StackTest.java | 235 -------------------- src/test/java/bjc/esodata/ThresholdSetTest.java | 79 ------- src/test/java/bjc/funcdata/TestMapCreation.java | 38 ---- src/test/java/bjc/funcdata/TestMapOperations.java | 153 ------------- src/test/java/bjc/functypes/IDTest.java | 21 -- src/test/java/bjc/test/TestUtils.java | 141 ++++++++++++ src/test/java/bjc/test/data/ArrayIteratorTest.java | 35 +++ src/test/java/bjc/test/data/BooleanToggleTest.java | 54 +++++ .../java/bjc/test/data/CircularIteratorTest.java | 59 +++++ src/test/java/bjc/test/data/EitherTest.java | 36 ++++ .../java/bjc/test/data/QueuedIteratorTest.java | 75 +++++++ src/test/java/bjc/test/esodata/AbbrevTreeTest.java | 46 ++++ src/test/java/bjc/test/esodata/MinMaxListTest.java | 54 +++++ src/test/java/bjc/test/esodata/NestListTest.java | 120 +++++++++++ src/test/java/bjc/test/esodata/StackTest.java | 238 +++++++++++++++++++++ .../java/bjc/test/esodata/ThresholdSetTest.java | 81 +++++++ .../java/bjc/test/funcdata/TestMapCreation.java | 40 ++++ .../java/bjc/test/funcdata/TestMapOperations.java | 155 ++++++++++++++ src/test/java/bjc/test/functypes/IDTest.java | 23 ++ 27 files changed, 1157 insertions(+), 1088 deletions(-) delete mode 100644 src/test/java/bjc/TestUtils.java delete mode 100644 src/test/java/bjc/data/ArrayIteratorTest.java delete mode 100644 src/test/java/bjc/data/BooleanToggleTest.java delete mode 100644 src/test/java/bjc/data/CircularIteratorTest.java delete mode 100644 src/test/java/bjc/data/EitherTest.java delete mode 100644 src/test/java/bjc/data/QueuedIteratorTest.java delete mode 100644 src/test/java/bjc/esodata/MinMaxListTest.java delete mode 100644 src/test/java/bjc/esodata/NestListTest.java delete mode 100644 src/test/java/bjc/esodata/StackTest.java delete mode 100644 src/test/java/bjc/esodata/ThresholdSetTest.java delete mode 100644 src/test/java/bjc/funcdata/TestMapCreation.java delete mode 100644 src/test/java/bjc/funcdata/TestMapOperations.java delete mode 100644 src/test/java/bjc/functypes/IDTest.java create mode 100644 src/test/java/bjc/test/TestUtils.java create mode 100644 src/test/java/bjc/test/data/ArrayIteratorTest.java create mode 100644 src/test/java/bjc/test/data/BooleanToggleTest.java create mode 100644 src/test/java/bjc/test/data/CircularIteratorTest.java create mode 100644 src/test/java/bjc/test/data/EitherTest.java create mode 100644 src/test/java/bjc/test/data/QueuedIteratorTest.java create mode 100644 src/test/java/bjc/test/esodata/AbbrevTreeTest.java create mode 100644 src/test/java/bjc/test/esodata/MinMaxListTest.java create mode 100644 src/test/java/bjc/test/esodata/NestListTest.java create mode 100644 src/test/java/bjc/test/esodata/StackTest.java create mode 100644 src/test/java/bjc/test/esodata/ThresholdSetTest.java create mode 100644 src/test/java/bjc/test/funcdata/TestMapCreation.java create mode 100644 src/test/java/bjc/test/funcdata/TestMapOperations.java create mode 100644 src/test/java/bjc/test/functypes/IDTest.java (limited to 'src/test/java') diff --git a/src/test/java/bjc/TestUtils.java b/src/test/java/bjc/TestUtils.java deleted file mode 100644 index 312ebaf..0000000 --- a/src/test/java/bjc/TestUtils.java +++ /dev/null @@ -1,141 +0,0 @@ -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 The type of the values. - * @param src The iterator to pull values from. - * @param vals The values to expect from the iterator. - */ - @SafeVarargs - public static void assertIteratorEquals(Iterator 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 The type of the 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 void assertIteratorEquals(boolean hasMore, Iterator 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 The type of the values. - * @param src The iterator to pull values from. - * @param vals The values to expect from the iterator. - */ - @SafeVarargs - public static void assertIteratorSet(Iterator src, T... vals) { - Set s1 = new HashSet<>(); - Set 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 The type of the 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 void assertIteratorSet(boolean hasMore, Iterator 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 The type of the values. - * @param src The list to read values from. - * @param exps The values to expect in the list. - */ - @SafeVarargs - public static void assertListEquals(List src, T... exps) { - assertEquals(exps.length, src.size()); - - int i = 0; - for (T act : src) { - T exp = exps[i++]; - - assertEquals(exp, act); - } - } - - /** - * Assert a stack has the given contents. - * - * @param - * The type of items in the stack. - * - * @param src - * The stack to inspect. - * @param exps - * The values that are expected. - */ - @SafeVarargs - public static void assertStackEquals(bjc.esodata.Stack src, T... exps) { - assertArrayEquals(exps, src.toArray()); - } -} diff --git a/src/test/java/bjc/data/ArrayIteratorTest.java b/src/test/java/bjc/data/ArrayIteratorTest.java deleted file mode 100644 index 223eea2..0000000 --- a/src/test/java/bjc/data/ArrayIteratorTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * - */ -package bjc.data; - -import static org.junit.Assert.*; - -import org.junit.*; - -/** - * Test ArrayIterator - * @author Ben Culkin - * - */ -public class ArrayIteratorTest { - - /** - * Test ArrayIterator - */ - @Test - public void test() { - ArrayIterator itr = new ArrayIterator<>("a", "b", "c"); - - assertTrue(itr.hasNext()); - assertEquals("a", itr.next()); - assertEquals("b", itr.next()); - assertEquals("c", itr.next()); - - assertFalse(itr.hasNext()); - assertNull(itr.next()); - } - -} diff --git a/src/test/java/bjc/data/BooleanToggleTest.java b/src/test/java/bjc/data/BooleanToggleTest.java deleted file mode 100644 index 0b0937f..0000000 --- a/src/test/java/bjc/data/BooleanToggleTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package bjc.data; - -import static org.junit.Assert.*; - -import org.junit.Test; - -/** - * 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()); - - BooleanToggle tog2 = new BooleanToggle(true); - - // Test equals/hashcode - assertEquals(tog, tog2); - assertEquals(tog.hashCode(), tog2.hashCode()); - - // Swap toggle - tog2.get(); - - assertNotEquals(tog.hashCode(), tog2.hashCode()); - assertNotEquals(tog, tog2); - - // Test toString - assertEquals("true", tog.toString()); - } -} diff --git a/src/test/java/bjc/data/CircularIteratorTest.java b/src/test/java/bjc/data/CircularIteratorTest.java deleted file mode 100644 index 8091e1c..0000000 --- a/src/test/java/bjc/data/CircularIteratorTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package bjc.data; - -import static bjc.TestUtils.*; -import java.util.*; - -import org.junit.Test; - -/** - * Test for circular iterators., - * - * @author bjculkin - * - */ -public class CircularIteratorTest { - /** - * Test regular repetition of the entire iterator. - */ - @Test - public void testRegular() { - List lst = Arrays.asList("a", "b", "c"); - - CircularIterator 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 lst = Arrays.asList("a", "b", "c"); - - CircularIterator 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"); - } - - /** - * Test that remove throws an exception. - */ - @Test(expected = UnsupportedOperationException.class) - public void testRemove() { - Iterator arrayItr = new ArrayIterator<>("a", "b"); - CircularIterator itr = new CircularIterator<>(() -> arrayItr); - - itr.remove(); - } -} diff --git a/src/test/java/bjc/data/EitherTest.java b/src/test/java/bjc/data/EitherTest.java deleted file mode 100644 index ef2d12b..0000000 --- a/src/test/java/bjc/data/EitherTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package bjc.data; - -import static org.junit.Assert.*; - -import java.util.*; - -import org.junit.*; - -@SuppressWarnings("javadoc") -public class EitherTest -{ - private Either leftEither; - private Either rightEither; - - @Before - public void setUp() throws Exception { - leftEither = Either.left("left"); - rightEither = Either.right("right"); - } - - @Test - public void testIsLeft() { - assertTrue("isLeft properly marks left eithers", leftEither.isLeft()); - assertFalse("isLeft properly marks right eithers", rightEither.isLeft()); - } - - @Test - public void testGetLeft() { - assertEquals("getLeft treats left eithers properly", - Optional.of("left"), leftEither.getLeft()); - assertEquals("getLeft treats right eithers properly", - Optional.empty(), rightEither.getLeft()); - } - -} diff --git a/src/test/java/bjc/data/QueuedIteratorTest.java b/src/test/java/bjc/data/QueuedIteratorTest.java deleted file mode 100644 index b880f97..0000000 --- a/src/test/java/bjc/data/QueuedIteratorTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package bjc.data; - -import static java.util.Arrays.asList; - -import org.junit.Test; - -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 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 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 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/MinMaxListTest.java b/src/test/java/bjc/esodata/MinMaxListTest.java deleted file mode 100644 index 08901f0..0000000 --- a/src/test/java/bjc/esodata/MinMaxListTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package bjc.esodata; - -import static org.junit.Assert.*; - -import java.util.*; - -import org.junit.*; - -@SuppressWarnings("javadoc") -public class MinMaxListTest { - private final static Comparator intComparator = (lhs, rhs) -> lhs - rhs;; - - @Test - public void minMaxListInitializesMinMax() { - MinMaxList list = new MinMaxList<>(intComparator, - 1, 2, 3, 4, 5); - - assertEquals("List contains 5 elements", 5, list.size()); - - assertEquals("Minimum is 1", 1, (int)list.minimum()); - assertEquals("Maximum is 5", 5, (int)list.maximum()); - } - - @Test - public void minMaxListAddUpdatesMinMax() { - MinMaxList list = new MinMaxList<>(intComparator, - 2, 3, 4); - - assertEquals("Minimum is 2", 2, (int)list.minimum()); - assertEquals("Maximum is 4", 4, (int)list.maximum()); - - list.add(1); - list.add(5); - - assertEquals("Minimum is 1", 1, (int)list.minimum()); - assertEquals("Maximum is 5", 5, (int)list.maximum()); - } - - public void minMaxListRemoveUpdatesMinMax() { - MinMaxList list = new MinMaxList<>(intComparator, - 1, 2, 3, 4, 5); - - assertEquals("Minimum is 1", 1, (int)list.minimum()); - assertEquals("Maximum is 5", 5, (int)list.maximum()); - - list.remove((Integer)1); - list.remove((Integer)5); - - assertEquals("Minimum is 2", 2, (int)list.minimum()); - assertEquals("Maximum is 4", 4, (int)list.maximum()); - } -} diff --git a/src/test/java/bjc/esodata/NestListTest.java b/src/test/java/bjc/esodata/NestListTest.java deleted file mode 100644 index ff3723e..0000000 --- a/src/test/java/bjc/esodata/NestListTest.java +++ /dev/null @@ -1,119 +0,0 @@ -package bjc.esodata; - -import static org.junit.Assert.*; - -import java.util.*; - -import org.junit.*; - -import bjc.*; - -@SuppressWarnings("javadoc") -public class NestListTest -{ - - @Test - public void testAddItemElement() { - NestList nl = new NestList<>(); - - assertEquals("NestLists are created empty", 0, nl.size()); - - nl.addItems("hello", "there"); - - assertEquals("Adding two items to an empty NestList makes it size 2", - 2, nl.size()); - } - - @Test - public void testAddItemNestListOfElement() { - NestList nl = new NestList<>(); - - NestList nl2 = new NestList<>(); - - nl2.addItem("friend"); - - nl.addItems("hello", "there"); - nl.addItem(nl2); - - assertEquals("Adding a sublist increases the size of NestList by 1", - 3, nl.size()); - } - - @Test - public void testAddSublist() { - NestList nl = new NestList<>(); - - nl.addSublist("here", "is", "a"); - nl.addItem("thing"); - - assertEquals("addSublist increases size of NestList by 1", 2, nl.size()); - } - - @Test - public void testFlatIterator() { - NestList nl1 = new NestList<>(); - NestList nl2 = new NestList<>(); - NestList nl3 = new NestList<>(); - - nl1.addItems("of", "means"); - - nl2.addItem(nl1); - - nl3.addItem("Hello"); - nl3.addSublist("my", "unfortunate"); - nl3.addItem("friend"); - nl3.addItem(nl2); - - TestUtils.assertIteratorEquals(nl3.flatIterator(), - "Hello", "my", "unfortunate", "friend", "of", "means"); - } - - @Test - public void testFlatten() { - NestList nl1A = new NestList<>(); - NestList nl2A = new NestList<>(); - NestList nl3A = new NestList<>(); - - nl1A.addItems("of", "means"); - - nl2A.addItem(nl1A); - - nl3A.addItem("Hello"); - nl3A.addSublist("my", "unfortunate"); - nl3A.addItem("friend"); - nl3A.addItem(nl2A); - - NestList nl1B = new NestList<>(); - NestList nl2B = new NestList<>(); - - nl1B.addItems("of", "means"); - - nl2B.addItems("Hello", "my", "unfortunate", "friend"); - nl2B.addItem(nl1B); - - assertEquals("Flatten removes one level of nesting", - nl2B, nl3A.flatten()); - } - - @Test - public void testDeepFlatten() { - NestList nl1 = new NestList<>(); - NestList nl2 = new NestList<>(); - NestList nl3 = new NestList<>(); - - nl1.addItems("of", "means"); - - nl2.addItem(nl1); - - nl3.addItem("Hello"); - nl3.addSublist("my", "unfortunate"); - nl3.addItem("friend"); - nl3.addItem(nl2); - - List testList = Arrays.asList( - "Hello", "my", "unfortunate", "friend", "of", "means"); - - assertEquals("deepFlatten flattens out all sublists", - testList, nl3.deepFlatten()); - } -} diff --git a/src/test/java/bjc/esodata/StackTest.java b/src/test/java/bjc/esodata/StackTest.java deleted file mode 100644 index b4b98cc..0000000 --- a/src/test/java/bjc/esodata/StackTest.java +++ /dev/null @@ -1,235 +0,0 @@ -package bjc.esodata; - -import org.junit.Test; - -import static bjc.TestUtils.*; - -import static org.junit.Assert.*; - -/** - * Tests of Stack. - * - * @author Ben Culkin - */ -@SuppressWarnings("javadoc") -public class StackTest { - @Test - public void testBasic() { - Stack 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 st = new SimpleStack<>(); - - st.pushAll("a", "b", "c"); - - Stack spst1 = st.spaghettify(); - Stack 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 st = new SimpleStack<>(); - - st.pushAll("a", "b", "c", "d"); - - st.drop(); - - // assertStackEquals goes from [top] -> [bottom] - assertStackEquals(st, "c", "b", "a"); - - st.drop(2); - - assertStackEquals(st, "a"); - - st.pushAll("b", "c", "d"); - - st.nip(); - st.nip(); - - assertStackEquals(st, "d", "a"); - - st.drop(); - - st.dup(); - - assertStackEquals(st, "a", "a"); - - st.pushAll("b", "c"); - - st.multidup(3, 1); - - assertStackEquals(st, "c", "b", "a", "c", "b", "a", "a"); - - st.drop(3); - - assertStackEquals(st, "c", "b", "a", "a"); - - st.over(); - - assertStackEquals(st, "b", "c", "b", "a", "a"); - - st.drop(2); - - assertStackEquals(st, "b", "a", "a"); - - st.multiover(2, 2); - - assertStackEquals(st, "a", "a", "a", "a", "b", "a", "a"); - - st.drop(6); - - assertStackEquals(st, "a"); - - st.push("b"); - st.push("c"); - - assertStackEquals(st, "c", "b", "a"); - - st.pick(); - - assertStackEquals(st, "a", "c", "b", "a"); - - st.swap(); - - assertStackEquals(st, "c", "a", "b", "a"); - - st.deepdup(); - - assertStackEquals(st, "c", "a", "a", "b", "a"); - - st.swap(); - st.deepswap(); - - assertStackEquals(st, "a", "a", "c", "b", "a"); - - st.rot(); - - assertStackEquals(st, "c", "a", "a", "b", "a"); - - st.invrot(); - - assertStackEquals(st, "a", "a", "c", "b", "a"); - } - - @SuppressWarnings("unchecked") - @Test - public void testDataComb() { - Stack stk = new SimpleStack<>(); - - stk.pushAll(2, 3, 4); - - assertStackEquals(stk, 4, 3, 2); - - stk.dip(st -> { - int x = stk.pop(); - int y = stk.pop(); - - stk.push(x + y); - }); - - assertStackEquals(stk, 4, 5); - - stk.dip(2, st -> { - stk.push(6); - }); - - assertStackEquals(stk, 4, 5, 6); - - stk.keep(st -> { - int v = st.pop(); - - st.push(v + 1); - }); - - assertStackEquals(stk, 4, 5, 5, 6); - - stk.multicleave(2, st -> { - int x = st.pop(); - int y = st.pop(); - - st.push(x + y); - }, st -> { - int x = st.pop(); - int y = st.pop(); - - st.push(y - x); - }); - - assertStackEquals(stk, 1, 9, 5, 6); - - stk.spread(st -> { - int x = st.pop(); - int y = st.pop(); - - st.push(x + y); - }, st -> { - int y = st.pop(); - - st.push(y + 1); - }); - - assertStackEquals(stk, 10, 6, 6); - - stk.apply(2, st -> { - int lhs = st.pop(); - st.push(lhs - st.pop()); - }); - - assertStackEquals(stk, 2); - } -} diff --git a/src/test/java/bjc/esodata/ThresholdSetTest.java b/src/test/java/bjc/esodata/ThresholdSetTest.java deleted file mode 100644 index c9403da..0000000 --- a/src/test/java/bjc/esodata/ThresholdSetTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package bjc.esodata; - -import org.junit.Test; - -import static bjc.TestUtils.*; - -import static bjc.esodata.ThresholdSet.*; - -import static org.junit.Assert.*; - -/** - * Tests for ThresholdSet - * - * @author Ben Culkin. - */ -@SuppressWarnings("javadoc") -public class ThresholdSetTest { - @Test - public void testAdd() { - ThresholdSet thst = TS("a", "b"); - - assertIteratorSet(false, thst.setView().iterator(), "a", "b"); - assertEquals(thst.setSize(), 2); - } - - @Test - public void testAddMulti() { - ThresholdSet thst = TS("a", "b", "a"); - - assertIteratorSet(false, thst.setView().iterator(), "b"); - assertEquals(thst.setSize(), 1); - } - - @Test - public void testAddMulti2() { - ThresholdSet thst = TS("a", "b"); - thst.add("a"); - - assertIteratorSet(false, thst.setView().iterator(), "b"); - assertEquals(thst.setSize(), 1); - } - - @Test - public void testContains() { - ThresholdSet 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 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 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"); - } -} diff --git a/src/test/java/bjc/funcdata/TestMapCreation.java b/src/test/java/bjc/funcdata/TestMapCreation.java deleted file mode 100644 index eeea591..0000000 --- a/src/test/java/bjc/funcdata/TestMapCreation.java +++ /dev/null @@ -1,38 +0,0 @@ -package bjc.funcdata; - -import static org.junit.Assert.*; - -import org.junit.*; - -@SuppressWarnings("javadoc") -public class TestMapCreation { - @Test - public void mapOfNothingCreatesEmptyMap() { - MapEx map = MapEx.of(); - - assertEquals("Map is empty", 0, map.size()); - } - - @Test(expected = IllegalArgumentException.class) - public void mapOfMismatchedCountErrors() { - @SuppressWarnings("unused") - MapEx map = MapEx.of("thing1"); - } - - @Test(expected = ClassCastException.class) - public void mapOfMismatchedTypeErrors() { - MapEx map = MapEx.of(1, 1.0); - - map.forEach((key, val) -> { - // An exception will be thrown here - }); - } - - @Test - public void mapOfCreatesWithGivenContents() { - MapEx map = MapEx.of("a", "A", "b", "B"); - - assertTrue("Constructed map contains key 'a'", map.containsKey("a")); - assertEquals("Constructed map has key 'a' mapped to value 'A'", "A", map.get("a").get()); - } -} diff --git a/src/test/java/bjc/funcdata/TestMapOperations.java b/src/test/java/bjc/funcdata/TestMapOperations.java deleted file mode 100644 index 7523d02..0000000 --- a/src/test/java/bjc/funcdata/TestMapOperations.java +++ /dev/null @@ -1,153 +0,0 @@ -package bjc.funcdata; - -import static org.junit.Assert.*; - -import java.util.*; - -import org.junit.*; - -@SuppressWarnings("javadoc") -public class TestMapOperations { - private MapEx map; - - @Before - public void setUp() throws Exception { - map = MapEx.of("a", "A", "b", "B"); - } - - @Test - public void sizeMatchesExpected() { - assertEquals("Constructed map is of size 2", 2, map.size()); - } - - @Test - public void containsExpectedKey() { - assertTrue("Constructed map contains key 'a'", map.containsKey("a")); - } - - @Test - public void getYieldsExpectedValue() { - assertEquals("Constructed map has key 'a' mapped to value 'A'", "A", map.get("a").get()); - } - - @Test - public void doesNotContainNotAddedKey() { - assertFalse("Constructed map doesn't contain key 'c'", map.containsKey("c")); - } - - public void getOfNonexistentKeyThrows() { - assertFalse("Getting a non-existant key yields an absent optional", map.get("c").isPresent()); - } - - @Test - public void putOfNonExistingKeyAddsValue() { - map.put("c", "C"); - - assertEquals("Constructed map now has 3 items", 3, map.size()); - assertEquals("Constructed map now has 'c' mapped to 'C'", "C", map.get("c").get()); - } - - @Test - public void putOfExistingKeyUpdatesValue() { - String val = map.put("a", "D"); - - assertEquals("Constructed map still contains 2 items", 2, map.size()); - assertEquals("Constructed map now has 'a' mapped to 'D'", "D", map.get("a").get()); - assertEquals("put method returned old value of 'A'", "A", val); - } - - @Test - public void forEachGetsExpectedElements() { - List result = new ArrayList<>(); - - map.forEach((key, value) -> { - result.add(key + " = " + value); - }); - - assertArrayEquals("For-each has the expected elements", - new String[] {"a = A", "b = B"}, - result.toArray()); - } - - @Test - public void forEachKeyGetsAddedKeys() { - List keys = new ArrayList<>(); - - map.forEachKey((key) -> { - keys.add(key); - }); - - assertArrayEquals("forEachKey gives the expected keys", - new String[] {"a", "b"}, - keys.toArray()); - } - - @Test - public void forEachValueGetsAddedValues() { - List keys = new ArrayList<>(); - - map.forEachValue((key) -> { - keys.add(key); - }); - - assertArrayEquals("forEachKey gives the expected values", - new String[] {"A", "B"}, - keys.toArray()); - } - - @Test - public void clearRemovesAllValues() { - map.clear(); - - assertEquals("A cleared map contains no items", 0, map.size()); - } - - @Test - public void removeOfExistingKeyRemovesKey() { - String removed = map.remove("a"); - - assertEquals("Constructed map now has one less element", 1, map.size()); - assertFalse("Constructed map no longer contains a removed key", map.containsKey("a")); - assertEquals("Remove returns the removed value", "A", removed); - } - - @Test - public void removeOfNonExistingKeyDoesntRemoveAnything() { - String removed = map.remove("c"); - - assertEquals("Constructed map still contains 2 elements", 2, map.size()); - assertNull("remove of a non-existing key returns null", removed); - } - - @Test - public void keyListReturnsListOfKeys() { - assertArrayEquals("Constructed map key-list has the expected elements", - new Object[] {"a", "b"}, - map.keyList().toArray(new String[0])); - } - - @Test - public void valueListReturnsListOfValues() { - assertArrayEquals("Constructed map value-list has the expected elements", - new Object[] {"A", "B"}, - map.valueList().toArray(new String[0])); - } - - @Test - public void mapIsThawedByDefault() { - assertFalse("isFrozen is false by default for a map", map.isFrozen()); - } - - @Test - public void canFreezeMap() { - assertTrue("canFreeze is true for a map", map.canFreeze()); - assertTrue("freeze freezes a map", map.freeze()); - assertTrue("isFrozen indicates a map is frozen", map.isFrozen()); - } - - @Test(expected = ObjectFrozen.class) - public void clearOfFrozenMapFails() { - map.freeze(); - map.clear(); - } -} diff --git a/src/test/java/bjc/functypes/IDTest.java b/src/test/java/bjc/functypes/IDTest.java deleted file mode 100644 index 69c6133..0000000 --- a/src/test/java/bjc/functypes/IDTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package bjc.functypes; - -import static org.junit.Assert.*; - -import java.util.function.*; - -import org.junit.*; - -@SuppressWarnings("javadoc") -public class IDTest { - - @Test - public void testID() { - UnaryOperator idOp = ID.id(); - - assertEquals("hello", idOp.apply("hello")); - - assertEquals(1, ID.id().apply(1)); - } - -} diff --git a/src/test/java/bjc/test/TestUtils.java b/src/test/java/bjc/test/TestUtils.java new file mode 100644 index 0000000..d79157d --- /dev/null +++ b/src/test/java/bjc/test/TestUtils.java @@ -0,0 +1,141 @@ +package bjc.test; + +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 The type of the values. + * @param src The iterator to pull values from. + * @param vals The values to expect from the iterator. + */ + @SafeVarargs + public static void assertIteratorEquals(Iterator 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 The type of the 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 void assertIteratorEquals(boolean hasMore, Iterator 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 The type of the values. + * @param src The iterator to pull values from. + * @param vals The values to expect from the iterator. + */ + @SafeVarargs + public static void assertIteratorSet(Iterator src, T... vals) { + Set s1 = new HashSet<>(); + Set 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 The type of the 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 void assertIteratorSet(boolean hasMore, Iterator 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 The type of the values. + * @param src The list to read values from. + * @param exps The values to expect in the list. + */ + @SafeVarargs + public static void assertListEquals(List src, T... exps) { + assertEquals(exps.length, src.size()); + + int i = 0; + for (T act : src) { + T exp = exps[i++]; + + assertEquals(exp, act); + } + } + + /** + * Assert a stack has the given contents. + * + * @param + * The type of items in the stack. + * + * @param src + * The stack to inspect. + * @param exps + * The values that are expected. + */ + @SafeVarargs + public static void assertStackEquals(bjc.esodata.Stack src, T... exps) { + assertArrayEquals(exps, src.toArray()); + } +} diff --git a/src/test/java/bjc/test/data/ArrayIteratorTest.java b/src/test/java/bjc/test/data/ArrayIteratorTest.java new file mode 100644 index 0000000..c446f53 --- /dev/null +++ b/src/test/java/bjc/test/data/ArrayIteratorTest.java @@ -0,0 +1,35 @@ +/** + * + */ +package bjc.test.data; + +import static org.junit.Assert.*; + +import org.junit.*; + +import bjc.data.ArrayIterator; + +/** + * Test ArrayIterator + * @author Ben Culkin + * + */ +public class ArrayIteratorTest { + + /** + * Test ArrayIterator + */ + @Test + public void test() { + ArrayIterator itr = new ArrayIterator<>("a", "b", "c"); + + assertTrue(itr.hasNext()); + assertEquals("a", itr.next()); + assertEquals("b", itr.next()); + assertEquals("c", itr.next()); + + assertFalse(itr.hasNext()); + assertNull(itr.next()); + } + +} diff --git a/src/test/java/bjc/test/data/BooleanToggleTest.java b/src/test/java/bjc/test/data/BooleanToggleTest.java new file mode 100644 index 0000000..7577340 --- /dev/null +++ b/src/test/java/bjc/test/data/BooleanToggleTest.java @@ -0,0 +1,54 @@ +package bjc.test.data; + +import static org.junit.Assert.*; + +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()); + + BooleanToggle tog2 = new BooleanToggle(true); + + // Test equals/hashcode + assertEquals(tog, tog2); + assertEquals(tog.hashCode(), tog2.hashCode()); + + // Swap toggle + tog2.get(); + + assertNotEquals(tog.hashCode(), tog2.hashCode()); + assertNotEquals(tog, tog2); + + // Test toString + assertEquals("true", tog.toString()); + } +} diff --git a/src/test/java/bjc/test/data/CircularIteratorTest.java b/src/test/java/bjc/test/data/CircularIteratorTest.java new file mode 100644 index 0000000..9dd7637 --- /dev/null +++ b/src/test/java/bjc/test/data/CircularIteratorTest.java @@ -0,0 +1,59 @@ +package bjc.test.data; + +import static bjc.test.TestUtils.*; +import java.util.*; + +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 lst = Arrays.asList("a", "b", "c"); + + CircularIterator 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 lst = Arrays.asList("a", "b", "c"); + + CircularIterator 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"); + } + + /** + * Test that remove throws an exception. + */ + @Test(expected = UnsupportedOperationException.class) + public void testRemove() { + Iterator arrayItr = new ArrayIterator<>("a", "b"); + CircularIterator itr = new CircularIterator<>(() -> arrayItr); + + itr.remove(); + } +} diff --git a/src/test/java/bjc/test/data/EitherTest.java b/src/test/java/bjc/test/data/EitherTest.java new file mode 100644 index 0000000..5e2613c --- /dev/null +++ b/src/test/java/bjc/test/data/EitherTest.java @@ -0,0 +1,36 @@ +package bjc.test.data; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.junit.*; + +import bjc.data.Either; + +public class EitherTest +{ + private Either leftEither; + private Either rightEither; + + @Before + public void setUp() throws Exception { + leftEither = Either.left("left"); + rightEither = Either.right("right"); + } + + @Test + public void testIsLeft() { + assertTrue("isLeft properly marks left eithers", leftEither.isLeft()); + assertFalse("isLeft properly marks right eithers", rightEither.isLeft()); + } + + @Test + public void testGetLeft() { + assertEquals("getLeft treats left eithers properly", + Optional.of("left"), leftEither.getLeft()); + assertEquals("getLeft treats right eithers properly", + Optional.empty(), rightEither.getLeft()); + } + +} diff --git a/src/test/java/bjc/test/data/QueuedIteratorTest.java b/src/test/java/bjc/test/data/QueuedIteratorTest.java new file mode 100644 index 0000000..a059e96 --- /dev/null +++ b/src/test/java/bjc/test/data/QueuedIteratorTest.java @@ -0,0 +1,75 @@ +package bjc.test.data; + +import static java.util.Arrays.asList; + +import org.junit.Test; + +import bjc.data.QueuedIterator; + +import static bjc.test.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 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 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 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/test/esodata/AbbrevTreeTest.java b/src/test/java/bjc/test/esodata/AbbrevTreeTest.java new file mode 100644 index 0000000..1332b63 --- /dev/null +++ b/src/test/java/bjc/test/esodata/AbbrevTreeTest.java @@ -0,0 +1,46 @@ +package bjc.test.esodata; + +import static org.junit.Assert.*; +import static bjc.test.TestUtils.*; + +import org.junit.Test; + +import bjc.esodata.AbbrevTree; + +@SuppressWarnings("javadoc") +public class AbbrevTreeTest { + private static class StringTree extends AbbrevTree { + // Alias type + public StringTree(AbbrevTree parent, String label, String data) { + super(parent, label, data); + } + + public StringTree(String label, String data) { + super(label, data); + } + } + + @Test + public void testGet() { + StringTree root = new StringTree("root", "a"); + + StringTree leaf1 = new StringTree(root, "leaf", "b1"); + StringTree node1 = new StringTree(root, "node1", "b2"); + + StringTree node2 = new StringTree(node1, "node2", "c1"); + StringTree leaf2 = new StringTree(node1, "leaf", "c2"); + + var list1 = root.nodes("node2"); + assertEquals(1, list1.size()); + assertIteratorSet(false, list1.iterator(), node2); + + var list2 = root.nodes("leaf"); + assertEquals(2, list2.size()); + assertIteratorSet(false, list2.iterator(), leaf1, leaf2); + + var list3 = root.nodes("leaf", "node1"); + assertEquals(1, list3.size()); + assertIteratorSet(false, list3.iterator(), leaf2); + } + +} \ No newline at end of file diff --git a/src/test/java/bjc/test/esodata/MinMaxListTest.java b/src/test/java/bjc/test/esodata/MinMaxListTest.java new file mode 100644 index 0000000..0337ef7 --- /dev/null +++ b/src/test/java/bjc/test/esodata/MinMaxListTest.java @@ -0,0 +1,54 @@ +package bjc.test.esodata; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.junit.*; + +import bjc.esodata.MinMaxList; + +@SuppressWarnings("javadoc") +public class MinMaxListTest { + private final static Comparator intComparator = (lhs, rhs) -> lhs - rhs;; + + @Test + public void minMaxListInitializesMinMax() { + MinMaxList list = new MinMaxList<>(intComparator, + 1, 2, 3, 4, 5); + + assertEquals("List contains 5 elements", 5, list.size()); + + assertEquals("Minimum is 1", 1, (int)list.minimum()); + assertEquals("Maximum is 5", 5, (int)list.maximum()); + } + + @Test + public void minMaxListAddUpdatesMinMax() { + MinMaxList list = new MinMaxList<>(intComparator, + 2, 3, 4); + + assertEquals("Minimum is 2", 2, (int)list.minimum()); + assertEquals("Maximum is 4", 4, (int)list.maximum()); + + list.add(1); + list.add(5); + + assertEquals("Minimum is 1", 1, (int)list.minimum()); + assertEquals("Maximum is 5", 5, (int)list.maximum()); + } + + public void minMaxListRemoveUpdatesMinMax() { + MinMaxList list = new MinMaxList<>(intComparator, + 1, 2, 3, 4, 5); + + assertEquals("Minimum is 1", 1, (int)list.minimum()); + assertEquals("Maximum is 5", 5, (int)list.maximum()); + + list.remove((Integer)1); + list.remove((Integer)5); + + assertEquals("Minimum is 2", 2, (int)list.minimum()); + assertEquals("Maximum is 4", 4, (int)list.maximum()); + } +} diff --git a/src/test/java/bjc/test/esodata/NestListTest.java b/src/test/java/bjc/test/esodata/NestListTest.java new file mode 100644 index 0000000..15b2bab --- /dev/null +++ b/src/test/java/bjc/test/esodata/NestListTest.java @@ -0,0 +1,120 @@ +package bjc.test.esodata; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.junit.*; + +import bjc.*; +import bjc.esodata.NestList; + +@SuppressWarnings("javadoc") +public class NestListTest +{ + + @Test + public void testAddItemElement() { + NestList nl = new NestList<>(); + + assertEquals("NestLists are created empty", 0, nl.size()); + + nl.addItems("hello", "there"); + + assertEquals("Adding two items to an empty NestList makes it size 2", + 2, nl.size()); + } + + @Test + public void testAddItemNestListOfElement() { + NestList nl = new NestList<>(); + + NestList nl2 = new NestList<>(); + + nl2.addItem("friend"); + + nl.addItems("hello", "there"); + nl.addItem(nl2); + + assertEquals("Adding a sublist increases the size of NestList by 1", + 3, nl.size()); + } + + @Test + public void testAddSublist() { + NestList nl = new NestList<>(); + + nl.addSublist("here", "is", "a"); + nl.addItem("thing"); + + assertEquals("addSublist increases size of NestList by 1", 2, nl.size()); + } + + @Test + public void testFlatIterator() { + NestList nl1 = new NestList<>(); + NestList nl2 = new NestList<>(); + NestList nl3 = new NestList<>(); + + nl1.addItems("of", "means"); + + nl2.addItem(nl1); + + nl3.addItem("Hello"); + nl3.addSublist("my", "unfortunate"); + nl3.addItem("friend"); + nl3.addItem(nl2); + + TestUtils.assertIteratorEquals(nl3.flatIterator(), + "Hello", "my", "unfortunate", "friend", "of", "means"); + } + + @Test + public void testFlatten() { + NestList nl1A = new NestList<>(); + NestList nl2A = new NestList<>(); + NestList nl3A = new NestList<>(); + + nl1A.addItems("of", "means"); + + nl2A.addItem(nl1A); + + nl3A.addItem("Hello"); + nl3A.addSublist("my", "unfortunate"); + nl3A.addItem("friend"); + nl3A.addItem(nl2A); + + NestList nl1B = new NestList<>(); + NestList nl2B = new NestList<>(); + + nl1B.addItems("of", "means"); + + nl2B.addItems("Hello", "my", "unfortunate", "friend"); + nl2B.addItem(nl1B); + + assertEquals("Flatten removes one level of nesting", + nl2B, nl3A.flatten()); + } + + @Test + public void testDeepFlatten() { + NestList nl1 = new NestList<>(); + NestList nl2 = new NestList<>(); + NestList nl3 = new NestList<>(); + + nl1.addItems("of", "means"); + + nl2.addItem(nl1); + + nl3.addItem("Hello"); + nl3.addSublist("my", "unfortunate"); + nl3.addItem("friend"); + nl3.addItem(nl2); + + List testList = Arrays.asList( + "Hello", "my", "unfortunate", "friend", "of", "means"); + + assertEquals("deepFlatten flattens out all sublists", + testList, nl3.deepFlatten()); + } +} diff --git a/src/test/java/bjc/test/esodata/StackTest.java b/src/test/java/bjc/test/esodata/StackTest.java new file mode 100644 index 0000000..7f896a7 --- /dev/null +++ b/src/test/java/bjc/test/esodata/StackTest.java @@ -0,0 +1,238 @@ +package bjc.test.esodata; + +import org.junit.Test; + +import bjc.esodata.SimpleStack; +import bjc.esodata.Stack; + +import static bjc.test.TestUtils.*; + +import static org.junit.Assert.*; + +/** + * Tests of Stack. + * + * @author Ben Culkin + */ +@SuppressWarnings("javadoc") +public class StackTest { + @Test + public void testBasic() { + Stack 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 st = new SimpleStack<>(); + + st.pushAll("a", "b", "c"); + + Stack spst1 = st.spaghettify(); + Stack 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 st = new SimpleStack<>(); + + st.pushAll("a", "b", "c", "d"); + + st.drop(); + + // assertStackEquals goes from [top] -> [bottom] + assertStackEquals(st, "c", "b", "a"); + + st.drop(2); + + assertStackEquals(st, "a"); + + st.pushAll("b", "c", "d"); + + st.nip(); + st.nip(); + + assertStackEquals(st, "d", "a"); + + st.drop(); + + st.dup(); + + assertStackEquals(st, "a", "a"); + + st.pushAll("b", "c"); + + st.multidup(3, 1); + + assertStackEquals(st, "c", "b", "a", "c", "b", "a", "a"); + + st.drop(3); + + assertStackEquals(st, "c", "b", "a", "a"); + + st.over(); + + assertStackEquals(st, "b", "c", "b", "a", "a"); + + st.drop(2); + + assertStackEquals(st, "b", "a", "a"); + + st.multiover(2, 2); + + assertStackEquals(st, "a", "a", "a", "a", "b", "a", "a"); + + st.drop(6); + + assertStackEquals(st, "a"); + + st.push("b"); + st.push("c"); + + assertStackEquals(st, "c", "b", "a"); + + st.pick(); + + assertStackEquals(st, "a", "c", "b", "a"); + + st.swap(); + + assertStackEquals(st, "c", "a", "b", "a"); + + st.deepdup(); + + assertStackEquals(st, "c", "a", "a", "b", "a"); + + st.swap(); + st.deepswap(); + + assertStackEquals(st, "a", "a", "c", "b", "a"); + + st.rot(); + + assertStackEquals(st, "c", "a", "a", "b", "a"); + + st.invrot(); + + assertStackEquals(st, "a", "a", "c", "b", "a"); + } + + @SuppressWarnings("unchecked") + @Test + public void testDataComb() { + Stack stk = new SimpleStack<>(); + + stk.pushAll(2, 3, 4); + + assertStackEquals(stk, 4, 3, 2); + + stk.dip(st -> { + int x = stk.pop(); + int y = stk.pop(); + + stk.push(x + y); + }); + + assertStackEquals(stk, 4, 5); + + stk.dip(2, st -> { + stk.push(6); + }); + + assertStackEquals(stk, 4, 5, 6); + + stk.keep(st -> { + int v = st.pop(); + + st.push(v + 1); + }); + + assertStackEquals(stk, 4, 5, 5, 6); + + stk.multicleave(2, st -> { + int x = st.pop(); + int y = st.pop(); + + st.push(x + y); + }, st -> { + int x = st.pop(); + int y = st.pop(); + + st.push(y - x); + }); + + assertStackEquals(stk, 1, 9, 5, 6); + + stk.spread(st -> { + int x = st.pop(); + int y = st.pop(); + + st.push(x + y); + }, st -> { + int y = st.pop(); + + st.push(y + 1); + }); + + assertStackEquals(stk, 10, 6, 6); + + stk.apply(2, st -> { + int lhs = st.pop(); + st.push(lhs - st.pop()); + }); + + assertStackEquals(stk, 2); + } +} diff --git a/src/test/java/bjc/test/esodata/ThresholdSetTest.java b/src/test/java/bjc/test/esodata/ThresholdSetTest.java new file mode 100644 index 0000000..142a641 --- /dev/null +++ b/src/test/java/bjc/test/esodata/ThresholdSetTest.java @@ -0,0 +1,81 @@ +package bjc.test.esodata; + +import org.junit.Test; + +import bjc.esodata.ThresholdSet; + +import static bjc.test.TestUtils.*; + +import static bjc.esodata.ThresholdSet.*; + +import static org.junit.Assert.*; + +/** + * Tests for ThresholdSet + * + * @author Ben Culkin. + */ +@SuppressWarnings("javadoc") +public class ThresholdSetTest { + @Test + public void testAdd() { + ThresholdSet thst = TS("a", "b"); + + assertIteratorSet(false, thst.setView().iterator(), "a", "b"); + assertEquals(thst.setView().size(), 2); + } + + @Test + public void testAddMulti() { + ThresholdSet thst = TS("a", "b", "a"); + + assertIteratorSet(false, thst.setView().iterator(), "b"); + assertEquals(thst.setView().size(), 1); + } + + @Test + public void testAddMulti2() { + ThresholdSet thst = TS("a", "b"); + thst.add("a"); + + assertIteratorSet(false, thst.setView().iterator(), "b"); + assertEquals(thst.setView().size(), 1); + } + + @Test + public void testContains() { + ThresholdSet 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 thst = TS("a", "a", "b"); + + thst.remove("a"); + + assertIteratorSet(false, thst.setView().iterator(), "a", "b"); + assertEquals(2, thst.setView().size()); + + thst.remove("a"); + + assertIteratorSet(false, thst.setView().iterator(), "b"); + assertEquals(1, thst.setView().size()); + } + + @Test + public void testSetTransparency() { + ThresholdSet 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"); + } +} diff --git a/src/test/java/bjc/test/funcdata/TestMapCreation.java b/src/test/java/bjc/test/funcdata/TestMapCreation.java new file mode 100644 index 0000000..4a06aa0 --- /dev/null +++ b/src/test/java/bjc/test/funcdata/TestMapCreation.java @@ -0,0 +1,40 @@ +package bjc.test.funcdata; + +import static org.junit.Assert.*; + +import org.junit.*; + +import bjc.funcdata.MapEx; + +@SuppressWarnings("javadoc") +public class TestMapCreation { + @Test + public void mapOfNothingCreatesEmptyMap() { + MapEx map = MapEx.of(); + + assertEquals("Map is empty", 0, map.size()); + } + + @Test(expected = IllegalArgumentException.class) + public void mapOfMismatchedCountErrors() { + @SuppressWarnings("unused") + MapEx map = MapEx.of("thing1"); + } + + @Test(expected = ClassCastException.class) + public void mapOfMismatchedTypeErrors() { + MapEx map = MapEx.of(1, 1.0); + + map.forEach((key, val) -> { + // An exception will be thrown here + }); + } + + @Test + public void mapOfCreatesWithGivenContents() { + MapEx map = MapEx.of("a", "A", "b", "B"); + + assertTrue("Constructed map contains key 'a'", map.containsKey("a")); + assertEquals("Constructed map has key 'a' mapped to value 'A'", "A", map.get("a").get()); + } +} diff --git a/src/test/java/bjc/test/funcdata/TestMapOperations.java b/src/test/java/bjc/test/funcdata/TestMapOperations.java new file mode 100644 index 0000000..fa38a79 --- /dev/null +++ b/src/test/java/bjc/test/funcdata/TestMapOperations.java @@ -0,0 +1,155 @@ +package bjc.test.funcdata; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.junit.*; + +import bjc.funcdata.MapEx; + +@SuppressWarnings("javadoc") +public class TestMapOperations { + private MapEx map; + + @Before + public void setUp() throws Exception { + map = MapEx.of("a", "A", "b", "B"); + } + + @Test + public void sizeMatchesExpected() { + assertEquals("Constructed map is of size 2", 2, map.size()); + } + + @Test + public void containsExpectedKey() { + assertTrue("Constructed map contains key 'a'", map.containsKey("a")); + } + + @Test + public void getYieldsExpectedValue() { + assertEquals("Constructed map has key 'a' mapped to value 'A'", "A", map.get("a").get()); + } + + @Test + public void doesNotContainNotAddedKey() { + assertFalse("Constructed map doesn't contain key 'c'", map.containsKey("c")); + } + + public void getOfNonexistentKeyThrows() { + assertFalse("Getting a non-existant key yields an absent optional", map.get("c").isPresent()); + } + + @Test + public void putOfNonExistingKeyAddsValue() { + map.put("c", "C"); + + assertEquals("Constructed map now has 3 items", 3, map.size()); + assertEquals("Constructed map now has 'c' mapped to 'C'", "C", map.get("c").get()); + } + + @Test + public void putOfExistingKeyUpdatesValue() { + String val = map.put("a", "D"); + + assertEquals("Constructed map still contains 2 items", 2, map.size()); + assertEquals("Constructed map now has 'a' mapped to 'D'", "D", map.get("a").get()); + assertEquals("put method returned old value of 'A'", "A", val); + } + + @Test + public void forEachGetsExpectedElements() { + List result = new ArrayList<>(); + + map.forEach((key, value) -> { + result.add(key + " = " + value); + }); + + assertArrayEquals("For-each has the expected elements", + new String[] {"a = A", "b = B"}, + result.toArray()); + } + + @Test + public void forEachKeyGetsAddedKeys() { + List keys = new ArrayList<>(); + + map.forEachKey((key) -> { + keys.add(key); + }); + + assertArrayEquals("forEachKey gives the expected keys", + new String[] {"a", "b"}, + keys.toArray()); + } + + @Test + public void forEachValueGetsAddedValues() { + List keys = new ArrayList<>(); + + map.forEachValue((key) -> { + keys.add(key); + }); + + assertArrayEquals("forEachKey gives the expected values", + new String[] {"A", "B"}, + keys.toArray()); + } + + @Test + public void clearRemovesAllValues() { + map.clear(); + + assertEquals("A cleared map contains no items", 0, map.size()); + } + + @Test + public void removeOfExistingKeyRemovesKey() { + String removed = map.remove("a"); + + assertEquals("Constructed map now has one less element", 1, map.size()); + assertFalse("Constructed map no longer contains a removed key", map.containsKey("a")); + assertEquals("Remove returns the removed value", "A", removed); + } + + @Test + public void removeOfNonExistingKeyDoesntRemoveAnything() { + String removed = map.remove("c"); + + assertEquals("Constructed map still contains 2 elements", 2, map.size()); + assertNull("remove of a non-existing key returns null", removed); + } + + @Test + public void keyListReturnsListOfKeys() { + assertArrayEquals("Constructed map key-list has the expected elements", + new Object[] {"a", "b"}, + map.keyList().toArray(new String[0])); + } + + @Test + public void valueListReturnsListOfValues() { + assertArrayEquals("Constructed map value-list has the expected elements", + new Object[] {"A", "B"}, + map.valueList().toArray(new String[0])); + } + + @Test + public void mapIsThawedByDefault() { + assertFalse("isFrozen is false by default for a map", map.isFrozen()); + } + + @Test + public void canFreezeMap() { + assertTrue("canFreeze is true for a map", map.canFreeze()); + assertTrue("freeze freezes a map", map.freeze()); + assertTrue("isFrozen indicates a map is frozen", map.isFrozen()); + } + + @Test(expected = ObjectFrozen.class) + public void clearOfFrozenMapFails() { + map.freeze(); + map.clear(); + } +} diff --git a/src/test/java/bjc/test/functypes/IDTest.java b/src/test/java/bjc/test/functypes/IDTest.java new file mode 100644 index 0000000..4e31644 --- /dev/null +++ b/src/test/java/bjc/test/functypes/IDTest.java @@ -0,0 +1,23 @@ +package bjc.test.functypes; + +import static org.junit.Assert.*; + +import java.util.function.*; + +import org.junit.*; + +import bjc.functypes.ID; + +@SuppressWarnings("javadoc") +public class IDTest { + + @Test + public void testID() { + UnaryOperator idOp = ID.id(); + + assertEquals("hello", idOp.apply("hello")); + + assertEquals(1, ID.id().apply(1)); + } + +} -- cgit v1.2.3