37 #define XAPIAN_UNITTEST 39 #define UNITTEST_CHECK_EXCEPTION \ 40 if (unittest_assertion_failed) { \ 41 const char * unittest_assertion_failed_ = unittest_assertion_failed;\ 42 unittest_assertion_failed = NULL;\ 43 throw unittest_assertion_failed_;\ 50 #define UNITTEST_ASSERT_LOCATION__(LINE,MSG) __FILE__":"#LINE": "#MSG 51 #define UNITTEST_ASSERT_LOCATION_(LINE,MSG) UNITTEST_ASSERT_LOCATION__(LINE,MSG) 52 #define UNITTEST_ASSERT_LOCATION(MSG) UNITTEST_ASSERT_LOCATION_(__LINE__,MSG) 53 #define UNITTEST_ASSERT_NOTHROW(COND, RET) \ 56 unittest_assertion_failed = UNITTEST_ASSERT_LOCATION(COND);\ 62 #include "../common/stringutils.h" 63 #include "../common/log2.h" 66 #define TEST_EXCEPTION(TYPE, CODE) \ 70 UNITTEST_CHECK_EXCEPTION \ 71 FAIL_TEST("Expected exception "#TYPE" not thrown"); \ 72 } catch (const TYPE &) { \ 77 #include "../common/closefrom.cc" 78 #include "../common/errno_to_string.cc" 79 #include "../common/fileutils.cc" 80 #include "../common/overflow.h" 81 #include "../common/parseint.h" 82 #include "../common/serialise-double.cc" 83 #include "../common/str.cc" 84 #include "../backends/uuids.cc" 85 #include "../net/length.cc" 86 #include "../net/serialise-error.cc" 87 #include "../api/error.cc" 88 #include "../api/sortable-serialise.cc" 91 #include "../common/msvc_dirent.cc" 94 #include "../api/constinfo.cc" 170 TEST_EQUAL(
r_r_p(
"C:rel/a/tive",
"c:\\foo\\bar"),
"C:\\foo\\rel/a/tive");
177 TEST_EQUAL(
r_r_p(
"rel/a/tive",
"\\\\SRV\\VOL\\DIR\\FILE"),
"\\\\SRV\\VOL\\DIR\\rel/a/tive");
178 TEST_EQUAL(
r_r_p(
"/abs/o/lute",
"\\\\SRV\\VOL\\FILE"),
"\\\\SRV\\VOL/abs/o/lute");
179 TEST_EQUAL(
r_r_p(
"/abs/o/lute",
"\\\\S\\V\\FILE"),
"\\\\S\\V/abs/o/lute");
183 TEST_EQUAL(
r_r_p(
"rel/a/tive",
"//SRV/VOL/DIR/FILE"),
"//SRV/VOL/DIR/rel/a/tive");
184 TEST_EQUAL(
r_r_p(
"/abs/o/lute",
"//SRV/VOL/FILE"),
"//SRV/VOL/abs/o/lute");
188 TEST_EQUAL(
r_r_p(
"/abs/o/lute",
"\\\\?\\C:\\wibble"),
"\\\\?\\C:\\abs\\o\\lute");
189 TEST_EQUAL(
r_r_p(
"/abs/o/lute",
"\\\\?\\UNC\\S\\V"),
"\\\\?\\UNC\\S\\V\\abs\\o\\lute");
190 TEST_EQUAL(
r_r_p(
"/abs/o/lute",
"\\\\?\\UNC\\S\\V\\"),
"\\\\?\\UNC\\S\\V\\abs\\o\\lute");
191 TEST_EQUAL(
r_r_p(
"/abs/o/lute",
"\\\\?\\UNC\\S\\V\\TMP\\README.TXT"),
"\\\\?\\UNC\\S\\V\\abs\\o\\lute");
192 TEST_EQUAL(
r_r_p(
"r/elativ/e",
"\\\\?\\C:\\wibble"),
"\\\\?\\C:\\r\\elativ\\e");
193 TEST_EQUAL(
r_r_p(
"r/elativ/e",
"\\\\?\\C:\\wibble\\wobble"),
"\\\\?\\C:\\wibble\\r\\elativ\\e");
194 #if 0 // Is this a valid testcase? It fails, but isn't relevant to Xapian. 195 TEST_EQUAL(
r_r_p(
"r/elativ/e",
"\\\\?\\UNC\\S\\V"),
"\\\\?\\UNC\\S\\V\\r\\elativ\\e");
197 TEST_EQUAL(
r_r_p(
"r/elativ/e",
"\\\\?\\UNC\\S\\V\\"),
"\\\\?\\UNC\\S\\V\\r\\elativ\\e");
198 TEST_EQUAL(
r_r_p(
"r/elativ/e",
"\\\\?\\UNC\\S\\V\\TMP\\README.TXT"),
"\\\\?\\UNC\\S\\V\\TMP\\r\\elativ\\e");
212 TEST(encoded.size() <
sizeof(buf));
213 memcpy(buf, encoded.data(), encoded.size());
217 const char * ptr[3] = { NULL, buf, NULL };
218 const char * end = ptr[1] + encoded.size();
220 if (ptr[1] != end || u != v) {
221 cout << u <<
" -> " << v <<
", difference = " << v - u << endl;
222 cout <<
"FLT_RADIX = " << FLT_RADIX << endl;
223 cout <<
"DBL_MAX_EXP = " << DBL_MAX_EXP << endl;
225 TEST_EQUAL(static_cast<const void*>(ptr[1]), static_cast<const void*>(end));
230 static const double test_values[] = {
250 for (p = test_values; p < test_values +
sizeof(test_values) /
sizeof(
double); ++p) {
259 #ifdef XAPIAN_HAS_REMOTE_BACKEND 264 while (n < 0xff000000) {
266 const char *p = s.data();
267 const char *p_end = p + s.size();
270 if (n != decoded_n || p != p_end)
tout <<
"[" << s <<
"]" << endl;
288 const char *p = s.data();
289 const char *p_end = p + s.size();
297 const char *p = s.data();
298 const char *p_end = p + s.size();
309 const char *p = s.data();
310 const char *p_end = p + s.size();
317 const char *p = s.data();
318 const char *p_end = p + s.size();
326 const char *p = s.data();
327 const char *p_end = p + s.size();
335 for (
size_t n = 2; n < 1000; n = (n + 1) * 2 + (n >> 1)) {
338 const char *p = s.data();
339 const char *p_end = p + s.size();
344 s.append(n - 1,
'x');
346 const char *p = s.data();
347 const char *p_end = p + s.size();
354 const char *p = s.data();
355 const char *p_end = p + s.size();
363 const char *p = s.data();
364 const char *p_end = p + s.size();
426 -3.14159265358979323846,
458 3.14159265358979323846,
477 bool started =
false;
480 tout <<
"Number: " << num <<
'\n';
482 tout <<
"String: " << str <<
'\n';
489 }
else if (prevnum > num) {
495 }
else if (prevstr > str) {
500 "Numbers " << prevnum <<
" and " << num <<
501 " don't sort the same way as their string " 513 const S max_val = numeric_limits<S>::max();
514 const S min_val = numeric_limits<S>::min();
515 tout <<
"Testing with tostring_helper" << endl;
516 std::ostringstream oss;
517 oss << (
long long)max_val;
522 oss << (
long long)min_val;
549 tostring_helper<char>();
550 tostring_helper<short>();
551 tostring_helper<int>();
552 tostring_helper<long>();
553 tostring_helper<long long>();
610 for (
int i = 0; i != 8; ++i) {
611 unsigned char ch = str[i];
616 for (
int i = 9; i != 13; ++i) {
617 unsigned char ch = str[i];
622 for (
int i = 14; i != 18; ++i) {
623 unsigned char ch = str[i];
628 for (
int i = 19; i != 23; ++i) {
629 unsigned char ch = str[i];
634 for (
int i = 24; i != 36; ++i) {
635 unsigned char ch = str[i];
664 TEST_EQUAL(str,
"00000000-0000-0000-0000-000000000000");
675 explicit A(
int x_) : x(x_) {}
686 B(
int x_,
bool & alive_) : x(x_), alive(alive_) {
699 opt_intrusive_base::release();
725 B * b1 =
new B{5, alive};
748 const auto ulong_max = numeric_limits<unsigned long>::max();
749 const auto uint_max = numeric_limits<unsigned int>::max();
750 const auto ushort_max = numeric_limits<unsigned short>::max();
751 const auto uchar_max = numeric_limits<unsigned char>::max();
753 unsigned long res_ulong;
755 unsigned short res_ushort;
756 unsigned char res_uchar;
845 const U max_val = numeric_limits<U>::max();
846 tout <<
"Testing with parseunsigned_helper" << endl;
858 if (max_val + 1ull != 0)
864 parseunsigned_helper<unsigned char>();
865 parseunsigned_helper<unsigned short>();
866 parseunsigned_helper<unsigned>();
867 parseunsigned_helper<unsigned long>();
868 parseunsigned_helper<unsigned long long>();
874 const S max_val = numeric_limits<S>::max();
875 const S min_val = numeric_limits<S>::min();
876 tout <<
"Testing with parsesigned_helper" << endl;
894 unsigned long long one_too_large = max_val + 1ull;
897 unsigned long long one_too_small_negated = 1ull - min_val;
903 parsesigned_helper<signed char>();
904 parsesigned_helper<short>();
905 parsesigned_helper<int>();
906 parsesigned_helper<long>();
907 parsesigned_helper<long long>();
915 #ifdef XAPIAN_HAS_REMOTE_BACKEND 935 int main(
int argc,
char **argv)
939 }
catch (
const char * e) {
static void test_serialiseerror1()
static void test_serialiselength2()
std::enable_if< std::is_unsigned< T1 >::value &&std::is_unsigned< T2 >::value &&std::is_unsigned< R >::value, bool >::type add_overflows(T1 a, T2 b, R &res)
Addition with overflow checking.
static void test_parsesigned1()
static void check_double_serialisation(double u)
#define TEST(a)
Test a condition, without an additional explanation for failure.
static void parse_command_line(int argc, char **argv)
Parse the command line arguments.
DatabaseOpeningError indicates failure to open a database.
static void parseunsigned_helper()
#define TEST_AND_EXPLAIN(a, b)
Test a condition, and display the test with an extra explanation if the condition fails...
static void test_suboverflows1()
a generic test suite engine
string serialise_error(const Xapian::Error &e)
Serialise a Xapian::Error object to a string.
std::string sortable_serialise(double value)
Convert a floating point number to a string, preserving sort order.
std::string encode_length(T len)
Encode a length as a variable-length string.
static string r_r_p(string a, const string &b)
static void parsesigned_helper()
static void test_serialiselength1()
void unserialise_error(const string &serialised_error, const string &prefix, const string &new_context)
Unserialise a Xapian::Error object and throw it.
#define TEST_REL(A, REL, B)
Test a relation holds,e.g. TEST_REL(a,>,b);.
static const char * unittest_assertion_failed
#define TEST_NOT_EQUAL(a, b)
Test for non-equality of two things.
double unserialise_double(const char **p, const char *end)
Unserialise a double serialised by serialise_double.
void description_append(std::string &desc, const std::string &s)
std::ostringstream tout
The debug printing stream.
void errno_to_string(int e, string &s)
#define TEST_EXCEPTION(TYPE, CODE)
static void test_parseunsigned1()
static void test_tostring1()
string str(int value)
Convert int to std::string.
std::string to_string() const
static void test_sortableserialise1()
void parse(const char *in)
Base class for objects managed by intrusive_ptr.
Base class for objects managed by opt_intrusive_ptr.
static void test_muloverflows1()
double sortable_unserialise(const std::string &serialised)
Convert a string encoded using sortable_serialise back to a floating point number.
std::string get_description() const
Return a string describing this object.
static void test_closefrom1()
void resolve_relative_path(string &path, const string &base)
Resolve path relative to base.
std::string serialise_double(double v)
Serialise a double to a string.
void decode_length_and_check(const char **p, const char *end, unsigned &out)
Decode a length encoded by encode_length.
static void test_movesupport1()
static void tostring_helper()
const char * get_error_string() const
Returns any system error string associated with this exception.
static constexpr unsigned BINARY_SIZE
The size of a UUID in bytes.
bool parse_signed(const char *p, T &res)
All exceptions thrown by Xapian are subclasses of Xapian::Error.
static const double test_sortableserialise_numbers[]
std::enable_if< std::is_unsigned< T1 >::value &&std::is_unsigned< T2 >::value &&std::is_unsigned< R >::value, bool >::type sub_overflows(T1 a, T2 b, R &res)
Subtraction with overflow checking.
#define TEST_STRINGS_EQUAL(a, b)
Test for equality of two strings.
static void test_strbool1()
Regression test for bug fixed in 1.1.1.
static int run(const test_desc *tests)
std::enable_if< std::is_unsigned< T1 >::value &&std::is_unsigned< T2 >::value &&std::is_unsigned< R >::value, bool >::type mul_overflows(T1 a, T2 b, R &res)
Multiplication with overflow checking.
<unistd.h>, but with compat.
static const test_desc tests[]
int main(int argc, char **argv)
static void test_addoverflows1()
const char * data() const
Structure holding a description of a test.
bool parse_unsigned(const char *p, T &res)
#define TEST_EQUAL(a, b)
Test for equality of two things.
A smart pointer that optionally uses intrusive reference counting.
A smart pointer that uses intrusive reference counting.
void decode_length(const char **p, const char *end, unsigned &out)
Decode a length encoded by encode_length.
DEFINE_TESTCASE_(simple_exceptions_work1)