File: internaltest.cc

package info (click to toggle)
xapian-core 1.4.3-2~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 21,224 kB
  • sloc: cpp: 113,806; ansic: 8,723; sh: 4,433; perl: 836; makefile: 567; tcl: 317; python: 40
file content (639 lines) | stat: -rw-r--r-- 16,266 bytes parent folder | download | duplicates (2)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/* internaltest.cc: test of the Xapian internals
 *
 * Copyright 1999,2000,2001 BrightStation PLC
 * Copyright 2002 Ananova Ltd
 * Copyright 2002,2003,2006,2007,2008,2009,2010,2011,2012,2015 Olly Betts
 * Copyright 2006 Lemur Consulting Ltd
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <config.h>

#include <xapian.h>

#include "safeerrno.h"

#include <iostream>
#include <string>

using namespace std;

#include "autoptr.h"
#include "testsuite.h"
#include "testutils.h"

#include "omassert.h"
#include "pack.h"
#include "str.h"

class Test_Exception {
    public:
	int value;
	Test_Exception(int value_) : value(value_) {}
};

// test that nested exceptions work correctly.
static bool test_exception1()
{
    try {
	try {
	    throw Test_Exception(1);
	} catch (...) {
	    try {
		throw Test_Exception(2);
	    } catch (...) {
	    }
	    throw;
	}
    } catch (const Test_Exception & e) {
	TEST_EQUAL(e.value, 1);
	return true;
    }
    return false;
}

// ###########################################
// # Tests of the reference counted pointers #
// ###########################################

class test_refcnt : public Xapian::Internal::intrusive_base {
    private:
	bool &deleted;
    public:
	test_refcnt(bool &deleted_) : deleted(deleted_) {
	    tout << "constructor\n";
	}
	Xapian::Internal::intrusive_ptr<const test_refcnt> test() {
	    return Xapian::Internal::intrusive_ptr<const test_refcnt>(this);
	}
	~test_refcnt() {
	    deleted = true;
	    tout << "destructor\n";
	}
};

static bool test_refcnt1()
{
    bool deleted = false;

    test_refcnt *p = new test_refcnt(deleted);

    TEST_EQUAL(p->_refs, 0);

    {
	Xapian::Internal::intrusive_ptr<test_refcnt> rcp(p);

	TEST_EQUAL(rcp->_refs, 1);

	{
	    Xapian::Internal::intrusive_ptr<test_refcnt> rcp2;
	    rcp2 = rcp;
	    TEST_EQUAL(rcp->_refs, 2);
	    // rcp2 goes out of scope here
	}

	TEST_AND_EXPLAIN(!deleted, "Object prematurely deleted!");
	TEST_EQUAL(rcp->_refs, 1);
	// rcp goes out of scope here
    }

    TEST_AND_EXPLAIN(deleted, "Object not properly deleted");

    return true;
}

// This is a regression test - our home-made equivalent of intrusive_ptr
// (which was called RefCntPtr) used to delete the object pointed to if you
// assigned it to itself and the reference count was 1.
static bool test_refcnt2()
{
    bool deleted = false;

    test_refcnt *p = new test_refcnt(deleted);

    Xapian::Internal::intrusive_ptr<test_refcnt> rcp(p);

    rcp = rcp;

    TEST_AND_EXPLAIN(!deleted, "Object deleted by self-assignment");

    return true;
}

// Class for testing AutoPtr<>.
class test_autoptr {
    bool &deleted;
  public:
    test_autoptr(bool &deleted_) : deleted(deleted_) {
	tout << "test_autoptr constructor\n";
    }
    ~test_autoptr() {
	deleted = true;
	tout << "test_autoptr destructor\n";
    }
};

// Test autoptr self-assignment.
static bool test_autoptr1()
{
    bool deleted = false;

    test_autoptr * raw_ptr = new test_autoptr(deleted);
    {
	AutoPtr<test_autoptr> ptr(raw_ptr);

	TEST_EQUAL(ptr.get(), raw_ptr);
	TEST(!deleted);

	ptr.reset(ptr.release());

	TEST_EQUAL(ptr.get(), raw_ptr);
	TEST(!deleted);

	ptr.swap(ptr);

	TEST_EQUAL(ptr.get(), raw_ptr);
	TEST(!deleted);

	swap(ptr, ptr);

	TEST_EQUAL(ptr.get(), raw_ptr);
	TEST(!deleted);
    }

    TEST(deleted);

    deleted = false;
    raw_ptr = new test_autoptr(deleted);

    bool deleted2 = false;
    test_autoptr * raw_ptr2 = new test_autoptr(deleted2);
    AutoPtr<test_autoptr> ptr(raw_ptr2);

    TEST_EQUAL(ptr.get(), raw_ptr2);
    TEST(!deleted);
    TEST(!deleted2);

    ptr.reset(raw_ptr);
    TEST_EQUAL(ptr.get(), raw_ptr);
    TEST(!deleted);
    TEST(deleted2);

    ptr.reset();
    TEST_EQUAL(ptr.get(), static_cast<test_autoptr*>(0));
    TEST(deleted);

    return true;
}

// test string comparisons
static bool test_stringcomp1()
{
    bool success = true;

    string s1;
    string s2;

    s1 = "foo";
    s2 = "foo";

    if ((s1 != s2) || (s1 > s2)) {
	success = false;
	tout << "String comparisons BADLY wrong" << endl;
    }

    s1 += '\0';

    if ((s1 == s2) || (s1 < s2)) {
	success = false;
	tout << "String comparisons don't cope with extra nulls" << endl;
    }

    s2 += '\0';

    s1 += 'a';
    s2 += 'z';

    if ((s1.length() != 5) || (s2.length() != 5)) {
	success = false;
	tout << "Lengths with added nulls wrong" << endl;
    }

    if ((s1 == s2) || !(s1 < s2)) {
	success = false;
	tout << "Characters after a null ignored in comparisons" << endl;
    }

    return success;
}

// By default Sun's C++ compiler doesn't call the destructor on a
// temporary object until the end of the block (contrary to what
// ISO C++ requires).  This is done in the name of "compatibility".
// Passing -features=tmplife to CC fixes this.  This check ensures
// that this actually works for Sun's C++ and any other compilers
// that might have this problem.
struct TempDtorTest {
    static int count;
    static TempDtorTest factory() { return TempDtorTest(); }
    TempDtorTest() { ++count; }
    ~TempDtorTest() { --count; }
};

int TempDtorTest::count = 0;

static bool test_temporarydtor1()
{
    TEST_EQUAL(TempDtorTest::count, 0);
    TempDtorTest::factory();
    TEST_EQUAL(TempDtorTest::count, 0);

    return true;
}

static bool test_static_assert1()
{
    // These tests aren't so useful now we're using C++11 static_assert(),
    // but it's not a bad idea to sanity check it.
    static_assert(true, "true");
    static_assert(1, "1");
    static_assert(-1, "-1");
    static_assert(42, "42");
    static_assert(sizeof(char) == 1, "sizeof(char) == 1");

    // FIXME: We should test cases which should fail, but these are hard to
    // check with our current test framework.

    STATIC_ASSERT_UNSIGNED_TYPE(bool);
    STATIC_ASSERT_UNSIGNED_TYPE(unsigned char);
    STATIC_ASSERT_UNSIGNED_TYPE(unsigned short);
    STATIC_ASSERT_UNSIGNED_TYPE(unsigned int);
    STATIC_ASSERT_UNSIGNED_TYPE(unsigned long);

    // FIXME: We should test cases which should fail, but these are hard to
    // check with our current test framework.

    STATIC_ASSERT_TYPE_DOMINATES(unsigned long, unsigned long);
    STATIC_ASSERT_TYPE_DOMINATES(unsigned int, unsigned int);
    STATIC_ASSERT_TYPE_DOMINATES(unsigned short, unsigned short);
    STATIC_ASSERT_TYPE_DOMINATES(unsigned char, unsigned char);

    STATIC_ASSERT_TYPE_DOMINATES(long, long);
    STATIC_ASSERT_TYPE_DOMINATES(int, int);
    STATIC_ASSERT_TYPE_DOMINATES(short, short);
    STATIC_ASSERT_TYPE_DOMINATES(signed char, signed char);

    STATIC_ASSERT_TYPE_DOMINATES(char, char);

    STATIC_ASSERT_TYPE_DOMINATES(unsigned long, unsigned int);
    STATIC_ASSERT_TYPE_DOMINATES(unsigned int, unsigned short);
    STATIC_ASSERT_TYPE_DOMINATES(unsigned short, unsigned char);

    STATIC_ASSERT_TYPE_DOMINATES(long, int);
    STATIC_ASSERT_TYPE_DOMINATES(int, short);
    STATIC_ASSERT_TYPE_DOMINATES(short, signed char);

    STATIC_ASSERT_TYPE_DOMINATES(long, unsigned char);
    STATIC_ASSERT_TYPE_DOMINATES(int, unsigned char);
    STATIC_ASSERT_TYPE_DOMINATES(short, unsigned char);

    // FIXME: We should test cases which should fail, but these are hard to
    // check with our current test framework.

    return true;
}

/// Test pack_uint_preserving_sort()
static bool test_pack_uint_preserving_sort1()
{
    string prev_packed;
    for (unsigned int i = 0; i != 1000; ++i) {
	string packed;
	pack_uint_preserving_sort(packed, i);
	const char * ptr = packed.data();
	const char * end = ptr + packed.size();
	unsigned int result;
	TEST(unpack_uint_preserving_sort(&ptr, end, &result));
	TEST_EQUAL(result, i);
	TEST(ptr == end);
	TEST_REL(prev_packed, <, packed);
	swap(prev_packed, packed);
    }
    for (unsigned int i = 2345; i < 65000; i += 113) {
	string packed;
	pack_uint_preserving_sort(packed, i);
	const char * ptr = packed.data();
	const char * end = ptr + packed.size();
	unsigned int result;
	TEST(unpack_uint_preserving_sort(&ptr, end, &result));
	TEST_EQUAL(result, i);
	TEST(ptr == end);
	TEST_REL(prev_packed, <, packed);
	swap(prev_packed, packed);
    }
    unsigned int prev = 64999;
    for (unsigned int i = 65000; i > prev; prev = i, i = (i << 1) ^ 1337) {
	string packed;
	pack_uint_preserving_sort(packed, i);
	const char * ptr = packed.data();
	const char * end = ptr + packed.size();
	unsigned int result;
	TEST(unpack_uint_preserving_sort(&ptr, end, &result));
	TEST_EQUAL(result, i);
	TEST(ptr == end);
	TEST_REL(prev_packed, <, packed);
	swap(prev_packed, packed);
    }

    /* Test packing multiple numbers to one string. */
    string packed;
    for (unsigned int i = 23456; i < 765432; i += 1131) {
	pack_uint_preserving_sort(packed, i);
    }
    const char * ptr = packed.data();
    const char * end = ptr + packed.size();
    for (unsigned int i = 23456; i < 765432; i += 1131) {
	unsigned int result;
	TEST(unpack_uint_preserving_sort(&ptr, end, &result));
	TEST_EQUAL(result, i);
    }
    TEST(ptr == end);

    return true;
}

/// Test C_pack_uint_preserving_sort()
static bool test_pack_uint_preserving_sort2()
{
    string prev_packed;
    for (unsigned int i = 0; i != 1000; ++i) {
	string packed;
	C_pack_uint_preserving_sort(packed, i);
	const char * ptr = packed.data();
	const char * end = ptr + packed.size();
	unsigned int result;
	TEST(C_unpack_uint_preserving_sort(&ptr, end, &result));
	TEST_EQUAL(result, i);
	TEST(ptr == end);
	TEST_REL(prev_packed, <, packed);
	swap(prev_packed, packed);
    }
    for (unsigned int i = 2345; i < 65000; i += 113) {
	string packed;
	C_pack_uint_preserving_sort(packed, i);
	const char * ptr = packed.data();
	const char * end = ptr + packed.size();
	unsigned int result;
	TEST(C_unpack_uint_preserving_sort(&ptr, end, &result));
	TEST_EQUAL(result, i);
	TEST(ptr == end);
	TEST_REL(prev_packed, <, packed);
	swap(prev_packed, packed);
    }
    unsigned int prev = 64999;
    for (unsigned int i = 65000; i > prev; prev = i, i = (i << 1) ^ 1337) {
	string packed;
	C_pack_uint_preserving_sort(packed, i);
	const char * ptr = packed.data();
	const char * end = ptr + packed.size();
	unsigned int result;
	TEST(C_unpack_uint_preserving_sort(&ptr, end, &result));
	TEST_EQUAL(result, i);
	TEST(ptr == end);
	TEST_REL(prev_packed, <, packed);
	swap(prev_packed, packed);
    }

    /* Test packing multiple numbers to one string. */
    string packed;
    for (unsigned int i = 23456; i < 765432; i += 1131) {
	C_pack_uint_preserving_sort(packed, i);
    }
    const char * ptr = packed.data();
    const char * end = ptr + packed.size();
    for (unsigned int i = 23456; i < 765432; i += 1131) {
	unsigned int result;
	TEST(C_unpack_uint_preserving_sort(&ptr, end, &result));
	TEST_EQUAL(result, i);
    }
    TEST(ptr == end);

    return true;
}

/// Test C_isupper() etc.
static bool test_chartype1()
{
    char tested[128];
    memset(tested, 0, sizeof(tested));
    for (int ch = '0'; ch != '9' + 1; ++ch) {
	tested[ch] = 1;
	TEST(!C_isupper(ch));
	TEST(!C_islower(ch));
	TEST(!C_isalpha(ch));
	TEST(C_isalnum(ch));
	TEST(C_isdigit(ch));
	TEST(C_isxdigit(ch));
	TEST(!C_isspace(ch));
	TEST(C_isnotupper(ch));
	TEST(C_isnotlower(ch));
	TEST(C_isnotalpha(ch));
	TEST(!C_isnotalnum(ch));
	TEST(!C_isnotdigit(ch));
	TEST(!C_isnotxdigit(ch));
	TEST(C_isnotspace(ch));
	TEST_EQUAL(hex_digit(ch), ch - '0');
    }

    for (int ch = 'A'; ch != 'F' + 1; ++ch) {
	tested[ch] = 1;
	TEST(C_isupper(ch));
	TEST(!C_islower(ch));
	TEST(C_isalpha(ch));
	TEST(C_isalnum(ch));
	TEST(!C_isdigit(ch));
	TEST(C_isxdigit(ch));
	TEST(!C_isspace(ch));
	TEST(!C_isnotupper(ch));
	TEST(C_isnotlower(ch));
	TEST(!C_isnotalpha(ch));
	TEST(!C_isnotalnum(ch));
	TEST(C_isnotdigit(ch));
	TEST(!C_isnotxdigit(ch));
	TEST(C_isnotspace(ch));
	TEST_EQUAL(hex_digit(ch), ch - 'A' + 10);
    }

    for (int ch = 'G'; ch != 'Z' + 1; ++ch) {
	tested[ch] = 1;
	TEST(C_isupper(ch));
	TEST(!C_islower(ch));
	TEST(C_isalpha(ch));
	TEST(C_isalnum(ch));
	TEST(!C_isdigit(ch));
	TEST(!C_isxdigit(ch));
	TEST(!C_isspace(ch));
	TEST(!C_isnotupper(ch));
	TEST(C_isnotlower(ch));
	TEST(!C_isnotalpha(ch));
	TEST(!C_isnotalnum(ch));
	TEST(C_isnotdigit(ch));
	TEST(C_isnotxdigit(ch));
	TEST(C_isnotspace(ch));
    }

    for (int ch = 'a'; ch != 'f' + 1; ++ch) {
	tested[ch] = 1;
	TEST(!C_isupper(ch));
	TEST(C_islower(ch));
	TEST(C_isalpha(ch));
	TEST(C_isalnum(ch));
	TEST(!C_isdigit(ch));
	TEST(C_isxdigit(ch));
	TEST(!C_isspace(ch));
	TEST(C_isnotupper(ch));
	TEST(!C_isnotlower(ch));
	TEST(!C_isnotalpha(ch));
	TEST(!C_isnotalnum(ch));
	TEST(C_isnotdigit(ch));
	TEST(!C_isnotxdigit(ch));
	TEST(C_isnotspace(ch));
	TEST_EQUAL(hex_digit(ch), ch - 'a' + 10);
    }

    for (int ch = 'g'; ch != 'z' + 1; ++ch) {
	tested[ch] = 1;
	TEST(!C_isupper(ch));
	TEST(C_islower(ch));
	TEST(C_isalpha(ch));
	TEST(C_isalnum(ch));
	TEST(!C_isdigit(ch));
	TEST(!C_isxdigit(ch));
	TEST(!C_isspace(ch));
	TEST(C_isnotupper(ch));
	TEST(!C_isnotlower(ch));
	TEST(!C_isnotalpha(ch));
	TEST(!C_isnotalnum(ch));
	TEST(C_isnotdigit(ch));
	TEST(C_isnotxdigit(ch));
	TEST(C_isnotspace(ch));
    }

    for (const char *p = "\t\n\f\r "; *p; ++p) {
	int ch = *p;
	tested[ch] = 1;
	TEST(!C_isupper(ch));
	TEST(!C_islower(ch));
	TEST(!C_isalpha(ch));
	TEST(!C_isalnum(ch));
	TEST(!C_isdigit(ch));
	TEST(!C_isxdigit(ch));
	TEST(C_isspace(ch));
	TEST(C_isnotupper(ch));
	TEST(C_isnotlower(ch));
	TEST(C_isnotalpha(ch));
	TEST(C_isnotalnum(ch));
	TEST(C_isnotdigit(ch));
	TEST(C_isnotxdigit(ch));
	TEST(!C_isnotspace(ch));
    }

    // Check remaining non-top-bit-set characters aren't anything.
    for (int ch = 0; ch != 128; ++ch) {
	if (tested[ch]) continue;
	TEST(!C_isupper(ch));
	TEST(!C_islower(ch));
	TEST(!C_isalpha(ch));
	TEST(!C_isalnum(ch));
	TEST(!C_isdigit(ch));
	TEST(!C_isxdigit(ch));
	TEST(!C_isspace(ch));
	TEST(C_isnotupper(ch));
	TEST(C_isnotlower(ch));
	TEST(C_isnotalpha(ch));
	TEST(C_isnotalnum(ch));
	TEST(C_isnotdigit(ch));
	TEST(C_isnotxdigit(ch));
	TEST(C_isnotspace(ch));
    }

    // Non-ASCII characters aren't anything for these functions.
    for (int ch = 128; ch != 256; ++ch) {
	TEST(!C_isupper(ch));
	TEST(!C_islower(ch));
	TEST(!C_isalpha(ch));
	TEST(!C_isalnum(ch));
	TEST(!C_isdigit(ch));
	TEST(!C_isxdigit(ch));
	TEST(!C_isspace(ch));
	TEST(C_isnotupper(ch));
	TEST(C_isnotlower(ch));
	TEST(C_isnotalpha(ch));
	TEST(C_isnotalnum(ch));
	TEST(C_isnotdigit(ch));
	TEST(C_isnotxdigit(ch));
	TEST(C_isnotspace(ch));
    }

    // Check signed char values work the same way.
    for (int ch = -128; ch != 0; ++ch) {
	TEST(!C_isupper(ch));
	TEST(!C_islower(ch));
	TEST(!C_isalpha(ch));
	TEST(!C_isalnum(ch));
	TEST(!C_isdigit(ch));
	TEST(!C_isxdigit(ch));
	TEST(!C_isspace(ch));
	TEST(C_isnotupper(ch));
	TEST(C_isnotlower(ch));
	TEST(C_isnotalpha(ch));
	TEST(C_isnotalnum(ch));
	TEST(C_isnotdigit(ch));
	TEST(C_isnotxdigit(ch));
	TEST(C_isnotspace(ch));
    }

    return true;
}

// ##################################################################
// # End of actual tests					    #
// ##################################################################

/// The lists of tests to perform
static const test_desc tests[] = {
    TESTCASE(exception1),
    TESTCASE(refcnt1),
    TESTCASE(refcnt2),
    TESTCASE(autoptr1),
    TESTCASE(stringcomp1),
    TESTCASE(temporarydtor1),
    TESTCASE(static_assert1),
    TESTCASE(pack_uint_preserving_sort1),
    TESTCASE(pack_uint_preserving_sort2),
    TESTCASE(chartype1),
    {0, 0}
};

int main(int argc, char **argv)
try {
    test_driver::parse_command_line(argc, argv);
    return test_driver::run(tests);
} catch (const char * e) {
    cout << e << endl;
    return 1;
}