public static void DoTest() { // Red-Black Tree Map collection var redBlackTree = new RedBlackTreeMap <int, string>(allowDuplicates: false); // Testing data KeyValuePair <int, string>[] values = new KeyValuePair <int, string> [10]; // Prepare the values array for (int i = 1; i <= 10; ++i) { var keyValPair = new KeyValuePair <int, string>(i, String.Format("Integer: {0}", i)); values[i - 1] = keyValPair; } // Test singular insert for (int i = 0; i < 10; ++i) { redBlackTree.Insert(values[i].Key, values[i].Value); } Assert.True(redBlackTree.Count == values.Length, "Expected the same number of items."); Assert.True(redBlackTree.Height < redBlackTree.Count, "Fail! Tree doesn't rebalance against sorted elements!"); redBlackTree.Clear(); // Test collection insert redBlackTree.Insert(values); Assert.True(redBlackTree.Height < redBlackTree.Count, "Fail! Tree doesn't rebalance against sorted elements!"); // Test enumeration of key-value pairs is still in oreder var enumerator = redBlackTree.GetInOrderEnumerator(); for (int i = 0; i < 10; ++i) { if (enumerator.MoveNext()) { var curr = enumerator.Current; if (curr.Key != values[i].Key || curr.Value != values[i].Value) { throw new Exception(); } } } // Test against re-shuffled insertions (not like above order) redBlackTree = new RedBlackTreeMap <int, string>(allowDuplicates: false); redBlackTree.Insert(4, "int4"); redBlackTree.Insert(5, "int5"); redBlackTree.Insert(7, "int7"); redBlackTree.Insert(2, "int2"); redBlackTree.Insert(1, "int1"); redBlackTree.Insert(3, "int3"); redBlackTree.Insert(6, "int6"); //redBlackTree.Insert(0, "int0"); redBlackTree.Insert(8, "int8"); redBlackTree.Insert(10, "int10"); redBlackTree.Insert(9, "int9"); Assert.True(redBlackTree.Count == values.Length, "Expected the same number of items."); Assert.True(redBlackTree.Height < redBlackTree.Count, "Fail! Tree doesn't rebalance against sorted elements!"); // ASSERT INSERTING DUPLICATES WOULD BREAK var insert_duplicate_passed = true; try { // 2 already exists in tree redBlackTree.Insert(2, "int2"); insert_duplicate_passed = true; } catch { insert_duplicate_passed = false; } Assert.True(insert_duplicate_passed == false, "Fail! The tree doesn't allow duplicates"); // Test find Assert.True(redBlackTree.Find(5).Key == 5, "Wrong find result!"); Assert.True(redBlackTree.FindMin().Key == 1, "Wrong min!"); Assert.True(redBlackTree.FindMax().Key == 10, "Wrong max!"); // Assert find raises exception on non-existing elements bool threwKeyNotFoundError = false; try { redBlackTree.Find(999999999); threwKeyNotFoundError = false; } catch (KeyNotFoundException) { threwKeyNotFoundError = true; } Assert.True(true == threwKeyNotFoundError, "Expected to catch KeyNotFoundException."); // PRINT TREE //Console.WriteLine("********************"); //Console.WriteLine(" [*] RED-BLACK TREE:\r\n"); //Console.WriteLine("********************"); //Console.WriteLine(redBlackTree.DrawTree()); //Console.WriteLine("\r\n"); // Assert count Assert.True(redBlackTree.Count == 10); // Assert existence and nonexistence of some items Assert.True(redBlackTree.Contains(1) == true); Assert.True(redBlackTree.Contains(3) == true); Assert.True(redBlackTree.Contains(999) == false); // ASSERT THAT EACH LEVEL HAS A DIFFERENT COLOR // TODO: Wrong color element "int4" // AssetLevelsDifferentColors(redBlackTree); // Do some deletions redBlackTree.Remove(7); redBlackTree.Remove(1); redBlackTree.Remove(3); // Assert count Assert.True(redBlackTree.Count == 7); // Assert nonexistence of previously existing items Assert.True(redBlackTree.Contains(1) == false); Assert.True(redBlackTree.Contains(3) == false); // Remove root value var oldRootKey = redBlackTree.Root.Key; redBlackTree.Remove(redBlackTree.Root.Key); // Assert count Assert.True(redBlackTree.Count == 6); // Assert nonexistence of old root's key Assert.True(redBlackTree.Contains(oldRootKey) == false); //// PRINT TREE //Console.WriteLine("********************"); //Console.WriteLine(" [*] RED-BLACK TREE:\r\n"); //Console.WriteLine("********************"); //Console.WriteLine(redBlackTree.DrawTree(includeValues: true)); //Console.WriteLine("\r\n"); //Console.ReadLine(); }//end-do-test