### Question:
Terms { synchronized void truncate( long fromIndex ) { if ( fromIndex < 0 || fromIndex < min ) { throw new IllegalStateException( "Cannot truncate a negative index. Tried to truncate from " + fromIndex ); } max = fromIndex - 1; int newSize = size; while ( newSize > 0 && indexes[newSize - 1] >= fromIndex ) { newSize--; } setSize( newSize ); } Terms( long prevIndex, long prevTerm );   }### Answer:
@Test public void shouldNotTruncateLessThanLowestIndex() { terms = new Terms( 5, 1 ); try { terms.truncate( 4 ); fail(); } catch ( IllegalStateException e ) { } }
@Test public void shouldTruncateAtExactBoundary() { long term = 5; long prevIndex = 10; terms = new Terms( prevIndex, term ); appendRange( prevIndex + 1, prevIndex + 10, term ); appendRange( prevIndex + 10, prevIndex + 20, term + 1 ); long truncateFromIndex = prevIndex + 10; terms.truncate( truncateFromIndex ); assertTermInRange( prevIndex + 1, prevIndex + 10, term ); assertTermInRange( prevIndex + 10, truncateFromIndex, -1 ); }
@Test public void shouldTruncateCompleteCurrentRange() { long term = 5; long prevIndex = 10; terms = new Terms( prevIndex, term ); appendRange( prevIndex + 1, prevIndex + 10, term ); appendRange( prevIndex + 10, prevIndex + 20, term + 1 ); appendRange( prevIndex + 20, prevIndex + 30, term + 2 ); long truncateFromIndex = prevIndex + 15; terms.truncate( truncateFromIndex ); assertTermInRange( prevIndex + 1, prevIndex + 10, term ); assertTermInRange( prevIndex + 10, truncateFromIndex, term + 1 ); assertTermInRange( truncateFromIndex, prevIndex + 30, -1 ); }
@Test public void shouldTruncateSeveralCompleteRanges() { long term = 5; long prevIndex = 10; terms = new Terms( prevIndex, term ); appendRange( prevIndex + 1, prevIndex + 10, term ); appendRange( prevIndex + 10, prevIndex + 20, term + 1 ); appendRange( prevIndex + 20, prevIndex + 30, term + 2 ); long truncateFromIndex = prevIndex + 5; terms.truncate( truncateFromIndex ); assertTermInRange( prevIndex + 1, truncateFromIndex, term ); assertTermInRange( truncateFromIndex, prevIndex + 30, -1 ); }
@Test public void shouldAppendAfterTruncate() { long term = 5; long prevIndex = 10; terms = new Terms( prevIndex, term ); appendRange( prevIndex + 1, prevIndex + 10, term ); appendRange( prevIndex + 10, prevIndex + 20, term + 10 ); long truncateFromIndex = prevIndex + 5; terms.truncate( truncateFromIndex ); appendRange( truncateFromIndex, truncateFromIndex + 20, term + 20 ); assertTermInRange( prevIndex + 1, truncateFromIndex, term ); assertTermInRange( truncateFromIndex, truncateFromIndex + 20, term + 20 ); }