summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/docsumformat/docsum-parse.cpp
blob: bc53d0f7d0d7d8d8e3fe2e3c15abc400e2d7ba67 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Copyright (C) 2001-2003 Fast Search & Transfer ASA
// Copyright (C) 2003 Overture Services Norway AS


#include <vespa/log/log.h>
LOG_SETUP("docsum-parse");
#include <vespa/fnet/frt/frt.h>
#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/searchsummary/docsummary/urlresult.h>
#include <vespa/searchsummary/docsummary/resultconfig.h>


// needed to resolve external symbol from httpd.h on AIX
void FastS_block_usr2() {}


class MyApp : public FastOS_Application
{
public:
    bool Equal(search::docsummary::ResConfigEntry *a, search::docsummary::ResConfigEntry *b);
    bool Equal(search::docsummary::ResultClass *a, search::docsummary::ResultClass *b);
    bool Equal(search::docsummary::ResultConfig *a, search::docsummary::ResultConfig *b);
    bool TestCorrect(const char *dirname, const char *filename);
    bool TestIncorrect(const char *dirname, const char *filename);
    int Main();
};


bool
MyApp::Equal(search::docsummary::ResConfigEntry *a, search::docsummary::ResConfigEntry *b)
{
    return ((a->_type == b->_type)
            && (strcmp(a->_bindname, b->_bindname) == 0));
}


bool
MyApp::Equal(search::docsummary::ResultClass *a, search::docsummary::ResultClass *b)
{
    bool rc = true;

    rc = rc && (a->GetNumEntries() == b->GetNumEntries());
    rc = rc && (a->GetClassID() == b->GetClassID());
    rc = rc && (strcmp(a->GetClassName(), b->GetClassName()) == 0);

    for (uint32_t i = 0; rc && i < a->GetNumEntries(); i++) {
        rc = rc && Equal(a->GetEntry(i), b->GetEntry(i));
    }

    return rc;
}


bool
MyApp::Equal(search::docsummary::ResultConfig *a, search::docsummary::ResultConfig *b)
{
    bool rc = true;

    search::docsummary::ResultClass *resClassA;
    search::docsummary::ResultClass *resClassB;

    rc = rc && (a->GetNumResultClasses() == b->GetNumResultClasses());

    resClassA = a->GetResultClasses();
    resClassB = b->GetResultClasses();

    while(rc && resClassA != NULL && resClassB != NULL) {
        rc = rc && Equal(resClassA, resClassB);
        resClassA = resClassA->GetNextClass();
        resClassB = resClassB->GetNextClass();
    }
    rc = rc && (resClassA == NULL);
    rc = rc && (resClassB == NULL);

    return rc;
}


bool
MyApp::TestCorrect(const char *dirname, const char *filename)
{
    char str1[512]; // test input file
    char str2[512]; // test output file
    char str3[512]; // summary.cf verification file

    search::docsummary::ResultConfig a;
    search::docsummary::ResultConfig b;
    search::docsummary::ResultConfig c;
    search::docsummary::ResultConfig d;

    sprintf(str1, "%s%s%s", dirname,
            FastOS_FileInterface::GetPathSeparator(), filename);
    sprintf(str2, "%s%sout.%s", dirname,
            FastOS_FileInterface::GetPathSeparator(), filename);
    sprintf(str3, "%s%sOK.%s", dirname,
            FastOS_FileInterface::GetPathSeparator(), filename);

    if (!a.ReadConfig(str1)) {
        LOG(error, "could not read config from : %s", str1);
        return false;
    }

    if (!a.WriteConfig(str2)) {
        LOG(error, "could not write config to : %s", str2);
        return false;
    }

    if (!b.ReadConfig(str2)) {
        LOG(error, "could not read config from : %s", str2);
        return false;
    }

    if (!c.ReadConfig(str3)) {
        LOG(error, "could not read config from : %s", str3);
        return false;
    }

    if (!Equal(&a, &b)) {
        LOG(error, "%s and %s does not contain the same config", str1, str2);
        return false;
    }

    if (!Equal(&a, &c)) {
        LOG(error, "%s and %s does not contain the same config", str1, str3);
        return false;
    }

    if (!Equal(&b, &c)) {
        LOG(error, "%s and %s does not contain the same config", str2, str3);
        return false;
    }

    FRT_RPCRequest *req = new FRT_RPCRequest();
    assert(req != NULL);
    c.GetConfig(req);
    d.SetConfig(req);
    if (!Equal(&c, &d)) {
        LOG(error, "RPC get/set failed (%s)", str3);
        req->SubRef();
        return false;
    }
    req->SubRef();

    return true;
}


bool
MyApp::TestIncorrect(const char *dirname, const char *filename)
{
    char str[512];

    sprintf(str, "%s%s%s", dirname,
            FastOS_FileInterface::GetPathSeparator(), filename);

    search::docsummary::ResultConfig resConfig;

    if (resConfig.ReadConfig(str)) {
        LOG(error, "'%s' did not give parse error", str);
        return false;
    }
    return true;
}


int
MyApp::Main()
{
    bool rc = true;

    FastOS_DirectoryScan dirScan("parsetest");
    LOG(info, "looking for input files in 'parsetest'...");
    while (dirScan.ReadNext()) {
        if (strncmp(dirScan.GetName(), "correct.", 8) == 0) {
            if (TestCorrect("parsetest", dirScan.GetName())) {
                LOG(info, "'%s' : positive test PASSED", dirScan.GetName());
            } else {
                LOG(error, "'%s' : positive test FAILED", dirScan.GetName());
                rc = false;
            }
        } else if (strncmp(dirScan.GetName(), "incorrect.", 10) == 0) {
            if (TestIncorrect("parsetest", dirScan.GetName())) {
                LOG(info, "'%s' : negative test PASSED", dirScan.GetName());
            } else {
                LOG(error, "'%s' : negative test FAILED", dirScan.GetName());
                rc = false;
            }
        }
    }
    return (rc ? 0 : 1);
}


int
main(int argc, char **argv)
{
    MyApp myapp;
    return myapp.Entry(argc, argv);
}