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

#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/vespalib/gtest/gtest.h>

using namespace search::attribute;

namespace search::fef {

TEST(AttributeContentTest, test_write_and_read)
{
    using UintContent = search::attribute::AttributeContent<uint32_t>;
    UintContent buf;
    EXPECT_EQ(buf.capacity(), 16u);
    EXPECT_EQ(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_EQ(buf.size(), 16u);
    for (i = 0, itr = buf.begin(); itr != buf.end(); ++i, ++itr) {
        EXPECT_EQ(*itr, i);
        EXPECT_EQ(buf[i], i);
    }
    EXPECT_EQ(i, 16u);

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

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

TEST(AttributeContentTest, test_fill)
{
    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_EQ(1u, buf.size());
    EXPECT_EQ(10, buf[0]);
    buf.fill(iav, 1);
    EXPECT_EQ(2u, buf.size());
    EXPECT_EQ(20, buf[0]);
    EXPECT_EQ(30, buf[1]);
    buf.fill(iav, 0);
    EXPECT_EQ(1u, buf.size());
    EXPECT_EQ(10, buf[0]);
}

}

GTEST_MAIN_RUN_ALL_TESTS()