private static void TestInsertAtBeginning()
    {
        AvlTreeList <string> list = new AvlTreeList <string>();

        list.Insert(0, "Sunday");
        list.Insert(0, "Monday");
        list.Insert(0, "Tuesday");
        AssertEquals(list.Count, 3);
        AssertEquals(list[0], "Tuesday");
        AssertEquals(list[1], "Monday");
        AssertEquals(list[2], "Sunday");
    }
    private static void TestInsertAtEnd()
    {
        AvlTreeList <string> list = new AvlTreeList <string>();

        list.Insert(0, "Saturday");
        list.Insert(1, "Friday");
        list.Insert(2, "Thursday");
        list.Insert(3, "Wednesday");
        AssertEquals(list.Count, 4);
        AssertEquals(list[0], "Saturday");
        AssertEquals(list[1], "Friday");
        AssertEquals(list[2], "Thursday");
        AssertEquals(list[3], "Wednesday");
    }
    // Stresses the self-balancing mechanism
    private static void TestInsertManyEnd()
    {
        AvlTreeList <int> list = new AvlTreeList <int>();

        for (int i = 299999; i >= 0; i--)
        {
            list.Insert(0, i);
        }

        for (int i = 0; i < list.Count; i++)
        {
            AssertEquals(list[i], i);
        }
    }
    // Adds in a weird binary pattern to stress arrays and linked lists
    private static void TestInsertManyEverywhere()
    {
        const int         N    = 18;
        AvlTreeList <int> list = new AvlTreeList <int>();

        list.Add(0);
        for (int i = N - 1; i >= 0; i--)
        {
            for (int j = 1 << i, k = 1; j < (1 << N); j += 2 << i, k += 2)
            {
                list.Insert(k, j);
            }
        }

        for (int i = 0; i < list.Count; i++)
        {
            AssertEquals(list[i], i);
        }
    }
    private static void TestInsertAtMiddle()
    {
        AvlTreeList <string> list = new AvlTreeList <string>();

        list.Insert(0, "Up");
        list.Insert(1, "Down");
        list.Insert(1, "Left");
        list.Insert(2, "Right");
        list.Insert(1, "Front");
        list.Insert(2, "Back");
        AssertEquals(list.Count, 6);
        AssertEquals(list[0], "Up");
        AssertEquals(list[1], "Front");
        AssertEquals(list[2], "Back");
        AssertEquals(list[3], "Left");
        AssertEquals(list[4], "Right");
        AssertEquals(list[5], "Down");
    }
Beispiel #6
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]);
                }
            }
        }
    }