aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/bytecomplens/bytecomp.cpp
blob: 63aa2da15f626d901a009a97208fbefab2f9947a (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
97
98
99
100
101
102
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <memory>
#include <vespa/fastos/fastos.h>
#include <vespa/log/log.h>
LOG_SETUP("bytecomplens_test");
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/random.h>
#include <vespa/searchlib/docstore/bytecomplens.h>


class Test : public vespalib::TestApp {
private:
    void testRandomLengths();

public:
    int Main() {
        TEST_INIT("bytecomplens_test");
        testRandomLengths();    TEST_FLUSH();
        TEST_DONE();
    }
};

TEST_APPHOOK(Test);


void
Test::testRandomLengths()
{
    vespalib::RandomGen rndgen(0x07031969);

#define TBLSIZ 0xc00000

    auto lentable = std::unique_ptr<uint32_t[]>(new uint32_t[TBLSIZ]);
    auto offtable = std::unique_ptr<uint64_t[]>(new uint64_t[TBLSIZ]);

    uint64_t offset = 16;

    for (int i = 0; i < TBLSIZ; i++) {
        int sel = rndgen.nextInt32();
        int val = rndgen.nextInt32();
        switch (sel & 0x7) {
        case 0:
            val &= 0x7F;
            break;
        case 1:
            val &= 0xFF;
            break;
        case 3:
            val &= 0x1FFF;
            break;
        case 4:
            val &= 0x3FFF;
            break;
        case 5:
            val &= 0x7FFF;
            break;
        case 6:
            val &= 0xFFFF;
            break;
        case 7:
        default:
            val &= 0xFFFFF;
            break;
        }
        offtable[i] = offset;
        lentable[i] = val;
        offset += val;
    }

    LOG(info, "made %d random offsets", TBLSIZ);

    search::ByteCompressedLengths foo;

    LOG(info, "empty BCL using %9ld bytes memory", foo.memoryUsed());

    foo.addOffsetTable(TBLSIZ/4, offtable.get());
    foo.addOffsetTable(TBLSIZ/4, offtable.get() + 1*(TBLSIZ/4));

    LOG(info, "half  BCL using %9ld bytes memory", foo.memoryUsed());

    search::ByteCompressedLengths bar;
    foo.swap(bar);
    bar.addOffsetTable(TBLSIZ/4, offtable.get() + 2*(TBLSIZ/4));
    bar.addOffsetTable(TBLSIZ/4, offtable.get() + 3*(TBLSIZ/4));
    foo.swap(bar);

    LOG(info, "full  BCL using %9ld bytes memory", foo.memoryUsed());

    LOG(info, "constructed %d byte compressed lengths", TBLSIZ-1);

    for (int i = 0; i < TBLSIZ-1; i++) {
        search::ByteCompressedLengths::OffLen offlen;
        offlen = foo.getOffLen(i);

        if ((i % 1000000) == 0) {
            LOG(info, "data blob [%d] length %ld offset %ld", i, offlen.length, offlen.offset);
        }
        EXPECT_EQUAL(lentable[i], offlen.length);
        EXPECT_EQUAL(offtable[i], offlen.offset);
    }
}