diff options
Diffstat (limited to 'src/test/java/com/amazon/carbonado/cursor')
3 files changed, 603 insertions, 0 deletions
diff --git a/src/test/java/com/amazon/carbonado/cursor/EmptyCursorFactory.java b/src/test/java/com/amazon/carbonado/cursor/EmptyCursorFactory.java new file mode 100644 index 0000000..fa27350 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/cursor/EmptyCursorFactory.java @@ -0,0 +1,30 @@ +/*
+ * Copyright 2006 Amazon Technologies, Inc. or its affiliates.
+ * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
+ * of Amazon Technologies, Inc. or its affiliates. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.amazon.carbonado.cursor;
+
+/**
+ * Creates new EmptyCursor instances, for tests in other packages.
+ *
+ * @author Brian S O'Neill
+ */
+public class EmptyCursorFactory {
+ public static EmptyCursor newEmptyCursor() {
+ return new EmptyCursor();
+ }
+}
diff --git a/src/test/java/com/amazon/carbonado/cursor/TestCursors.java b/src/test/java/com/amazon/carbonado/cursor/TestCursors.java new file mode 100644 index 0000000..f063a59 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/cursor/TestCursors.java @@ -0,0 +1,389 @@ +/*
+ * Copyright 2006 Amazon Technologies, Inc. or its affiliates.
+ * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
+ * of Amazon Technologies, Inc. or its affiliates. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.amazon.carbonado.cursor;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import com.amazon.carbonado.Cursor;
+import com.amazon.carbonado.FetchException;
+
+import com.amazon.carbonado.stored.Dummy;
+import com.amazon.carbonado.stored.StorableTestMinimal;
+
+/**
+ *
+ * @author Brian S O'Neill
+ */
+public class TestCursors extends TestCase {
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(TestCursors.class);
+ }
+
+ public TestCursors(String name) {
+ super(name);
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+
+ public void testUnion() throws Exception {
+ Cursor<Element> left, right, union;
+
+ // Two empty sets.
+ left = createElements();
+ right = createElements();
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union);
+
+ // Right set empty.
+ left = createElements(1, 2, 3, 4);
+ right = createElements();
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union, 1, 2, 3, 4);
+
+ // Left set empty.
+ left = createElements();
+ right = createElements(3, 4, 5, 6);
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union, 3, 4, 5, 6);
+
+ // No overlap.
+ left = createElements(1, 2, 3 );
+ right = createElements( 4, 5, 6);
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union, 1, 2, 3, 4, 5, 6);
+
+ // Overlap.
+ left = createElements(1, 2, 3, 4 );
+ right = createElements( 3, 4, 5, 6);
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union, 1, 2, 3, 4, 5, 6);
+
+ // Swapped overlap.
+ left = createElements( 3, 4, 5, 6);
+ right = createElements(1, 2, 3, 4 );
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union, 1, 2, 3, 4, 5, 6);
+
+ // Equivalent.
+ left = createElements(1, 2, 3, 4, 5, 6);
+ right = createElements(1, 2, 3, 4, 5, 6);
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union, 1, 2, 3, 4, 5, 6);
+
+ // Complex.
+ left = createElements(1, 2, 3, 5, 6, 7, 11, 12, 13, 17, 18, 19);
+ right = createElements(1, 2, 4, 5, 6, 9, 10, 13, 14, 17, 18 );
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19);
+
+ // Dups.
+ left = createElements(1, 2, 2, 3, 3, 3, 3 );
+ right = createElements(1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4);
+ union = new UnionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(union, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4);
+ }
+
+ public void testIntersection() throws Exception {
+ Cursor<Element> left, right, inter;
+
+ // Two empty sets.
+ left = createElements();
+ right = createElements();
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter);
+
+ // Right set empty.
+ left = createElements(1, 2, 3, 4);
+ right = createElements();
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter);
+
+ // Left set empty.
+ left = createElements();
+ right = createElements(3, 4, 5, 6);
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter);
+
+ // No overlap.
+ left = createElements(1, 2, 3 );
+ right = createElements( 4, 5, 6);
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter);
+
+ // Overlap.
+ left = createElements(1, 2, 3, 4 );
+ right = createElements( 3, 4, 5, 6);
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter, 3, 4);
+
+ // Swapped overlap.
+ left = createElements( 3, 4, 5, 6);
+ right = createElements(1, 2, 3, 4 );
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter, 3, 4);
+
+ // Equivalent.
+ left = createElements(1, 2, 3, 4, 5, 6);
+ right = createElements(1, 2, 3, 4, 5, 6);
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter, 1, 2, 3, 4, 5, 6);
+
+ // Complex.
+ left = createElements(1, 2, 3, 5, 6, 7, 11, 12, 13, 17, 18, 19);
+ right = createElements(1, 2, 4, 5, 6, 9, 10, 13, 14, 17, 18 );
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter, 1, 2, 5, 6, 13, 17, 18);
+
+ // Dups.
+ left = createElements(1, 2, 2, 3, 3, 3, 3 );
+ right = createElements(1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4);
+ inter = new IntersectionCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(inter, 1, 2, 2);
+ }
+
+ public void testDifference() throws Exception {
+ Cursor<Element> left, right, diff;
+
+ // Two empty sets.
+ left = createElements();
+ right = createElements();
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff);
+
+ // Right set empty.
+ left = createElements(1, 2, 3, 4);
+ right = createElements();
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 1, 2, 3, 4);
+
+ // Left set empty.
+ left = createElements();
+ right = createElements(3, 4, 5, 6);
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff);
+
+ // No overlap.
+ left = createElements(1, 2, 3 );
+ right = createElements( 4, 5, 6);
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 1, 2, 3);
+
+ // Overlap.
+ left = createElements(1, 2, 3, 4 );
+ right = createElements( 3, 4, 5, 6);
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 1, 2);
+
+ // Swapped overlap.
+ left = createElements( 3, 4, 5, 6);
+ right = createElements(1, 2, 3, 4 );
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 5, 6);
+
+ // Equivalent.
+ left = createElements(1, 2, 3, 4, 5, 6);
+ right = createElements(1, 2, 3, 4, 5, 6);
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff);
+
+ // Complex.
+ left = createElements(1, 2, 3, 5, 6, 7, 11, 12, 13, 17, 18, 19);
+ right = createElements(1, 2, 4, 5, 6, 9, 10, 13, 14, 17, 18 );
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 3, 7, 11, 12, 19);
+
+ // Dups.
+ left = createElements(1, 2, 2, 3, 3, 3, 3 );
+ right = createElements(1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4);
+ diff = new DifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 3, 3, 3, 3);
+ }
+
+ public void testSymmetricDifference() throws Exception {
+ Cursor<Element> left, right, diff;
+
+ // Two empty sets.
+ left = createElements();
+ right = createElements();
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff);
+
+ // Right set empty.
+ left = createElements(1, 2, 3, 4);
+ right = createElements();
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 1, 2, 3, 4);
+
+ // Left set empty.
+ left = createElements();
+ right = createElements(3, 4, 5, 6);
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 3, 4, 5, 6);
+
+ // No overlap.
+ left = createElements(1, 2, 3 );
+ right = createElements( 4, 5, 6);
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 1, 2, 3, 4, 5, 6);
+
+ // Overlap.
+ left = createElements(1, 2, 3, 4 );
+ right = createElements( 3, 4, 5, 6);
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 1, 2, 5, 6);
+
+ // Swapped overlap.
+ left = createElements( 3, 4, 5, 6);
+ right = createElements(1, 2, 3, 4 );
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 1, 2, 5, 6);
+
+ // Equivalent.
+ left = createElements(1, 2, 3, 4, 5, 6);
+ right = createElements(1, 2, 3, 4, 5, 6);
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff);
+
+ // Complex.
+ left = createElements(1, 2, 3, 5, 6, 7, 11, 12, 13, 17, 18, 19);
+ right = createElements(1, 2, 4, 5, 6, 9, 10, 13, 14, 17, 18 );
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 3, 4, 7, 9, 10, 11, 12, 14, 19);
+
+ // Dups.
+ left = createElements(1, 2, 2, 3, 3, 3, 3 );
+ right = createElements(1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4);
+ diff = new SymmetricDifferenceCursor<Element>(left, right, new ElementComparator());
+
+ compareElements(diff, 1, 1, 1, 1, 2, 3, 3, 3, 3, 4, 4, 4);
+ }
+
+ private Cursor<Element> createElements(int... ids) {
+ Arrays.sort(ids);
+ Element[] elements = new Element[ids.length];
+ for (int i=0; i<ids.length; i++) {
+ elements[i] = new Element(ids[i]);
+ }
+ return new IteratorCursor<Element>(Arrays.asList(elements));
+ }
+
+ private void compareElements(Cursor<Element> elements, int... expectedIDs)
+ throws FetchException
+ {
+ for (int id : expectedIDs) {
+ if (elements.hasNext()) {
+ Element e = elements.next();
+ if (e.getId() != id) {
+ fail("Element mismatch: expected=" + id + ", actual=" + e.getId());
+ elements.close();
+ return;
+ }
+ } else {
+ fail("Too few elements in cursor");
+ return;
+ }
+ }
+
+ if (elements.hasNext()) {
+ Element e = elements.next();
+ fail("Too many elements in cursor: " + e.getId());
+ elements.close();
+ }
+ }
+
+ private static class Element extends Dummy implements StorableTestMinimal {
+ private int mID;
+
+ Element(int id) {
+ mID = id;
+ }
+
+ public int getId() {
+ return mID;
+ }
+
+ public void setId(int id) {
+ mID = id;
+ }
+
+ public String toString() {
+ return "element " + mID;
+ }
+ }
+
+ private static class ElementComparator implements Comparator<Element> {
+ public int compare(Element a, Element b) {
+ int ai = a.getId();
+ int bi = b.getId();
+ if (ai < bi) {
+ return -1;
+ } else if (ai > bi) {
+ return 1;
+ }
+ return 0;
+ }
+ }
+}
diff --git a/src/test/java/com/amazon/carbonado/cursor/TestGroupedCursor.java b/src/test/java/com/amazon/carbonado/cursor/TestGroupedCursor.java new file mode 100644 index 0000000..3d5e42f --- /dev/null +++ b/src/test/java/com/amazon/carbonado/cursor/TestGroupedCursor.java @@ -0,0 +1,184 @@ +/*
+ * Copyright 2006 Amazon Technologies, Inc. or its affiliates.
+ * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
+ * of Amazon Technologies, Inc. or its affiliates. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.amazon.carbonado.cursor;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import com.amazon.carbonado.Cursor;
+import com.amazon.carbonado.FetchException;
+
+/**
+ *
+ *
+ * @author Brian S O'Neill
+ */
+public class TestGroupedCursor extends TestCase {
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+
+ public static TestSuite suite() {
+ return new TestSuite(TestGroupedCursor.class);
+ }
+
+ public TestGroupedCursor(String name) {
+ super(name);
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+
+ public void testGrouping() throws Exception {
+ List<Triple> triples = new ArrayList<Triple>();
+ triples.add(new Triple(1, 1, 1));
+ triples.add(new Triple(1, 1, 2));
+ triples.add(new Triple(1, 1, 3));
+ triples.add(new Triple(1, 2, 5));
+ triples.add(new Triple(1, 2, 9));
+ triples.add(new Triple(3, 1, 10));
+ triples.add(new Triple(3, 2, 16));
+ triples.add(new Triple(3, 2, 100));
+
+ // Should already be sorted, but no harm done
+ Collections.sort(triples);
+
+ Cursor<Pair> cursor = new GroupedCursor<Triple, Pair>
+ (new IteratorCursor<Triple>(triples), Triple.class, "a", "b")
+ {
+ private Pair aggregate;
+
+ protected void beginGroup(Triple groupLeader) {
+ aggregate = new Pair(groupLeader.getA(), groupLeader.getB());
+ aggregate.add(groupLeader.getC());
+ }
+
+ protected void addToGroup(Triple groupMember) {
+ aggregate.add(groupMember.getC());
+ }
+
+ protected Pair finishGroup() {
+ return aggregate;
+ }
+ };
+
+ List<Pair> pairs = new ArrayList<Pair>();
+
+ while (cursor.hasNext()) {
+ pairs.add(cursor.next());
+ }
+
+ assertEquals(4, pairs.size());
+
+ assertEquals(1, pairs.get(0).getA());
+ assertEquals(1, pairs.get(0).getB());
+ assertEquals(6, pairs.get(0).sum);
+
+ assertEquals(1, pairs.get(1).getA());
+ assertEquals(2, pairs.get(1).getB());
+ assertEquals(14, pairs.get(1).sum);
+
+ assertEquals(3, pairs.get(2).getA());
+ assertEquals(1, pairs.get(2).getB());
+ assertEquals(10, pairs.get(2).sum);
+
+ assertEquals(3, pairs.get(3).getA());
+ assertEquals(2, pairs.get(3).getB());
+ assertEquals(116, pairs.get(3).sum);
+ }
+
+ static int compare(int x, int y) {
+ if (x < y) {
+ return -1;
+ } else if (x > y) {
+ return 1;
+ }
+ return 0;
+ }
+
+ public static class Pair implements Comparable {
+ final int a;
+ final int b;
+
+ int sum;
+
+ Pair(int a, int b) {
+ this.a = a;
+ this.b = b;
+ }
+
+ public int getA() {
+ return a;
+ }
+
+ public int getB() {
+ return b;
+ }
+
+ public void add(int x) {
+ sum += x;
+ }
+
+ public int compareTo(Object obj) {
+ Pair other = (Pair) obj;
+ int result = compare(a, other.a);
+ if (result == 0) {
+ result = compare(b, other.b);
+ }
+ return result;
+ }
+
+ public String toString() {
+ return "a=" + a + ", b=" + b + ", sum=" + sum;
+ }
+ }
+
+ public static class Triple extends Pair {
+ final int c;
+
+ Triple(int a, int b, int c) {
+ super(a, b);
+ this.c = c;
+ }
+
+ public int getC() {
+ return c;
+ }
+
+ public int compareTo(Object obj) {
+ int result = super.compareTo(obj);
+ if (result == 0) {
+ Triple other = (Triple) obj;
+ result = compare(c, other.c);
+ }
+ return result;
+ }
+
+ public String toString() {
+ return "a=" + a + ", b=" + b + ", c=" + c;
+ }
+ }
+}
|