@Test public void testHead1() { final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(1); assertEquals(ring.length(), 1); ring.setHead(100, 100); assertEquals(ring.head(), 100); assertEquals(ring.get(100), (Integer) 100); ring.setHead(123, 100); assertEquals(ring.head(), 123); assertEquals(ring.get(123), (Integer) 100); }
@Test public void testAsMap() { final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10); assertEquals(ring.length(), 10); ring.setHead(100, 100); for (int k = 101; k < 110; k++) { ring.set(k, k); } ring.set(102, null); ring.set(107, null); assertEquals(ring.count(), 8); final Map<Integer, Integer> map = ring.asMap(); assertEquals(map.size(), ring.count()); // SortedMap<Integer, Integer> map1 = new TreeMap<Integer, // Integer>(map); for (int k = 100; k < 110; k++) { final Integer ringItem = ring.get(k); final Integer mapItem = map.get(k); assertEquals(ringItem, mapItem); } }
@Test public void testAsList() { final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10); assertEquals(ring.length(), 10); ring.setHead(100, 100); for (int k = 100; k < 110; k++) { ring.set(k, k); } ring.set(102, null); ring.set(107, null); assertEquals(ring.count(), 8); final List<Integer> list = ring.asList(); assertEquals(list.size(), ring.count()); int index = 0; for (int k = 100; k < 110; k++) { final Integer ringItem = ring.get(k); if (ringItem == null) { continue; } final Integer listItem = list.get(index++); assertEquals(ringItem, listItem); } }
@Test public void testAsArray() { final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10); assertEquals(ring.length(), 10); ring.setHead(100, 100); for (int k = 100; k < 110; k++) { ring.set(k, k); } ring.set(102, null); ring.set(107, null); assertEquals(ring.count(), 8); final Integer[] clone1 = ring.asArray(Integer.class); final Integer[] clone2 = ring.asArray(Integer.class); assertFalse(clone1 == clone2); assertTrue(Arrays.equals(clone1, clone2)); for (int k = 0; k < 10; k++) { assertEquals(ring.get(100 + k), clone1[k]); assertEquals(ring.get(100 + k), clone2[k]); } }
@Test public void testHead() { final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10); assertEquals(ring.length(), 10); ring.setHead(100, 100); assertEquals(ring.count(), 1); assertEquals(ring.get(100), (Integer) 100); for (int k = 101; k < 110; k++) { assertEquals(ring.get(k), null); } for (int k = 100; k < 110; k++) { ring.set(k, k); } assertEquals(ring.count(), 10); for (int k = 100; k < 110; k++) { assertEquals(ring.get(k), (Integer) k); } ring.setHead(105, 205); assertEquals(ring.count(), 5); assertEquals(ring.head(), 105); assertEquals(ring.tail(), 114); assertEquals(ring.get(105), (Integer) 205); for (int k = 106; k < 110; k++) { assertEquals(ring.get(k), (Integer) k); } for (int k = 110; k < 115; k++) { assertEquals(ring.get(k), (Integer) null); } ring.set(114, 114); assertEquals(ring.count(), 6); assertEquals(ring.get(114), (Integer) 114); }
@Test(expected = ArrayIndexOutOfBoundsException.class) public void testSetException2() { final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10); ring.setHead(100, 100); ring.set(110, 110); }