### Question:
TextSegment { public int toTextOffset(int documentOffset) { int textOffset = 0; for (Line line : lines) { int lineRelativeOffset = documentOffset - line.getOffset(); int linePlusSeparatorLength = line.getText().length() + 1; if (lineRelativeOffset >= 0 && lineRelativeOffset < linePlusSeparatorLength) { return textOffset + lineRelativeOffset; } textOffset += linePlusSeparatorLength; } throw new IllegalArgumentException(); } TextSegment(Iterable<Line> lines); String getText(); List<Line> getLines(); int offsetOf(int textOffset); int toTextOffset(int documentOffset); @Override String toString(); Line getLineAtOffset(int textOffset);  }### Answer:
@Test public void toTextOffset() { TextSegment segment = new TextSegment(ImmutableList.of(new Line(1, 10, "abc"), new Line(2, 15, "def"))); assertEquals(0, segment.toTextOffset(10)); assertEquals(2, segment.toTextOffset(12)); assertEquals(4, segment.toTextOffset(15)); assertEquals(6, segment.toTextOffset(17)); try { segment.toTextOffset(19); fail("expected exception"); } catch (IllegalArgumentException e) { } try { segment.toTextOffset(9); fail("expected exception"); } catch (IllegalArgumentException e) { } }