local CircularBuffer = require('circular-buffer') describe('circular-buffer', function() it('reading an empty buffer throws a BufferEmptyException', function() local buffer = CircularBuffer:new(1) assert.has_error((function() buffer:read() end), 'buffer is empty') end) it('write and read back one item', function() local buffer = CircularBuffer:new(1) buffer:write('1') assert.are.equals('1', buffer:read()) assert.has_error((function() buffer:read() end), 'buffer is empty') end) it('write and read back multiple items', function() local buffer = CircularBuffer:new(2) buffer:write('1') buffer:write('2') assert.are.equals('1', buffer:read()) assert.are.equals('2', buffer:read()) assert.has_error((function() buffer:read() end), 'buffer is empty') end) it('clearing a buffer', function() local buffer = CircularBuffer:new(2) buffer:write('1') buffer:write('2') buffer:clear() assert.has_error((function() buffer:read() end), 'buffer is empty') buffer:write('3') buffer:write('4') assert.are.equals('3', buffer:read()) assert.are.equals('4', buffer:read()) end) it('alternate write and read', function() local buffer = CircularBuffer:new(2) buffer:write('1') assert.are.equals('1', buffer:read()) buffer:write('2') assert.are.equals('2', buffer:read()) end) it('reads back oldest item', function() local buffer = CircularBuffer:new(3) buffer:write('1') buffer:write('2') buffer:read() buffer:write('3') assert.are.equals('2', buffer:read()) assert.are.equals('3', buffer:read()) end) it('writes of nil do not occupy buffer', function() local buffer = CircularBuffer:new(3) buffer:write(nil) for i = 1, 3 do buffer:write(tostring(i)) end assert.are.equals('1', buffer:read()) end) it('writing to a full buffer throws a BufferFullException', function() local buffer = CircularBuffer:new(2) buffer:write('1') buffer:write('2') assert.has_error((function() buffer:write('A') end), 'buffer is full') end) it('forced writes over write oldest item in a full buffer', function() local buffer = CircularBuffer:new(2) buffer:write('1') buffer:write('2') buffer:forceWrite('A') assert.are.equals('2', buffer:read()) assert.are.equals('A', buffer:read()) assert.has_error((function() buffer:read() end), 'buffer is empty') end) it('forced writes act like normal writes in a non-full buffer', function() local buffer = CircularBuffer:new(2) buffer:write('1') buffer:forceWrite('2') assert.are.equals('1', buffer:read()) assert.are.equals('2', buffer:read()) assert.has_error((function() buffer:read() end), 'buffer is empty') end) it('alternate force write and read into full buffer', function() local buffer = CircularBuffer:new(5) for i = 1, 3 do buffer:write(tostring(i)) end buffer:read() buffer:read() buffer:write('4') buffer:read() for i = 5, 8 do buffer:write(tostring(i)) end buffer:forceWrite('A') buffer:forceWrite('B') assert.are.equals('6', buffer:read()) assert.are.equals('7', buffer:read()) assert.are.equals('8', buffer:read()) assert.are.equals('A', buffer:read()) assert.are.equals('B', buffer:read()) assert.has_error((function() buffer:read() end), 'buffer is empty') end) end)