xapian-core  2.0.0
testutils.h
Go to the documentation of this file.
1 
4 /* Copyright 1999,2000,2001 BrightStation PLC
5  * Copyright 2002,2003,2007,2008,2015,2023 Olly Betts
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef XAPIAN_INCLUDED_TESTUTILS_H
23 #define XAPIAN_INCLUDED_TESTUTILS_H
24 
25 #include "testsuite.h"
26 #include <xapian.h>
27 
28 // ######################################################################
29 // Useful display operators
30 
31 std::ostream &operator<<(std::ostream &os,
32  const std::vector<Xapian::docid> &ints);
33 
34 // ######################################################################
35 // Useful comparison operators
36 
37 // Test that the weights and docids in two mset ranges are the same.
38 bool
39 mset_range_is_same(const Xapian::MSet &mset1, unsigned int first1,
40  const Xapian::MSet &mset2, unsigned int first2,
41  unsigned int count);
42 
43 // Test that the weights and docids in the mset and an array are the same.
44 bool
45 mset_range_is_same(const Xapian::MSet& mset, unsigned int first,
46  const std::pair<Xapian::docid, double> to_compare[],
47  unsigned int count);
48 
49 // Test that the weights in two mset ranges are the same, ignoring docids.
50 bool
51 mset_range_is_same_weights(const Xapian::MSet &mset1, unsigned int first1,
52  const Xapian::MSet &mset2, unsigned int first2,
53  unsigned int count);
54 
55 bool operator==(const Xapian::MSet &first, const Xapian::MSet &second);
56 
57 inline bool operator!=(const Xapian::MSet &first, const Xapian::MSet &second)
58 {
59  return !(first == second);
60 }
61 
62 void mset_expect_order(const Xapian::MSet &A,
63  Xapian::docid d1 = 0, Xapian::docid d2 = 0,
64  Xapian::docid d3 = 0, Xapian::docid d4 = 0,
65  Xapian::docid d5 = 0, Xapian::docid d6 = 0,
66  Xapian::docid d7 = 0, Xapian::docid d8 = 0,
67  Xapian::docid d9 = 0, Xapian::docid d10 = 0,
68  Xapian::docid d11 = 0, Xapian::docid d12 = 0);
69 
70 void test_mset_order_equal(const Xapian::MSet &mset1,
71  const Xapian::MSet &mset2);
72 
73 // ######################################################################
74 // Useful test macros
75 
77 #define TEST_MSET_SIZE(M, S) TEST_AND_EXPLAIN(((M).size() == (S)), \
78  "MSet '" STRINGIZE(M) "' is not of expected size: was '" << \
79  (M).size() << "' expected '" << (S) << "':\n" << \
80  "Full mset was:\n" << (M))
81 
83 #define TEST_EXCEPTION_(TYPE, CODE, EXACT, FAIL_TO_THROW_ACTION) \
84  do { \
85  expected_exception = STRINGIZE(TYPE); \
86  if (strncmp(expected_exception, "Xapian::", \
87  CONST_STRLEN("Xapian::")) == 0) { \
88  expected_exception += CONST_STRLEN("Xapian::"); \
89  } \
90  try { \
91  CODE; \
92  FAIL_TO_THROW_ACTION; \
93  } catch (const TYPE& xap_ex_obj_) { \
94  if (EXACT) { \
95  if (strcmp(expected_exception, xap_ex_obj_.get_type()) != 0) { \
96  FAIL_TEST("Caught subclass " << xap_ex_obj_.get_type() << \
97  " of expected " << expected_exception); \
98  } \
99  } \
100  } \
101  expected_exception = NULL;\
102  } while (0)
103 
104 #define DEFAULT_FAIL_TO_THROW_ACTION_ \
105  FAIL_TEST("Expected " << expected_exception << " not thrown")
106 
108 #define TEST_EXCEPTION_BASE_CLASS(TYPE, CODE) \
109  TEST_EXCEPTION_(TYPE, CODE, false, DEFAULT_FAIL_TO_THROW_ACTION_)
110 
112 #define TEST_EXCEPTION(TYPE, CODE) \
113  TEST_EXCEPTION_(TYPE, CODE, true, DEFAULT_FAIL_TO_THROW_ACTION_)
114 
119 #define TEST_EXCEPTION_BASE_CLASS3(TYPE, CODE, FAIL_TO_THROW_ACTION) \
120  TEST_EXCEPTION_(TYPE, CODE, false, FAIL_TO_THROW_ACTION)
121 
126 #define TEST_EXCEPTION3(TYPE, CODE, FAIL_TO_THROW_ACTION) \
127  TEST_EXCEPTION_(TYPE, CODE, true, FAIL_TO_THROW_ACTION)
128 
129 #endif // XAPIAN_INCLUDED_TESTUTILS_H
Definition: unittest.cc:650
Class representing a list of search results.
Definition: mset.h:46
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
a generic test suite engine
bool operator!=(const Xapian::MSet &first, const Xapian::MSet &second)
Definition: testutils.h:57
void mset_expect_order(const Xapian::MSet &A, Xapian::docid d1=0, Xapian::docid d2=0, Xapian::docid d3=0, Xapian::docid d4=0, Xapian::docid d5=0, Xapian::docid d6=0, Xapian::docid d7=0, Xapian::docid d8=0, Xapian::docid d9=0, Xapian::docid d10=0, Xapian::docid d11=0, Xapian::docid d12=0)
Definition: testutils.cc:224
bool operator==(const Xapian::MSet &first, const Xapian::MSet &second)
Definition: testutils.cc:140
std::ostream & operator<<(std::ostream &os, const std::vector< Xapian::docid > &ints)
Definition: testutils.cc:34
bool mset_range_is_same_weights(const Xapian::MSet &mset1, unsigned int first1, const Xapian::MSet &mset2, unsigned int first2, unsigned int count)
Definition: testutils.cc:110
bool mset_range_is_same(const Xapian::MSet &mset1, unsigned int first1, const Xapian::MSet &mset2, unsigned int first2, unsigned int count)
Definition: testutils.cc:45
void test_mset_order_equal(const Xapian::MSet &mset1, const Xapian::MSet &mset2)
Definition: testutils.cc:233
Public interfaces for the Xapian library.