aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/sortresults/sortresults_test.cpp
blob: bbd6d0b72ce3528252a0be3e1daa008d15010acb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;
}