diff options
Diffstat (limited to 'searchlib/src/tests/sortresults/sorttest.cpp')
-rw-r--r-- | searchlib/src/tests/sortresults/sorttest.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/searchlib/src/tests/sortresults/sorttest.cpp b/searchlib/src/tests/sortresults/sorttest.cpp new file mode 100644 index 00000000000..4c3326f4cbc --- /dev/null +++ b/searchlib/src/tests/sortresults/sorttest.cpp @@ -0,0 +1,99 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright (C) 2001-2003 Fast Search & Transfer ASA +// Copyright (C) 2003 Overture Services Norway AS + + +#include <vespa/fastos/fastos.h> +#include <vespa/searchlib/common/bitvector.h> +#include <vespa/searchlib/common/sortresults.h> + +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]._rankValue; + for(i = 0; i < n; i++) { + if (i < ntop && i > 0 + && array[i]._rankValue > array[i - 1]._rankValue) { + printf("ERROR: rank(%d) > rank(%d)\n", + i, i - 1); + ok = false; + break; + } + if (i >= ntop && + array[i]._rankValue > 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; +} |