use of org.apache.lucene.index.TermsEnum.SeekStatus in project lucene-solr by apache.
the class LuceneTestCase method assertTermsSeekingEquals.
private void assertTermsSeekingEquals(String info, Terms leftTerms, Terms rightTerms) throws IOException {
// just an upper bound
int numTests = atLeast(20);
Random random = random();
TermsEnum leftEnum = null;
// collect this number of terms from the left side
HashSet<BytesRef> tests = new HashSet<>();
int numPasses = 0;
while (numPasses < 10 && tests.size() < numTests) {
leftEnum = leftTerms.iterator();
BytesRef term = null;
while ((term = leftEnum.next()) != null) {
int code = random.nextInt(10);
if (code == 0) {
// the term
tests.add(BytesRef.deepCopyOf(term));
} else if (code == 1) {
// truncated subsequence of term
term = BytesRef.deepCopyOf(term);
if (term.length > 0) {
// truncate it
term.length = random.nextInt(term.length);
}
} else if (code == 2) {
// term, but ensure a non-zero offset
byte[] newbytes = new byte[term.length + 5];
System.arraycopy(term.bytes, term.offset, newbytes, 5, term.length);
tests.add(new BytesRef(newbytes, 5, term.length));
} else if (code == 3) {
switch(random().nextInt(3)) {
case 0:
// before the first term
tests.add(new BytesRef());
break;
case 1:
// past the last term
tests.add(new BytesRef(new byte[] { (byte) 0xFF, (byte) 0xFF }));
break;
case 2:
// random term
tests.add(new BytesRef(TestUtil.randomSimpleString(random())));
break;
default:
throw new AssertionError();
}
}
}
numPasses++;
}
TermsEnum rightEnum = rightTerms.iterator();
ArrayList<BytesRef> shuffledTests = new ArrayList<>(tests);
Collections.shuffle(shuffledTests, random);
for (BytesRef b : shuffledTests) {
if (rarely()) {
// make new enums
leftEnum = leftTerms.iterator();
rightEnum = rightTerms.iterator();
}
final boolean seekExact = random().nextBoolean();
if (seekExact) {
assertEquals(info, leftEnum.seekExact(b), rightEnum.seekExact(b));
} else {
SeekStatus leftStatus = leftEnum.seekCeil(b);
SeekStatus rightStatus = rightEnum.seekCeil(b);
assertEquals(info, leftStatus, rightStatus);
if (leftStatus != SeekStatus.END) {
assertEquals(info, leftEnum.term(), rightEnum.term());
assertTermStatsEquals(info, leftEnum, rightEnum);
}
}
}
}
Aggregations