From e401fb037c555eb18db54708037a796523258c32 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 10 Jul 2019 17:10:51 -0400 Subject: General improvements to stack --- src/test/java/bjc/esodata/StackTest.java | 152 ++++++++++++++++++++++++ src/test/java/bjc/esodata/ThresholdSetTest.java | 60 ++++++++-- 2 files changed, 199 insertions(+), 13 deletions(-) create mode 100644 src/test/java/bjc/esodata/StackTest.java (limited to 'src/test/java/bjc/esodata') 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 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(); + + // 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 index b37f866..6995ad8 100644 --- a/src/test/java/bjc/esodata/ThresholdSetTest.java +++ b/src/test/java/bjc/esodata/ThresholdSetTest.java @@ -3,11 +3,13 @@ package bjc.esodata; import org.junit.Test; import bjc.TestUtils; -import bjc.esodata.ThresholdSet; -import java.util.Iterator; +import java.util.*; import static bjc.TestUtils.*; + +import static bjc.esodata.ThresholdSet.*; + import static org.junit.Assert.*; /** @@ -18,29 +20,61 @@ import static org.junit.Assert.*; public class ThresholdSetTest { @Test public void testAdd() { - ThresholdSet thst = new ThresholdSet<>(); - - thst.addKeys("a", "b"); + ThresholdSet thst = TS("a", "b"); - assertIteratorEquals(false, thst.setView().iterator(), "a", "b"); + assertIteratorSet(false, thst.setView().iterator(), "a", "b"); + assertEquals(thst.setSize(), 2); } @Test public void testAddMulti() { - ThresholdSet thst = new ThresholdSet<>(); + ThresholdSet thst = TS("a", "b", "a"); - thst.addKeys("a", "b", "a"); + assertIteratorSet(false, thst.setView().iterator(), "b"); + assertEquals(thst.setSize(), 1); + } - assertIteratorEquals(false, thst.setView().iterator(), "b"); + @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 = new ThresholdSet<>(); + 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.addKeys("a", "a", "b"); - thst.removeKeys("a"); + thst.setView().add("b"); + assertIteratorSet(false, thst.setView().iterator(), "a", "c"); + assertEquals(2, thst.setView().size()); - assertIteratorEquals(false, thst.setView().iterator(), "a", "b"); + thst.setView().remove("c"); + assertIteratorSet(false, thst.setView().iterator(), "a"); } } -- cgit v1.2.3