/// <summary> /// Applies Capacity+1 times the same test to the same buffer "logical" content but with /// different internal offsets each time. /// The "logical" initial buffer content is restored each time and at the end of the test. /// </summary> static void TestWithInternalOffsets <T>(FIFOBuffer <T> f, Action <FIFOBuffer <T> > testPredicate) { var saved = f.ToArray(); for (int iTry = 0; iTry <= f.Capacity; ++iTry) { f.Clear(); for (int i = 0; i < iTry; ++i) { f.Push(default(T)); } foreach (var i in saved) { f.Push(i); } while (f.Count > saved.Length) { f.Pop(); } f.SequenceEqual(saved).Should().BeTrue(); testPredicate(f); } foreach (var i in saved) { f.Push(i); } f.Truncate(saved.Length); }
public void FIFO_ToArray_method() { int[] initialArray = new int[7]; initialArray[0] = initialArray[6] = -1; FIFOBuffer <int> f = new FIFOBuffer <int>(5); f.ToArray().Length.Should().Be(0); f.Invoking(sut => sut.CopyTo(null)).Should().Throw <ArgumentNullException>(); f.Invoking(sut => sut.CopyTo(null, 0)).Should().Throw <ArgumentNullException>(); f.Invoking(sut => sut.CopyTo(null, 0, 0)).Should().Throw <ArgumentNullException>(); TestWithInternalOffsets(f, b => { var array = (int[])initialArray.Clone(); b.Push(1); b.CopyTo(array, 3, 2); array.SequenceEqual(new int[] { -1, 0, 0, 1, 0, 0, -1 }).Should().BeTrue(); array[3] = 0; b.Push(2); b.CopyTo(array, 3, 2); array.SequenceEqual(new int[] { -1, 0, 0, 1, 2, 0, -1 }).Should().BeTrue(); array[3] = 0; array[4] = 0; b.Push(3); b.CopyTo(array, 3, 3); array.SequenceEqual(new int[] { -1, 0, 0, 1, 2, 3, -1 }).Should().BeTrue(); array[3] = 0; array[4] = 0; array[5] = 0; b.Push(4); b.CopyTo(array, 3, 3); array.SequenceEqual(new int[] { -1, 0, 0, 2, 3, 4, -1 }).Should().BeTrue(); array[3] = 0; array[4] = 0; array[5] = 0; b.CopyTo(array, 2, 4); array.SequenceEqual(new int[] { -1, 0, 1, 2, 3, 4, -1 }).Should().BeTrue(); array[3] = 0; array[4] = 0; array[5] = 0; b.CopyTo(array, 2, 5).Should().Be(4); array.SequenceEqual(new int[] { -1, 0, 1, 2, 3, 4, -1 }).Should().BeTrue("Sentinel is not changed: there is only 4 items to copy."); b.Invoking(sut => sut.CopyTo(array, 2, 6)).Should().Throw <IndexOutOfRangeException>("Even if the items fit, there must be an exception."); b.Truncate(1); b.Peek().Should().Be(4); b.Push(60); b.Push(61); b.Push(62); b.Push(63); b.Push(7); // oldest b.Push(8); b.Push(9); b.Push(10); b.Push(11); b[0].Should().Be(7); array[3] = 0; array[4] = 0; array[5] = 0; b.CopyTo(array, 1).Should().Be(5); array.SequenceEqual(new int[] { -1, 7, 8, 9, 10, 11, -1 }).Should().BeTrue("Sentinel is not changed: there is only 5 items to copy."); array[5] = 0; b.CopyTo(array, 0).Should().Be(5); array.SequenceEqual(new int[] { 7, 8, 9, 10, 11, 0, -1 }).Should().BeTrue(); b.CopyTo(array, 5).Should().Be(2); array.SequenceEqual(new int[] { 7, 8, 9, 10, 11, 10, 11 }).Should().BeTrue(); }); }
static void AssertContains <T>(FIFOBuffer <T> f, params T[] values) { f.Count.Should().Be(values.Length); f.SequenceEqual(values).Should().BeTrue(); f.ToArray().SequenceEqual(values).Should().BeTrue(); }
static void AssertContains <T>(FIFOBuffer <T> f, params T[] values) { Assert.That(f.Count, Is.EqualTo(values.Length)); CollectionAssert.AreEqual(f, values); CollectionAssert.AreEqual(f.ToArray(), values); }
public void FIFO_ToArray_method() { int[] initialArray = new int[7]; initialArray[0] = initialArray[6] = -1; FIFOBuffer <int> f = new FIFOBuffer <int>(5); CollectionAssert.AreEqual(f.ToArray(), new int[0]); Assert.Throws <ArgumentNullException>(() => f.CopyTo(null)); Assert.Throws <ArgumentNullException>(() => f.CopyTo(null, 0)); Assert.Throws <ArgumentNullException>(() => f.CopyTo(null, 0, 0)); TestWithInternalOffsets(f, b => { var array = (int[])initialArray.Clone(); b.Push(1); b.CopyTo(array, 3, 2); CollectionAssert.AreEqual(array, new int[] { -1, 0, 0, 1, 0, 0, -1 }); array[3] = 0; b.Push(2); b.CopyTo(array, 3, 2); CollectionAssert.AreEqual(array, new int[] { -1, 0, 0, 1, 2, 0, -1 }); array[3] = 0; array[4] = 0; b.Push(3); b.CopyTo(array, 3, 3); CollectionAssert.AreEqual(array, new int[] { -1, 0, 0, 1, 2, 3, -1 }); array[3] = 0; array[4] = 0; array[5] = 0; b.Push(4); b.CopyTo(array, 3, 3); CollectionAssert.AreEqual(array, new int[] { -1, 0, 0, 2, 3, 4, -1 }); array[3] = 0; array[4] = 0; array[5] = 0; b.CopyTo(array, 2, 4); CollectionAssert.AreEqual(array, new int[] { -1, 0, 1, 2, 3, 4, -1 }); array[3] = 0; array[4] = 0; array[5] = 0; Assert.That(b.CopyTo(array, 2, 5), Is.EqualTo(4)); CollectionAssert.AreEqual(array, new int[] { -1, 0, 1, 2, 3, 4, -1 }, "Sentinel is not changed: there is only 4 items to copy."); Assert.Throws <IndexOutOfRangeException>(() => b.CopyTo(array, 2, 6), "Even if the items fit, there must be an exception."); b.Truncate(1); Assert.That(b.Peek(), Is.EqualTo(4)); b.Push(60); b.Push(61); b.Push(62); b.Push(63); b.Push(7); // oldest b.Push(8); b.Push(9); b.Push(10); b.Push(11); Assert.That(b[0], Is.EqualTo(7)); array[3] = 0; array[4] = 0; array[5] = 0; Assert.That(b.CopyTo(array, 1), Is.EqualTo(5)); CollectionAssert.AreEqual(array, new int[] { -1, 7, 8, 9, 10, 11, -1 }, "Sentinel is not changed: there is only 5 items to copy."); array[5] = 0; Assert.That(b.CopyTo(array, 0), Is.EqualTo(5)); CollectionAssert.AreEqual(array, new int[] { 7, 8, 9, 10, 11, 0, -1 }); Assert.That(b.CopyTo(array, 5), Is.EqualTo(2)); CollectionAssert.AreEqual(array, new int[] { 7, 8, 9, 10, 11, 10, 11 }); }); }
private static void AssertContains <T>(FIFOBuffer <T> f, params T[] values) { Assert.That(f.Count, Is.EqualTo(values.Length)); Assert.That(f.SequenceEqual(values), Is.True); Assert.That(f.ToArray().SequenceEqual(values), Is.True); }