Bug in Put(T item) method

Jul 15, 2011 at 11:27 AM

I haven't tried actually using or running this class but, from code inspection, there is a very obvious bug in the Put method.

The post-increment of the tail index should be a pre-increment. Replacing the tail increment and test by the following will fix the bug:

if (++tail == capacity)
{
    tail = 0;
}
Mar 23, 2012 at 9:36 PM

This is true. The following test will show it.   changing to the pre-increment, as shown above will make the test pass.

        [Test]
        public void Put_WithOverflowOn_CirclesToBeginning()
        {
            var values = new[] {1, 2, 3, 4, 5, 6};

            var buffer = new CircularBuffer<int>(3) {AllowOverflow = true};

            foreach (var value in values)
            {
                buffer.Put(value);
            }
            //the buffer should now contain 4,5,6 because 1,2,3 were overwritten
            var item1 = buffer.Get();
            var item2 = buffer.Get();
            var item3 = buffer.Get();

            Assert.AreEqual(4, item1);
            Assert.AreEqual(5, item2);
            Assert.AreEqual(6, item3);
        }

 
Coordinator
Jul 7, 2012 at 2:11 AM

This is true. Thanks for pointing it out; it should now be fixed.