Пример #1
0
        public void CloneAsTrieNode()
        {
            SequenceTerminatorTrieNode <Char> aNode = new SequenceTerminatorTrieNode <Char>('a', null);
            SequenceTerminatorTrieNode <Char> bNode = new SequenceTerminatorTrieNode <Char>('b', aNode);

            aNode.AddChildNode(bNode.Item, bNode);
            SequenceTerminatorTrieNode <Char> cNode = new SequenceTerminatorTrieNode <Char>('c', bNode);

            bNode.AddChildNode(cNode.Item, cNode);
            SequenceTerminatorTrieNode <Char> dNode = new SequenceTerminatorTrieNode <Char>('d', bNode);

            bNode.AddChildNode(dNode.Item, dNode);
            bNode.SetSubTreeSize('c', 2);
            bNode.SetSubTreeSize('d', 3);

            TrieNode <Char> clonedNode = bNode.CloneAsTrieNode();

            Assert.IsNotInstanceOf(typeof(SequenceTerminatorTrieNode <Char>), clonedNode);
            Assert.AreEqual('b', clonedNode.Item);
            Assert.IsTrue(clonedNode.ChildNodeExists('c'));
            Assert.IsTrue(clonedNode.ChildNodeExists('d'));
            Assert.AreEqual(2, clonedNode.GetSubtreeSize('c'));
            Assert.AreEqual(3, clonedNode.GetSubtreeSize('d'));
            Assert.AreNotSame(bNode, clonedNode);
            Assert.AreSame(aNode, clonedNode.ParentNode);
            Assert.AreSame(cNode, clonedNode.GetChildNode('c'));
            Assert.AreSame(dNode, clonedNode.GetChildNode('d'));
        }
Пример #2
0
        public void Replicate()
        {
            TrieNode <Char> aNode = new TrieNode <Char>('a', null);
            TrieNode <Char> bNode = new TrieNode <Char>('b', aNode);

            aNode.AddChildNode(bNode.Item, bNode);
            TrieNode <Char> cNode = new TrieNode <Char>('c', bNode);

            bNode.AddChildNode(cNode.Item, cNode);
            TrieNode <Char> dNode = new TrieNode <Char>('d', bNode);

            bNode.AddChildNode(dNode.Item, dNode);
            bNode.SetSubTreeSize('c', 2);
            bNode.SetSubTreeSize('d', 3);

            TrieNode <Char> replicaNode = bNode.Replicate();

            Assert.AreEqual('b', replicaNode.Item);
            Assert.IsTrue(replicaNode.ChildNodeExists('c'));
            Assert.IsTrue(replicaNode.ChildNodeExists('d'));
            Assert.AreEqual(2, replicaNode.GetSubtreeSize('c'));
            Assert.AreEqual(3, replicaNode.GetSubtreeSize('d'));
            Assert.AreNotSame(bNode, replicaNode);
            Assert.AreNotSame(aNode, replicaNode.ParentNode);
            Assert.AreNotSame(cNode, replicaNode.GetChildNode('c'));
            Assert.AreNotSame(dNode, replicaNode.GetChildNode('d'));


            // Test replicating a node with null parent
            replicaNode = aNode.Replicate();
            Assert.AreEqual('a', replicaNode.Item);
            Assert.IsNull(replicaNode.ParentNode);
            Assert.IsTrue(replicaNode.ChildNodeExists('b'));
            Assert.AreNotSame(bNode, replicaNode.GetChildNode('b'));
        }
        public void Constructor_RootNodeReference()
        {
            TrieNode <Char> rootNode = null;

            testTrie = new Trie <Char>(out rootNode);
            foreach (Char[] currentTestWord in new List <Char[]> {
                "cop".ToCharArray(), "d".ToCharArray(), "apple".ToCharArray(), "apps".ToCharArray(), "app".ToCharArray()
            })
            {
                testTrie.Insert(currentTestWord);
            }

            Assert.AreEqual(3, rootNode.ChildNodes.Count());
            Assert.IsTrue(rootNode.ChildNodeExists('a'));
            Assert.IsTrue(rootNode.ChildNodeExists('c'));
            Assert.IsTrue(rootNode.ChildNodeExists('d'));
        }
Пример #4
0
        public void Constructor_RootNodeReference()
        {
            TrieNode <Char> rootNode = null;

            testCharacterTrie = new CharacterTrie(out rootNode);
            foreach (String currentTestWord in new List <String>()
            {
                "cop", "d", "apple", "apps", "app"
            })
            {
                testCharacterTrie.Insert(currentTestWord);
            }

            Assert.AreEqual(3, rootNode.ChildNodes.Count());
            Assert.IsTrue(rootNode.ChildNodeExists('a'));
            Assert.IsTrue(rootNode.ChildNodeExists('c'));
            Assert.IsTrue(rootNode.ChildNodeExists('d'));
        }
Пример #5
0
        public void RemoveAllChildren()
        {
            TrieNode <Char> aNode = new TrieNode <Char>('a', null);
            TrieNode <Char> bNode = new TrieNode <Char>('b', aNode);

            aNode.AddChildNode(bNode.Item, bNode);
            TrieNode <Char> cNode = new TrieNode <Char>('c', aNode);

            aNode.AddChildNode(cNode.Item, cNode);

            Assert.IsTrue(aNode.ChildNodeExists('b'));
            Assert.IsTrue(aNode.ChildNodeExists('c'));

            aNode.RemoveAllChildren();

            Assert.IsFalse(aNode.ChildNodeExists('b'));
            Assert.IsFalse(aNode.ChildNodeExists('c'));
        }
Пример #6
0
        public void ChildNodeExists()
        {
            TrieNode <Char> aNode = new TrieNode <Char>('a', null);
            TrieNode <Char> bNode = new TrieNode <Char>('b', aNode);

            aNode.AddChildNode(bNode.Item, bNode);
            TrieNode <Char> cNode = new TrieNode <Char>('c', aNode);

            aNode.AddChildNode(cNode.Item, cNode);

            Boolean result = aNode.ChildNodeExists('b');

            Assert.IsTrue(result);

            result = aNode.ChildNodeExists('c');

            Assert.IsTrue(result);

            result = aNode.ChildNodeExists('d');

            Assert.IsFalse(result);
        }
Пример #7
0
        public void RemoveChildNode()
        {
            TrieNode <Char> aNode = new TrieNode <Char>('a', null);
            TrieNode <Char> bNode = new TrieNode <Char>('b', aNode);

            aNode.AddChildNode(bNode.Item, bNode);
            TrieNode <Char> cNode = new TrieNode <Char>('c', aNode);

            aNode.AddChildNode(cNode.Item, cNode);

            Assert.IsTrue(aNode.ChildNodeExists('b'));

            aNode.RemoveChildNode('b');

            Assert.IsFalse(aNode.ChildNodeExists('b'));

            ArgumentException e = Assert.Throws <ArgumentException>(delegate
            {
                aNode.GetSubtreeSize('b');
            });

            Assert.That(e.Message, NUnit.Framework.Does.StartWith("A child node for the specified item 'b' does not exist."));
            Assert.AreEqual("item", e.ParamName);
        }
Пример #8
0
        public void Constructor_RootNodeReferenceMaintainedAfterClearIsCalled()
        {
            TrieNode <Char> rootNode = null;

            testCharacterTrie = new CharacterTrie(out rootNode);
            foreach (String currentTestWord in new List <String>()
            {
                "cop", "d", "apple", "apps", "app"
            })
            {
                testCharacterTrie.Insert(currentTestWord);
            }
            testCharacterTrie.Clear();
            testCharacterTrie.Insert("apple");

            Assert.AreEqual(1, rootNode.ChildNodes.Count());
            Assert.IsTrue(rootNode.ChildNodeExists('a'));
        }