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.AddWithIndex(next); } } for (int i = 0; i < 1000; i++) { for (var next = rng.Next(1000); !set.Contains(next);) { set.Add(next); sd.AddWithIndex(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 AddReverseSequentialTest() { var sd = new SortedDeque <int>(); for (var r = 1000; r >= 0; r--) { sd.AddWithIndex(r); } }
public void AddSequentialTest() { var sd = new SortedDeque <int>(); for (var r = 0; r < 1000; r++) { sd.AddWithIndex(r); } }
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.AddWithIndex(next); } } for (int i = 0; i < 1000; i++) { for (var next = rng.Next(1000); !set.Contains(next);) { set.Add(next); sd.AddWithIndex(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.AddWithIndex(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.AddWithIndex(i); } else { set.Add(-i); sd.AddWithIndex(-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; } } } }