@Test public void testAppendIllegalBufferOrOffsetOrLength() { try { sb.append(new byte[0], 0, 1); fail("IndexOutOfBoundsException expected but not thrown"); } catch (IndexOutOfBoundsException expected) { } try { sb.append(new byte[0], 1, 0); fail("IndexOutOfBoundsException expected but not thrown"); } catch (IndexOutOfBoundsException expected) { } try { sb.append(new byte[10], 0, 11); fail("IndexOutOfBoundsException expected but not thrown"); } catch (IndexOutOfBoundsException expected) { } try { sb.append(new byte[10], 1, 10); fail("IndexOutOfBoundsException expected but not thrown"); } catch (IndexOutOfBoundsException expected) { } }
@Test public void testAppendNegativeLength() { try { sb.append(new byte[0], 0, -1); fail("IndexOutOfBoundsException expected but not thrown"); } catch (IndexOutOfBoundsException expected) { } }
@Test public void testAppendNullBuffer() { try { sb.append(null, 0, 1); fail("NullPointerException expected but not thrown"); } catch (NullPointerException expected) { } }
@Test public void testAppendZeroNativePointer() { sb.dispose(); try { sb.append(new byte[1], 0, 1); fail("IllegalStateException expected but not thrown"); } catch (IllegalStateException expected) { } sb = null; }
@Test public void testSizeRandomIncrements() { int expected = 0; assertEquals(expected, sb.size()); for (int i = 0; i < 100; i++) { int increment = random.nextInt(SEGMENT_SIZE * 10); sb.append(new byte[increment], 0, increment); expected += increment; assertEquals(expected, sb.size()); } }
private void append(byte[] data) { int offset = random.nextBoolean() ? random.nextInt(100) : 0; int extraLength = random.nextBoolean() ? random.nextInt(200) : 0; byte[] buffer = g(0, offset + data.length + extraLength); System.arraycopy(data, 0, buffer, offset, data.length); sb.append(buffer, offset, data.length); for (int i = 0; i < offset; i++) { assertEquals((byte) (i & 0xff), buffer[i]); } for (int i = offset + data.length; i < buffer.length; i++) { assertEquals((byte) (i & 0xff), buffer[i]); } }
@Test public void testSizePredefinedIncrements() { int[] increments = new int[] { 1, 5, 10, 100, 1000, SEGMENT_SIZE, SEGMENT_SIZE * 2, SEGMENT_SIZE * 10, }; int expected = 0; assertEquals(expected, sb.size()); for (int increment : increments) { sb.append(new byte[increment], 0, increment); expected += increment; assertEquals(expected, sb.size()); } }
private void append(double length) { byte[] data = g(0, (int) length); sb.append(data, 0, data.length); }