コード例 #1
0
        public void TestQueuedAvlTrees()
        {
            qtree = new QueuedAVLTree <string, string>(1);
            List <string> hashes = new List <string>();
            List <string> values = new List <string>();

            Assert.IsTrue(qtree.Count == 0);
            qtree.Validate();
            qtree.Add("wolo", "123");
            Assert.AreEqual(qtree["wolo"], "123");
            Assert.IsTrue(qtree.Count == 1);
            Assert.IsTrue(qtree.ContainsKey("wolo"));
            Assert.IsTrue(qtree.Contains(new KeyValuePair <string, string>("wolo", "123")));
            Assert.IsFalse(qtree.ContainsKey("wolo1"));
            Assert.IsFalse(qtree.Contains(new KeyValuePair <string, string>("wolo", "1234")));
            Assert.IsFalse(qtree.Contains(new KeyValuePair <string, string>("wolo1", "123")));
            Assert.IsFalse(qtree.Remove(new KeyValuePair <string, string>("wolo", "1234")));
            Assert.IsFalse(qtree.Remove(new KeyValuePair <string, string>("wolo1", "123")));
            Assert.IsTrue(qtree.Count == 1);
            qtree.Validate();
            qtree.Add("yolo", "0123");
            Assert.AreEqual(qtree["yolo"], "0123");
            Assert.IsTrue(qtree.Count == 1);
            Assert.IsTrue(qtree.ContainsKey("yolo"));
            Assert.IsTrue(qtree.Contains(new KeyValuePair <string, string>("yolo", "0123")));
            Assert.IsFalse(qtree.Contains(new KeyValuePair <string, string>("wolo", "123")));
            Assert.IsFalse(qtree.ContainsKey("wolo"));
            qtree.Validate();
            Assert.IsTrue(qtree.Remove(new KeyValuePair <string, string>("yolo", "0123")));
            Assert.IsTrue(qtree.Count == 0);
            Assert.IsFalse(qtree.Contains(new KeyValuePair <string, string>("yolo", "0123")));
            Assert.IsFalse(qtree.ContainsKey("yolo"));
            qtree.Validate();
            qtree["wolo"] = "abc";
            Assert.IsTrue(qtree.Count == 1);
            Assert.IsTrue(qtree.Remove("wolo"));
            qtree.Clear();
            Assert.IsTrue(qtree.Count == 0);
            qtree.Validate();

            qtree = new QueuedAVLTree <string, string>(10);
            ExecuteTestQueuedTree(10);
            qtree.Clear();

            qtree = new QueuedAVLTree <string, string>(1024);
            ExecuteTestQueuedTree(1024);
            qtree.Clear();
        }
コード例 #2
0
        public void TestQueuedAvlTreesError()
        {
            Console.Write("--- THE BUG HAS BEEN: ");

            var qt = new QueuedAVLTree <string, string>(10);

            for (int i = 0; i < 20; i += 2)
            {
                qt.Add(new KeyValuePair <string, string>(i.ToString("00"), i.ToString("00")));
            }

            Assert.IsTrue(qt.Count == 10);

            for (int i = 1; i < 21; i += 2)
            {
                qt.Add(new KeyValuePair <string, string>(i.ToString("00"), i.ToString("00")));
                Assert.IsTrue(qt[i.ToString("00")] == i.ToString("00"));
                qt.Validate();
                Assert.IsTrue(qt.Count == 10);
            }

            Console.WriteLine("FIXED! --- ");
        }
コード例 #3
0
        public void ExecuteTestQueuedTree(int size)
        {
            List <string> hashes = new List <string>();
            List <string> values = new List <string>();

            Console.WriteLine("Small Tests...");
            Assert.IsTrue(qtree.Count == 0);
            qtree.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(qtree.ContainsKey("key"));
            Assert.IsFalse(qtree.ContainsKey("value"));
            Assert.IsTrue(qtree["key"] == "value");
            Assert.IsTrue(qtree.Count == 1);
            qtree.Validate();
            qtree.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(qtree.ContainsKey("key"));
            Assert.IsFalse(qtree.ContainsKey("value"));
            Assert.IsTrue(qtree["key"] == "value2");
            Assert.IsTrue(qtree.Count == 1);
            qtree.Validate();
            qtree.Add(new KeyValuePair <string, string>("key", "avalue"));
            Assert.IsTrue(qtree.ContainsKey("key"));
            Assert.IsFalse(qtree.ContainsKey("value"));
            Assert.IsTrue(qtree["key"] == "avalue");
            Assert.IsTrue(qtree.Count == 1);
            qtree.Validate();
            Assert.IsFalse(qtree.Remove("value"));
            Assert.IsTrue(qtree.Remove("key"));
            qtree.Validate();
            Assert.IsTrue(qtree.Count == 0);
            qtree.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(qtree.Count == 1);
            qtree.Clear();
            Assert.IsTrue(qtree.Count == 0);
            qtree.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(qtree.ContainsKey("key"));
            Assert.IsFalse(qtree.ContainsKey("value"));
            Assert.IsTrue(qtree["key"] == "value");
            Assert.IsTrue(qtree.Count == 1);
            qtree.Validate();
            qtree.Clear();
            Assert.IsFalse(qtree.Remove(""));
            Assert.IsFalse(qtree.Remove(new KeyValuePair <string, string>("", "")));

            Console.WriteLine("Adding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(qtree.Count == i);
                hashes.Add(Hash.GetHash());
                values.Add(Hash.GetHash());
                qtree[hashes[i]] = values[i];
                Assert.IsTrue(qtree[hashes[i]] == values[i]);
                Assert.IsTrue(qtree.Keys.Contains(hashes[i]));
                Assert.IsTrue(qtree.Values.Contains(values[i]));
                qtree.Validate();
            }

            Console.WriteLine("Overriding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(qtree[hashes[i]] == values[i]);
                values[i]        = Hash.GetHash();
                qtree[hashes[i]] = values[i];
                Assert.IsTrue(qtree.Count == size);
            }

            Console.WriteLine("Checking...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(qtree[hashes[i]] == values[i]);
                Assert.IsTrue(qtree.Keys.Contains(hashes[i]));
                Assert.IsTrue(qtree.Values.Contains(values[i]));
            }

            Console.WriteLine("Validating...");

            qtree.Validate();

            Console.WriteLine("Deleting...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(qtree.Count == size - i);
                Assert.IsTrue(qtree.ContainsKey(hashes[i]));
                Assert.IsTrue(qtree[hashes[i]] != default(string));
                Assert.IsTrue(qtree.Remove(hashes[i]));
                Assert.IsFalse(qtree.Keys.Contains(hashes[i]));
                Assert.IsFalse(qtree.Values.Contains(values[i]));

                if (true)
                {
                    for (int j = i + 1; j < size; j++)
                    {
                        Assert.IsFalse(qtree[hashes[j]].Contains(hashes[j]));
                    }

                    for (int j = 0; j < i; j++)
                    {
                        Assert.IsFalse(qtree.Remove(hashes[j]));
                    }
                }

                Assert.IsTrue(qtree[hashes[i]] == default(string));
                qtree.Validate();
            }

            hashes.Clear();
            values.Clear();

            Console.WriteLine("Adding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(qtree.Count == i);
                hashes.Add(Hash.GetHash());
                values.Add(Hash.GetHash());
                qtree[hashes[i]] = values[i];
                Assert.IsTrue(qtree[hashes[i]] == values[i]);
                Assert.IsTrue(qtree.Keys.Contains(hashes[i]));
                Assert.IsTrue(qtree.Values.Contains(values[i]));
                qtree.Validate();
            }

            Console.WriteLine("Overflowing...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(qtree.Count == size);
                hashes.Add(Hash.GetHash());
                values.Add(Hash.GetHash());
                qtree.Validate();
                qtree[hashes[size + i]] = values[size + i];
                qtree.Validate();
                Assert.IsTrue(qtree[hashes[size + i]] == values[size + i]);
                Assert.IsTrue(qtree.Keys.Contains(hashes[size + i]));
                Assert.IsTrue(qtree.Values.Contains(values[size + i]));
                qtree.Validate();
            }

            Console.WriteLine("Overriding...");

            for (int i = 0; i < size; i++)
            {
                values[i] = Hash.GetHash();
                qtree[hashes[size + i]] = values[size + i];
                Assert.IsTrue(qtree[hashes[size + i]] == values[size + i]);
                Assert.IsTrue(qtree.Keys.Contains(hashes[size + i]));
                Assert.IsTrue(qtree.Values.Contains(values[size + i]));
                Assert.IsTrue(qtree.Count == size);
                qtree.Validate();
            }

            Console.WriteLine("Validating...");

            qtree.Validate();

            Console.WriteLine("Deleting...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(qtree.Count == size - i);
                Assert.IsTrue(qtree.ContainsKey(hashes[size + i]));
                Assert.IsTrue(qtree[hashes[size + i]] != default(string));
                Assert.IsTrue(qtree.Remove(hashes[size + i]));
                Assert.IsFalse(qtree.Keys.Contains(hashes[size + i]));
                Assert.IsFalse(qtree.Values.Contains(values[size + i]));

                if (true)
                {
                    for (int j = i + 1; j < size; j++)
                    {
                        Assert.IsFalse(qtree[hashes[size + j]].Contains(hashes[size + j]));
                    }

                    for (int j = 0; j < i; j++)
                    {
                        Assert.IsFalse(qtree.Remove(hashes[size + j]));
                    }
                }

                Assert.IsTrue(qtree[hashes[size + i]] == default(string));
                qtree.Validate();
            }
        }