public void AddRemoveTest() { var rng = new System.Random(); for (int r = 0; r < 1000; r++) { var sd = new SortedDeque <int>(); var set = new HashSet <int>(); for (int i = 0; i < 50; i++) { for (var next = rng.Next(1000); !set.Contains(next);) { set.Add(next); sd.TryAdd(next); } } for (int i = 0; i < 1000; i++) { for (var next = rng.Next(1000); !set.Contains(next);) { set.Add(next); sd.TryAdd(next); } Assert.AreEqual(set.Count, sd.Count); Assert.AreEqual(set.Sum(), sd.Sum()); var first = sd.RemoveFirst(); set.Remove(first); Assert.AreEqual(set.Count, sd.Count); Assert.AreEqual(set.Sum(), sd.Sum()); Assert.IsTrue(DequeIsSorted(sd)); var c = 0; var prev = 0; foreach (var e in sd) { if (c == 0) { c++; prev = e; } else { Assert.IsTrue(e > prev); prev = e; } } } } }
public void AddRemoveTestWithRemoveElement() { var rng = new System.Random(); for (int r = 0; r < 1000; r++) { var sd = new SortedDeque <int>(); var set = new HashSet <int>(); for (int i = 0; i < 50; i++) { for (var next = rng.Next(1000); !set.Contains(next);) { set.Add(next); sd.TryAdd(next); } } for (int i = 0; i < 1000; i++) { for (var next = rng.Next(1000); !set.Contains(next);) { set.Add(next); sd.TryAdd(next); } Assert.AreEqual(set.Count, sd.Count); Assert.AreEqual(set.Sum(), sd.Sum()); var first = sd.First; sd.Remove(first); set.Remove(first); Assert.AreEqual(set.Count, sd.Count); Assert.AreEqual(set.Sum(), sd.Sum()); Assert.IsTrue(DequeIsSorted(sd)); } } }
public void CouldRemoveInTheMiddle() { for (int r = 0; r < 1000; r++) { var rng = new System.Random(); var sd = new SortedDeque <int>(); var set = new HashSet <int>(); for (int i = 0; i < 100; i++) { for (var next = rng.Next(1000); !set.Contains(next);) { set.Add(next); sd.TryAdd(next); } } while (sd.Count > 0) { var midElement = sd._buffer[((sd._firstOffset + sd._count / 2) % sd._buffer.Length)]; sd.Remove(midElement); set.Remove(midElement); Assert.AreEqual(set.Count, sd.Count); Assert.AreEqual(set.Sum(), sd.Sum()); Assert.IsTrue(DequeIsSorted(sd)); var c = 0; var prev = 0; foreach (var e in sd) { if (c == 0) { c++; prev = e; } else { Assert.IsTrue(e > prev); prev = e; } } } } }
public void CouldRemoveInTheMiddleSplit() { var rng = new System.Random(); var sd = new SortedDeque <int>(); var set = new HashSet <int>(); for (int i = 0; i < 100; i++) { if (i % 2 == 0) { set.Add(i); sd.TryAdd(i); } else { set.Add(-i); sd.TryAdd(-i); } } { var midElement = sd._buffer[0]; //[.___ ____] sd.Remove(midElement); set.Remove(midElement); Assert.AreEqual(set.Count, sd.Count); Assert.AreEqual(set.Sum(), sd.Sum()); Assert.IsTrue(DequeIsSorted(sd)); var c = 0; var prev = 0; foreach (var e in sd) { if (c == 0) { c++; prev = e; } else { Assert.IsTrue(e > prev); prev = e; } } } { var midElement = sd._buffer[sd._buffer.Length - 1]; //[___ ____.] sd.Remove(midElement); set.Remove(midElement); Assert.AreEqual(set.Count, sd.Count); Assert.AreEqual(set.Sum(), sd.Sum()); Assert.IsTrue(DequeIsSorted(sd)); var c = 0; var prev = 0; foreach (var e in sd) { if (c == 0) { c++; prev = e; } else { Assert.IsTrue(e > prev); prev = e; } } } }