// Path: src/main/java/org/elasticsearch/action/allterms/TransportAllTermsShardAction.java
// protected static List<TermsEnum> getTermsEnums(AllTermsShardRequest request, List<LeafReaderContext> leaves) {
//     List<TermsEnum> termIters = new ArrayList<>();
//     try {
//         for (LeafReaderContext reader : leaves) {
//             termIters.add(reader.reader().terms(request.field()).iterator());
//         }
//     } catch (IOException e) {
//     }
//     return termIters;
// }


import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.index.NoMergeScheduler;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.test.ESTestCase;
import org.junit.After;
import org.junit.Before;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.elasticsearch.action.allterms.TransportAllTermsShardAction.getTermsEnums;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.core.IsEqual.equalTo;
    public void testFindSmallestTermFromNotExistentTerm() throws IOException {
        SmallestTermAndExhausted smallestTermAndExhausted = getSmallestTermAndExhausted("foo");
        BytesRef smallestTerm = smallestTermAndExhausted.getSmallestTerm();
        int[] exhausted = smallestTermAndExhausted.getExhausted();
        assertThat(smallestTerm.utf8ToString(), equalTo("forget"));
        int i = -1;
        int numExhausted = 0;
        for (TermsEnum termsEnum : smallestTermAndExhausted.getTermsIters()) {
            i++;
            if (exhausted[i] == 1) {
                numExhausted++;
            } else {
                assertThat(termsEnum.term().utf8ToString().compareTo("foo"), greaterThanOrEqualTo(0));
            }
        }
        assertThat(numExhausted, equalTo(2));
    }

    public void testFindSmallestAllExhausted() throws IOException {
        SmallestTermAndExhausted smallestTermAndExhausted = getSmallestTermAndExhausted("zonk");
        BytesRef smallestTerm = smallestTermAndExhausted.getSmallestTerm();
        int[] exhausted = smallestTermAndExhausted.getExhausted();
        assertThat(smallestTerm, equalTo(null));
        for (int i = 0; i < 4; i++) {
            assertThat(exhausted[i], equalTo(1));
        }
    }

    private SmallestTermAndExhausted getSmallestTermAndExhausted(String from) throws IOException {
        AllTermsShardRequest request = new AllTermsShardRequest(new AllTermsRequest(), "index", 0, "field", 1, from, 0);
// Path: src/main/java/org/elasticsearch/action/allterms/TransportAllTermsShardAction.java
// protected static List<TermsEnum> getTermsEnums(AllTermsShardRequest request, List<LeafReaderContext> leaves) {
//     List<TermsEnum> termIters = new ArrayList<>();
//     try {
//         for (LeafReaderContext reader : leaves) {
//             termIters.add(reader.reader().terms(request.field()).iterator());
//         }
//     } catch (IOException e) {
//     }
//     return termIters;
// }


// Path: src/test/java/org/elasticsearch/action/allterms/AllTermsTests.java
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.index.NoMergeScheduler;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.test.ESTestCase;
import org.junit.After;
import org.junit.Before;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.elasticsearch.action.allterms.TransportAllTermsShardAction.getTermsEnums;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.core.IsEqual.equalTo;

    public void testFindSmallestTermFromNotExistentTerm() throws IOException {
        SmallestTermAndExhausted smallestTermAndExhausted = getSmallestTermAndExhausted("foo");
        BytesRef smallestTerm = smallestTermAndExhausted.getSmallestTerm();
        int[] exhausted = smallestTermAndExhausted.getExhausted();
        assertThat(smallestTerm.utf8ToString(), equalTo("forget"));
        int i = -1;
        int numExhausted = 0;
        for (TermsEnum termsEnum : smallestTermAndExhausted.getTermsIters()) {
            i++;
            if (exhausted[i] == 1) {
                numExhausted++;
            } else {
                assertThat(termsEnum.term().utf8ToString().compareTo("foo"), greaterThanOrEqualTo(0));
            }
        }
        assertThat(numExhausted, equalTo(2));
    }

    public void testFindSmallestAllExhausted() throws IOException {
        SmallestTermAndExhausted smallestTermAndExhausted = getSmallestTermAndExhausted("zonk");
        BytesRef smallestTerm = smallestTermAndExhausted.getSmallestTerm();
        int[] exhausted = smallestTermAndExhausted.getExhausted();
        assertThat(smallestTerm, equalTo(null));
        for (int i = 0; i < 4; i++) {
            assertThat(exhausted[i], equalTo(1));
        }
    }

    private SmallestTermAndExhausted getSmallestTermAndExhausted(String from) throws IOException {
        AllTermsShardRequest request = new AllTermsShardRequest(new AllTermsRequest(), "index", 0, "field", 1, from, 0);
        List<TermsEnum> termIters = getTermsEnums(request, reader.leaves());