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 }); }); }
public void FIFO_change_capacity_preserves_items() { FIFOBuffer <int> f = new FIFOBuffer <int>(0); Assert.That(f.Capacity, Is.EqualTo(0)); AssertEmpty(f); f.Push(5); AssertEmpty(f); f.Push(12); AssertEmpty(f); f.Capacity = 1; Assert.That(f.Capacity, Is.EqualTo(1)); AssertEmpty(f); f.Push(5); AssertContains(f, 5); f.Push(6); AssertContains(f, 6); f.Capacity = 2; Assert.That(f.Capacity, Is.EqualTo(2)); AssertContains(f, 6); f.Push(7); AssertContains(f, 6, 7); f.Push(8); AssertContains(f, 7, 8); f.Capacity = 4; Assert.That(f.Capacity, Is.EqualTo(4)); AssertContains(f, 7, 8); f.Push(9); AssertContains(f, 7, 8, 9); f.Push(10); AssertContains(f, 7, 8, 9, 10); f.Push(11); AssertContains(f, 8, 9, 10, 11); f.Capacity = 7; Assert.That(f.Capacity, Is.EqualTo(7)); AssertContains(f, 8, 9, 10, 11); f.Push(12); AssertContains(f, 8, 9, 10, 11, 12); f.Push(13); AssertContains(f, 8, 9, 10, 11, 12, 13); f.Push(14); AssertContains(f, 8, 9, 10, 11, 12, 13, 14); f.Push(15); AssertContains(f, 9, 10, 11, 12, 13, 14, 15); f.Capacity = 2; Assert.That(f.Capacity, Is.EqualTo(2)); AssertContains(f, 14, 15); f.Capacity = 3; Assert.That(f.Capacity, Is.EqualTo(3)); AssertContains(f, 14, 15); f.Push(16); AssertContains(f, 14, 15, 16); f.Capacity = 2; Assert.That(f.Capacity, Is.EqualTo(2)); AssertContains(f, 15, 16); f.Capacity = 1; Assert.That(f.Capacity, Is.EqualTo(1)); AssertContains(f, 16); f.Capacity = 0; Assert.That(f.Capacity, Is.EqualTo(0)); AssertEmpty(f); f.Capacity = 2; f.Capacity = 2; Assert.That(f.Capacity, Is.EqualTo(2)); Assert.That(f.ToString(), Is.EqualTo(String.Format("Count = {0} (Capacity = {1})", 0, 2))); //ExceptionTest Assert.Throws <ArgumentException>(() => f.Capacity = -1); Assert.Throws <ArgumentException>(() => new FIFOBuffer <int>(-1)); Assert.Throws <IndexOutOfRangeException>(() => f.CopyTo(new int[2], 0, -1)); }