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]); } } } }
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]); } } }