aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/fef/featurenameparser/featurenameparser_test.cpp
blob: 33491aa6fff47cb3ee80ade7d990bc1e8a8ce2fd (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/log.h>
LOG_SETUP("featurenameparser_test");
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/searchlib/fef/featurenameparser.h>
#include <vector>
#include <string>

using namespace search::fef;

struct ParamList {
    std::vector<vespalib::string> list;
    ParamList() : list() {}
    ParamList(const std::vector<vespalib::string> &l) : list(l) {}
    ParamList &add(const vespalib::string &str) {
        list.push_back(str);
        return *this;
    }
    bool operator==(const ParamList &rhs) const {
        return rhs.list == list;
    }
};

std::ostream &operator<<(std::ostream &os, const ParamList &pl) {
    os << std::endl;
    for (uint32_t i = 0; i < pl.list.size(); ++i) {
        os << "  " << pl.list[i] << std::endl;
    }
    return os;
}

class Test : public vespalib::TestApp
{
public:
    bool testParse(const vespalib::string &input, bool valid,
                   const vespalib::string &base, ParamList pl,
                   const vespalib::string &output);
    void testFile(const vespalib::string &name);
    int Main() override;
};

bool
Test::testParse(const vespalib::string &input, bool valid,
                const vespalib::string &base, ParamList pl,
                const vespalib::string &output)
{
    bool ok = true;
    FeatureNameParser parser(input);
    if (!parser.valid()) {
        LOG(warning, "parse error: input:'%s', rest:'%s'",
            input.c_str(), input.substr(parser.parsedBytes()).c_str());
    }
    ok &= EXPECT_EQUAL(parser.valid(), valid);
    ok &= EXPECT_EQUAL(parser.baseName(), base);
    ok &= EXPECT_EQUAL(ParamList(parser.parameters()), pl);
    ok &= EXPECT_EQUAL(parser.output(), output);
    return ok;
}

void
Test::testFile(const vespalib::string &name)
{
    char buf[4_Ki];
    uint32_t lineN = 0;
    FILE *f = fopen(name.c_str(), "r");
    ASSERT_TRUE(f != 0);
    while (fgets(buf, sizeof(buf), f) != NULL) {
        ++lineN;
        vespalib::string line(buf);
        if (*line.rbegin() == '\n') {
            line.resize(line.size() - 1);
        }
        if (line.empty() || line[0] == '#') {
            continue;
        }
        uint32_t idx = line.find("<=>");
        if (!EXPECT_TRUE(idx < line.size())) {
            LOG(error, "(%s:%u): malformed line: '%s'",
                name.c_str(), lineN, line.c_str());
        } else {
            vespalib::string input = line.substr(0, idx);
            vespalib::string expect = line.substr(idx + strlen("<=>"));
            if (!EXPECT_EQUAL(FeatureNameParser(input).featureName(), expect)) {
                LOG(error, "(%s:%u): test failed: '%s'",
                    name.c_str(), lineN, line.c_str());
            }
        }
    }
    ASSERT_TRUE(!ferror(f));
    fclose(f);
}

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

    // normal cases
    EXPECT_TRUE(testParse("foo", true, "foo", ParamList(), ""));
    EXPECT_TRUE(testParse("foo.out", true, "foo", ParamList(), "out"));
    EXPECT_TRUE(testParse("foo(a)", true, "foo", ParamList().add("a"), ""));
    EXPECT_TRUE(testParse("foo(a,b)", true, "foo", ParamList().add("a").add("b"), ""));
    EXPECT_TRUE(testParse("foo(a,b).out", true, "foo", ParamList().add("a").add("b"), "out"));

    // @ in feature name (for macros)
    EXPECT_TRUE(testParse("foo@", true, "foo@", ParamList(), ""));
    EXPECT_TRUE(testParse("foo@.out", true, "foo@", ParamList(), "out"));
    EXPECT_TRUE(testParse("foo@(a)", true, "foo@", ParamList().add("a"), ""));
    EXPECT_TRUE(testParse("foo@(a,b)", true, "foo@", ParamList().add("a").add("b"), ""));
    EXPECT_TRUE(testParse("foo@(a,b).out", true, "foo@", ParamList().add("a").add("b"), "out"));

    // $ in feature name (for macros)
    EXPECT_TRUE(testParse("foo$", true, "foo$", ParamList(), ""));
    EXPECT_TRUE(testParse("foo$.out", true, "foo$", ParamList(), "out"));
    EXPECT_TRUE(testParse("foo$(a)", true, "foo$", ParamList().add("a"), ""));
    EXPECT_TRUE(testParse("foo$(a,b)", true, "foo$", ParamList().add("a").add("b"), ""));
    EXPECT_TRUE(testParse("foo$(a,b).out", true, "foo$", ParamList().add("a").add("b"), "out"));

    // de-quoting of parameters
    EXPECT_TRUE(testParse("foo(a,\"b\")", true, "foo", ParamList().add("a").add("b"), ""));
    EXPECT_TRUE(testParse("foo(a,\" b \")", true, "foo", ParamList().add("a").add(" b "), ""));
    EXPECT_TRUE(testParse("foo( \"a\" , \" b \" )", true, "foo", ParamList().add("a").add(" b "), ""));
    EXPECT_TRUE(testParse("foo(\"\\\"\\\\\\t\\n\\r\\f\\x20\")", true, "foo", ParamList().add("\"\\\t\n\r\f "), ""));

    // only default output if '.' not specified
    EXPECT_TRUE(testParse("foo.", false, "", ParamList(), ""));
    EXPECT_TRUE(testParse("foo(a,b).", false, "", ParamList(), ""));

    // string cannot end in parameter list
    EXPECT_TRUE(testParse("foo(", false, "", ParamList(), ""));
    EXPECT_TRUE(testParse("foo(a", false, "", ParamList(), ""));
    EXPECT_TRUE(testParse("foo(a\\", false, "", ParamList(), ""));
    EXPECT_TRUE(testParse("foo(a\\)", false, "", ParamList(), ""));
    EXPECT_TRUE(testParse("foo(a,", false, "", ParamList(), ""));
    EXPECT_TRUE(testParse("foo(a,b", false, "", ParamList(), ""));

    // empty parameters
    EXPECT_TRUE(testParse("foo()", true, "foo", ParamList().add(""), ""));
    EXPECT_TRUE(testParse("foo(,)", true, "foo", ParamList().add("").add(""), ""));
    EXPECT_TRUE(testParse("foo(,,)", true, "foo", ParamList().add("").add("").add(""), ""));
    EXPECT_TRUE(testParse("foo(,x,)", true, "foo", ParamList().add("").add("x").add(""), ""));
    EXPECT_TRUE(testParse("foo(  )", true, "foo", ParamList().add(""), ""));
    EXPECT_TRUE(testParse("foo(  ,  ,  )", true, "foo", ParamList().add("").add("").add(""), ""));
    EXPECT_TRUE(testParse("foo( \t , \n , \r , \f )", true, "foo", ParamList().add("").add("").add("").add(""), ""));

    testFile(TEST_PATH("parsetest.txt"));
    TEST_DONE();
}

TEST_APPHOOK(Test);