summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/docsumformat/docsum-pack.cpp
blob: 07aceea83e0a4c101e249060f2803c9b9d1d80cf (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchlib/util/rawbuf.h>
#include <vespa/searchsummary/docsummary/general_result.h>
#include <vespa/searchsummary/docsummary/resultconfig.h>
#include <vespa/searchsummary/docsummary/resultpacker.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/log/log.h>
LOG_SETUP("docsum-pack");

using namespace search::docsummary;

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


class MyApp
{
private:
    bool               _rc;
    uint32_t           _cnt;
    ResultConfig _config;
    ResultPacker _packer;

public:
    MyApp();
    ~MyApp();

    // log test results
    void ReportTestResult(uint32_t line, bool rc);
    bool RTR(uint32_t line, bool rc)
    { ReportTestResult(line, rc); return rc; }

    // compare runtime info (,but ignore result class)
    bool Equal(ResEntry *a, ResEntry *b);
    bool Equal(GeneralResult *a, GeneralResult *b);

    void TestIntValue(uint32_t line, GeneralResult *gres, const char *field, uint32_t value);
    void TestDoubleValue(uint32_t line, GeneralResult *gres, const char *field, double value);
    void TestInt64Value(uint32_t line, GeneralResult *gres, const char *field, uint64_t value);
    void TestStringValue(uint32_t line, GeneralResult *gres, const char *field, const char *value);
    void TestDataValue(uint32_t line, GeneralResult *gres, const char *field, const char *value);

    void TestFailLong();
    void TestFailShort();
    void TestFailOrder();
    void TestBasicInplace();

    int main(int argc, char **argv);
};

MyApp::MyApp()
    : _rc(false),
      _cnt(0u),
      _config(),
      _packer(&_config)
{}

MyApp::~MyApp() = default;

void
MyApp::ReportTestResult(uint32_t line, bool rc)
{
    _cnt++;

    if (rc) {
        LOG(info, "Test case %d: SUCCESS", _cnt);
    } else {
        LOG(error, "Test case %d: FAIL (see %s:%d)", _cnt, __FILE__, line);
        _rc = false;
    }
}


bool
MyApp::Equal(ResEntry *a, ResEntry *b)
{
    if (a->_type != b->_type)
        return false;

    if (a->_intval != b->_intval)
        return false;

    if (a->_type != RES_INT &&
        memcmp(a->_pt, b->_pt, a->_intval) != 0)
        return false;

    return true;
}


bool
MyApp::Equal(GeneralResult *a, GeneralResult *b)
{
    uint32_t numEntries = a->GetClass()->GetNumEntries();

    if (b->GetClass()->GetNumEntries() != numEntries)
        return false;

    for (uint32_t i = 0; i < numEntries; i++) {

        if (!Equal(a->GetEntry(i), b->GetEntry(i)))
            return false;

        if (a->GetClass()->GetEntry(i)->_bindname != b->GetClass()->GetEntry(i)->_bindname)
            return false;
    }

    return true;
}

void
MyApp::TestIntValue(uint32_t line, GeneralResult *gres, const char *field, uint32_t value)
{
    ResEntry *entry = (gres != nullptr) ? gres->GetPresentEntry(field) : nullptr;

    bool rc = (entry != nullptr &&
               entry->_type == RES_INT &&
               entry->_intval == value);

    RTR(line, rc);
}

void
MyApp::TestDoubleValue(uint32_t line, GeneralResult *gres, const char *field, double value)
{
    ResEntry *entry = (gres != nullptr) ? gres->GetPresentEntry(field) : nullptr;

    bool rc = (entry != nullptr &&
               entry->_type == RES_DOUBLE &&
               entry->_doubleval == value);

    RTR(line, rc);
}

void
MyApp::TestInt64Value(uint32_t line, GeneralResult *gres, const char *field, uint64_t value)
{
    ResEntry *entry = (gres != nullptr) ? gres->GetPresentEntry(field) : nullptr;

    bool rc = (entry != nullptr &&
               entry->_type == RES_INT64 &&
               entry->_int64val == value);

    RTR(line, rc);
}


void
MyApp::TestStringValue(uint32_t line, GeneralResult *gres, const char *field, const char *value)
{
    ResEntry *entry = (gres != nullptr) ? gres->GetPresentEntry(field) : nullptr;

    bool rc = (entry != nullptr &&
               entry->_type == RES_STRING &&
               entry->_stringlen == strlen(value) &&
               strncmp(entry->_stringval, value, entry->_stringlen) == 0);

    if (!rc && entry != nullptr) {
        LOG(warning,"string value '%.*s' != '%s'",
            (int) entry->_stringlen, entry->_stringval, value);
    }

    RTR(line, rc);
}

void
MyApp::TestDataValue(uint32_t line, GeneralResult *gres, const char *field, const char *value)
{
    ResEntry *entry = (gres != nullptr) ? gres->GetPresentEntry(field) : nullptr;

    bool rc = (entry != nullptr &&
               entry->_type == RES_DATA &&
               entry->_datalen == strlen(value) &&
               strncmp(entry->_dataval, value, entry->_datalen) == 0);

    RTR(line, rc);
}

void
MyApp::TestFailLong()
{
    const char *buf;
    uint32_t buflen;

    uint32_t intval     = 4;
    uint16_t shortval   = 2;
    uint8_t  byteval    = 1;
    float    floatval   = 4.5;
    double   doubleval  = 8.75;
    uint64_t int64val   = 8;
    const char *strval  = "This is a string";
    const char *datval  = "This is data";
    const char *lstrval = "This is a long string";
    const char *ldatval = "This is long data";

    RTR(__LINE__, _packer.Init(0));
    RTR(__LINE__, _packer.AddInteger(intval));
    RTR(__LINE__, _packer.AddShort(shortval));
    RTR(__LINE__, _packer.AddByte(byteval));
    RTR(__LINE__, _packer.AddFloat(floatval));
    RTR(__LINE__, _packer.AddDouble(doubleval));
    RTR(__LINE__, _packer.AddInt64(int64val));
    RTR(__LINE__, _packer.AddString(strval, strlen(strval)));
    RTR(__LINE__, _packer.AddData(datval, strlen(datval)));
    RTR(__LINE__, _packer.AddLongString(lstrval, strlen(lstrval)));
    RTR(__LINE__, _packer.AddLongData(ldatval, strlen(ldatval)));
    RTR(__LINE__, !_packer.AddByte(byteval));
    RTR(__LINE__, !_packer.GetDocsumBlob(&buf, &buflen));
}

void
MyApp::TestFailShort()
{
    const char *buf;
    uint32_t buflen;

    uint32_t intval     = 4;
    uint16_t shortval   = 2;
    uint8_t  byteval    = 1;
    float    floatval   = 4.5;
    double   doubleval  = 8.75;
    uint64_t int64val   = 8;
    const char *strval  = "This is a string";
    const char *datval  = "This is data";
    const char *lstrval = "This is a long string";

    RTR(__LINE__, _packer.Init(0));
    RTR(__LINE__, _packer.AddInteger(intval));
    RTR(__LINE__, _packer.AddShort(shortval));
    RTR(__LINE__, _packer.AddByte(byteval));
    RTR(__LINE__, _packer.AddFloat(floatval));
    RTR(__LINE__, _packer.AddDouble(doubleval));
    RTR(__LINE__, _packer.AddInt64(int64val));
    RTR(__LINE__, _packer.AddString(strval, strlen(strval)));
    RTR(__LINE__, _packer.AddData(datval, strlen(datval)));
    RTR(__LINE__, _packer.AddLongString(lstrval, strlen(lstrval)));
    RTR(__LINE__, !_packer.GetDocsumBlob(&buf, &buflen));
}


void
MyApp::TestFailOrder()
{
    const char *buf;
    uint32_t buflen;

    uint32_t intval     = 4;
    uint16_t shortval   = 2;
    uint8_t  byteval    = 1;
    float    floatval   = 4.5;
    double   doubleval  = 8.75;
    uint64_t int64val   = 8;
    const char *strval  = "This is a string";
    const char *datval  = "This is data";
    const char *lstrval = "This is a long string";
    const char *ldatval = "This is long data";

    RTR(__LINE__, _packer.Init(0));
    RTR(__LINE__, _packer.AddInteger(intval));
    RTR(__LINE__, _packer.AddShort(shortval));
    RTR(__LINE__, !_packer.AddString(strval, strlen(strval)));
    RTR(__LINE__, !_packer.AddByte(byteval));
    RTR(__LINE__, !_packer.AddFloat(floatval));
    RTR(__LINE__, !_packer.AddDouble(doubleval));
    RTR(__LINE__, !_packer.AddInt64(int64val));
    RTR(__LINE__, !_packer.AddData(datval, strlen(datval)));
    RTR(__LINE__, !_packer.AddLongString(lstrval, strlen(lstrval)));
    RTR(__LINE__, !_packer.AddLongData(ldatval, strlen(ldatval)));
    RTR(__LINE__, !_packer.GetDocsumBlob(&buf, &buflen));
}



void
MyApp::TestBasicInplace()
{
    const char *buf;
    uint32_t buflen;

    const ResultClass *resClass;
    GeneralResult *gres;

    uint32_t intval     = 4;
    uint16_t shortval   = 2;
    uint8_t  byteval    = 1;
    float    floatval   = 4.5;
    double   doubleval  = 8.75;
    uint64_t int64val   = 8;
    const char *strval  = "This is a string";
    const char *datval  = "This is data";
    const char *lstrval = "This is a long string";
    const char *ldatval = "This is long data";

    RTR(__LINE__, _packer.Init(0));
    RTR(__LINE__, _packer.AddInteger(intval));
    RTR(__LINE__, _packer.AddShort(shortval));
    RTR(__LINE__, _packer.AddByte(byteval));
    RTR(__LINE__, _packer.AddFloat(floatval));
    RTR(__LINE__, _packer.AddDouble(doubleval));
    RTR(__LINE__, _packer.AddInt64(int64val));
    RTR(__LINE__, _packer.AddString(strval, strlen(strval)));
    RTR(__LINE__, _packer.AddData(datval, strlen(datval)));
    RTR(__LINE__, _packer.AddLongString(lstrval, strlen(lstrval)));
    RTR(__LINE__, _packer.AddLongData(ldatval, strlen(ldatval)));
    RTR(__LINE__, _packer.GetDocsumBlob(&buf, &buflen));

    resClass = _config.LookupResultClass(_config.GetClassID(buf, buflen));
    if (resClass == nullptr) {
        gres = nullptr;
    } else {
        DocsumStoreValue value(buf, buflen);
        gres = new GeneralResult(resClass);
        if (!gres->inplaceUnpack(value)) {
            delete gres;
            gres = nullptr;
        }
    }

    RTR(__LINE__, gres != nullptr);
    TestIntValue   (__LINE__, gres, "integer",    4);
    TestIntValue   (__LINE__, gres, "short",      2);
    TestIntValue   (__LINE__, gres, "byte",       1);
    TestDoubleValue(__LINE__, gres, "float",      floatval);
    TestDoubleValue(__LINE__, gres, "double",     doubleval);
    TestInt64Value (__LINE__, gres, "int64",      int64val);
    TestStringValue(__LINE__, gres, "string",     strval);
    TestDataValue  (__LINE__, gres, "data",       datval);
    TestStringValue(__LINE__, gres, "longstring", lstrval);
    TestDataValue  (__LINE__, gres, "longdata",   ldatval);
    RTR(__LINE__, (gres != nullptr &&
                   gres->GetClass()->GetNumEntries() == 10));
    RTR(__LINE__, (gres != nullptr &&
                   gres->GetClass()->GetClassID() == 0));
    delete gres;
}

int
MyApp::main(int, char **)
{
    _rc  = true;
    _cnt = 0;

    ResultClass *resClass;

    resClass = _config.AddResultClass("c0", 0);
    resClass->AddConfigEntry("integer",    RES_INT);
    resClass->AddConfigEntry("short",      RES_SHORT);
    resClass->AddConfigEntry("byte",       RES_BYTE);
    resClass->AddConfigEntry("float",      RES_FLOAT);
    resClass->AddConfigEntry("double",     RES_DOUBLE);
    resClass->AddConfigEntry("int64",      RES_INT64);
    resClass->AddConfigEntry("string",     RES_STRING);
    resClass->AddConfigEntry("data",       RES_DATA);
    resClass->AddConfigEntry("longstring", RES_LONG_STRING);
    resClass->AddConfigEntry("longdata",   RES_LONG_DATA);

    resClass = _config.AddResultClass("c1", 1);
    resClass->AddConfigEntry("text", RES_STRING);
    resClass->AddConfigEntry("data", RES_DATA);

    resClass = _config.AddResultClass("c2", 2);
    resClass->AddConfigEntry("text", RES_LONG_STRING);
    resClass->AddConfigEntry("data", RES_LONG_DATA);

    TestFailLong();
    TestFailShort();
    TestFailOrder();
    TestBasicInplace();

    LOG(info, "CONCLUSION: %s", (_rc) ? "SUCCESS" : "FAIL");
    return (_rc ? 0 : 1);
}

int main(int argc, char **argv) {
    vespalib::SignalHandler::PIPE.ignore();
    MyApp myapp;
    return myapp.main(argc, argv);
}