Пример #1
0
        private static IdTree CreateRandomTree(int numberOfLevels, int maximumNumberOfNodes)
        {
            if (_idCount >= maximumNumberOfNodes)
            {
                return(null);
            }

            var randomTree = new IdTree {
                RootNode = new IdNode {
                    Id = _idCount++
                }
            };

            if (numberOfLevels == 0)
            {
                return(randomTree);
            }

            var numberOfChildren = RandomNumber.Between(1, 5);
            var nextLevel        = numberOfLevels - 1;

            for (int i = 0; i < numberOfChildren; i++)
            {
                var tree = CreateRandomTree(nextLevel, maximumNumberOfNodes);
                if (tree != null)
                {
                    tree.RootNode.Parent = randomTree.RootNode;
                    randomTree.RootNode.Children.Add(tree.RootNode);
                }
            }

            return(randomTree);
        }
        private void AddChildToNonRootParent(int parentId, IdNode node, IdTree tree)
        {
            var parent = _deserializedNodes[parentId];

            node.Parent = parent;
            parent.Children.Add(node);
        }
        private void DeserializeIntoTree(BinaryReader binaryReader, IdTree tree)
        {
            //nodes need to be deserialized in the same order they were serialized.  In this case, the parent node id is serialized first
            //followed by the current node id.  Only the root node has no parent.
            var parentId      = ReadInt(binaryReader);
            var currentNodeId = ReadInt(binaryReader);

            if (parentId == -1 || currentNodeId == -1)
            {
                return;
            }

            var node = new IdNode {
                Id = currentNodeId
            };

            _deserializedNodes.Add(currentNodeId, node);

            AddChildNodeToParent(parentId, node, tree);

            if (binaryReader.BaseStream.Position != binaryReader.BaseStream.Length)
            {
                DeserializeIntoTree(binaryReader, tree);
            }
        }
Пример #4
0
        public void CanCreateIdTreeWithSingleNode()
        {
            const int rootNodeId = 1;
            var       idNode     = new IdNode(rootNodeId);
            var       idTree     = new IdTree(idNode);

            Assert.IsNotNull(idTree);
            Assert.That(idTree.RootNode.Id, Is.EqualTo(rootNodeId));
        }
Пример #5
0
 private void AddChildNodeToParent(int parentId, IdNode node, IdTree tree)
 {
     if (parentId == tree.RootNode.Id)
     {
         AddChildToRoot(node, tree);
     }
     else
     {
         AddChildToNonRootParent(parentId, node, tree);
     }
 }
        public void CanSerializeEmptyTree()
        {
            var idTree     = new IdTree();
            var serializer = new IdTreeSerializer();

            byte[] serializedIdTree = serializer.Serialize(idTree);

            int deserializedId = BitConverter.ToInt32(serializedIdTree, 0);

            Assert.That(deserializedId, Is.EqualTo(IdNode.EmptyNodeId));
        }
Пример #7
0
        public byte[] Serialize(IdTree tree)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream))
                {
                    SerializeNode(tree.RootNode, binaryWriter);
                }

                return(memoryStream.ToArray());
            }
        }
Пример #8
0
        private void AddChildToNonRootParent(int parentId, IdNode node, IdTree tree)
        {
            var parentNode = tree.FindNode(parentId);

            if (parentNode == null)
            {
                return;
            }

            node.Parent = parentNode;
            parentNode.Children.Add(node);
        }
        private IdTree BuildBomberTree()
        {
            var bomber           = new IdNode(100);
            var fuselage         = new IdNode(200, bomber);
            var engineerFuselage = new IdNode(300, fuselage);
            var testFuselage     = new IdNode(400, fuselage);
            var bombs            = new IdNode(500, bomber);
            var engineerBombs    = new IdNode(600, bombs);
            var testBombs        = new IdNode(700, bombs);

            var wbsTree = new IdTree(bomber);

            return(wbsTree);
        }
        public void CanSerializeTreeWithSingleNodeWithNoParentAndNoChildren()
        {
            const int rootNodeId = 100;
            var       rootNode   = new IdNode(rootNodeId);
            var       idTree     = new IdTree(rootNode);

            var serializer = new IdTreeSerializer();

            byte[] serializedIdTree = serializer.Serialize(idTree);

            int actualRootNodeId   = BitConverter.ToInt32(serializedIdTree, 0);
            int actualParentNodeId = BitConverter.ToInt32(serializedIdTree, 4);

            Assert.That(actualRootNodeId, Is.EqualTo(rootNodeId));
            Assert.That(actualParentNodeId, Is.EqualTo(IdNode.EmptyNodeId));
        }
Пример #11
0
        public IdTree Deserialize(byte[] treeBytes)
        {
            _watch.Reset();
            _watch.Start();
            var deserializedTree = new IdTree();

            using (var memoryStream = new MemoryStream(treeBytes))
            {
                using (var binaryReader = new BinaryReader(memoryStream))
                {
                    var rootnode = new IdNode {
                        Id = binaryReader.ReadInt32()
                    };
                    deserializedTree.RootNode = rootnode;
                    DeserializeIntoTree(binaryReader, deserializedTree);
                }
            }
            _watch.Stop();

            return(deserializedTree);
        }
        public void CanDeserializeWbsTree()
        {
            IdTree bomberTree = BuildBomberTree();
            var    serializer = new IdTreeSerializer();

            byte[] serializedBomber = serializer.Serialize(bomberTree);

            IdTree deserializedBomberTree = serializer.DeSerialize(serializedBomber);

            Assert.IsNotNull(deserializedBomberTree.RootNode);
            IdNode bomber = deserializedBomberTree.RootNode;

            Assert.That(bomber.Id, Is.EqualTo(100));

            IdNode fuselage = bomber.Children[0];

            Assert.That(fuselage.Id, Is.EqualTo(200));
            Assert.That(fuselage.Parent.Id, Is.EqualTo(100));

            IdNode engineerFuselage = fuselage.Children[0];

            Assert.That(engineerFuselage.Id, Is.EqualTo(300));

            IdNode testFuselage = fuselage.Children[1];

            Assert.That(testFuselage.Id, Is.EqualTo(400));

            IdNode bombs = bomber.Children[1];

            Assert.That(bombs.Id, Is.EqualTo(500));

            IdNode engineerBombs = bombs.Children[0];

            Assert.That(engineerBombs.Id, Is.EqualTo(600));

            IdNode testBombs = bombs.Children[1];

            Assert.That(testBombs.Id, Is.EqualTo(700));
        }
        public void CanSerializeTreeWithOneChild()
        {
            const int rootNodeId  = 100;
            const int childNodeId = 200;
            var       rootNode    = new IdNode(rootNodeId);
            var       childNode   = new IdNode(childNodeId, rootNode);
            var       idTree      = new IdTree(rootNode);

            var serializer = new IdTreeSerializer();

            byte[] serializedIdTree = serializer.Serialize(idTree);

            int actualRootNodeId   = BitConverter.ToInt32(serializedIdTree, 0);
            int actualParentNodeId = BitConverter.ToInt32(serializedIdTree, 4);
            int actualChildCount   = BitConverter.ToInt32(serializedIdTree, 8);
            int actualChildId      = BitConverter.ToInt32(serializedIdTree, 12);

            Assert.That(actualRootNodeId, Is.EqualTo(rootNodeId));
            Assert.That(actualParentNodeId, Is.EqualTo(IdNode.EmptyNodeId));
            Assert.That(actualChildCount, Is.EqualTo(1));
            Assert.That(actualChildId, Is.EqualTo(childNodeId));
        }
Пример #14
0
        private static void PrintTree(IdTree tree)
        {
            string indent = string.Empty;

            Print(tree.RootNode, indent, tree.RootNode.Children.Any());
        }
Пример #15
0
        private static IdTree CreateStaticTestTree()
        {
            var tree = new IdTree();

            var rootNode = new IdNode {
                Id = 1
            };

            var secondNode = new IdNode {
                Id = 2, Parent = rootNode
            };

            secondNode.Children = new List <IdNode>
            {
                new IdNode {
                    Id = 5, Parent = secondNode
                },
                new IdNode {
                    Id = 6, Parent = secondNode
                },
                new IdNode {
                    Id = 7, Parent = secondNode
                }
            };

            rootNode.Children.Add(secondNode);

            var thirdNode = new IdNode {
                Id = 3, Parent = rootNode
            };

            thirdNode.Children = new List <IdNode>
            {
                new IdNode {
                    Id = 8, Parent = thirdNode
                },
                new IdNode {
                    Id = 9, Parent = thirdNode
                },
            };

            rootNode.Children.Add(thirdNode);

            var fourthNode = new IdNode {
                Id = 4, Parent = rootNode
            };

            fourthNode.Children = new List <IdNode>
            {
                new IdNode {
                    Id = 10, Parent = fourthNode
                },
                new IdNode {
                    Id = 11, Parent = fourthNode
                },
            };

            var forthNodeChildWithChildren = new IdNode {
                Id = 12, Parent = fourthNode
            };

            forthNodeChildWithChildren.Children = new List <IdNode>
            {
                new IdNode {
                    Id = 13, Parent = forthNodeChildWithChildren
                },
                new IdNode {
                    Id = 14, Parent = forthNodeChildWithChildren
                }
            };
            fourthNode.Children.Add(forthNodeChildWithChildren);

            rootNode.Children.Add(fourthNode);

            rootNode.Children.Add(new IdNode {
                Id = 15, Parent = rootNode
            });

            tree.RootNode = rootNode;

            return(tree);
        }
Пример #16
0
 private static void AddChildToRoot(IdNode node, IdTree tree)
 {
     node.Parent = tree.RootNode;
     tree.RootNode.Children.Add(node);
 }
Пример #17
0
        public void CanCreateEmptyIdTree()
        {
            var idTree = new IdTree();

            Assert.That(idTree.RootNode.Id, Is.EqualTo(IdNode.EmptyNodeId));
        }