public void testExceptions() { CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append("test"); assertEquals("test", t.toString()); try { t.charAt(-1); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.charAt(4); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.subSequence(0, 5); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } try { t.subSequence(5, 0); fail("Should throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException iobe) { } }
public void testCharSequenceInterface() { final String s = "0123456789"; final CharTermAttributeImpl t = new CharTermAttributeImpl(); t.append(s); assertEquals(s.length(), t.length()); assertEquals("12", t.subSequence(1, 3).toString()); assertEquals(s, t.subSequence(0, s.length()).toString()); assertTrue(Pattern.matches("01\\d+", t)); assertTrue(Pattern.matches("34", t.subSequence(3, 5))); assertEquals(s.subSequence(3, 7).toString(), t.subSequence(3, 7).toString()); for (int i = 0; i < s.length(); i++) { assertTrue(t.charAt(i) == s.charAt(i)); } }