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

        for (int i = 0; i < 10; i++)
        {
            list.Add(null);
        }
        list[0] = "zero";
        list[1] = "ten";
        list[2] = "twenty";
        list[3] = "thirty";
        list[4] = "forty";
        list[5] = "fifty";
        list[6] = "sixty";
        list[7] = "seventy";
        list[8] = "eighty";
        list[9] = "ninety";
        AssertEquals(list.Count, 10);
        AssertEquals(list[0], "zero");
        AssertEquals(list[1], "ten");
        AssertEquals(list[2], "twenty");
        AssertEquals(list[3], "thirty");
        AssertEquals(list[4], "forty");
        AssertEquals(list[5], "fifty");
        AssertEquals(list[6], "sixty");
        AssertEquals(list[7], "seventy");
        AssertEquals(list[8], "eighty");
        AssertEquals(list[9], "ninety");
    }
예제 #2
0
 private void AddValues(AvlTreeList <int> tree)
 {
     for (int i = 0; i < _values.Length; i++)
     {
         var number = _values[i];
         tree.Add(number);
     }
 }
    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);
    }
    // Stresses the self-balancing mechanism
    private static void TestInsertManyBeginning()
    {
        AvlTreeList <int> list = new AvlTreeList <int>();

        for (int i = 0; i < 300000; i++)
        {
            list.Add(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 TestAdd()
    {
        AvlTreeList <string> list = new AvlTreeList <string>();

        list.Add("January");
        list.Add("February");
        list.Add("March");
        list.Add("April");
        list.Add("May");
        list.Add("June");
        list.CheckStructure();
        AssertEquals(list.Count, 6);
        AssertEquals(list[0], "January");
        AssertEquals(list[1], "February");
        AssertEquals(list[2], "March");
        AssertEquals(list[3], "April");
        AssertEquals(list[4], "May");
        AssertEquals(list[5], "June");
    }
    private static void TestRemoveAt()
    {
        AvlTreeList <char> list = new AvlTreeList <char>();

        {
            string str = "the quick brown fox jumped over the lazy dog";
            for (int i = 0; i < str.Length; i++)
            {
                list.Add(str[i]);
            }
            AssertEquals(list.Count, str.Length);
        }

        AssertEquals(list[2], 'e');  list.RemoveAt(2);
        AssertEquals(list[4], 'u');  list.RemoveAt(4);
        AssertEquals(list[3], 'q');  list.RemoveAt(3);
        AssertEquals(list[2], ' ');  list.RemoveAt(2);
        AssertEquals(list[12], 'f');  list.RemoveAt(12);
        AssertEquals(list[11], ' ');  list.RemoveAt(11);
        AssertEquals(list[10], 'n');  list.RemoveAt(10);
        AssertEquals(list[9], 'w');  list.RemoveAt(9);
        AssertEquals(list[11], ' ');  list.RemoveAt(11);
        AssertEquals(list[11], 'j');  list.RemoveAt(11);
        AssertEquals(list[11], 'u');  list.RemoveAt(11);
        AssertEquals(list[10], 'x');  list.RemoveAt(10);
        AssertEquals(list[11], 'p');  list.RemoveAt(11);
        AssertEquals(list[12], 'd');  list.RemoveAt(12);
        AssertEquals(list[11], 'e');  list.RemoveAt(11);
        AssertEquals(list[13], 'v');  list.RemoveAt(13);
        AssertEquals(list[13], 'e');  list.RemoveAt(13);
        AssertEquals(list[19], 'l');  list.RemoveAt(19);
        AssertEquals(list[20], 'z');  list.RemoveAt(20);
        AssertEquals(list[19], 'a');  list.RemoveAt(19);
        AssertEquals(list[18], ' ');  list.RemoveAt(18);
        AssertEquals(list[22], 'g');  list.RemoveAt(22);

        {
            String str = "thick broom or they do";
            AssertEquals(list.Count, str.Length);
            for (int i = 0; i < str.Length; i++)
            {
                AssertEquals(list[i], str[i]);
            }
        }

        AssertEquals(list[0], 't');  list.RemoveAt(0);
        AssertEquals(list[2], 'c');  list.RemoveAt(2);
        AssertEquals(list[2], 'k');  list.RemoveAt(2);
        AssertEquals(list[2], ' ');  list.RemoveAt(2);
        AssertEquals(list[2], 'b');  list.RemoveAt(2);
        AssertEquals(list[2], 'r');  list.RemoveAt(2);
        AssertEquals(list[2], 'o');  list.RemoveAt(2);
        AssertEquals(list[2], 'o');  list.RemoveAt(2);
        AssertEquals(list[4], 'o');  list.RemoveAt(4);
        AssertEquals(list[7], 'h');  list.RemoveAt(7);
        AssertEquals(list[5], ' ');  list.RemoveAt(5);
        AssertEquals(list[5], 't');  list.RemoveAt(5);
        AssertEquals(list[9], 'o');  list.RemoveAt(9);
        AssertEquals(list[7], ' ');  list.RemoveAt(7);
        AssertEquals(list[6], 'y');  list.RemoveAt(6);

        {
            String str = "him red";
            AssertEquals(list.Count, str.Length);
            for (int i = 0; i < str.Length; i++)
            {
                AssertEquals(list[i], str[i]);
            }
        }
    }