Exemplo n.º 1
0
 public void TryRemove_Testing()
 {
     // removing a non-existing value should return false
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         tree.Add(1);
         tree.Add(3);
         Assert.IsFalse(tree.TryRemove(2).Success);
     }
     // normal remove checking
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         tree.Add(1);
         tree.Add(2);
         tree.Add(3);
         Assert.IsTrue(tree.Count is 3);
         Assert.IsTrue(tree.TryRemove(1).Success);
         Assert.IsFalse(tree.Contains(1));
         Assert.IsTrue(tree.Count is 2);
         Assert.IsTrue(tree.TryRemove(2).Success);
         Assert.IsFalse(tree.Contains(2));
         Assert.IsTrue(tree.Count is 1);
         Assert.IsTrue(tree.TryRemove(3).Success);
         Assert.IsFalse(tree.Contains(3));
         Assert.IsTrue(tree.Count is 0);
     }
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Iterate(100, i => tree.Add(i));
         Assert.IsTrue(tree.Count is 100);
         Iterate(100, i => tree.Remove(i));
         Assert.IsTrue(tree.Count is 0);
     }
 }
Exemplo n.º 2
0
 public void Add_Testing()
 {
     // adding duplicate values should throw exceptions
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         tree.Add(1);
         Assert.ThrowsException <ArgumentException>(() => tree.Add(1));
     }
     // normal add checking
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         tree.Add(1);
         tree.Add(2);
         tree.Add(3);
         Assert.IsTrue(tree.Count is 3);
         Assert.IsTrue(tree.Contains(1));
         Assert.IsTrue(tree.Contains(2));
         Assert.IsTrue(tree.Contains(3));
     }
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Iterate(100, i => tree.Add(i));
         Assert.IsTrue(tree.Count == 100);
         Iterate(100, i => Assert.IsTrue(tree.Contains(i)));
     }
 }
Exemplo n.º 3
0
        public void AvlTreeLinked_AddRunTime()
        {
            IAvlTree <Person> tree = AvlTreeLinked.New <Person>(
                (a, b) => Compare(a.FirstName, b.FirstName));

            foreach (Person person in RandomTestData !)
            {
                tree.Add(person);
            }
        }
Exemplo n.º 4
0
        public void Remove_Testing()
        {
            // removing a non-existing value should throw exceptions
            {
                IAvlTree <int> tree = AvlTreeLinked.New <int>();
                tree.Add(1);
                tree.Add(3);
                Assert.ThrowsException <ArgumentException>(() => tree.Remove(2));
            }
            // normal remove checking
            {
                IAvlTree <int> tree = AvlTreeLinked.New <int>();
                tree.Add(1);
                tree.Add(2);
                tree.Add(3);
                Assert.IsTrue(tree.Count is 3);
                tree.Remove(1);
                Assert.IsFalse(tree.Contains(1));
                Assert.IsTrue(tree.Count is 2);
                tree.Remove(2);
                Assert.IsFalse(tree.Contains(2));
                Assert.IsTrue(tree.Count is 1);
                tree.Remove(3);
                Assert.IsFalse(tree.Contains(3));
                Assert.IsTrue(tree.Count is 0);
            }
            {
                IAvlTree <int> tree = AvlTreeLinked.New <int>();
                Iterate(100, i => tree.Add(i));
                Assert.IsTrue(tree.Count is 100);
                Iterate(100, i => tree.Remove(i));
                Assert.IsTrue(tree.Count is 0);
            }
            // large randomized data set
            {
                const int      count = 1000;
                IAvlTree <int> tree  = AvlTreeLinked.New <int>();
                for (int i = 0; i < count; i++)
                {
                    Assert.IsTrue(tree.Count == i);
                    tree.Add(i);
                    Assert.IsTrue(tree.Count == i + 1);
                    Assert.IsTrue(tree.Contains(i));

                    int stepperCount = 0;
                    tree.Stepper(x => stepperCount++);
                    Assert.IsTrue(stepperCount == tree.Count);
                }
                int[] values = new int[count];
                for (int i = 0; i < count; i++)
                {
                    values[i] = i;
                }
                Random random = new(7);
                Shuffle <int>(values, random);
                for (int i = 0; i < count; i++)
                {
                    Assert.IsTrue(tree.Count == count - i);
                    tree.Remove(values[i]);
                    Assert.IsTrue(tree.Count == count - i - 1);
                    Assert.IsFalse(tree.Contains(values[i]));

                    int stepperCount = 0;
                    tree.Stepper(x => stepperCount++);
                    Assert.IsTrue(stepperCount == tree.Count);
                }
            }
        }