summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/bjc/TestUtils.java42
-rw-r--r--src/test/java/bjc/data/ArrayIteratorTest.java33
-rw-r--r--src/test/java/bjc/data/BooleanToggleTest.java17
-rw-r--r--src/test/java/bjc/data/CircularIteratorTest.java15
-rw-r--r--src/test/java/bjc/data/EitherTest.java35
-rw-r--r--src/test/java/bjc/esodata/MinMaxListTest.java52
-rw-r--r--src/test/java/bjc/esodata/NestListTest.java119
-rw-r--r--src/test/java/bjc/funcdata/TestMapCreation.java38
-rw-r--r--src/test/java/bjc/funcdata/TestMapOperations.java153
-rw-r--r--src/test/java/bjc/functypes/IDTest.java21
10 files changed, 496 insertions, 29 deletions
diff --git a/src/test/java/bjc/TestUtils.java b/src/test/java/bjc/TestUtils.java
index 29e59e1..312ebaf 100644
--- a/src/test/java/bjc/TestUtils.java
+++ b/src/test/java/bjc/TestUtils.java
@@ -13,10 +13,9 @@ 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.
+ * @param <T> 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 <T> void assertIteratorEquals(Iterator<T> src, T... vals) {
@@ -35,12 +34,10 @@ public class TestUtils {
/**
* 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.
+ * @param <T> 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 <T> void assertIteratorEquals(boolean hasMore, Iterator<T> src,
@@ -60,10 +57,9 @@ 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.
+ * @param <T> 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 <T> void assertIteratorSet(Iterator<T> src, T... vals) {
@@ -88,12 +84,10 @@ public class TestUtils {
/**
* 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.
+ * @param <T> 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 <T> void assertIteratorSet(boolean hasMore, Iterator<T> src,
@@ -113,11 +107,9 @@ public class TestUtils {
/**
* 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.
+ * @param <T> 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 <T> void assertListEquals(List<T> src, T... exps) {
diff --git a/src/test/java/bjc/data/ArrayIteratorTest.java b/src/test/java/bjc/data/ArrayIteratorTest.java
new file mode 100644
index 0000000..223eea2
--- /dev/null
+++ b/src/test/java/bjc/data/ArrayIteratorTest.java
@@ -0,0 +1,33 @@
+/**
+ *
+ */
+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<String> 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
index bf7a04f..0b0937f 100644
--- a/src/test/java/bjc/data/BooleanToggleTest.java
+++ b/src/test/java/bjc/data/BooleanToggleTest.java
@@ -1,6 +1,6 @@
package bjc.data;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
import org.junit.Test;
@@ -33,5 +33,20 @@ public class BooleanToggleTest {
// 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
index dfe2a17..8091e1c 100644
--- a/src/test/java/bjc/data/CircularIteratorTest.java
+++ b/src/test/java/bjc/data/CircularIteratorTest.java
@@ -1,9 +1,7 @@
package bjc.data;
import static bjc.TestUtils.*;
-
-import java.util.Arrays;
-import java.util.List;
+import java.util.*;
import org.junit.Test;
@@ -45,4 +43,15 @@ public class CircularIteratorTest {
// 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<String> arrayItr = new ArrayIterator<>("a", "b");
+ CircularIterator<String> itr = new CircularIterator<>(() -> arrayItr);
+
+ itr.remove();
+ }
}
diff --git a/src/test/java/bjc/data/EitherTest.java b/src/test/java/bjc/data/EitherTest.java
new file mode 100644
index 0000000..ef2d12b
--- /dev/null
+++ b/src/test/java/bjc/data/EitherTest.java
@@ -0,0 +1,35 @@
+package bjc.data;
+
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.junit.*;
+
+@SuppressWarnings("javadoc")
+public class EitherTest
+{
+ private Either<String, String> leftEither;
+ private Either<String, String> 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/esodata/MinMaxListTest.java b/src/test/java/bjc/esodata/MinMaxListTest.java
new file mode 100644
index 0000000..08901f0
--- /dev/null
+++ b/src/test/java/bjc/esodata/MinMaxListTest.java
@@ -0,0 +1,52 @@
+package bjc.esodata;
+
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.junit.*;
+
+@SuppressWarnings("javadoc")
+public class MinMaxListTest {
+ private final static Comparator<Integer> intComparator = (lhs, rhs) -> lhs - rhs;;
+
+ @Test
+ public void minMaxListInitializesMinMax() {
+ MinMaxList<Integer> 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<Integer> 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<Integer> 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
new file mode 100644
index 0000000..ff3723e
--- /dev/null
+++ b/src/test/java/bjc/esodata/NestListTest.java
@@ -0,0 +1,119 @@
+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<String> 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<String> nl = new NestList<>();
+
+ NestList<String> 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<String> 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<String> nl1 = new NestList<>();
+ NestList<String> nl2 = new NestList<>();
+ NestList<String> 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<String> nl1A = new NestList<>();
+ NestList<String> nl2A = new NestList<>();
+ NestList<String> nl3A = new NestList<>();
+
+ nl1A.addItems("of", "means");
+
+ nl2A.addItem(nl1A);
+
+ nl3A.addItem("Hello");
+ nl3A.addSublist("my", "unfortunate");
+ nl3A.addItem("friend");
+ nl3A.addItem(nl2A);
+
+ NestList<String> nl1B = new NestList<>();
+ NestList<String> 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<String> nl1 = new NestList<>();
+ NestList<String> nl2 = new NestList<>();
+ NestList<String> nl3 = new NestList<>();
+
+ nl1.addItems("of", "means");
+
+ nl2.addItem(nl1);
+
+ nl3.addItem("Hello");
+ nl3.addSublist("my", "unfortunate");
+ nl3.addItem("friend");
+ nl3.addItem(nl2);
+
+ List<String> 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/funcdata/TestMapCreation.java b/src/test/java/bjc/funcdata/TestMapCreation.java
new file mode 100644
index 0000000..eeea591
--- /dev/null
+++ b/src/test/java/bjc/funcdata/TestMapCreation.java
@@ -0,0 +1,38 @@
+package bjc.funcdata;
+
+import static org.junit.Assert.*;
+
+import org.junit.*;
+
+@SuppressWarnings("javadoc")
+public class TestMapCreation {
+ @Test
+ public void mapOfNothingCreatesEmptyMap() {
+ MapEx<String, String> map = MapEx.of();
+
+ assertEquals("Map is empty", 0, map.size());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void mapOfMismatchedCountErrors() {
+ @SuppressWarnings("unused")
+ MapEx<String, String> map = MapEx.of("thing1");
+ }
+
+ @Test(expected = ClassCastException.class)
+ public void mapOfMismatchedTypeErrors() {
+ MapEx<String, String> map = MapEx.of(1, 1.0);
+
+ map.forEach((key, val) -> {
+ // An exception will be thrown here
+ });
+ }
+
+ @Test
+ public void mapOfCreatesWithGivenContents() {
+ MapEx<String, String> 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
new file mode 100644
index 0000000..7523d02
--- /dev/null
+++ b/src/test/java/bjc/funcdata/TestMapOperations.java
@@ -0,0 +1,153 @@
+package bjc.funcdata;
+
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.junit.*;
+
+@SuppressWarnings("javadoc")
+public class TestMapOperations {
+ private MapEx<String, String> 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<String> 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<String> 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<String> 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
new file mode 100644
index 0000000..69c6133
--- /dev/null
+++ b/src/test/java/bjc/functypes/IDTest.java
@@ -0,0 +1,21 @@
+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<String> idOp = ID.id();
+
+ assertEquals("hello", idOp.apply("hello"));
+
+ assertEquals(1, ID.id().apply(1));
+ }
+
+}