Latest of libtest.
[awesomized/libmemcached] / libtest / comparison.hpp
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #pragma once
23
24 #include <typeinfo>
25
26 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
27 #include <libmemcached-1.0/memcached.h>
28 #include <libmemcachedutil-1.0/ostream.hpp>
29 #endif
30
31 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
32 #include <libgearman-1.0/ostream.hpp>
33 #endif
34
35 namespace libtest {
36
37 LIBTEST_API
38 bool gdb_is_caller(void);
39
40 LIBTEST_API
41 bool valgrind_is_caller(void);
42
43 LIBTEST_API
44 bool _in_valgrind(const char *file, int line, const char *func);
45
46 template <class T_comparable, class T_hint>
47 bool _compare_truth_hint(const char *file, int line, const char *func, T_comparable __expected, const char *assertation_label, T_hint __hint)
48 {
49 if (__expected == false)
50 {
51 libtest::stream::make_cerr(file, line, func) << "Assertation \"" << assertation_label << "\" failed, hint: " << __hint;
52 return false;
53 }
54
55 return true;
56 }
57
58 template <class T1_comparable, class T2_comparable>
59 bool _compare(const char *file, int line, const char *func, const T1_comparable& __expected, const T2_comparable& __actual, bool use_io)
60 {
61 if (__expected != __actual)
62 {
63 if (use_io)
64 {
65 libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\"";
66 }
67
68 return false;
69 }
70
71 return true;
72 }
73
74 template <class T_comparable>
75 bool _compare_zero(const char *file, int line, const char *func, T_comparable __actual)
76 {
77 if (T_comparable(0) != __actual)
78 {
79 libtest::stream::make_cerr(file, line, func) << "Expected 0 got \"" << __actual << "\"";
80 return false;
81 }
82
83 return true;
84 }
85
86 template <class T_comparable>
87 bool _truth(const char *file, int line, const char *func, T_comparable __truth)
88 {
89 if (bool(__truth))
90 {
91 libtest::stream::make_cerr(file, line, func) << "Assertion failed for " << func << "() with \"" << __truth << "\"";
92 return false;
93 }
94
95 return true;
96 }
97
98 template <class T1_comparable, class T2_comparable, class T_hint>
99 bool _compare_hint(const char *file, int line, const char *func, T1_comparable __expected, T2_comparable __actual, T_hint __hint, bool io_error= true)
100 {
101 if (__expected != __actual)
102 {
103 if (io_error)
104 {
105 libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\"" << " Additionally: \"" << __hint << "\"";
106 }
107
108 return false;
109 }
110
111 return true;
112 }
113
114 } // namespace libtest