aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/fef/attributecontent/attributecontent_test.cpp
blob: 1c75d47a134c47ab42357148953f690d686e4265 (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
103
104
105
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchcommon/attribute/attributecontent.h>
#include <vespa/searchcommon/attribute/config.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/attribute/integerbase.h>
#include <vespa/log/log.h>
LOG_SETUP("attributecontent_test");

using namespace search::attribute;

namespace search {
namespace fef {

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

public:
    int Main() override;
};

void
Test::testWriteAndRead()
{
    using UintContent = search::attribute::AttributeContent<uint32_t>;
    UintContent buf;
    EXPECT_EQUAL(buf.capacity(), 16u);
    EXPECT_EQUAL(buf.size(), 0u);

    uint32_t i;
    uint32_t * data;
    const uint32_t * itr;
    for (i = 0, data = buf.data(); i < 16; ++i, ++data) {
        *data = i;
    }
    buf.setSize(16);
    EXPECT_EQUAL(buf.size(), 16u);
    for (i = 0, itr = buf.begin(); itr != buf.end(); ++i, ++itr) {
        EXPECT_EQUAL(*itr, i);
        EXPECT_EQUAL(buf[i], i);
    }
    EXPECT_EQUAL(i, 16u);

    buf.allocate(10);
    EXPECT_EQUAL(buf.capacity(), 16u);
    EXPECT_EQUAL(buf.size(), 16u);
    buf.allocate(32);
    EXPECT_EQUAL(buf.capacity(), 32u);
    EXPECT_EQUAL(buf.size(), 0u);

    for (i = 0, data = buf.data(); i < 32; ++i, ++data) {
        *data = i;
    }
    buf.setSize(32);
    EXPECT_EQUAL(buf.size(), 32u);
    for (i = 0, itr = buf.begin(); itr != buf.end(); ++i, ++itr) {
        EXPECT_EQUAL(*itr, i);
        EXPECT_EQUAL(buf[i], i);
    }
    EXPECT_EQUAL(i, 32u);
}

void
Test::testFill()
{
    Config cfg(BasicType::INT32, CollectionType::ARRAY);
    AttributeVector::SP av = AttributeFactory::createAttribute("aint32", cfg);
    av->addDocs(2);
    IntegerAttribute * ia = static_cast<IntegerAttribute *>(av.get());
    ia->append(0, 10, 0);
    ia->append(1, 20, 0);
    ia->append(1, 30, 0);
    av->commit();
    const IAttributeVector & iav = *av.get();
    IntegerContent buf;
    buf.fill(iav, 0);
    EXPECT_EQUAL(1u, buf.size());
    EXPECT_EQUAL(10, buf[0]);
    buf.fill(iav, 1);
    EXPECT_EQUAL(2u, buf.size());
    EXPECT_EQUAL(20, buf[0]);
    EXPECT_EQUAL(30, buf[1]);
    buf.fill(iav, 0);
    EXPECT_EQUAL(1u, buf.size());
    EXPECT_EQUAL(10, buf[0]);
}

int
Test::Main()
{
    TEST_INIT("attributecontent_test");

    testWriteAndRead();
    testFill();

    TEST_DONE();
}

} // namespace fef
} // namespace search

TEST_APPHOOK(search::fef::Test);