public void TreeShouldBeValidAfterManyRandomDeletions() { int seed = 12345; int RandomNext() { seed = (seed * 69069) + 12345; return((seed >> 16) & 0x7FFF); } WeightedRedBlackTree <int, int> tree = new WeightedRedBlackTree <int, int>(); List <Tuple <int, int> > insertions = new List <Tuple <int, int> >(); for (int i = 0; i < 500; i++) { int key = RandomNext(); insertions.Add(new Tuple <int, int>(key, i)); Assert.That(tree.Count, Is.EqualTo(i)); Assert.That(tree.TryGetValue(key, out int oldValue), Is.False); Assert.That(tree.ContainsKey(key), Is.False); tree.Add(key, i); if (IsFibonacci(i)) { tree.Validate(); } Assert.That(tree.ContainsKey(key), Is.True); Assert.That(tree[key], Is.EqualTo(i)); Assert.That(tree.TryGetValue(key, out int newValue), Is.True); Assert.That(newValue, Is.EqualTo(i)); Assert.That(tree.Count, Is.EqualTo(i + 1)); } for (int i = 0; i < 500; i++) { int key = insertions[i].Item1; int value = insertions[i].Item2; Assert.That(tree.ContainsKey(key), Is.True); Assert.That(tree[key], Is.EqualTo(value)); Assert.That(tree.TryGetValue(key, out int newValue), Is.True); Assert.That(newValue, Is.EqualTo(value)); Assert.That(tree.Count, Is.EqualTo(500 - i)); Assert.That(tree.Remove(key), Is.True); if (IsFibonacci(i)) { tree.Validate(); } Assert.That(tree.ContainsKey(key), Is.False); Assert.That(tree.TryGetValue(key, out int oldValue), Is.False); Assert.That(tree.Count, Is.EqualTo(500 - i - 1)); } }
public void NewTreeShouldBeEmpty() { WeightedRedBlackTree <int, int> tree = new WeightedRedBlackTree <int, int>(); Assert.That(tree.Root, Is.Null); Assert.That(tree.Count, Is.Zero); tree.Validate(); }