### Question:
Terms { synchronized void append( long index, long term ) { if ( index != max + 1 ) { throw new IllegalStateException( format( "Must append in order. %s but expected index is %d", appendMessage( index, term ), max + 1 ) ); } else if ( size > 0 && term < terms[size - 1] ) { throw new IllegalStateException( format( "Non-monotonic term. %s but highest term is %d", appendMessage( index, term ), terms[size - 1] ) ); } max = index; if ( size == 0 || term != terms[size - 1] ) { setSize( size + 1 ); indexes[size - 1] = index; terms[size - 1] = term; } } Terms( long prevIndex, long prevTerm );   }### Answer:
@Test public void shouldOnlyAcceptInOrderIndexes() { long prevIndex = 3; long term = 3; terms = new Terms( prevIndex, term ); try { terms.append( prevIndex, term ); fail(); } catch ( IllegalStateException e ) { } terms.append( prevIndex + 1, term ); terms.append( prevIndex + 2, term ); terms.append( prevIndex + 3, term ); try { terms.append( prevIndex + 5, term ); fail(); } catch ( IllegalStateException e ) { } terms.append( prevIndex + 4, term ); terms.append( prevIndex + 5, term ); terms.append( prevIndex + 6, term ); }
@Test public void shouldOnlyAcceptMonotonicTerms() { long term = 5; long prevIndex = 10; terms = new Terms( prevIndex, term ); terms.append( prevIndex + 1, term ); terms.append( prevIndex + 2, term ); terms.append( prevIndex + 3, term + 1 ); terms.append( prevIndex + 4, term + 1 ); terms.append( prevIndex + 5, term + 2 ); terms.append( prevIndex + 6, term + 2 ); try { terms.append( prevIndex + 7, term + 1 ); fail(); } catch ( IllegalStateException e ) { } }