Skip to content

Commit

Permalink
Fleshed out some add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
decitrig committed Oct 27, 2011
1 parent 9f833ba commit 5acb830
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
14 changes: 12 additions & 2 deletions junit/net/decitrig/galaxy/GalaxyTreeTest.java
Expand Up @@ -6,18 +6,28 @@


public class GalaxyTreeTest { public class GalaxyTreeTest {


private static final Galaxy GALAXY2 = new Galaxy(1, 3.0, 3.0);
private static final Galaxy GALAXY1 = new Galaxy(1, 2.0, 2.0);

@Test @Test
public void testAddIncrementsSize() { public void testAddIncrementsSize() {
GalaxyTree tree = new GalaxyTree(); GalaxyTree tree = new GalaxyTree();
assertEquals(0, tree.size()); assertEquals(0, tree.size());
assertTrue(tree.isEmpty()); assertTrue(tree.isEmpty());
tree.add(new Galaxy(1, 2.0, 2.0)); assertTrue(tree.add(GALAXY1));
assertEquals(1, tree.size()); assertEquals(1, tree.size());
assertFalse(tree.isEmpty()); assertFalse(tree.isEmpty());


tree.add(new Galaxy(2, 3.0, 3.0)); assertTrue(tree.add(GALAXY2));
assertEquals(2, tree.size()); assertEquals(2, tree.size());
assertFalse(tree.isEmpty()); assertFalse(tree.isEmpty());
} }


@Test
public void testInsertDuplicate() {
GalaxyTree tree = new GalaxyTree();
assertTrue(tree.add(GALAXY1));
assertFalse(tree.add(new Galaxy(1, 2.0, 2.0)));
assertEquals(1, tree.size());
}
} }
28 changes: 22 additions & 6 deletions src/net/decitrig/galaxy/GalaxyTree.java
@@ -1,6 +1,7 @@
package net.decitrig.galaxy; package net.decitrig.galaxy;


import java.util.AbstractCollection; import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;


Expand Down Expand Up @@ -46,6 +47,16 @@ public int compare(Galaxy a, Galaxy b) {
} }
} }


@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}

@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}

private static class LatitudeComparator implements Comparator<Galaxy> { private static class LatitudeComparator implements Comparator<Galaxy> {
static final LatitudeComparator instance = new LatitudeComparator(); static final LatitudeComparator instance = new LatitudeComparator();
@Override @Override
Expand Down Expand Up @@ -104,12 +115,7 @@ public boolean isEmpty() {
} }


public Pair<Node, Boolean> insert(Galaxy galaxy, int level) { public Pair<Node, Boolean> insert(Galaxy galaxy, int level) {
int result; int result = compare(galaxy, level);
if (level % 2 == 0) {
result = LongitudeComparator.instance.compare(galaxy, this.galaxy);
} else {
result = LatitudeComparator.instance.compare(galaxy, this.galaxy);
}
if (result < 0) { if (result < 0) {
Pair<Node, Boolean> pair = left.insert(galaxy, level + 1); Pair<Node, Boolean> pair = left.insert(galaxy, level + 1);
left = pair.head(); left = pair.head();
Expand All @@ -122,5 +128,15 @@ public Pair<Node, Boolean> insert(Galaxy galaxy, int level) {
return Pair.of((Node) this, false); return Pair.of((Node) this, false);
} }
} }

private int compare(Galaxy galaxy, int level) {
int result;
if (level % 2 == 0) {
result = LongitudeComparator.instance.compare(galaxy, this.galaxy);
} else {
result = LatitudeComparator.instance.compare(galaxy, this.galaxy);
}
return result;
}
} }
} }

0 comments on commit 5acb830

Please sign in to comment.