Пример #1
0
        public void TestTreeSetString()
        {
            var set = new TreeSet <string>(StringComparer.InvariantCultureIgnoreCase);

            string[] contents =
            {
                "water supply",
                "Threat",
                "emergency",
                "admiration",
                "public relations",
                "lignite",
                "water vapor",
                "Tranquilizer",
                "insurrectionist",
                "Threatened",
                "Cocaine",
                "water level",
                "neuromuscular",
                "Proton Magnetic Resonance",
                "Recession",
                "treacherous",
                "carbon monoxide poisoning"
            };
            foreach (var s in contents)
            {
                set.Add(s);
            }

            string prefix = "t";
            int    count  = 0;

            foreach (var item in set.CreateWindow("t", "tZZZZZZZ"))
            {
                Console.WriteLine(item);
                count++;
            }
            Assert.AreEqual(4, count);
        }
Пример #2
0
        public void TestPrioQueueMod()
        {
            Random r = new Random(0);

            int[] values         = new int[10000];
            var   notYetInserted = new TreeSet <int>();
            var   notYetDeleted  = new TreeSet <int>();

            for (int i = 0; i < 10000; i++)
            {
                values[i] = r.Next(1000);
                notYetInserted.Add(i);
            }

            var pq = new PrioQueueMod <int>((x, y) => values[x] - values[y], new ArrayLocator <int>(10000));

            while (notYetInserted.Count > 0 || notYetDeleted.Count > 0)
            {
                int n = r.Next(Math.Min(100, notYetInserted.Count + 1));
                while (n-- > 0)
                {
                    int id = notYetInserted.GetItem(r.Next(notYetInserted.Count));
                    notYetInserted.Remove(id);
                    notYetDeleted.Add(id);
                    pq.Insert(id);
                    Assert.IsTrue(pq.IsHeap(0));
                }
                n = r.Next(Math.Min(100, notYetDeleted.Count + 1));
                while (n-- > 0)
                {
                    int id = notYetDeleted.GetItem(r.Next(notYetDeleted.Count));
                    notYetDeleted.Remove(id);
                    pq.Erase(id);
                    Assert.IsTrue(pq.Empty || pq.IsHeap(0));
                }
            }
        }
Пример #3
0
        public void TestTreeSetStartEnd()
        {
            int[] contents = new[] { 2, 4, 7, 8, 12, 14, 16, 18, 19, 23, 27, 35, 37, 40 };
            var   treeSet  = new TreeSet <int>();

            for (int index = 0; index < contents.Length; index++)
            {
                treeSet.Add(contents[index]);
            }
            Assert.AreEqual(contents.Length, treeSet.Count);

            for (int i = 0; i < 50; i++)
            {
                int expectedStart = -1, expectedEnd = -1;
                int j = 0;
                while (j < contents.Length && contents[j] < i)
                {
                    j++;
                }
                if (j < contents.Length)
                {
                    expectedStart = contents[j];
                }
                j = contents.Length - 1;
                while (j >= 0 && contents[j] > i)
                {
                    j--;
                }
                if (j >= 0)
                {
                    expectedEnd = contents[j];
                }

                var startNode = treeSet.FindStartNode(i);
                var stopNode  = treeSet.FindEndNode(i);

                int actualStart = startNode == null ? -1 : startNode.Item;
                int actualEnd   = stopNode == null ? -1 : stopNode.Item;

                Assert.AreEqual(expectedStart, actualStart);
                Assert.AreEqual(expectedEnd, actualEnd);
            }

            for (int i = 0; i < 50; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    int actualCount = 0, last = -1;
                    foreach (int x in treeSet.CreateWindow(i, j))
                    {
                        Assert.IsTrue(x > last);
                        Assert.IsTrue(Array.IndexOf(contents, x) >= 0);
                        last = x;
                        actualCount++;
                    }
                    int expectedCount = 0;
                    foreach (int x in contents)
                    {
                        if (x >= i && x <= j)
                        {
                            expectedCount++;
                        }
                    }
                    Assert.AreEqual(expectedCount, actualCount);
                }
            }
        }