public void testGrow() { CharTermAttributeImpl t = new CharTermAttributeImpl(); StringBuilder buf = new StringBuilder("ab"); for (int i = 0; i < 20; i++) { char[] content = buf.toString().toCharArray(); t.copyBuffer(content, 0, content.length); assertEquals(buf.length(), t.length()); assertEquals(buf.toString(), t.toString()); buf.append(buf.toString()); } assertEquals(1048576, t.length()); // now as a StringBuilder, first variant t = new CharTermAttributeImpl(); buf = new StringBuilder("ab"); for (int i = 0; i < 20; i++) { t.setEmpty().append(buf); assertEquals(buf.length(), t.length()); assertEquals(buf.toString(), t.toString()); buf.append(t); } assertEquals(1048576, t.length()); // Test for slow growth to a long term t = new CharTermAttributeImpl(); buf = new StringBuilder("a"); for (int i = 0; i < 20000; i++) { t.setEmpty().append(buf); assertEquals(buf.length(), t.length()); assertEquals(buf.toString(), t.toString()); buf.append("a"); } assertEquals(20000, t.length()); }
public void testAppendableInterfaceWithLongSequences() { CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append((CharSequence) "01234567890123456789012345678901234567890123456789"); t.append( (CharSequence) CharBuffer.wrap("01234567890123456789012345678901234567890123456789".toCharArray()), 3, 50); assertEquals( "0123456789012345678901234567890123456789012345678934567890123456789012345678901234567890123456789", t.toString()); t.setEmpty().append((CharSequence) new StringBuilder("01234567890123456789"), 5, 17); assertEquals((CharSequence) "567890123456", t.toString()); t.append(new StringBuffer(t)); assertEquals((CharSequence) "567890123456567890123456", t.toString()); // very wierd, to test if a subSlice is wrapped correct :) CharBuffer buf = CharBuffer.wrap("012345678901234567890123456789".toCharArray(), 3, 15); assertEquals("345678901234567", buf.toString()); t.setEmpty().append(buf, 1, 14); assertEquals("4567890123456", t.toString()); // finally use a completely custom CharSequence that is not catched by instanceof checks final String longTestString = "012345678901234567890123456789"; t.append( new CharSequence() { @Override public char charAt(int i) { return longTestString.charAt(i); } @Override public int length() { return longTestString.length(); } @Override public CharSequence subSequence(int start, int end) { return longTestString.subSequence(start, end); } @Override public String toString() { return longTestString; } }); assertEquals("4567890123456" + longTestString, t.toString()); }
public void testToString() throws Exception { char[] b = {'a', 'l', 'o', 'h', 'a'}; CharTermAttributeImpl t = new CharTermAttributeImpl(); t.copyBuffer(b, 0, 5); assertEquals("aloha", t.toString()); t.setEmpty().append("hi there"); assertEquals("hi there", t.toString()); }
public void testAppendableInterface() { CharTermAttributeImpl t = new CharTermAttributeImpl(); Formatter formatter = new Formatter(t, Locale.ROOT); formatter.format("%d", 1234); assertEquals("1234", t.toString()); formatter.format("%d", 5678); assertEquals("12345678", t.toString()); t.append('9'); assertEquals("123456789", t.toString()); t.append((CharSequence) "0"); assertEquals("1234567890", t.toString()); t.append((CharSequence) "0123456789", 1, 3); assertEquals("123456789012", t.toString()); t.append((CharSequence) CharBuffer.wrap("0123456789".toCharArray()), 3, 5); assertEquals("12345678901234", t.toString()); t.append((CharSequence) t); assertEquals("1234567890123412345678901234", t.toString()); t.append((CharSequence) new StringBuilder("0123456789"), 5, 7); assertEquals("123456789012341234567890123456", t.toString()); t.append((CharSequence) new StringBuffer(t)); assertEquals("123456789012341234567890123456123456789012341234567890123456", t.toString()); // very wierd, to test if a subSlice is wrapped correct :) CharBuffer buf = CharBuffer.wrap("0123456789".toCharArray(), 3, 5); assertEquals("34567", buf.toString()); t.setEmpty().append((CharSequence) buf, 1, 2); assertEquals("4", t.toString()); CharTermAttribute t2 = new CharTermAttributeImpl(); t2.append("test"); t.append((CharSequence) t2); assertEquals("4test", t.toString()); t.append((CharSequence) t2, 1, 2); assertEquals("4teste", t.toString()); try { t.append((CharSequence) t2, 1, 5); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.append((CharSequence) t2, 1, 0); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } t.append((CharSequence) null); assertEquals("4testenull", t.toString()); }