private static void TestClear()
    {
        AvlTreeList <int> list = new AvlTreeList <int>();

        for (int i = 0; i < 20; i++)
        {
            list.Add(i * i);
        }

        list.Clear();
        AssertEquals(list.Count, 0);

        list.Add(-1);
        list.Add(-8);
        list.Add(-27);
        AssertEquals(list.Count, 3);
        AssertEquals(list[0], -1);
        AssertEquals(list[1], -8);
        AssertEquals(list[2], -27);
    }
예제 #2
0
    private static void TestAgainstNetListRandomly()
    {
        Random            rand  = new Random();
        IList <int>       list0 = new List <int>();
        AvlTreeList <int> list1 = new AvlTreeList <int>();
        int size = 0;

        for (int i = 0; i < 100000; i++)
        {
            int op = rand.Next(100);

            if (op < 1)                // Clear
            {
                list1.CheckStructure();
                list0.Clear();
                list1.Clear();
                size = 0;
            }
            else if (op < 2)                  // Set
            {
                if (size > 0)
                {
                    int index = rand.Next(size);
                    int val   = rand.Next();
                    list0[index] = val;
                    list1[index] = val;
                }
            }
            else if (op < 30)                  // Random insertion
            {
                int n = rand.Next(100) + 1;
                for (int j = 0; j < n; j++)
                {
                    int index = rand.Next(size + 1);
                    int val   = rand.Next();
                    list0.Insert(index, val);
                    list1.Insert(index, val);
                }
                size += n;
            }
            else if (op < 50)                  // Ascending insertion
            {
                int n      = rand.Next(100) + 1;
                int offset = rand.Next(size + 1);
                for (int j = 0; j < n; j++, offset++)
                {
                    int val = rand.Next();
                    list0.Insert(offset, val);
                    list1.Insert(offset, val);
                }
                size += n;
            }
            else if (op < 70)                  // Descending insertion
            {
                int n      = rand.Next(100) + 1;
                int offset = rand.Next(size + 1);
                for (int j = 0; j < n; j++)
                {
                    int val = rand.Next();
                    list0.Insert(offset, val);
                    list1.Insert(offset, val);
                }
                size += n;
            }
            else if (op < 80)                  // Random deletion
            {
                int n = rand.Next(100) + 1;
                for (int j = 0; j < n && size > 0; j++, size--)
                {
                    int index = rand.Next(size);
                    AssertEquals(list0[index], list1[index]);
                    list0.RemoveAt(index);
                    list1.RemoveAt(index);
                }
            }
            else if (op < 90)                  // Ascending deletion
            {
                int n = rand.Next(100) + 1;
                if (size > 0)
                {
                    int offset = rand.Next(size);
                    for (int j = 0; j < n && offset < size; j++, size--)
                    {
                        AssertEquals(list0[offset], list1[offset]);
                        list0.RemoveAt(offset);
                        list1.RemoveAt(offset);
                    }
                }
            }
            else if (op < 100)                  // Descending deletion
            {
                int n = rand.Next(100) + 1;
                if (size > 0)
                {
                    int offset = rand.Next(size);
                    for (int j = 0; j < n && offset >= 0; j++, offset--, size--)
                    {
                        AssertEquals(list0[offset], list1[offset]);
                        list0.RemoveAt(offset);
                        list1.RemoveAt(offset);
                    }
                }
            }
            else
            {
                throw new SystemException();
            }

            AssertEquals(size, list0.Count);
            AssertEquals(size, list1.Count);
            if (size > 0)
            {
                for (int j = 0; j < 10; j++)
                {
                    int index = rand.Next(size);
                    AssertEquals(list0[index], list1[index]);
                }
            }
        }
    }