From 95db633e090ccd36fc3ee7b6905d8e941970ff74 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Thu, 18 May 2023 22:07:02 -0500 Subject: [PATCH] update unit tests for QuadTree refactoring --- .../core/util/objects/quadTree/QuadTree.java | 39 +++++++++++++++++++ core/src/test/java/tests/QuadTreeTest.java | 20 +++++----- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java b/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java index aa502ba8e..3bda99294 100644 --- a/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java +++ b/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java @@ -241,6 +241,45 @@ public class QuadTree } }); + +// // remove out of bound nodes and clean up empty nodes +// // Note: this will iterate over a lot of unnecessary nodes, hopefully speed won't be an issue +// Iterator rootNodePosIterator = this.rootNodePosIterator(); +// while (rootNodePosIterator.hasNext()) +// { +// // get the root node (regular nodeIterators won't return them if they are out of bounds) +// DhSectionPos rootPos = rootNodePosIterator.next(); +// QuadNode rootNode = this.getOrSetNode(rootPos, false, null, false); +// if (rootNode == null) +// { +// continue; +// } +// +// // remove any child nodes that are out of bounds +// Iterator> nodeIterator = this.nodeIterator(); +// while (nodeIterator.hasNext()) +// { +// QuadNode node = nodeIterator.next(); +// if(!this.isSectionPosInBounds(node.sectionPos)) +// { +// // node is out of bounds +// +// // FIXME(?) this appears to potentially return large nodes that are partially or entirely in bounds +// +// if (node.getNonNullChildCount() == 0) +// { +// // no child nodes, can be safely removed +// nodeIterator.remove(); +// } +// else +// { +// // node can't be removed, but its value can be set to null +// node.value = null; +// } +// } +// } +// } + } public final DhBlockPos2D getCenterBlockPos() { return this.centerBlockPos; } diff --git a/core/src/test/java/tests/QuadTreeTest.java b/core/src/test/java/tests/QuadTreeTest.java index c90937d5b..8e2fd827f 100644 --- a/core/src/test/java/tests/QuadTreeTest.java +++ b/core/src/test/java/tests/QuadTreeTest.java @@ -687,7 +687,7 @@ public class QuadTreeTest rootNode.setValue(sectionPos, 1); } - Assert.assertEquals("node not filled", 4, rootNode.getChildValueCount()); + Assert.assertEquals("node not filled", 4, rootNode.getNonNullChildCount()); for (int i = 0; i < 4; i++) @@ -722,7 +722,7 @@ public class QuadTreeTest DhSectionPos childPos = childPosIterator.next(); centerRootNode.setValue(childPos, 1); } - Assert.assertEquals("center node not filled", 4, centerRootNode.getChildValueCount()); + Assert.assertEquals("center node not filled", 4, centerRootNode.getNonNullChildCount()); @@ -742,7 +742,7 @@ public class QuadTreeTest offsetRootNode.setValue(childPos, 1); } // TODO James thought this shouldn't work for all 4 nodes, but he must've thought wrong. - Assert.assertEquals("offset should only contain some children.", 4, offsetRootNode.getChildValueCount()); + Assert.assertEquals("offset should only contain some children.", 4, offsetRootNode.getNonNullChildCount()); } @@ -782,8 +782,8 @@ public class QuadTreeTest // validate nodes were added - Assert.assertEquals(4, rootNode.getChildValueCount()); - Assert.assertEquals(4, rootNode.getNode(midNodePos).getChildValueCount()); + Assert.assertEquals(4, rootNode.getNonNullChildCount()); + Assert.assertEquals(4, rootNode.getNode(midNodePos).getNonNullChildCount()); @@ -792,30 +792,30 @@ public class QuadTreeTest // remove two leaf nodes from the root DhSectionPos leafPos = new DhSectionPos((byte)9, 1, 1); rootNode.setValue(leafPos, null); - Assert.assertEquals(3, rootNode.getChildValueCount()); + Assert.assertEquals(3, rootNode.getNonNullChildCount()); Assert.assertNull("Node wasn't deleted", rootNode.getNode(leafPos)); leafPos = new DhSectionPos((byte)9, 0, 1); rootNode.setValue(leafPos, null); - Assert.assertEquals(2, rootNode.getChildValueCount()); + Assert.assertEquals(2, rootNode.getNonNullChildCount()); Assert.assertNull("Node wasn't deleted", rootNode.getNode(leafPos)); // remove // remove all child nodes - Assert.assertEquals(4, rootNode.getNode(midNodePos).getChildValueCount()); + Assert.assertEquals(4, rootNode.getNode(midNodePos).getNonNullChildCount()); // remove all but one, mid-node should still be present rootNode.setValue(new DhSectionPos((byte)8, 0, 0), null); rootNode.setValue(new DhSectionPos((byte)8, 0, 1), null); rootNode.setValue(new DhSectionPos((byte)8, 1, 0), null); - Assert.assertEquals(1, rootNode.getNode(midNodePos).getChildValueCount()); + Assert.assertEquals(1, rootNode.getNode(midNodePos).getNonNullChildCount()); // remove last mid-node child, mid-node should now be removed rootNode.setValue(new DhSectionPos((byte)8, 1, 1), null); Assert.assertNull("Mid node not deleted.", rootNode.getNode(midNodePos)); - Assert.assertEquals(3, rootNode.getChildValueCount()); + Assert.assertEquals(3, rootNode.getNonNullChildCount()); }