Buggy code:
@ SuppressCodecs ( "Lucene3x" ) public abstract class SorterTestBase extends LuceneTestCase { static final class NormsSimilarity extends Similarity { private final Similarity in ; public NormsSimilarity ( Similarity in ) { } @ Override public long computeNorm ( FieldInvertState state ) { } @ Override public SimWeight computeWeight ( float queryBoost , CollectionStatistics collectionStats , TermStatistics ... termStats ) { } @ Override public ExactSimScorer exactSimScorer ( SimWeight weight , AtomicReaderContext context ) throws IOException { } @ Override public SloppySimScorer sloppySimScorer ( SimWeight weight , AtomicReaderContext context ) throws IOException { } } static final class PositionsTokenStream extends TokenStream { private final CharTermAttribute term ; private final PayloadAttribute payload ; private final OffsetAttribute offset ; private int pos ; private int off ; public PositionsTokenStream ( ) { } @ Override public boolean incrementToken ( ) throws IOException { } void setId ( int id ) { } } protected static final String ID_FIELD = "id" ; protected static final String DOCS_ENUM_FIELD = "docs" ; protected static final String DOCS_ENUM_TERM = "$all$" ; protected static final String DOC_POSITIONS_FIELD = "positions" ; protected static final String DOC_POSITIONS_TERM = "$all$" ; protected static final String NUMERIC_DV_FIELD = "numeric" ; protected static final String NORMS_FIELD = "norm" ; protected static final String BINARY_DV_FIELD = "binary" ; protected static final String SORTED_DV_FIELD = "sorted" ; protected static final String SORTED_SET_DV_FIELD = "sorted_set" ; protected static final String TERM_VECTORS_FIELD = "term_vectors" ; private static final FieldType TERM_VECTORS_TYPE = new FieldType ( TextField . TYPE_NOT_STORED ) ; private static final FieldType POSITIONS_TYPE = new FieldType ( TextField . TYPE_NOT_STORED ) ; protected static Directory dir ; protected static AtomicReader reader ; protected static Integer [ ] sortedValues ; private static Document doc ( final int id , SorterTestBase . PositionsTokenStream positions ) { } public static void createIndex ( Directory dir , int numDocs , Random random ) throws IOException { } @ BeforeClass public static void beforeClassSorterTestBase ( ) throws Exception { } @ AfterClass public static void afterClassSorterTestBase ( ) throws Exception { } @ Test public void testBinaryDocValuesField ( ) throws Exception { } @ Test public void testDocsAndPositionsEnum ( ) throws Exception { TermsEnum termsEnum = SorterTestBase . reader . terms ( SorterTestBase . DOC_POSITIONS_FIELD ) . iterator ( null ) ; assertEquals ( FOUND , termsEnum . seekCeil ( new BytesRef ( SorterTestBase . DOC_POSITIONS_TERM ) ) ) ; DocsAndPositionsEnum sortedPositions = termsEnum . docsAndPositions ( null , null ) ; int doc ; while ( ( doc = sortedPositions . nextDoc ( ) ) != ( DocIdSetIterator . NO_MORE_DOCS ) ) { int freq = sortedPositions . freq ( ) ; assertEquals ( ( "incorrect<seq2seq4repair_space>freq<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , ( ( ( SorterTestBase . sortedValues [ doc ] . intValue ( ) ) // 10 ) + 1 ) , freq ) ; for ( int i = 0 ; i < freq ; i ++ ) { assertEquals ( ( "incorrect<seq2seq4repair_space>position<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , i , sortedPositions . nextPosition ( ) ) ; if ( ! ( doesntSupportOffsets . contains ( _TestUtil . getPostingsFormat ( SorterTestBase . DOC_POSITIONS_FIELD ) ) ) ) { assertEquals ( ( "incorrect<seq2seq4repair_space>startOffset<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , i , sortedPositions . startOffset ( ) ) ; assertEquals ( ( "incorrect<seq2seq4repair_space>endOffset<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , i , sortedPositions . endOffset ( ) ) ; } assertEquals ( ( "incorrect<seq2seq4repair_space>payload<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , ( freq - i ) , Integer . parseInt ( sortedPositions . getPayload ( ) . utf8ToString ( ) ) ) ; } } final DocsAndPositionsEnum reuse = sortedPositions ; sortedPositions = termsEnum . docsAndPositions ( null , reuse ) ; if ( sortedPositions instanceof SortingDocsAndPositionsEnum ) { assertTrue ( ( ( SortingDocsAndPositionsEnum ) ( sortedPositions ) ) . reused ( reuse ) ) ; } doc = 0 ; <START_BUG> while ( ( doc = sortedPositions . advance ( doc ) ) != ( DocIdSetIterator . NO_MORE_DOCS ) ) { <END_BUG> int freq = sortedPositions . freq ( ) ; assertEquals ( ( "incorrect<seq2seq4repair_space>freq<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , ( ( ( SorterTestBase . sortedValues [ doc ] . intValue ( ) ) // 10 ) + 1 ) , freq ) ; for ( int i = 0 ; i < freq ; i ++ ) { assertEquals ( ( "incorrect<seq2seq4repair_space>position<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , i , sortedPositions . nextPosition ( ) ) ; if ( ! ( doesntSupportOffsets . contains ( _TestUtil . getPostingsFormat ( SorterTestBase . DOC_POSITIONS_FIELD ) ) ) ) { assertEquals ( ( "incorrect<seq2seq4repair_space>startOffset<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , i , sortedPositions . startOffset ( ) ) ; assertEquals ( ( "incorrect<seq2seq4repair_space>endOffset<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , i , sortedPositions . endOffset ( ) ) ; } assertEquals ( ( "incorrect<seq2seq4repair_space>payload<seq2seq4repair_space>for<seq2seq4repair_space>doc=" + doc ) , ( freq - i ) , Integer . parseInt ( sortedPositions . getPayload ( ) . utf8ToString ( ) ) ) ; } } } Bits randomLiveDocs ( int maxDoc ) { } @ Test public void testDocsEnum ( ) throws Exception { } @ Test public void testNormValues ( ) throws Exception { } @ Test public void testNumericDocValuesField ( ) throws Exception { } @ Test public void testSortedDocValuesField ( ) throws Exception { } @ Test public void testSortedSetDocValuesField ( ) throws Exception { } @ Test public void testTermVectors ( ) throws Exception { } }

Fix:
while ( ( doc = sortedPositions . advance ( ( doc + ( _TestUtil . nextInt ( random ( ) , 1 , 5 ) ) ) ) ) != ( DocIdSetIterator . NO_MORE_DOCS ) ) {