@Test public void testSerialization() throws Exception { final CharArrayBuffer orig = new CharArrayBuffer(32); orig.append('a'); orig.append('b'); orig.append('c'); final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream(); try (final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer)) { outStream.writeObject(orig); } final byte[] raw = outbuffer.toByteArray(); final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw); final ObjectInputStream inStream = new ObjectInputStream(inBuffer); final CharArrayBuffer clone = (CharArrayBuffer) inStream.readObject(); Assert.assertEquals(orig.capacity(), clone.capacity()); Assert.assertEquals(orig.length(), clone.length()); final char[] data = clone.toCharArray(); Assert.assertNotNull(data); Assert.assertEquals(3, data.length); Assert.assertEquals('a', data[0]); Assert.assertEquals('b', data[1]); Assert.assertEquals('c', data[2]); }
@Test public void testExpandAppend() throws Exception { final CharArrayBuffer buffer = new CharArrayBuffer(4); Assert.assertEquals(4, buffer.capacity()); final char[] tmp = new char[] { '1', '2', '3', '4'}; buffer.append(tmp, 0, 2); buffer.append(tmp, 0, 4); buffer.append(tmp, 0, 0); Assert.assertEquals(8, buffer.capacity()); Assert.assertEquals(6, buffer.length()); buffer.append(tmp, 0, 4); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(10, buffer.length()); Assert.assertEquals("1212341234", buffer.toString()); }
@Test public void testConstructor() throws Exception { final CharArrayBuffer buffer = new CharArrayBuffer(16); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(0, buffer.length()); Assert.assertNotNull(buffer.array()); Assert.assertEquals(16, buffer.array().length); try { new CharArrayBuffer(-1); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testSimpleAppend() throws Exception { final CharArrayBuffer buffer = new CharArrayBuffer(16); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(0, buffer.length()); final char[] b1 = buffer.toCharArray(); Assert.assertNotNull(b1); Assert.assertEquals(0, b1.length); Assert.assertTrue(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); final char[] tmp = new char[] { '1', '2', '3', '4'}; buffer.append(tmp, 0, tmp.length); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(4, buffer.length()); Assert.assertFalse(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); final char[] b2 = buffer.toCharArray(); Assert.assertNotNull(b2); Assert.assertEquals(4, b2.length); for (int i = 0; i < tmp.length; i++) { Assert.assertEquals(tmp[i], b2[i]); Assert.assertEquals(tmp[i], buffer.charAt(i)); } Assert.assertEquals("1234", buffer.toString()); buffer.clear(); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(0, buffer.length()); Assert.assertTrue(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); }