public void Test_LeafSubgrid_IsEmpty() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); ILeafSubGrid leaf = new LeafSubGrid(tree, null, tree.NumLevels); leaf.IsEmpty().Should().BeTrue(); }
public void Test_SubGridTree_InvalidCreation_TreeLevels() { // Test creating invalid subgrid trees ISubGridTree invalid = null; try { invalid = new SubGridTree(0, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); Assert.True(false, "SubGridTree permitted creation with invalid subgrid tree level"); } catch (Exception e) { Assert.True(e is ArgumentException, "Invalid exception raised for invalid argument to SubGridTree constructor"); } try { invalid = new SubGridTree(10, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); Assert.True(false, "SubGridTree permitted creation with invalid subgrid tree level"); } catch (Exception e) { Assert.True(e is ArgumentException, "Invalid exception raised for invalid argument to SubGridTree constructor"); } try { invalid = new SubGridTree(10, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); Assert.True(false, "SubGridTree permitted creation with invalid subgrid tree level"); } catch (Exception e) { Assert.True(e is ArgumentException, "Invalid exception raised for invalid argument to SubGridTree constructor"); } }
public void Test_NodeSubGrid_ExceedSparcityLimit() { var tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); var parentSubgrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 2); var sparcityLimit = NodeSubGrid.SubGridTreeNodeCellSparcityLimit; // Add sparcity limit + 1 child node subgrids to the parent for (int i = 0; i < sparcityLimit + 1; i++) { var subgrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 1); parentSubgrid.SetSubGrid(i % SubGridTreeConsts.SubGridTreeDimension, i / SubGridTreeConsts.SubGridTreeDimension, subgrid); } parentSubgrid.CountNonNullCells().Should().Be((int)sparcityLimit + 1); // Read through the sub grids added, plus another one rto cover access failure for (int i = 0; i < sparcityLimit + 2; i++) { var subgrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 1); subgrid.Should().NotBeNull(); } var subGrid = parentSubgrid.GetSubGrid((int)(sparcityLimit + 2) % SubGridTreeConsts.SubGridTreeDimension, (int)(sparcityLimit + 2) / SubGridTreeConsts.SubGridTreeDimension); subGrid.Should().BeNull(); // Drain the sub grids back out of the node for (int i = 0; i < sparcityLimit + 1; i++) { parentSubgrid.SetSubGrid(i % SubGridTreeConsts.SubGridTreeDimension, i / SubGridTreeConsts.SubGridTreeDimension, null); } parentSubgrid.CountNonNullCells().Should().Be(0); }
public void Test_LeafSubgrid_Creation() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); ILeafSubGrid leaf = null; // Test creation of a leaf node without an owner tree try { leaf = new LeafSubGrid(null, null, (byte)(tree.NumLevels + 1)); Assert.True(false, "Was able to create a leaf subgrid with no owning tree"); } catch (Exception) { // As expected } // Test creation of a leaf node at an inappropriate level try { leaf = new LeafSubGrid(tree, null, (byte)(tree.NumLevels + 1)); Assert.True(false, "Was able to create a leaf subgrid at an inappropriate level"); } catch (Exception) { // As expected } leaf = new LeafSubGrid(tree, null, tree.NumLevels); Assert.True(leaf != null && leaf.Level == tree.NumLevels); }
public void Test_NodeSubGrid_ForEachSubgrid_SpatialSubsetScans() { int count; SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); INodeSubGrid subgrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 1); INodeSubGrid parentSubgrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 2); // Fill all of the parent subgrid with new child subgrids using SetSubGrid for (int i = 0; i < SubGridTreeConsts.CellsPerSubGrid; i++) { parentSubgrid.SetSubGrid((byte)(i / SubGridTreeConsts.SubGridTreeDimension), (byte)(i % SubGridTreeConsts.SubGridTreeDimension), new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 1)); } // Iterate over a subset of the subgrids counting them (from the cell at (10, 10) to the cell at (29, 29) // an interval of 20 cells in the X and Y dimensions count = 0; parentSubgrid.ForEachSubGrid(x => { count++; return(SubGridProcessNodeSubGridResult.OK); }, 10, 10, 29, 29); // ==> Should scan 400 cells Assert.Equal(400, count); }
public void Test_SubGrid_ContainsOTGCell() { ISubGrid leafSubgrid = null; SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); // Create a leaf subgrid with it's cell origin (IndexOriginOffset, IndexOriginOffset) // matching the real work coordaintge origin (0, 0) leafSubgrid = tree.ConstructPathToCell(tree.IndexOriginOffset, tree.IndexOriginOffset, SubGridPathConstructionType.CreateLeaf); Assert.NotNull(leafSubgrid); Assert.True(leafSubgrid.OriginX == tree.IndexOriginOffset && leafSubgrid.OriginX == tree.IndexOriginOffset, "Failed to create leaf node at the expected location"); // Check that a 1m x 1m square (the size of the cells in the subgridtree created above) registers as being // a part of the newly created subgrid. First, get the cell enclosing that worl location and then ask // the subgrid if it contains it int CellX, CellY; Assert.True(tree.CalculateIndexOfCellContainingPosition(0.5, 0.5, out CellX, out CellY), "Failed to get cell index for (0.5, 0.5)"); Assert.True(leafSubgrid.ContainsOTGCell(CellX, CellY), "Leaf subgrid denies enclosing the OTG cell at (0.5, 0.5)"); }
public void Test_SubGrid_SetAbsoluteOriginPosition() { ISubGrid subgrid = null; SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); subgrid = new SubGrid(tree, null, 2); // create a node to be a chile of the root node // Test setting origin for unattached subgrid subgrid.SetAbsoluteOriginPosition(100, 100); Assert.True(subgrid.OriginX == 100 && subgrid.OriginY == 100, "SetAbsoluteOriginPosition did not set origin position for subgrid"); // Add subgrid to the root (which will set it's parent and prevent the origin position from // being changed and will throw an exception) tree.Root.SetSubGrid(0, 0, subgrid); try { subgrid.SetAbsoluteOriginPosition(100, 100); Assert.True(false, "Setting absolute position for node with a parent did not raise an exception"); } catch (Exception) { // As expected` } }
public void Test_SubGridTree_Clear() { // Create a tree with the default number of levels (representing cells at the on-the-ground level) ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); int count; // Test clearing the tree with no elements in it tree.Clear(); // Verify tree is empty count = 0; tree.ScanAllSubGrids(x => { count++; return(true); }); Assert.Equal(0, count); // Add a single subgrid to the tree and verify it can be cleared ISubGrid subgrid = new SubGrid(tree, null, 2); // Subgrid at second level, we will attach it as a child of root tree.Root.SetSubGrid(0, 0, subgrid); // Verify tree has a single subgrid other than root count = 0; tree.Root.ForEachSubGrid(x => { count++; return(SubGridProcessNodeSubGridResult.OK); }); Assert.Equal(1, count); tree.Clear(); // Verify tree is empty count = 0; tree.Root.ForEachSubGrid(x => { count++; return(SubGridProcessNodeSubGridResult.OK); }); Assert.Equal(0, count); }
public void Test_NodeSubGrid_Creation() { SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); INodeSubGrid subgrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 1); Assert.NotNull(subgrid); }
public void Test_NodeSubGrid_ScanSubGrids_TerminateProcessing() { SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); INodeSubGrid parentSubgrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 2); // Fill all of the parent subgrid with new child subgrids using SetSubGrid for (int i = 0; i < SubGridTreeConsts.CellsPerSubGrid; i++) { parentSubgrid.SetSubGrid((byte)(i / SubGridTreeConsts.SubGridTreeDimension), (byte)(i % SubGridTreeConsts.SubGridTreeDimension), new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 1)); } int leafCount = 0, nodeCount = 0; parentSubgrid.ScanSubGrids(tree.FullCellExtent(), leafSubgrid => { leafCount++; return(true); }, nodeSubgrid => { nodeCount++; return(SubGridProcessNodeSubGridResult.TerminateProcessing); }); Assert.Equal(0, leafCount); Assert.Equal(1, nodeCount); }
public void Test_SubGridTree_LocateSubGridContaining_BottomLevel_EmptyTree() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); var subGrid = tree.LocateSubGridContaining(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset); subGrid.Should().BeNull(); }
public void Test_NodeSubGrid_IsEmpty() { SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); INodeSubGrid subgrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 1); Assert.True(subgrid.IsEmpty(), "Node subgrid not empty after creation"); Assert.Equal(0, subgrid.CountChildren()); }
public void Test_SubGridTree_SerialiseEmptySubGridTree_ToBytes() { const int EXPECTED_SIZE = 37; ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); tree.ToBytes().Length.Should().Be(EXPECTED_SIZE); }
public void Test_GenericLeafSubGridTests_Creation_WithinTree() { var tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, GenericLeafSubGrid <bool> >()); var generic = new GenericLeafSubGrid <bool>(tree, null, SubGridTreeConsts.SubGridTreeLevels); generic.Should().NotBeNull(); generic.Owner.Should().Be(tree); generic.Parent.Should().Be(null); }
public void Test_SubGridTree_CalculateIndexOfCellContainingPosition_ExtremeOrdinateRange() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); Assert.True(tree.CalculateIndexOfCellContainingPosition(tree.MaxOrdinate, tree.MaxOrdinate, out int CellX, out int CellY)); Assert.True(tree.CalculateIndexOfCellContainingPosition(-tree.MaxOrdinate, -tree.MaxOrdinate, out CellX, out CellY)); Assert.True(tree.CalculateIndexOfCellContainingPosition(tree.MaxOrdinate, -tree.MaxOrdinate, out CellX, out CellY)); Assert.True(tree.CalculateIndexOfCellContainingPosition(-tree.MaxOrdinate, tree.MaxOrdinate, out CellX, out CellY)); }
public void Test_SubGridSegmentCellPassIterator_NonStaticTests_SetIteratorElevationRange() { var tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1, new SubGridFactory <NodeSubGrid, ServerSubGridTreeLeaf>()); var iterator = new SubGridSegmentCellPassIterator_NonStatic(new SubGridSegmentIterator(new ServerSubGridTreeLeaf(tree, null, SubGridTreeConsts.SubGridTreeLevels, StorageMutability.Mutable), new SubGridDirectory(), null)); iterator.SetIteratorElevationRange(12.0, 23.0); Assert.True(iterator.SegmentIterator.IterationState.MinIterationElevation == 12.0 && iterator.SegmentIterator.IterationState.MaxIterationElevation == 23.0, "CellX/Y not set correctly"); }
public void Test_SubGridTree_LocateSubGridContaining_BottomLevel_NonEmptyTree() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); tree.ConstructPathToCell(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset, SubGridPathConstructionType.CreateLeaf); var subGrid = tree.LocateSubGridContaining(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset); subGrid.Should().NotBeNull(); }
public void Test_SubGrid_IsEmpty() { ISubGrid leafSubgrid = null; SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); leafSubgrid = new SubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels); Assert.False(leafSubgrid.IsEmpty(), "Base subgrid class identifying itself as empty"); }
public void Test_SubGridTree_SerialiseEmptySubGridTree_ToStreamWithOutStream() { const int EXPECTED_SIZE = 37; ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); var MS = tree.ToStream(); MS.Length.Should().Be(EXPECTED_SIZE); }
public void Test_SubGridSegmentCellPassIterator_NonStaticTests_SetTimeRangeTest() { var tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1, new SubGridFactory <NodeSubGrid, ServerSubGridTreeLeaf>()); var iterator = new SubGridSegmentCellPassIterator_NonStatic(new SubGridSegmentIterator(new ServerSubGridTreeLeaf(tree, null, SubGridTreeConsts.SubGridTreeLevels, StorageMutability.Mutable), new SubGridDirectory(), null)); iterator.SetTimeRange(true, DateTime.SpecifyKind(new DateTime(2000, 1, 1), DateTimeKind.Utc), DateTime.SpecifyKind(new DateTime(2000, 1, 2), DateTimeKind.Utc)); Assert.True(iterator.IteratorStartTime == DateTime.SpecifyKind(new DateTime(2000, 1, 1), DateTimeKind.Utc) && iterator.IteratorEndTime == DateTime.SpecifyKind(new DateTime(2000, 1, 2), DateTimeKind.Utc), "Iteration start and end date not set correctly"); }
public void Test_SubGridSegmentCellPassIterator_NonStaticTests_Initialise() { var tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1, new SubGridFactory <NodeSubGrid, ServerSubGridTreeLeaf>()); var iterator = new SubGridSegmentCellPassIterator_NonStatic(new SubGridSegmentIterator(new ServerSubGridTreeLeaf(tree, null, SubGridTreeConsts.SubGridTreeLevels, StorageMutability.Mutable), new SubGridDirectory(), null)); iterator.SegmentIterator.IterationDirection = IterationDirection.Forwards; iterator.Initialise(); iterator.SegmentIterator.IterationDirection = IterationDirection.Backwards; iterator.Initialise(); }
public void Test_SubGrid_ToString() { SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); var child = new NodeSubGrid(tree, null, 3); // create a node to be a child of the root node var parent = new NodeSubGrid(tree, null, 2); // create a node to be a child of the root node parent.SetSubGrid(0, 0, child); child.ToString().Should().Be($"Level:{3}, OriginX:{child.OriginX}, OriginY:{child.OriginY}"); }
public void Test_SubGridTree_GetCellOriginPosition() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); double cx, cy; // Get a cell at the origin of the world coordinate system and test its origin location given a 1m cell size tree.GetCellOriginPosition(tree.IndexOriginOffset, tree.IndexOriginOffset, out cx, out cy); Assert.True(Math.Abs(cx) < 0.001 && Math.Abs(cy) < 0.001, "Cell origin for (IndexOriginOffset, IndexOriginOffset) <> (0.0, 0.0) as expected"); }
public void Test_SubGridTree_GetCellExtents() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); // Get a cell at the origin of the world coordinate system and test its extents given a 1m cell size BoundingWorldExtent3D extent = tree.GetCellExtents(tree.IndexOriginOffset, tree.IndexOriginOffset); Assert.True(Math.Abs(extent.MinX) < 0.001 && Math.Abs(extent.MinY) < 0.001 && (Math.Abs(extent.MinX) - 1.0) < 0.001 && (Math.Abs(extent.MinY) - 1.0) < 0.001, "Cell extents for (IndexOriginOffset, IndexOriginOffset) <> (0.0, 0.0 -> 1.0, 1.0) as expected"); }
public void Test_SubGridTree_CreateUnattachedLeaf() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); // Ask for an unattached leaf subgrid and verify it checks out ILeafSubGrid leaf = tree.CreateUnattachedLeaf(); Assert.NotNull(leaf); Assert.True(leaf.Level == tree.NumLevels && leaf.Owner == tree, "Leaf not configured correctly"); }
public void Test_SubGridTree_ConstructPathToCell_CreatePathToLeaf() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); // Add a node subgrid with ConstructPathToCell with CreatePathToLeaf creation path type (in the bottom left corner // of the cell address space and verify a new node subgrid came back) ISubGrid subgrid = tree.ConstructPathToCell(0, 0, SubGridPathConstructionType.CreatePathToLeaf); Assert.True(subgrid != null && subgrid.Level == tree.NumLevels - 1, "Failed to create a node subgrid down to NumLevels - 1 in tree"); }
public void Test_SubGridTree_CountLeafSubgridsInMemory() { ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); Assert.Equal(0, tree.CountLeafSubGridsInMemory()); // Add a leaf node and check the count is now 1 ISubGrid subgrid = tree.ConstructPathToCell(0, 0, SubGridPathConstructionType.CreateLeaf); Assert.Equal(1, tree.CountLeafSubGridsInMemory()); }
public void Test_SubGridFactory_Creation() { ISubGridFactory factory = new SubGridFactory <NodeSubGrid, LeafSubGrid>(); Assert.NotNull(factory); // Create a tree for the factory to create sub grids for ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, factory); Assert.NotNull(tree); }
public void Test_SubGridTreeSparseCellRecord_Creation() { SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); ISubGrid leafSubgrid = new SubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels); SubGridTreeSparseCellRecord sparseCell = new SubGridTreeSparseCellRecord(15, 15, leafSubgrid); Assert.True(sparseCell.CellX == 15 && sparseCell.CellY == 15 && sparseCell.Cell == leafSubgrid, "Sparse subgrid tree cell record failed to initialise"); }
public void Test_NodeSubGrid_ForEachSubgrid_NodeFunctorWithIndices_InvalidCellRange() { SubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>()); INodeSubGrid subGrid = new NodeSubGrid(tree, null, SubGridTreeConsts.SubGridTreeLevels - 1); Action act = () => subGrid.ForEachSubGrid((x, y, s) => SubGridProcessNodeSubGridResult.TerminateProcessing, SubGridTreeConsts.SubGridTreeDimension, 0, 2 * SubGridTreeConsts.SubGridTreeDimension, 2 * SubGridTreeConsts.SubGridTreeDimension); act.Should().Throw <ArgumentException>().WithMessage("Minimum sub grid cell X/Y bounds are out of range"); act = () => subGrid.ForEachSubGrid((x, y, s) => SubGridProcessNodeSubGridResult.TerminateProcessing, 0, SubGridTreeConsts.SubGridTreeDimension, 2 * SubGridTreeConsts.SubGridTreeDimension, 2 * SubGridTreeConsts.SubGridTreeDimension); act.Should().Throw <ArgumentException>().WithMessage("Minimum sub grid cell X/Y bounds are out of range"); }