示例#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);
     }
 }
示例#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)));
     }
 }
示例#3
0
 public void TryAdd_Testing()
 {
     // adding duplicate values should return false
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Assert.IsTrue(tree.TryAdd(1).Success);
         Assert.IsFalse(tree.TryAdd(1).Success);
     }
     // normal add checking
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Assert.IsTrue(tree.TryAdd(1).Success);
         Assert.IsTrue(tree.TryAdd(2).Success);
         Assert.IsTrue(tree.TryAdd(3).Success);
         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.TryAdd(i));
         Assert.IsTrue(tree.Count is 100);
         Iterate(100, i => Assert.IsTrue(tree.Contains(i)));
     }
 }
示例#4
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);
            }
        }
示例#5
0
 public void GetEnumerable_Testing()
 {
                 #pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Assert.IsTrue(tree.Count() is 0);
     }
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Assert.IsTrue(tree.TryAdd(1).Success);
         Assert.IsTrue(tree.Count() is 1);
         Assert.IsTrue(tree.SequenceEqual(Ɐ(1)));
     }
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Assert.IsTrue(tree.TryAdd(1).Success);
         Assert.IsTrue(tree.TryAdd(2).Success);
         Assert.IsTrue(tree.TryAdd(3).Success);
         Assert.IsTrue(tree.Count() is 3);
         Assert.IsTrue(tree.SequenceEqual(Ɐ(1, 2, 3)));
     }
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Assert.IsTrue(tree.TryAdd(1).Success);
         Assert.IsTrue(tree.TryAdd(2).Success);
         Assert.IsTrue(tree.TryAdd(3).Success);
         Assert.IsTrue(tree.TryAdd(4).Success);
         Assert.IsTrue(tree.TryAdd(5).Success);
         Assert.IsTrue(tree.TryAdd(6).Success);
         Assert.IsTrue(tree.TryAdd(7).Success);
         Assert.IsTrue(tree.TryAdd(8).Success);
         Assert.IsTrue(tree.TryAdd(9).Success);
         Assert.IsTrue(tree.Count() is 9);
         Assert.IsTrue(tree.SequenceEqual(Ɐ(1, 2, 3, 4, 5, 6, 7, 8, 9)));
     }
                 #pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
 }
示例#6
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);
                }
            }
        }