InputTextReader { public void seek(int position) { int backtrackLength = Math.max(_index - position, 0); _backtracked += backtrackLength; _index = position; checkBacktrackLoop(backtrackLength); } InputTextReader(String content); char read(); String peek(int numberChars); char peekChar(int offset); boolean canReadChars(int numberChars); boolean eof(); int getPosition(); int getBacktrackedCount(); void seek(int position); void goBack();  }
@Test public void testSeek() { InputTextReader reader = new InputTextReader(CONTENT); reader.seek(4); Assert.assertEquals(reader.read(), CONTENT.charAt(4)); reader.seek(1); Assert.assertEquals(reader.read(), CONTENT.charAt(1)); }
@Test(expectedExceptions = NegativeArraySizeException.class, expectedExceptionsMessageRegExp = ".*" + CONTENT + ".*") public void testEndlessLoopDetection() { InputTextReader reader = new InputTextReader(CONTENT); for (int i = 0; i < InputTextReader.MAX_BACKTRACK_MULTIPLIER + 1; i++) { reader.seek(CONTENT.length()); reader.seek(0); } }