private void TestSorting(int precisionStep)
{
string field = "field" + precisionStep;
// 10 random tests, the index order is ascending,
// so using a reverse sort field should retun descending documents
int num = TestUtil.NextInt(Random(), 10, 20);
for (int i = 0; i < num; i++)
{
int lower = (int)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
int upper = (int)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
if (lower > upper)
{
int a = lower;
lower = upper;
upper = a;
}
Query tq = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, true);
TopDocs topDocs = Searcher.Search(tq, null, NoDocs, new Sort(new SortField(field, SortField.Type_e.INT, true)));
if (topDocs.TotalHits == 0)
{
continue;
}
ScoreDoc[] sd = topDocs.ScoreDocs;
Assert.IsNotNull(sd);
int last = (int)Searcher.Doc(sd[0].Doc).GetField(field).NumericValue;
for (int j = 1; j < sd.Length; j++)
{
int act = (int)Searcher.Doc(sd[j].Doc).GetField(field).NumericValue;
Assert.IsTrue(last > act, "Docs should be sorted backwards");
last = act;
}
}
}