summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/sortresults/sortresults_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'searchlib/src/tests/sortresults/sortresults_test.cpp')
-rw-r--r--searchlib/src/tests/sortresults/sortresults_test.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/searchlib/src/tests/sortresults/sortresults_test.cpp b/searchlib/src/tests/sortresults/sortresults_test.cpp
new file mode 100644
index 00000000000..bbd6d0b72ce
--- /dev/null
+++ b/searchlib/src/tests/sortresults/sortresults_test.cpp
@@ -0,0 +1,96 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/searchlib/common/bitvector.h>
+#include <vespa/searchlib/common/sortresults.h>
+#include <cassert>
+
+using search::RankedHit;
+
+unsigned int
+myrandom()
+{
+ unsigned int r;
+ r = random() & 0xffff;
+ r <<= 16;
+ r += random() & 0xffff;
+ return r;
+}
+
+
+bool
+test_sort(unsigned int caseNum, unsigned int n, unsigned int ntop)
+{
+ bool ok = true;
+ double minmax;
+ unsigned int i;
+ RankedHit *array;
+
+ if (ntop == 0) {
+ printf("CASE %03d: [%d/%d] PASS\n", caseNum, ntop, n);
+ return true;
+ }
+ if (ntop > n)
+ ntop = n;
+
+ array = new RankedHit[n];
+ assert(array != NULL);
+
+ for (i = 0; i < n; i++) {
+ array[i]._docId = i;
+ array[i]._rankValue = myrandom();
+ }
+ FastS_SortResults(array, n, ntop);
+
+ minmax = array[ntop - 1].getRank();
+ for(i = 0; i < n; i++) {
+ if (i < ntop && i > 0
+ && array[i].getRank() > array[i - 1].getRank()) {
+ printf("ERROR: rank(%d) > rank(%d)\n",
+ i, i - 1);
+ ok = false;
+ break;
+ }
+ if (i >= ntop &&
+ array[i].getRank() > minmax) {
+ printf("ERROR: rank(%d) > rank(%d)\n",
+ i, ntop - 1);
+ ok = false;
+ break;
+ }
+ }
+ delete [] array;
+ printf("CASE %03d: [%d/%d] %s\n", caseNum, ntop, n,
+ (ok)? "PASS" : "FAIL");
+ return ok;
+}
+
+
+int
+main(int argc, char **argv)
+{
+ (void) argc;
+ (void) argv;
+
+ bool ok = true;
+ unsigned int caseNum = 0;
+ unsigned int i;
+
+ ok &= test_sort(++caseNum, 1, 1);
+ for (i = 0; i < 5; i++) {
+ ok &= test_sort(++caseNum, 2, 2);
+ }
+ for (i = 0; i < 5; i++) {
+ ok &= test_sort(++caseNum, 50, 50);
+ }
+ for (i = 0; i < 5; i++) {
+ ok &= test_sort(++caseNum, 50000, 1);
+ ok &= test_sort(++caseNum, 50000, 500);
+ ok &= test_sort(++caseNum, 50000, 1000);
+ ok &= test_sort(++caseNum, 50000, 2000);
+ ok &= test_sort(++caseNum, 50000, 5000);
+ ok &= test_sort(++caseNum, 50000, 10000);
+ ok &= test_sort(++caseNum, 50000, 50000);
+ }
+ printf("CONCLUSION: TEST %s\n", (ok)? "PASSED" : "FAILED");
+ return (ok)? 0 : 1;
+}