aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/fef/table/table_test.cpp
blob: b0a47a8bdbce9d346b77bfad00e2fa9f9a4e2b53 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 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/searchlib/fef/filetablefactory.h>
#include <vespa/searchlib/fef/functiontablefactory.h>
#include <vespa/searchlib/fef/tablemanager.h>
#include <fstream>
#include <iostream>

namespace search {
namespace fef {

class TableTest : public vespalib::TestApp
{
private:
    bool assertTable(const Table & act, const Table & exp);
    bool assertCreateTable(const ITableFactory & tf, const vespalib::string & name, const Table & exp);
    void testTable();
    void testFileTableFactory();
    void testFunctionTableFactory();
    void testTableManager();

    const std::string _tables1Dir;
    const std::string _tables2Dir;
public:
    TableTest();
    ~TableTest();
    int Main() override;
};

TableTest::TableTest() :
    vespalib::TestApp(),
    _tables1Dir(TEST_PATH("tables1")),
    _tables2Dir(TEST_PATH("tables2"))
{
}

TableTest::~TableTest() {}

bool
TableTest::assertTable(const Table & act, const Table & exp)
{
    if (!EXPECT_EQUAL(act.size(), exp.size())) return false;
    for (size_t i = 0; i < act.size(); ++i) {
        if (!EXPECT_APPROX(act[i], exp[i], 0.01)) return false;
    }
    return true;
}

bool
TableTest::assertCreateTable(const ITableFactory & tf, const vespalib::string & name, const Table & exp)
{
    Table::SP t = tf.createTable(name);
    if (!EXPECT_TRUE(t.get() != NULL)) return false;
    return assertTable(*t, exp);
}

void
TableTest::testTable()
{
    Table t;
    EXPECT_EQUAL(t.size(), 0u);
    EXPECT_EQUAL(t.max(), -std::numeric_limits<double>::max());
    t.add(1).add(2);
    EXPECT_EQUAL(t.size(), 2u);
    EXPECT_EQUAL(t.max(), 2);
    EXPECT_EQUAL(t[0], 1);
    EXPECT_EQUAL(t[1], 2);
    t.add(10);
    EXPECT_EQUAL(t.size(), 3u);
    EXPECT_EQUAL(t.max(), 10);
    EXPECT_EQUAL(t[2], 10);
    t.add(5);
    EXPECT_EQUAL(t.size(), 4u);
    EXPECT_EQUAL(t.max(), 10);
    EXPECT_EQUAL(t[3], 5);
}

void
TableTest::testFileTableFactory()
{
    {
        FileTableFactory ftf(_tables1Dir);
        EXPECT_TRUE(assertCreateTable(ftf, "a", Table().add(1.5).add(2.25).add(3)));
        EXPECT_TRUE(ftf.createTable("b").get() == NULL);
    }
    {
        FileTableFactory ftf(_tables1Dir);
        EXPECT_TRUE(ftf.createTable("a").get() != NULL);
    }
}

void
TableTest::testFunctionTableFactory()
{
    FunctionTableFactory ftf(2);
    EXPECT_TRUE(assertCreateTable(ftf, "expdecay(400,12)",
               Table().add(400).add(368.02)));
    EXPECT_TRUE(assertCreateTable(ftf, "loggrowth(1000,5000,1)",
               Table().add(5000).add(5693.15)));
    EXPECT_TRUE(assertCreateTable(ftf, "linear(10,100)",
               Table().add(100).add(110)));
    // specify table size
    EXPECT_TRUE(assertCreateTable(ftf, "expdecay(400,12,3)",
               Table().add(400).add(368.02).add(338.60)));
    EXPECT_TRUE(assertCreateTable(ftf, "loggrowth(1000,5000,1,3)",
               Table().add(5000).add(5693.15).add(6098.61)));
    EXPECT_TRUE(assertCreateTable(ftf, "linear(10,100,3)",
               Table().add(100).add(110).add(120)));
    EXPECT_TRUE(ftf.createTable("expdecay()").get() == NULL);
    EXPECT_TRUE(ftf.createTable("expdecay(10)").get() == NULL);
    EXPECT_TRUE(ftf.createTable("loggrowth()").get() == NULL);
    EXPECT_TRUE(ftf.createTable("linear()").get() == NULL);
    EXPECT_TRUE(ftf.createTable("none").get() == NULL);
    EXPECT_TRUE(ftf.createTable("none(").get() == NULL);
    EXPECT_TRUE(ftf.createTable("none)").get() == NULL);
    EXPECT_TRUE(ftf.createTable("none)(").get() == NULL);
}

void
TableTest::testTableManager()
{
    {
        TableManager tm;
        tm.addFactory(ITableFactory::SP(new FileTableFactory(_tables1Dir)));
        tm.addFactory(ITableFactory::SP(new FileTableFactory(_tables2Dir)));

        {
            const Table * t = tm.getTable("a"); // from tables1
            ASSERT_TRUE(t != NULL);
            EXPECT_TRUE(assertTable(*t, Table().add(1.5).add(2.25).add(3)));
            EXPECT_TRUE(t == tm.getTable("a"));
        }
        {
            const Table * t = tm.getTable("b"); // from tables2
            ASSERT_TRUE(t != NULL);
            EXPECT_TRUE(assertTable(*t, Table().add(40).add(50).add(60)));
            EXPECT_TRUE(t == tm.getTable("b"));
        }
        {
            EXPECT_TRUE(tm.getTable("c") == NULL);
            EXPECT_TRUE(tm.getTable("c") == NULL);
        }
    }
    {
        TableManager tm;
        ASSERT_TRUE(tm.getTable("a") == NULL);
    }
}

int
TableTest::Main()
{
    TEST_INIT("table_test");

    testTable();
    testFileTableFactory();
    testFunctionTableFactory();
    testTableManager();

    TEST_DONE();
}

}
}

TEST_APPHOOK(search::fef::TableTest);