예제 #1
0
        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)");
        }
예제 #2
0
        public void Test_SubGridTree_LocateSubGridContaining_LocateClosestSubGridContaining_NoLeaves()
        {
            ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>());

            tree.ConstructPathToCell(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset, SubGridPathConstructionType.CreatePathToLeaf);

            var subGrid = tree.LocateClosestSubGridContaining(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.SubGridTreeLevels);

            subGrid.Should().NotBeNull();
            subGrid.Level.Should().Be(SubGridTreeConsts.SubGridTreeLevels - 1);

            tree.ConstructPathToCell(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset, SubGridPathConstructionType.CreateLeaf);

            subGrid = tree.LocateClosestSubGridContaining(SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.DefaultIndexOriginOffset, SubGridTreeConsts.SubGridTreeLevels);
            subGrid.Should().NotBeNull();
        }
예제 #3
0
        public void Test_SubGridTree_ConstructPathToCell_ReturnExistingLeafOnly()
        {
            ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>());

            // Add a leaf node with ConstructPathToCell with leaf creation path type (in the bottom left corner
            // of the cell address space and verify a new leaf subgrid came back)
            ISubGrid subgrid = tree.ConstructPathToCell(0, 0, SubGridPathConstructionType.CreateLeaf);

            // Retrieve the newly created leaf node with ConstructPathToCell with existing leaf only path type
            // (in the bottom left corner of the cell address space and verify a new leaf node came back)
            ISubGrid subgrid2 = tree.ConstructPathToCell(0, 0, SubGridPathConstructionType.ReturnExistingLeafOnly);

            Assert.NotNull(subgrid);

            Assert.True(subgrid == subgrid2 && subgrid.Equals(subgrid2),
                        "Retrieve leaf subgrid is not the same as the one added");
        }
예제 #4
0
        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();
        }
예제 #5
0
        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());
        }
예제 #6
0
        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");
        }
예제 #7
0
        public void Test_SubGridTree_ConstructPathToCell_CreateLeaf()
        {
            ISubGridTree tree = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, new SubGridFactory <NodeSubGrid, LeafSubGrid>());

            // Add a leaf node with ConstructPathToCell with leaf creation path type (in the bottom left corner
            // of the cell address space and verify a new leaf subgrid came back)
            ISubGrid subgrid = tree.ConstructPathToCell(0, 0, SubGridPathConstructionType.CreateLeaf);

            Assert.NotNull(subgrid);

            Assert.True(subgrid != null && subgrid.Level == tree.NumLevels && subgrid.Owner == tree && subgrid.Parent != null,
                        "Subgrid added to tree is not correctly set up");
        }
예제 #8
0
        public void Test_ConstructPathToCell_WithThreadContention()
        {
            var mockSubGridFactory = new Mock <SubGridFactory <NodeSubGrid, LeafSubGrid> >();

            mockSubGridFactory.Setup(x => x.GetSubGrid(It.IsAny <ISubGridTree>(), It.IsAny <byte>()))
            .Returns((ISubGridTree tree, byte level) =>
            {
                if (level < tree.NumLevels)
                {
                    var mockSubGrid          = new Mock <NodeSubGrid>();
                    mockSubGrid.Object.Owner = tree;
                    mockSubGrid.Object.Level = level;

                    int numCalls = 0;
                    mockSubGrid
                    .Setup(x => x.GetSubGridContainingCell(It.IsAny <int>(), It.IsAny <int>()))
                    .Returns((int cellX, int cellY) =>
                    {
                        numCalls++;
                        if (numCalls == 1)
                        {
                            return(null);
                        }
                        if (numCalls >= 2)
                        {
                            return new NodeSubGrid()
                            {
                                Owner = tree,
                                Level = (byte)(level + 1) // Need to create the sub grid at the lower level
                            }
                        }
                        ;;

                        return(null); // Should never get here
                    });

                    return(mockSubGrid.Object);
                }

                // We are only creating the path to the leaf, so should not be asked to create a leaf sub grid
                return(null);
            });

            // This test creates a tree then simulates thread contention in the ConstructPathToCell to test handling of this scenario
            var _tree   = new SubGridTree(SubGridTreeConsts.SubGridTreeLevels, 1.0, mockSubGridFactory.Object);
            var subGrid = _tree.ConstructPathToCell(0, 0, SubGridPathConstructionType.CreatePathToLeaf);

            subGrid.Should().NotBeNull();
        }
예제 #9
0
        public void Test_SubGridTree_ConstructPathToCell_InvalidPathConstructionType()
        {
            const SubGridPathConstructionType invalidType = (SubGridPathConstructionType)123;
            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)

            Action act = () =>
            {
                var _ = tree.ConstructPathToCell(0, 0, invalidType);
            };

            act.Should().Throw <TRexSubGridTreeException>().WithMessage($"Unknown SubGridPathConstructionType: {invalidType}");
        }