From bbefaad1de12cea3210593c17db6e12334eb7903 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Fri, 16 Sep 2022 18:35:06 -0400 Subject: Adjust a few things --- .../bjc/funcdata/bst/BinarySearchTreeExample.java | 8 +- src/main/java/bjc/data/Contexts.java | 96 +++++++------- src/main/java/bjc/data/GeneratingIterator.java | 2 +- src/main/java/bjc/data/MarkListIterator.java | 9 ++ src/main/java/bjc/data/Pair.java | 14 +- src/main/java/bjc/esodata/AbbrevMap2.java | 2 +- src/main/java/bjc/esodata/AbbrevTree.java~ | 75 ----------- src/main/java/bjc/esodata/NestList.java | 3 +- src/main/java/bjc/esodata/spool/Spool.java | 5 + src/main/java/bjc/esodata/spool/Spooler.java | 5 + src/main/java/bjc/funcdata/FunctionalList.java | 18 +++ src/main/java/bjc/functypes/optics/Lens.java | 13 ++ src/main/java/bjc/functypes/optics/LensX.java | 67 ++++++++++ src/main/java/bjc/functypes/optics/Lenses.java | 147 +++++++++++++++++++++ .../java/bjc/functypes/optics/MutableLens.java | 29 ++++ src/main/java/bjc/functypes/optics/Optic.java | 15 +++ src/main/java/bjc/functypes/optics/PrismX.java | 5 + .../bjc/functypes/optics/impl/package-info.java | 1 + .../java/bjc/functypes/optics/package-info.java | 4 + src/main/java/module-info.java | 6 + src/test/java/bjc/data/MarkListIteratorTest.java | 41 ------ src/test/java/bjc/test/data/EitherTest.java | 1 + .../java/bjc/test/data/GeneratingIteratorTest.java | 20 +++ .../java/bjc/test/data/MarkListIteratorTest.java | 42 ++++++ src/test/java/bjc/test/data/SimpleTreeTest.java | 29 ++++ .../java/bjc/test/data/TransformIteratorTest.java | 22 +++ src/test/java/bjc/test/esodata/NestListTest.java | 1 - .../java/bjc/test/functypes/optics/LensesTest.java | 18 +++ .../bjc/test/functypes/optics/package-info.java | 1 + 29 files changed, 524 insertions(+), 175 deletions(-) delete mode 100644 src/main/java/bjc/esodata/AbbrevTree.java~ create mode 100644 src/main/java/bjc/esodata/spool/Spool.java create mode 100644 src/main/java/bjc/esodata/spool/Spooler.java create mode 100644 src/main/java/bjc/functypes/optics/Lens.java create mode 100644 src/main/java/bjc/functypes/optics/LensX.java create mode 100644 src/main/java/bjc/functypes/optics/Lenses.java create mode 100644 src/main/java/bjc/functypes/optics/MutableLens.java create mode 100644 src/main/java/bjc/functypes/optics/Optic.java create mode 100644 src/main/java/bjc/functypes/optics/PrismX.java create mode 100644 src/main/java/bjc/functypes/optics/impl/package-info.java create mode 100644 src/main/java/bjc/functypes/optics/package-info.java delete mode 100644 src/test/java/bjc/data/MarkListIteratorTest.java create mode 100644 src/test/java/bjc/test/data/GeneratingIteratorTest.java create mode 100644 src/test/java/bjc/test/data/MarkListIteratorTest.java create mode 100644 src/test/java/bjc/test/data/SimpleTreeTest.java create mode 100644 src/test/java/bjc/test/data/TransformIteratorTest.java create mode 100644 src/test/java/bjc/test/functypes/optics/LensesTest.java create mode 100644 src/test/java/bjc/test/functypes/optics/package-info.java diff --git a/src/example/java/bjc/funcdata/bst/BinarySearchTreeExample.java b/src/example/java/bjc/funcdata/bst/BinarySearchTreeExample.java index 9be96be..258b17d 100644 --- a/src/example/java/bjc/funcdata/bst/BinarySearchTreeExample.java +++ b/src/example/java/bjc/funcdata/bst/BinarySearchTreeExample.java @@ -64,11 +64,9 @@ public class BinarySearchTreeExample { * Unused CLI args */ public static void main(final String[] args) { - final Scanner input = new Scanner(System.in); - - runExample(input, System.out); - - input.close(); + try (Scanner input = new Scanner(System.in)) { + runExample(input, System.out); + } } private static void runExample(final Scanner input, OutputStream outpt) { diff --git a/src/main/java/bjc/data/Contexts.java b/src/main/java/bjc/data/Contexts.java index b028ad1..f587fe6 100644 --- a/src/main/java/bjc/data/Contexts.java +++ b/src/main/java/bjc/data/Contexts.java @@ -37,67 +37,67 @@ public class Contexts { public static Context create(Context parent) { return new ContextImpl(parent); } +} - private static class NullContextImpl implements Context { - @Override - public Context getParent() { - return this; - } +class NullContextImpl implements Context { + @Override + public Context getParent() { + return this; + } - @Override - public void register(String name, Object o) { - throw new UnsupportedOperationException(); - } + @Override + public void register(String name, Object o) { + throw new UnsupportedOperationException(); + } - @Override - public Object get(String name) { - throw new NoSuchElementException(); - } - - @Override - public T get(Class contract) { - throw new NoSuchElementException(); - } + @Override + public Object get(String name) { + throw new NoSuchElementException(); } + + @Override + public T get(Class contract) { + throw new NoSuchElementException(); + } +} - private static class ContextImpl implements Context { +class ContextImpl implements Context { - private final Context parent; + private final Context parent; - private final Map objects; + private final Map objects; - public ContextImpl(Context parent) { - this.parent = parent; - this.objects = new HashMap<>(); - } + public ContextImpl(Context parent) { + this.parent = parent; + this.objects = new HashMap<>(); + } - @Override - public void register(String name, Object o) { - objects.put(name, o); - } + @Override + public void register(String name, Object o) { + objects.put(name, o); + } - @Override - public Object get(String name) { - if (objects.containsKey(name)) { - return objects.get(name); - } - return parent.get(name); + @Override + public Object get(String name) { + if (objects.containsKey(name)) { + return objects.get(name); } + return parent.get(name); + } - @SuppressWarnings("unchecked") - @Override - public T get(Class contract) { - for (Object o : objects.values()) { - if (contract.isInstance(o)) { - return (T) o; - } + @SuppressWarnings("unchecked") + @Override + public T get(Class contract) { + for (Object o : objects.values()) { + if (contract.isInstance(o)) { + return (T) o; } - return parent.get(contract); } + return parent.get(contract); + } - @Override - public Context getParent() { - return parent; - } + @Override + public Context getParent() { + return parent; } -} +} \ No newline at end of file diff --git a/src/main/java/bjc/data/GeneratingIterator.java b/src/main/java/bjc/data/GeneratingIterator.java index f926833..8900b94 100644 --- a/src/main/java/bjc/data/GeneratingIterator.java +++ b/src/main/java/bjc/data/GeneratingIterator.java @@ -42,7 +42,7 @@ public class GeneratingIterator implements Iterator { @Override public boolean hasNext() { - return stpper.test(state); + return !stpper.test(state); } /* diff --git a/src/main/java/bjc/data/MarkListIterator.java b/src/main/java/bjc/data/MarkListIterator.java index 725b050..35baa28 100644 --- a/src/main/java/bjc/data/MarkListIterator.java +++ b/src/main/java/bjc/data/MarkListIterator.java @@ -33,6 +33,15 @@ public class MarkListIterator implements ListIterator { this.marks = new ArrayDeque<>(); } + /** + * Get the current element of the iterator. + * + * @return The current iterator of the element + */ + public E current() { + return cache.get(currIdx); + } + /** * Create a new marking list iterator. * diff --git a/src/main/java/bjc/data/Pair.java b/src/main/java/bjc/data/Pair.java index baf1894..1d4be5e 100644 --- a/src/main/java/bjc/data/Pair.java +++ b/src/main/java/bjc/data/Pair.java @@ -1,5 +1,8 @@ package bjc.data; +import java.util.Formattable; +import java.util.FormattableFlags; +import java.util.Formatter; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; @@ -18,7 +21,7 @@ import bjc.funcdata.theory.Bifunctor; * The type of the right side of the pair. * */ -public interface Pair extends Bifunctor { +public interface Pair extends Bifunctor, Formattable { /** * Bind a function across the values in this pair. * @@ -247,4 +250,13 @@ public interface Pair extends Bifunctor Pair pair(Left left, Right right) { return new SimplePair<>(left, right); } + + @Override + default void formatTo(Formatter formatter, int flags, int width, int precision) { + if ((flags & FormattableFlags.ALTERNATE) != 0) { + formatter.format("(%s, %s)", getLeft(), getRight()); + } else { + formatter.format("Pair [l=%s, r=%s", getLeft(), getRight()); + } + } } diff --git a/src/main/java/bjc/esodata/AbbrevMap2.java b/src/main/java/bjc/esodata/AbbrevMap2.java index 259c963..341a28d 100644 --- a/src/main/java/bjc/esodata/AbbrevMap2.java +++ b/src/main/java/bjc/esodata/AbbrevMap2.java @@ -41,7 +41,7 @@ public class AbbrevMap2 { } // Generate all of the strings a given word could be abbreviated as - private List genAbbrevs(String word) { + private static List genAbbrevs(String word) { List retList = new ArrayList<>(); int len = word.length(); diff --git a/src/main/java/bjc/esodata/AbbrevTree.java~ b/src/main/java/bjc/esodata/AbbrevTree.java~ deleted file mode 100644 index a391028..0000000 --- a/src/main/java/bjc/esodata/AbbrevTree.java~ +++ /dev/null @@ -1,75 +0,0 @@ -package bjc.esodata; - -import java.util.*; - -/** - * A labeled tree, where you can reference sub-nodes by their label as long as the reference is unambiguous. - * - * Inspired by the way that you can reference COBOL members by their name, as long as it is unambiguous. If it is ambiguous, you can instead use parent nodes to disambiguate. - * - * @param The type of data contained in the nodes. - */ -public class AbbrevTree { - /** Represents a single node in an AbbrevTree */ - public interface AbbrevNode { - /** The label for the node. */ - public String getLabel(); - /** The data for the node. */ - public Contained getData(); - /** - * Add a child to this node. - * - * If a node already exists with the given label, it will be replaced. - * - * @param label The label for the child. - * @param contained The data contained in the child. - */ - public void addChild(String label, String contained); - /** - * Get a child, starting the search from this node. - * @param labels The label(s) to search for. If the label is ambiguous, provide additional ones to make it unambiguous. - */ - public AbbrevNode getNode(String... labels); - } - - public Contained get(String... labels) { - return null; - } -} - -class AbbrevNode { - private String label; - private Contained data; - - private List> children; - private AbbrevTree container; - - /** - * Create a new AbbrevNode with a label and contents. - * @param label The label for this node. - * @param data The data contained in the node. - * @param container The tree that contains the node. - */ - public AbbrevNode(String label, Contained data, AbbrevTree container) { - this(container); - - this.label = label; - this.data = data; - } - - /** - * Get the label for this node. - * @return The label for this node. - */ - public String getLabel() { - return label; - } - - /** - * Get the data for this node. - * @return The data for this node. - */ - public Contained getData() { - return data; - } -} diff --git a/src/main/java/bjc/esodata/NestList.java b/src/main/java/bjc/esodata/NestList.java index eccaf9d..9d9149c 100644 --- a/src/main/java/bjc/esodata/NestList.java +++ b/src/main/java/bjc/esodata/NestList.java @@ -351,8 +351,7 @@ public class NestList extends AbstractList { + +} diff --git a/src/main/java/bjc/esodata/spool/Spooler.java b/src/main/java/bjc/esodata/spool/Spooler.java new file mode 100644 index 0000000..a222a25 --- /dev/null +++ b/src/main/java/bjc/esodata/spool/Spooler.java @@ -0,0 +1,5 @@ +package bjc.esodata.spool; + +public interface Spooler { + public Spool getSpool(); +} diff --git a/src/main/java/bjc/funcdata/FunctionalList.java b/src/main/java/bjc/funcdata/FunctionalList.java index 88f49c4..c9c3a9f 100644 --- a/src/main/java/bjc/funcdata/FunctionalList.java +++ b/src/main/java/bjc/funcdata/FunctionalList.java @@ -6,6 +6,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Consumer; @@ -487,4 +488,21 @@ public class FunctionalList implements Cloneable, ListEx { return sb.toString(); } + + @Override + public int hashCode() { + return Objects.hash(wrapped); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + FunctionalList other = (FunctionalList) obj; + return Objects.equals(wrapped, other.wrapped); + } } diff --git a/src/main/java/bjc/functypes/optics/Lens.java b/src/main/java/bjc/functypes/optics/Lens.java new file mode 100644 index 0000000..0a7d4cd --- /dev/null +++ b/src/main/java/bjc/functypes/optics/Lens.java @@ -0,0 +1,13 @@ +package bjc.functypes.optics; + +/** + * A type-invariant var Laarhoven lens. + * + * @author bjcul + * + * @param The item this lens can focus on + * @param The field this lens focuses on + */ +public interface Lens extends LensX { + // Alias type +} diff --git a/src/main/java/bjc/functypes/optics/LensX.java b/src/main/java/bjc/functypes/optics/LensX.java new file mode 100644 index 0000000..d2e9c30 --- /dev/null +++ b/src/main/java/bjc/functypes/optics/LensX.java @@ -0,0 +1,67 @@ +package bjc.functypes.optics; + +import static bjc.functypes.optics.Lenses.immutable; + +import java.util.function.BiFunction; +import java.util.function.Function; + +/** + * A type-variant lens + * + * @author Ben Culkin + * + * @param The first type the lens is used on + * @param The second 'whole' type + * @param The first 'part' type + * @param The second 'part' type + */ +public interface LensX extends Optic { + /** + * Retrieve the focused value of this lens. + * + * @param source The item to use the lens on. + * + * @return The value from the given whole this lens focuses on. + */ + P1 get(W1 source); + + /** + * Create an updated version of the item this lens focuses on. + * + * @param source The item to use the lens on. + * @param val The new value. + * + * @return The updated item the lens was used on. + */ + W2 set(W1 source, P2 val); + + /** + * Update the focused value. + * + * NOTE: It will often be more efficient to implement this directly. The + * implementation here is provided for convenience. + * + * @param source The item to use the lens on. + * @param action The action to applied to the focused item. + * + * @return The updated item the lens was used on. + */ + default W2 update(W1 source, Function action) { + return set(source, action.apply(get(source))); + } + + /** + * Compose two type-variant lenses together. + * + * @param The first type the second lens focuses on + * @param The second type the second lens focuses on. + * + * @param other The second lens to use. + * + * @return A lens composed from this one and the given one. + */ + default LensX compose(LensX other) { + return immutable((whole) -> other.get(get(whole)), + (whole, val) -> update(whole, (P1 val2) -> other.set(val2, val))); + } +} \ No newline at end of file diff --git a/src/main/java/bjc/functypes/optics/Lenses.java b/src/main/java/bjc/functypes/optics/Lenses.java new file mode 100644 index 0000000..05a64e7 --- /dev/null +++ b/src/main/java/bjc/functypes/optics/Lenses.java @@ -0,0 +1,147 @@ +package bjc.functypes.optics; + +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Function; + +import bjc.data.Holder; +import bjc.data.Pair; + +/** + * Utility and constructor functions for lenses + * + * @author bjcul + * + */ +public class Lenses { + /** + * Create an immutable lens from a pair of functions. + * + * @param The first type the lens is used on + * @param The second type the lens is used on + * @param The first type the lens focuses on + * @param The second type the lens focuses on. + * + * @param getter The getter for the lens + * @param setter The setter for the lens + * + * @return The lens composed from the two given functions + */ + public static LensX immutable(Function getter, + BiFunction setter) { + return new FunctionalLensX<>(getter, setter); + } + + /** + * Create a mutable lens from a pair of functions. + * + * @param The type the lens is used on + * @param The type the lens is focused on + * + * @param getter The getter for the lens + * @param mutator The mutator for the lens + * + * @return The mutable lens composed from the two given functions + */ + public static MutableLens mutable(Function getter, + BiConsumer mutator) { + return new FunctionalMutableLens<>(getter, mutator); + } + + /** + * Create a lens that reflects over the value in a holder + * + * @param The first type contained in the holder + * @param The second type contained in the holder + * + * @return A lens that focuses on the value of a holder + */ + public static LensX, Holder, Part1, Part2> holder() { + return immutable((hld) -> hld.getValue(), (hld, val) -> hld.map((vl) -> val)); + } + + /** + * Create a lens for updating tagged pairs. + * + * TODO: Should this be on a Tagged class that is isomorphic to pair + * instead? + * + * @param The first data type + * @param The second data type + * @param The tag type + * + * @return A lens that operates on the value of a tagged pair. + */ + public static LensX, Pair, A, B> tagged() { + return immutable((par) -> par.getRight(), (par, val) -> par.mapRight((vl) -> val)); + } + + /** + * Creates a lens which focuses on a piece of internal state. + * + * @param The first state type + * @param The second state type + * + * @param val The initial state value + * + * @return A lens that focuses on the given internal state. + */ + public static LensX state(A val) { + Holder hold = Holder.of(val); + return immutable((whole) -> hold.getValue(), (whole, vl) -> hold.map((arg) -> vl)); + } + + /** + * Creates a lens which focuses on a piece of mutable internal state. + * + * @param The state type + * + * @param val The initial state value + * + * @return A lens that focuses on the given internal state. + */ + public static MutableLens stateM(A val) { + Holder hold = Holder.of(val); + return mutable((whole) -> hold.getValue(), (whole, vl) -> hold.replace(vl)); + } +} + +final class FunctionalLensX implements LensX { + private final BiFunction setter; + private final Function getter; + + FunctionalLensX(Function getter, BiFunction setter) { + this.setter = setter; + this.getter = getter; + } + + @Override + public P1 get(W1 source) { + return getter.apply(source); + } + + @Override + public W2 set(W1 source, P2 val) { + return setter.apply(source, val); + } +} + +final class FunctionalMutableLens implements MutableLens { + private final BiConsumer mutator; + private final Function getter; + + FunctionalMutableLens(Function getter, BiConsumer mutator) { + this.mutator = mutator; + this.getter = getter; + } + + @Override + public Part get(Whole source) { + return getter.apply(source); + } + + @Override + public void mutate(Whole source, Part val) { + mutator.accept(source, val); + } +} diff --git a/src/main/java/bjc/functypes/optics/MutableLens.java b/src/main/java/bjc/functypes/optics/MutableLens.java new file mode 100644 index 0000000..b6fca5d --- /dev/null +++ b/src/main/java/bjc/functypes/optics/MutableLens.java @@ -0,0 +1,29 @@ +package bjc.functypes.optics; + +/** + * A type-invariant lens for mutating objects. + * + * Note that there is no type-variant version, because that wouldn't make much sense. + * + * Also, mixing mutable and immutable lenses may lead to confusion. + * + * @author bjcul + * + * @param The type the lens is used on + * @param The type the lens is focused on + */ +public interface MutableLens extends Lens { + /** + * Apply a mutation to an item. + * + * @param source The item to use the lens on. + * @param val The new value for the focused field. + */ + void mutate(Whole source, Part val); + + @Override + default Whole set(Whole source, Part val) { + mutate(source, val); + return source; + } +} diff --git a/src/main/java/bjc/functypes/optics/Optic.java b/src/main/java/bjc/functypes/optics/Optic.java new file mode 100644 index 0000000..949eeca --- /dev/null +++ b/src/main/java/bjc/functypes/optics/Optic.java @@ -0,0 +1,15 @@ +package bjc.functypes.optics; + +/** + * General interface for optics of varying sorts + * + * @author bjcul + * + * @param The first item the optic is used on + * @param The second item the optic is used on + * @param The first item the optic focuses on + * @param The second item the optic focuses on + */ +public interface Optic { + // Marker interface +} diff --git a/src/main/java/bjc/functypes/optics/PrismX.java b/src/main/java/bjc/functypes/optics/PrismX.java new file mode 100644 index 0000000..a9c0dd2 --- /dev/null +++ b/src/main/java/bjc/functypes/optics/PrismX.java @@ -0,0 +1,5 @@ +package bjc.functypes.optics; + +public interface PrismX extends Optic { + +} diff --git a/src/main/java/bjc/functypes/optics/impl/package-info.java b/src/main/java/bjc/functypes/optics/impl/package-info.java new file mode 100644 index 0000000..f309401 --- /dev/null +++ b/src/main/java/bjc/functypes/optics/impl/package-info.java @@ -0,0 +1 @@ +package bjc.functypes.optics.impl; \ No newline at end of file diff --git a/src/main/java/bjc/functypes/optics/package-info.java b/src/main/java/bjc/functypes/optics/package-info.java new file mode 100644 index 0000000..c187df9 --- /dev/null +++ b/src/main/java/bjc/functypes/optics/package-info.java @@ -0,0 +1,4 @@ +/** + * + */ +package bjc.functypes.optics; \ No newline at end of file diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 84d1b2f..fdbf1b6 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,3 +1,9 @@ +/** + * Contains various more esoteric data structures. + * + * @author bjculkin + * + */ module esodata { exports bjc.data; exports bjc.esodata; diff --git a/src/test/java/bjc/data/MarkListIteratorTest.java b/src/test/java/bjc/data/MarkListIteratorTest.java deleted file mode 100644 index 6eb4994..0000000 --- a/src/test/java/bjc/data/MarkListIteratorTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package bjc.data; - -import static org.junit.Assert.*; - -import java.util.Arrays; -import java.util.List; - -import org.junit.Test; - -import bjc.test.TestUtils; - -/** - * Tests for {@link MarkListIterator} - * @author bjcul - * - */ -@SuppressWarnings("javadoc") -public class MarkListIteratorTest { - - @Test - public void test() { - List list1 = Arrays.asList("a", "b", "c"); - - MarkListIterator itr1 = new MarkListIterator<>(list1); - itr1.mark(); - - assertFalse(itr1.hasPrevious()); - assertTrue(itr1.hasNext()); - TestUtils.assertIteratorEquals(itr1, "a", "b", "c"); - - assertTrue(itr1.hasPrevious()); - assertFalse(itr1.hasNext()); - - itr1.rollback(false); - - assertFalse(itr1.hasPrevious()); - assertTrue(itr1.hasNext()); - TestUtils.assertIteratorEquals(itr1, "a", "b", "c"); - } - -} diff --git a/src/test/java/bjc/test/data/EitherTest.java b/src/test/java/bjc/test/data/EitherTest.java index 5e2613c..04cff09 100644 --- a/src/test/java/bjc/test/data/EitherTest.java +++ b/src/test/java/bjc/test/data/EitherTest.java @@ -8,6 +8,7 @@ import org.junit.*; import bjc.data.Either; +@SuppressWarnings("javadoc") public class EitherTest { private Either leftEither; diff --git a/src/test/java/bjc/test/data/GeneratingIteratorTest.java b/src/test/java/bjc/test/data/GeneratingIteratorTest.java new file mode 100644 index 0000000..e1c5fbd --- /dev/null +++ b/src/test/java/bjc/test/data/GeneratingIteratorTest.java @@ -0,0 +1,20 @@ +package bjc.test.data; + +import static bjc.test.TestUtils.assertIteratorEquals; +import java.util.Iterator; + +import org.junit.Test; + +import bjc.data.GeneratingIterator; + +@SuppressWarnings("javadoc") +public class GeneratingIteratorTest { + + @Test + public void test() { + Iterator itr = new GeneratingIterator<>("", str -> str + "A", str -> str.equalsIgnoreCase("AAAA")); + + assertIteratorEquals(false, itr, "A", "AA", "AAA", "AAAA"); + } + +} diff --git a/src/test/java/bjc/test/data/MarkListIteratorTest.java b/src/test/java/bjc/test/data/MarkListIteratorTest.java new file mode 100644 index 0000000..366755f --- /dev/null +++ b/src/test/java/bjc/test/data/MarkListIteratorTest.java @@ -0,0 +1,42 @@ +package bjc.test.data; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +import bjc.data.MarkListIterator; +import bjc.test.TestUtils; + +/** + * Tests for {@link MarkListIterator} + * @author bjcul + * + */ +@SuppressWarnings("javadoc") +public class MarkListIteratorTest { + + @Test + public void test() { + List list1 = Arrays.asList("a", "b", "c"); + + MarkListIterator itr1 = new MarkListIterator<>(list1); + itr1.mark(); + + assertFalse(itr1.hasPrevious()); + assertTrue(itr1.hasNext()); + TestUtils.assertIteratorEquals(itr1, "a", "b", "c"); + + assertTrue(itr1.hasPrevious()); + assertFalse(itr1.hasNext()); + + itr1.rollback(false); + + assertFalse(itr1.hasPrevious()); + assertTrue(itr1.hasNext()); + TestUtils.assertIteratorEquals(itr1, "a", "b", "c"); + } + +} diff --git a/src/test/java/bjc/test/data/SimpleTreeTest.java b/src/test/java/bjc/test/data/SimpleTreeTest.java new file mode 100644 index 0000000..db7457b --- /dev/null +++ b/src/test/java/bjc/test/data/SimpleTreeTest.java @@ -0,0 +1,29 @@ +package bjc.test.data; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import bjc.data.SimpleTree; + +public class SimpleTreeTest { + + @Test + public void test() { + SimpleTree tree1 = new SimpleTree<>("a"); + SimpleTree tree2 = new SimpleTree<>("a"); + + assertEquals(tree1, tree2); + + tree1.addChild("b"); + + assertNotEquals(tree1, tree2); + + tree2.addChild("b"); + + tree1.equals(tree2); + + assertEquals(tree1, tree2); + } + +} diff --git a/src/test/java/bjc/test/data/TransformIteratorTest.java b/src/test/java/bjc/test/data/TransformIteratorTest.java new file mode 100644 index 0000000..8a10b10 --- /dev/null +++ b/src/test/java/bjc/test/data/TransformIteratorTest.java @@ -0,0 +1,22 @@ +package bjc.test.data; + +import static bjc.test.TestUtils.assertIteratorEquals; +import java.util.Iterator; + +import org.junit.Test; + +import bjc.data.ArrayIterator; +import bjc.data.TransformIterator; + +@SuppressWarnings("javadoc") +public class TransformIteratorTest { + + @Test + public void test() { + Iterator itr1 = new ArrayIterator<>("a", "b", "c"); + Iterator itr2 = new TransformIterator<>(itr1, str -> str + "X"); + + assertIteratorEquals(itr2, "aX", "bX", "cX"); + } + +} diff --git a/src/test/java/bjc/test/esodata/NestListTest.java b/src/test/java/bjc/test/esodata/NestListTest.java index 5b91126..c97859e 100644 --- a/src/test/java/bjc/test/esodata/NestListTest.java +++ b/src/test/java/bjc/test/esodata/NestListTest.java @@ -6,7 +6,6 @@ import java.util.*; import org.junit.*; -import bjc.*; import bjc.esodata.NestList; import bjc.test.TestUtils; diff --git a/src/test/java/bjc/test/functypes/optics/LensesTest.java b/src/test/java/bjc/test/functypes/optics/LensesTest.java new file mode 100644 index 0000000..ff13a42 --- /dev/null +++ b/src/test/java/bjc/test/functypes/optics/LensesTest.java @@ -0,0 +1,18 @@ +package bjc.test.functypes.optics; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import bjc.data.Holder; +import bjc.functypes.optics.Lenses; + +public class LensesTest { + + @Test + public void test() { + Holder holder = Holder.of(5); + + } + +} diff --git a/src/test/java/bjc/test/functypes/optics/package-info.java b/src/test/java/bjc/test/functypes/optics/package-info.java new file mode 100644 index 0000000..45e69ec --- /dev/null +++ b/src/test/java/bjc/test/functypes/optics/package-info.java @@ -0,0 +1 @@ +package bjc.test.functypes.optics; \ No newline at end of file -- cgit v1.2.3