public void CanMutateThroughAsDictionaryWeighted() { WeightedRedBlackTree <int, string> tree = new WeightedRedBlackTree <int, string> { { 1, "1" }, { 2, "2" }, { 3, "3" }, { 4, "4" }, { 5, "5" }, }; IDictionary <int, string> dictionary = tree.AsDictionary(); dictionary[3] = "foo"; Assert.That(tree.Find(3).Value, Is.EqualTo("foo")); dictionary.Add(6, "6"); Assert.That(tree.Find(6).Value, Is.EqualTo("6")); dictionary.Add(new KeyValuePair <int, string>(7, "7")); Assert.That(tree.Find(7).Value, Is.EqualTo("7")); Assert.That(dictionary.Remove(3), Is.True); Assert.That(tree.Find(3), Is.Null); Assert.That(dictionary.Remove(new KeyValuePair <int, string>(8, "8")), Is.False); dictionary.Add(8, null); Assert.That(dictionary.Remove(new KeyValuePair <int, string>(2, null)), Is.False); Assert.That(dictionary.Remove(new KeyValuePair <int, string>(2, "2")), Is.True); Assert.That(tree.Find(2), Is.Null); Assert.That(dictionary.Remove(new KeyValuePair <int, string>(8, "8")), Is.False); Assert.That(dictionary.Remove(new KeyValuePair <int, string>(8, null)), Is.True); Assert.That(tree.Find(8), Is.Null); CollectionAssert.AreEqual(tree.KeyValuePairs, new[] { new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(4, "4"), new KeyValuePair <int, string>(5, "5"), new KeyValuePair <int, string>(6, "6"), new KeyValuePair <int, string>(7, "7"), }); dictionary.Clear(); Assert.That(tree.Root, Is.Null); Assert.That(tree.Count, Is.Zero); }
public void CannotRemoveKeysThatDontExist() { WeightedRedBlackTree <int, int> tree = new WeightedRedBlackTree <int, int> { { 1, 10 }, { 3, 20 }, { 5, 30 }, { 7, 40 }, { 9, 50 }, }; Assert.That(tree.Remove(4), Is.False); CollectionAssert.AreEqual(tree.KeyValuePairs, new[] { new KeyValuePair <int, int>(1, 10), new KeyValuePair <int, int>(3, 20), new KeyValuePair <int, int>(5, 30), new KeyValuePair <int, int>(7, 40), new KeyValuePair <int, int>(9, 50), }); Assert.That(tree.Remove(3), Is.True); CollectionAssert.AreEqual(tree.KeyValuePairs, new[] { new KeyValuePair <int, int>(1, 10), new KeyValuePair <int, int>(5, 30), new KeyValuePair <int, int>(7, 40), new KeyValuePair <int, int>(9, 50), }); Assert.That(tree.Remove(1), Is.True); CollectionAssert.AreEqual(tree.KeyValuePairs, new[] { new KeyValuePair <int, int>(5, 30), new KeyValuePair <int, int>(7, 40), new KeyValuePair <int, int>(9, 50), }); Assert.That(tree.Remove(7), Is.True); CollectionAssert.AreEqual(tree.KeyValuePairs, new[] { new KeyValuePair <int, int>(5, 30), new KeyValuePair <int, int>(9, 50), }); Assert.That(tree.Remove(9), Is.True); CollectionAssert.AreEqual(tree.KeyValuePairs, new[] { new KeyValuePair <int, int>(5, 30), }); WeightedRedBlackTreeNode <int, int> fiveNode = tree.Find(5); Assert.That(tree.Remove(5), Is.True); tree.DeleteNode(null); // Make this is always a no-op. CollectionAssert.AreEqual(tree.KeyValuePairs, new KeyValuePair <int, int> [0]); Assert.That(tree.Remove(3), Is.False); Assert.That(tree.Count, Is.Zero); Assert.That(tree.Root, Is.Null); tree.DeleteNode(fiveNode); // Make sure nothing bad happens with an empty tree. }