CircularBuffer { public static <ELEMENT_TYPE> ICircularBuffer<ELEMENT_TYPE> create(final int capacity) { return new CircularBufferImpl<ELEMENT_TYPE>(capacity); } private  CircularBuffer(); static ICircularBuffer<ELEMENT_TYPE> create(final int capacity);  }
@Test public void testCreateBuffer() { final int capacity = 100; final ICircularBuffer<Object> buffer = CircularBuffer.create(capacity); Assert.assertEquals(capacity, buffer.capacity()); assertBufferIsEmpty(buffer); }
@Test public void testAdd() { final int capacity = 100; final ICircularBuffer<Integer> buffer = CircularBuffer.create(capacity); for (int i = 0; i < capacity; i++) { final Integer element = Integer.valueOf(i); buffer.add(element); Assert.assertEquals(i + 1, buffer.size()); Assert.assertEquals(element, buffer.get(i)); } Assert.assertEquals(capacity, buffer.size()); for (int i = 0; i < 2 * capacity; i++) { final Integer previousElement = Integer.valueOf(capacity + (i - 1)); final Integer element = Integer.valueOf(capacity + i); Assert.assertEquals(previousElement, buffer.get(buffer.capacity() - 1)); buffer.add(element); Assert.assertEquals(capacity, buffer.size()); Assert.assertEquals(element, buffer.get(buffer.capacity() - 1)); Assert.assertEquals(previousElement, buffer.get(buffer.capacity() - 2)); } }
@Test public void testRemove() { final int capacity = 5; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); buffer.add("1"); buffer.add("2"); buffer.add("3"); buffer.add("4"); buffer.add("5"); buffer.add("6"); assertBufferHasStringValues(buffer, "2", "3", "4", "5", "6"); Assert.assertEquals(5, buffer.size()); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(4, buffer.size()); assertBufferHasStringValues(buffer, "2", "3", "4", "5", null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, "2", "3", "4", null, null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(2, buffer.size()); assertBufferHasStringValues(buffer, "2", "3", null, null, null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(1, buffer.size()); assertBufferHasStringValues(buffer, "2", null, null, null, null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null); Assert.assertFalse(buffer.removeLast()); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null); buffer.add("A"); buffer.add("B"); buffer.add("C"); assertBufferHasStringValues(buffer, "A", "B", "C", null, null); Assert.assertEquals(3, buffer.size()); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(2, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", null, null, null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(1, buffer.size()); assertBufferHasStringValues(buffer, "A", null, null, null, null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null); Assert.assertFalse(buffer.removeLast()); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null); buffer.add("A1"); buffer.add("B1"); buffer.add("C1"); buffer.add("D1"); buffer.add("E1"); assertBufferHasStringValues(buffer, "A1", "B1", "C1", "D1", "E1"); Assert.assertEquals(5, buffer.size()); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(4, buffer.size()); assertBufferHasStringValues(buffer, "A1", "B1", "C1", "D1", null); buffer.add("E2"); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "A1", "B1", "C1", "D1", "E2"); buffer.add("E2"); buffer.add("F2"); buffer.add("G2"); buffer.add("H2"); buffer.add("I2"); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "E2", "F2", "G2", "H2", "I2"); buffer.add("J2"); buffer.add("K2"); buffer.add("L2"); buffer.add("M2"); buffer.add("N2"); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "J2", "K2", "L2", "M2", "N2"); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(4, buffer.size()); assertBufferHasStringValues(buffer, "J2", "K2", "L2", "M2", null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, "J2", "K2", "L2", null, null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(2, buffer.size()); assertBufferHasStringValues(buffer, "J2", "K2", null, null, null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(1, buffer.size()); assertBufferHasStringValues(buffer, "J2", null, null, null, null); Assert.assertTrue(buffer.removeLast()); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null); Assert.assertFalse(buffer.removeLast()); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null); }
@Test public void testCirculate() { final int capacity = 5; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); buffer.add("A"); assertBufferHasStringValues(buffer, "A"); Assert.assertEquals(1, buffer.size()); buffer.add("B"); assertBufferHasStringValues(buffer, "A", "B"); Assert.assertEquals(2, buffer.size()); buffer.add("C"); assertBufferHasStringValues(buffer, "A", "B", "C"); Assert.assertEquals(3, buffer.size()); buffer.add("D"); assertBufferHasStringValues(buffer, "A", "B", "C", "D"); Assert.assertEquals(4, buffer.size()); buffer.add("E"); assertBufferHasStringValues(buffer, "A", "B", "C", "D", "E"); Assert.assertEquals(capacity, buffer.size()); buffer.add("F"); assertBufferHasStringValues(buffer, "B", "C", "D", "E", "F"); Assert.assertEquals(capacity, buffer.size()); buffer.add("G"); assertBufferHasStringValues(buffer, "C", "D", "E", "F", "G"); Assert.assertEquals(capacity, buffer.size()); buffer.add("H"); assertBufferHasStringValues(buffer, "D", "E", "F", "G", "H"); Assert.assertEquals(capacity, buffer.size()); buffer.add("I"); assertBufferHasStringValues(buffer, "E", "F", "G", "H", "I"); Assert.assertEquals(capacity, buffer.size()); buffer.add("J"); assertBufferHasStringValues(buffer, "F", "G", "H", "I", "J"); Assert.assertEquals(capacity, buffer.size()); buffer.add(null); assertBufferHasStringValues(buffer, "G", "H", "I", "J", null); Assert.assertEquals(capacity, buffer.size()); buffer.add(null); assertBufferHasStringValues(buffer, "H", "I", "J", null, null); Assert.assertEquals(capacity, buffer.size()); buffer.add(null); assertBufferHasStringValues(buffer, "I", "J", null, null, null); Assert.assertEquals(capacity, buffer.size()); buffer.add(null); assertBufferHasStringValues(buffer, "J", null, null, null, null); Assert.assertEquals(capacity, buffer.size()); buffer.add(null); assertBufferHasStringValues(buffer, null, null, null, null, null); Assert.assertEquals(capacity, buffer.size()); }
@Test public void testSetElement() { final int capacity = 5; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); buffer.add("A"); assertBufferHasStringValues(buffer, "A"); Assert.assertEquals(1, buffer.size()); buffer.set(0, "A#"); assertBufferHasStringValues(buffer, "A#"); Assert.assertEquals(1, buffer.size()); buffer.add("B"); buffer.add("C"); assertBufferHasStringValues(buffer, "A#", "B", "C"); Assert.assertEquals(3, buffer.size()); buffer.set(2, "C#"); assertBufferHasStringValues(buffer, "A#", "B", "C#"); Assert.assertEquals(3, buffer.size()); buffer.add("D"); buffer.add("E"); assertBufferHasStringValues(buffer, "A#", "B", "C#", "D", "E"); Assert.assertEquals(capacity, buffer.size()); buffer.set(4, "E#"); assertBufferHasStringValues(buffer, "A#", "B", "C#", "D", "E#"); Assert.assertEquals(capacity, buffer.size()); buffer.add("F"); assertBufferHasStringValues(buffer, "B", "C#", "D", "E#", "F"); buffer.set(4, "F#"); assertBufferHasStringValues(buffer, "B", "C#", "D", "E#", "F#"); buffer.set(0, "A"); buffer.set(1, "B"); buffer.set(2, "C"); buffer.set(3, "D"); buffer.set(4, "E"); assertBufferHasStringValues(buffer, "A", "B", "C", "D", "E"); }
@Test public void testFillBufferWithNull() { final int capacity = 5; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); buffer.add("A"); buffer.add("B"); buffer.add("C"); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C"); buffer.fill(); Assert.assertEquals(capacity, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C", null, null); buffer.add("D"); assertBufferHasStringValues(buffer, "B", "C", null, null, "D"); buffer.fill(); assertBufferHasStringValues(buffer, "B", "C", null, null, "D"); }
@Test public void testFillEmptyBufferWithNull() { final int capacity = 5; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); Assert.assertEquals(0, buffer.size()); buffer.fill(); Assert.assertEquals(capacity, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null); }
@Test public void testFillBuffer() { final int capacity = 5; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); buffer.add("A"); buffer.add("B"); buffer.add("C"); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C"); buffer.fill("FOO"); Assert.assertEquals(capacity, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C", "FOO", "FOO"); buffer.add("D"); assertBufferHasStringValues(buffer, "B", "C", "FOO", "FOO", "D"); buffer.fill(); assertBufferHasStringValues(buffer, "B", "C", "FOO", "FOO", "D"); buffer.clear(); buffer.add("1"); buffer.add("2"); buffer.add("3"); buffer.add("4"); buffer.add("5"); buffer.add("6"); buffer.removeLast(); Assert.assertEquals(4, buffer.size()); assertBufferHasStringValues(buffer, "2", "3", "4", "5", null); buffer.fill(); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "2", "3", "4", "5", null); buffer.add("7"); assertBufferHasStringValues(buffer, "3", "4", "5", null, "7"); buffer.setSize(2); Assert.assertEquals(2, buffer.size()); assertBufferHasStringValues(buffer, "3", "4", null, null, null); buffer.fill(); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "3", "4", null, null, null); buffer.removeLast(); buffer.removeLast(); buffer.removeLast(); Assert.assertEquals(2, buffer.size()); assertBufferHasStringValues(buffer, "3", "4", null, null, null); buffer.fill(); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "3", "4", null, null, null); }
@Test public void testFillEmptyBuffer() { final int capacity = 5; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); Assert.assertEquals(0, buffer.size()); buffer.fill("FOO"); Assert.assertEquals(capacity, buffer.size()); assertBufferHasStringValues(buffer, "FOO", "FOO", "FOO", "FOO", "FOO"); }
@Test public void testSetSize() { final int capacity = 6; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); Assert.assertEquals(0, buffer.size()); buffer.add("A"); buffer.add("B"); buffer.add("C"); buffer.add("D"); buffer.add("E"); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C", "D", "E", null); buffer.setSize(3); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C", null, null, null); buffer.setSize(5); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C", null, null, null); buffer.setSize(0); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null, null); buffer.add("F"); buffer.add("G"); buffer.add("H"); buffer.add("I"); buffer.add("J"); buffer.add("K"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, "F", "G", "H", "I", "J", "K"); buffer.setSize(5); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "F", "G", "H", "I", "J", null); buffer.add("K2"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, "F", "G", "H", "I", "J", "K2"); buffer.add("L"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", "J", "K2", "L"); buffer.setSize(5); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", "J", "K2", null); buffer.add("L2"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", "J", "K2", "L2"); buffer.setSize(5); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", "J", "K2", null); buffer.setSize(3); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", null, null, null); buffer.setSize(1); Assert.assertEquals(1, buffer.size()); assertBufferHasStringValues(buffer, "G", null, null, null, null, null); buffer.setSize(0); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null, null); buffer.setSize(3); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null, null); buffer.add("A"); Assert.assertEquals(4, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, "A", null, null); buffer.setSize(6); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, "A", null, null); buffer.add("B"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, null, null, "A", null, null, "B"); buffer.setSize(2); Assert.assertEquals(2, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null, null); buffer.add("C"); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, null, null, "C", null, null, null); }
@Test public void testSetSizeWithManualClear() { final ICircularBuffer<String> buffer = CircularBuffer.create(6); Assert.assertEquals(0, buffer.size()); buffer.add("A"); buffer.add("B"); buffer.add("C"); buffer.add("D"); buffer.add("E"); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C", "D", "E", null); buffer.set(3, null); buffer.set(4, null); buffer.setSize(3, false); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C", null, null, null); buffer.setSize(5, false); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "A", "B", "C", null, null, null); buffer.set(0, null); buffer.set(1, null); buffer.set(2, null); buffer.setSize(0, false); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null, null); buffer.add("F"); buffer.add("G"); buffer.add("H"); buffer.add("I"); buffer.add("J"); buffer.add("K"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, "F", "G", "H", "I", "J", "K"); buffer.set(5, null); buffer.setSize(5, false); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "F", "G", "H", "I", "J", null); buffer.add("K2"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, "F", "G", "H", "I", "J", "K2"); buffer.add("L"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", "J", "K2", "L"); buffer.set(5, null); buffer.setSize(5, false); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", "J", "K2", null); buffer.add("L2"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", "J", "K2", "L2"); buffer.set(5, null); buffer.setSize(5, false); Assert.assertEquals(5, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", "J", "K2", null); buffer.set(4, null); buffer.set(3, null); buffer.setSize(3, false); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, "G", "H", "I", null, null, null); buffer.set(2, null); buffer.set(1, null); buffer.setSize(1, false); Assert.assertEquals(1, buffer.size()); assertBufferHasStringValues(buffer, "G", null, null, null, null, null); buffer.set(0, null); buffer.setSize(0, false); Assert.assertEquals(0, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null, null); buffer.setSize(3, false); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null, null); buffer.add("A"); Assert.assertEquals(4, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, "A", null, null); buffer.setSize(6, false); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, "A", null, null); buffer.add("B"); Assert.assertEquals(6, buffer.size()); assertBufferHasStringValues(buffer, null, null, "A", null, null, "B"); buffer.set(5, null); buffer.set(4, null); buffer.set(3, null); buffer.set(2, null); buffer.setSize(2, false); Assert.assertEquals(2, buffer.size()); assertBufferHasStringValues(buffer, null, null, null, null, null, null); buffer.add("C"); Assert.assertEquals(3, buffer.size()); assertBufferHasStringValues(buffer, null, null, "C", null, null, null); }
@Test public void testClear() { final int capacity = 100; final ICircularBuffer<Integer> buffer = CircularBuffer.create(capacity); for (int i = 0; i < 2 * capacity; i++) { final Integer element = Integer.valueOf(i); buffer.add(element); } Assert.assertEquals(capacity, buffer.size()); buffer.clear(); assertBufferIsEmpty(buffer); }
@Test(expected = IndexOutOfBoundsException.class) public void testGetWithNegativeIndex() { final int capacity = 100; final ICircularBuffer<Integer> buffer = CircularBuffer.create(capacity); buffer.get(-1); }
@Test(expected = IndexOutOfBoundsException.class) public void testGetWithIndexIsCapacity() { final int capacity = 100; final ICircularBuffer<Integer> buffer = CircularBuffer.create(capacity); buffer.get(capacity); }
@Test(expected = IndexOutOfBoundsException.class) public void testGetWithIndexIsGreaterCapacity() { final int capacity = 100; final ICircularBuffer<Integer> buffer = CircularBuffer.create(capacity); buffer.get(capacity + 1); }
@Test(expected = IndexOutOfBoundsException.class) public void testSetEmptyBufferWithIndexIsGreaterSize() { final int capacity = 100; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); buffer.set(0, "FOO"); }
@Test(expected = IndexOutOfBoundsException.class) public void testSetBufferWithIndexIsGreaterSize() { final int capacity = 100; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); buffer.add("FOO"); buffer.set(1, "FIGHTER"); }
@Test(expected = IndexOutOfBoundsException.class) public void testSetBufferWithNegativeIndex() { final int capacity = 100; final ICircularBuffer<String> buffer = CircularBuffer.create(capacity); buffer.add("FOO"); buffer.set(-1, "FIGHTER"); }
@Test(expected = IndexOutOfBoundsException.class) public void testSetSizeWithNegativeIndex() { CircularBuffer.create(5).setSize(-1); }
@Test(expected = IndexOutOfBoundsException.class) public void testSetSizeWithGreaterThanCapacity() { CircularBuffer.create(5).setSize(6); }