Example #1
0
        public void AVLTreeDeleteTest()
        {
            AVLTree <int> tree = new AVLTree <int>();

            for (int i = 1; i <= 40; i++)
            {
                tree.Insert(i);
            }
            for (int i = 11; i <= 30; i++)
            {
                tree.DeleteNode(i);
            }
            List <int> list = new List <int>();

            tree.InOrderTraverse(x => list.Add(x.Value));
            Assert.AreEqual(20, list.Count);
            CollectionAssertEx.IsCollectionAscOrdered(list);
            tree.InOrderTraverse(x => {
                int hl = AVLTree <int> .GetHeight(x.Left);
                int hr = AVLTree <int> .GetHeight(x.Right);
                if (Math.Abs(hl - hr) > 1)
                {
                    Assert.Fail();
                }
            });
        }
Example #2
0
 public void Delta1Test2()
 {
     BoyerMooreStringMatcher.Delta1 delta1 = CreateStringMatcher("LONG LONG STRING").GetDelta1();
     char[] chars = new char[] { 'L', 'O', 'N', 'G', 'S', 'T', 'R', 'I', ' ' };
     CollectionAssertEx.AreEqual(new int[] { 10, 9, 1, 0, 5, 4, 3, 2, 6 }, chars.Select(x => delta1[x]));
     CollectionAssertEx.TrueForAllItems(CharsRange().Except(chars).Select(x => delta1[x]), x => x == 16);
 }
        public void InsertTest2()
        {
            tree.Insert("cute", 1);
            tree.Insert("cup", 2);
            tree.Insert("cute", 11);
            tree.Insert("cup", 22);

            Triplet[] expected = new Triplet[] {
                new Triplet('c', 0, false),

                null,
                new Triplet('u', 0, false),
                null,

                null,
                new Triplet('t', 0, false),
                null,

                new Triplet('p', 2, true),
                new Triplet('e', 1, true),
                null,
                null, null, null, null, null, null
            };
            CollectionAssertEx.AreEqual(expected, tree.BFS(x => new Triplet(x.Char, x.Value, x.IsEos)));
        }
Example #4
0
        public void GetItemsTest1()
        {
            RectMatrix <string, int> matrix = new RectMatrix <string, int>();

            matrix.EnsureSize(4, 3);
            matrix[0, 0]               = "a";
            matrix[0, 1]               = "b";
            matrix[0, 2]               = "c";
            matrix[1, 0]               = "d";
            matrix[1, 1]               = "e";
            matrix[1, 2]               = "f";
            matrix[2, 0]               = "g";
            matrix[2, 1]               = "h";
            matrix[2, 2]               = "i";
            matrix[3, 0]               = "j";
            matrix[3, 1]               = "k";
            matrix[3, 2]               = "l";
            matrix.RowAttributes[0]    = 2;
            matrix.RowAttributes[1]    = 4;
            matrix.RowAttributes[2]    = 6;
            matrix.RowAttributes[3]    = 8;
            matrix.ColumnAttributes[0] = 12;
            matrix.ColumnAttributes[1] = 8;
            matrix.ColumnAttributes[2] = 4;
            var result = matrix.GetItems((row, column, rowAttribute, columnAttribute, x) => (rowAttribute > 3 && rowAttribute < 7 && columnAttribute < 9) || rowAttribute == 8);

            CollectionAssertEx.AreEqual(new string[] { "e", "f", "h", "i", "j", "k", "l" }, result);
        }
Example #5
0
 public void Delta1Test1()
 {
     BoyerMooreStringMatcher.Delta1 delta1 = CreateStringMatcher("TEXT").GetDelta1();
     char[] chars = new char[] { 'T', 'X', 'E' };
     CollectionAssertEx.AreEqual(new int[] { 0, 1, 2 }, chars.Select(x => delta1[x]));
     CollectionAssertEx.TrueForAllItems(CharsRange().Except(chars).Select(x => delta1[x]), x => x == 4);
 }
Example #6
0
        public void DeleteTest()
        {
            trie.Insert("Word3", null);
            trie.Insert("Word2", 22);
            trie.Insert("Word1", null);
            trie.Insert("OtherWord3");
            trie.Insert("OtherWord2");
            trie.Insert("OtherWord1");

            trie.Delete("Word2");
            trie.Delete("OtherWord2");

            trie.AssertNodes(
                new TrieNode("Word3", null),
                new TrieNode("Word1", null),
                new TrieNode("OtherWord3", null),
                new TrieNode("OtherWord1", null));

            trie.Delete("OtherWord1");
            trie.Delete("Word3");
            trie.AssertNodes(
                new TrieNode("Word1", null),
                new TrieNode("OtherWord3", null));

            trie.Delete("OtherWord3");
            trie.Delete("OtherWord3");
            trie.AssertNodes(new TrieNode("Word1", null));

            trie.Delete("Word1");
            CollectionAssertEx.IsEmpty(TrieTestHelper.CollectKeys(trie));
        }
        public void GetAncestorsTest()
        {
            BinaryTree <int>     tree      = BuildTestBigBinaryTree();
            BinaryTreeNode <int> n8        = tree.Root.Left.Right.Right;
            IEnumerable <int>    ancestors = tree.GetAncestors(n8).Select(x => x.Value);

            CollectionAssertEx.AreEqual(new int[] { 1, 2, 5 }, ancestors);
        }
Example #8
0
        public void SuffixTreeNode_DefaultsTest()
        {
            SuffixTreeNode node = new SuffixTreeNode(-1, false);

            Assert.AreEqual(-1, node.Index);
            Assert.IsFalse(node.IsTerminal);
            CollectionAssertEx.IsEmpty(node.GetKeys());
            CollectionAssertEx.IsEmpty(node.GetChildren());
        }
Example #9
0
        public void DisjointSetItemsTest()
        {
            DisjointSet <int> disjointSet = new DisjointSet <int>();

            CollectionAssertEx.IsEmpty(disjointSet.Items);
            disjointSet.MakeSet(1);
            disjointSet.MakeSet(2);
            disjointSet.MakeSet(3);
            CollectionAssertEx.AreEquivalent(new int[] { 3, 1, 2 }, disjointSet.Items);
        }
        public void ValuesCollectionEnumeratorSimpleTest()
        {
            List <HashMapDataValue> valueList = new List <HashMapDataValue>();

            foreach (var value in this.hashMap.Values)
            {
                valueList.Add(value);
            }
            CollectionAssertEx.IsEmpty(valueList);
        }
        public void KeysCollectionEnumeratorSimpleTest()
        {
            List <HashMapDataKey> keyList = new List <HashMapDataKey>();

            foreach (var key in this.hashMap.Keys)
            {
                keyList.Add(key);
            }
            CollectionAssertEx.IsEmpty(keyList);
        }
Example #12
0
        public void SuffixGroup_BuildGroupsTest2()
        {
            SuffixGroup group = new SuffixGroup("a$", "na$", "ana$", "nana$", "anana$", "banana$");

            SuffixGroup[] expected = new SuffixGroup[] {
                new SuffixGroup("a$", "ana$", "anana$"),
                new SuffixGroup("na$", "nana$"),
                new SuffixGroup("banana$"),
            };
            CollectionAssertEx.AreEquivalent(expected, group.BuildGroups());
        }
Example #13
0
        public void QueueToArrayTest()
        {
            Queue <int> queue = new Queue <int>();

            CollectionAssertEx.IsEmpty(queue.ToArray());
            queue.EnQueue(1);
            queue.EnQueue(2);
            queue.EnQueue(3);
            queue.EnQueue(4);
            CollectionAssert.AreEqual(new int[] { 1, 2, 3, 4 }, queue.ToArray());
        }
 public void KeysCollectionCopyToTest()
 {
     HashMapDataKey[] keys = new HashMapDataKey[8];
     this.hashMap.Add(new HashMapDataKey(1), new HashMapDataValue("val1"));
     this.hashMap.Add(new HashMapDataKey(2), new HashMapDataValue("val2"));
     this.hashMap.Add(new HashMapDataKey(3), new HashMapDataValue("val3"));
     this.hashMap.Keys.CopyTo(keys, 5);
     HashMapDataKey[] expectedKeys = new HashMapDataKey[] {
         null, null, null, null, null,
         new HashMapDataKey(1),
         new HashMapDataKey(2),
         new HashMapDataKey(3)
     };
     CollectionAssertEx.AreEquivalent(expectedKeys, keys);
 }
Example #15
0
        public void DisjointSetMakeSetTest()
        {
            DisjointSet <int> disjointSet = new DisjointSet <int>();

            disjointSet.MakeSet(1);
            disjointSet.MakeSet(2);
            disjointSet.MakeSet(3);
            disjointSet.MakeSet(4);
            disjointSet.MakeSet(5);
            CollectionAssertEx.AreEquivalent(new int[] { 1, 3, 5, 4, 2 }, disjointSet.Items);
            var coreItems = disjointSet.Items.OrderBy(x => x).Select(x => disjointSet.GetItemCore(x));

            CollectionAssertEx.AreEqual(new int[] { 1, 2, 3, 4, 5 }, coreItems.Select(x => x.Parent));
            CollectionAssertEx.AreEqual(new int[] { 0, 0, 0, 0, 0 }, coreItems.Select(x => x.Rank));
        }
Example #16
0
        public void SuffixTreeTest4()
        {
            SuffixTree tree = new SuffixTree('$', "PREFIX");

            SuffixTreeNodeTriplet[] expected = new SuffixTreeNodeTriplet[] {
                NonTerminal("EFIX$", "FIX$", "IX$", "PREFIX$", "REFIX$", "X$"),
                Terminal(2),
                Terminal(3),
                Terminal(4),
                Terminal(0),
                Terminal(1),
                Terminal(5),
            };
            CollectionAssertEx.AreEqual(expected, tree.BFS());
        }
 public void ValuesCollectionCopyToTest()
 {
     HashMapDataValue[] values = new HashMapDataValue[8];
     this.hashMap.Add(new HashMapDataKey(1), new HashMapDataValue("val1"));
     this.hashMap.Add(new HashMapDataKey(2), new HashMapDataValue("val2"));
     this.hashMap.Add(new HashMapDataKey(3), new HashMapDataValue("val3"));
     this.hashMap.Values.CopyTo(values, 5);
     HashMapDataValue[] expectedValues = new HashMapDataValue[] {
         null, null, null, null, null,
         new HashMapDataValue("val1"),
         new HashMapDataValue("val2"),
         new HashMapDataValue("val3")
     };
     CollectionAssertEx.AreEquivalent(expectedValues, values);
 }
        public void ValuesCollectionEnumeratorHeavyTest()
        {
            var numberList = Enumerable.Range(1, 100);

            HashMapDataKey[] keys = numberList
                                    .Select(x => new HashMapDataKey(x))
                                    .ToArray();
            HashMapDataValue[] values = numberList
                                        .Select(x => new HashMapDataValue(x.ToString()))
                                        .ToArray();
            for (int n = 0; n < keys.Length; n++)
            {
                this.hashMap.Add(keys[n], values[n]);
            }
            CollectionAssertEx.AreEquivalent(values, this.hashMap.Values);
        }
Example #19
0
        public void SortAscTest3()
        {
            var array = new[] {
                new { Age = 1D, Name = "Ann" },
                new { Age = 11.5D, Name = "Dr" },
                new { Age = 11D, Name = "Trish" },
                new { Age = 15.7D, Name = "Jeff" },
                new { Age = 2D, Name = "Ren" },
                new { Age = 40.3D, Name = "Pol" },
                new { Age = 8D, Name = "Tarjan" },
            };

            this.sorter.Sort(array, (x, y) => x.Age.CompareTo(y.Age));
            CollectionAssertEx.AreEqual(new double[] { 1, 2, 8, 11, 11.5, 15.7, 40.3 }, array.Select(x => x.Age));
            CollectionAssertEx.AreEqual(new string[] { "Ann", "Ren", "Tarjan", "Trish", "Dr", "Jeff", "Pol" }, array.Select(x => x.Name));
        }
Example #20
0
        public void MaxBinaryHeapPercolateUpTest()
        {
            var heapData = GetMaxHeapTestData();

            Assert.AreEqual(0, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 0));
            Assert.AreEqual(1, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 1));
            Assert.AreEqual(2, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 2));
            heapData[5] = new BinaryHeapBase <int, int> .KeyValuePair(20, 20);

            Assert.AreEqual(0, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 5));
            CollectionAssertEx.AreEqual(new int[] { 20, 13, 17, 1, 4, 6, 5 }, heapData.Select(x => x.Key));
            heapData[6] = new BinaryHeapBase <int, int> .KeyValuePair(18, 18);

            Assert.AreEqual(2, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 6));
            CollectionAssertEx.AreEqual(new int[] { 20, 13, 18, 1, 4, 6, 17 }, heapData.Select(x => x.Key));
        }
        public void ClearTest()
        {
            HashMapDataKey key1 = new HashMapDataKey(1);
            HashMapDataKey key2 = new HashMapDataKey(2);

            HashMapDataValue val1 = new HashMapDataValue("val1");
            HashMapDataValue val2 = new HashMapDataValue("val2");

            this.hashMap.Add(key1, val1);
            this.hashMap.Add(key2, val2);
            CollectionAssertEx.IsNotEmpty(this.hashMap.Keys);
            CollectionAssertEx.IsNotEmpty(this.hashMap.Values);

            this.hashMap.Clear();
            CollectionAssertEx.IsEmpty(this.hashMap.Keys);
            CollectionAssertEx.IsEmpty(this.hashMap.Values);
        }
Example #22
0
        public void SuffixTreeTest1()
        {
            SuffixTree tree = new SuffixTree('$', "tatat");

            SuffixTreeNodeTriplet[] expected = new SuffixTreeNodeTriplet[] {
                NonTerminal("at", "t"),
                NonTerminal("$", "at$"),
                NonTerminal("$", "at"),
                Terminal(3),
                Terminal(1),
                Terminal(4),
                NonTerminal("$", "at$"),
                Terminal(2),
                Terminal(0),
            };
            CollectionAssertEx.AreEqual(expected, tree.BFS());
        }
Example #23
0
        public void MaxBinaryHeapPercolateDownTest()
        {
            var heapData = GetMaxHeapTestData();

            Assert.AreEqual(3, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 3));
            Assert.AreEqual(4, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 4));
            Assert.AreEqual(5, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 5));
            Assert.AreEqual(6, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 6));
            heapData[0] = new BinaryHeapBase <int, int> .KeyValuePair(0, 0);

            Assert.AreEqual(4, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 0));
            CollectionAssertEx.AreEqual(new int[] { 13, 4, 6, 1, 0, 2, 5 }, heapData.Select(x => x.Key));
            heapData[0] = new BinaryHeapBase <int, int> .KeyValuePair(3, 3);

            Assert.AreEqual(6, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 0));
            CollectionAssertEx.AreEqual(new int[] { 6, 4, 5, 1, 0, 2, 3 }, heapData.Select(x => x.Key));
        }
Example #24
0
        public void MinBinaryHeapPercolateUpTest()
        {
            var heapData = GetMinHeapTestData();

            Assert.AreEqual(0, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 0));
            Assert.AreEqual(1, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 1));
            Assert.AreEqual(2, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 2));
            heapData[3] = new BinaryHeapBase <int, int> .KeyValuePair(0, 0);

            Assert.AreEqual(0, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 3));
            CollectionAssertEx.AreEqual(new int[] { 0, 1, 2, 15, 17, 4, 3 }, heapData.Select(x => x.Key));
            heapData[6] = new BinaryHeapBase <int, int> .KeyValuePair(1, 1);

            Assert.AreEqual(2, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 6));
            Assert.AreEqual(1, heapData[2].Key);
            CollectionAssertEx.AreEqual(new int[] { 0, 1, 1, 15, 17, 4, 2 }, heapData.Select(x => x.Key));
        }
Example #25
0
        public void MinBinaryHeapPercolateDownTest()
        {
            var heapData = GetMinHeapTestData();

            Assert.AreEqual(3, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 3));
            Assert.AreEqual(4, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 4));
            Assert.AreEqual(5, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 5));
            Assert.AreEqual(6, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 6));
            heapData[0] = new BinaryHeapBase <int, int> .KeyValuePair(40, 40);

            Assert.AreEqual(6, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 0));
            CollectionAssertEx.AreEqual(new int[] { 2, 15, 3, 16, 17, 4, 40 }, heapData.Select(x => x.Key));
            heapData[2] = new BinaryHeapBase <int, int> .KeyValuePair(20, 20);

            Assert.AreEqual(5, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 2));
            CollectionAssertEx.AreEqual(new int[] { 2, 15, 4, 16, 17, 20, 40 }, heapData.Select(x => x.Key));
        }
        public void KeysCollectionEnumeratorTest()
        {
            this.hashMap.Add(new HashMapDataKey(1), new HashMapDataValue("val1"));
            this.hashMap.Add(new HashMapDataKey(2), new HashMapDataValue("val2"));
            this.hashMap.Add(new HashMapDataKey(3), new HashMapDataValue("val3"));
            List <HashMapDataKey> keyList = new List <HashMapDataKey>(4);

            foreach (var key in this.hashMap.Keys)
            {
                keyList.Add(key);
            }
            HashMapDataKey[] expectedKeys = new HashMapDataKey[] {
                new HashMapDataKey(1),
                new HashMapDataKey(2),
                new HashMapDataKey(3)
            };
            CollectionAssertEx.AreEquivalent(expectedKeys, keyList);
        }
        public void ValuesCollectionEnumeratorTest()
        {
            this.hashMap.Add(new HashMapDataKey(1), new HashMapDataValue("val1"));
            this.hashMap.Add(new HashMapDataKey(2), new HashMapDataValue("val2"));
            this.hashMap.Add(new HashMapDataKey(3), new HashMapDataValue("val3"));
            List <HashMapDataValue> valueList = new List <HashMapDataValue>(4);

            foreach (var value in this.hashMap.Values)
            {
                valueList.Add(value);
            }
            HashMapDataValue[] expectedValues = new HashMapDataValue[] {
                new HashMapDataValue("val1"),
                new HashMapDataValue("val2"),
                new HashMapDataValue("val3")
            };
            CollectionAssertEx.AreEquivalent(expectedValues, valueList);
        }
Example #28
0
        public void DisjointSetRemoveSetTest()
        {
            DisjointSet <int> disjointSet = new DisjointSet <int>();

            disjointSet.MakeSet(1);
            disjointSet.MakeSet(2);
            disjointSet.MakeSet(3);
            disjointSet.MakeSet(4);
            disjointSet.MakeSet(5);
            disjointSet.Union(2, 3);
            disjointSet.Union(4, 5);
            CollectionAssertEx.AreEquivalent(new int[] { 1, 3, 5, 4, 2 }, disjointSet.Items);
            disjointSet.RemoveSet(2);
            CollectionAssertEx.AreEquivalent(new int[] { 1, 4, 5 }, disjointSet.Items);
            disjointSet.RemoveSet(5);
            CollectionAssertEx.AreEquivalent(new int[] { 1 }, disjointSet.Items);
            disjointSet.RemoveSet(1);
            CollectionAssertEx.IsEmpty(disjointSet.Items);
        }
        public void InsertTest1()
        {
            tree.Insert("cute", 1);
            tree.Insert("cup", 2);
            tree.Insert("at");
            tree.Insert("as");
            tree.Insert("he", 3);
            tree.Insert("us", 4);
            tree.Insert("i", 1);

            Triplet[] expected = new Triplet[] {
                new Triplet('c', 0, false),

                new Triplet('a', 0, false),
                new Triplet('u', 0, false),
                new Triplet('h', 0, false),

                null,
                new Triplet('t', 0, true),
                null,
                null,
                new Triplet('t', 0, false),
                null,
                null,
                new Triplet('e', 3, true),
                new Triplet('u', 0, false),

                new Triplet('s', 0, true),
                null,
                null,
                new Triplet('p', 2, true),
                new Triplet('e', 1, true),
                null,
                null,
                null,
                null,
                new Triplet('i', 1, true),
                new Triplet('s', 4, true),
                null,
                null, null, null, null, null, null, null, null, null, null, null, null, null, null, null
            };
            CollectionAssertEx.AreEqual(expected, tree.BFS(x => new Triplet(x.Char, x.Value, x.IsEos)));
        }
        public void DeleteTest3()
        {
            tree.Insert("abc");
            tree.Delete("abc1");
            tree.Delete("a");
            tree.Delete("b");
            tree.Delete("c");

            Triplet[] expected = new Triplet[] {
                new Triplet('a', 0, false),

                null,
                new Triplet('b', 0, false),
                null,

                null,
                new Triplet('c', 0, true),
                null,
                null, null, null
            };
            CollectionAssertEx.AreEqual(expected, tree.BFS(x => new Triplet(x.Char, x.Value, x.IsEos)));
        }