Lion fixes, distcheck fix:
[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/memcached.h>
28 #endif
29
30 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
31 #include <libgearman/gearman.h>
32 #endif
33
34 namespace libtest {
35
36 template <class T_comparable, class T_hint>
37 bool _compare_truth_hint(const char *file, int line, const char *func, T_comparable __expected, const char *assertation_label, T_hint __hint)
38 {
39 if (__expected == false)
40 {
41 libtest::stream::make_cerr(file, line, func) << "Assertation \"" << assertation_label << "\" failed, hint: " << __hint;
42 return false;
43 }
44
45 return true;
46 }
47
48 template <class T_comparable>
49 bool _compare(const char *file, int line, const char *func, const T_comparable __expected, const T_comparable __actual)
50 {
51 if (__expected != __actual)
52 {
53 if (typeid(__expected) == typeid(test_return_t))
54 {
55 const char *expected_str= test_strerror(test_return_t(__expected));
56 const char *got_str= test_strerror(test_return_t(__actual));
57
58 libtest::stream::make_cerr(file, line, func) << "Expected \""
59 << expected_str
60 << "\" got \""
61 << got_str
62 << "\"";
63 }
64 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
65 else if (typeid(__expected) == typeid(memcached_return_t))
66 {
67 libtest::stream::make_cerr(file, line, func) << "Expected \""
68 << memcached_strerror(NULL, memcached_return_t(__expected))
69 << "\" got \""
70 << memcached_strerror(NULL, memcached_return_t(__actual)) << "\"";
71 }
72 #endif
73 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
74 else if (typeid(__expected) == typeid(gearman_return_t))
75 {
76 libtest::stream::make_cerr(file, line, func) << "Expected \""
77 << gearman_strerror(gearman_return_t(__expected))
78 << "\" got \""
79 << gearman_strerror(gearman_return_t(__actual)) << "\"";
80 }
81 #endif
82 else
83 {
84 libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\"";
85 }
86 return false;
87 }
88
89 return true;
90 }
91
92 template <class T_comparable>
93 bool _compare_zero(const char *file, int line, const char *func, T_comparable __actual)
94 {
95 if (T_comparable(0) != __actual)
96 {
97 libtest::stream::make_cerr(file, line, func) << "Expected 0 got \"" << __actual << "\"";
98 return false;
99 }
100
101 return true;
102 }
103
104 template <class T_comparable>
105 bool _truth(const char *file, int line, const char *func, T_comparable __truth)
106 {
107 if (bool(__truth))
108 {
109 libtest::stream::make_cerr(file, line, func) << "Assertion failed for " << func << "() with \"" << __truth << "\"";
110 return false;
111 }
112
113 return true;
114 }
115
116 template <class T_comparable, class T_hint>
117 bool _compare_hint(const char *file, int line, const char *func, T_comparable __expected, T_comparable __actual, T_hint __hint)
118 {
119 if (__expected != __actual)
120 {
121 if (typeid(__expected) == typeid(test_return_t))
122 {
123 const char *expected_str= test_strerror(test_return_t(__expected));
124 const char *got_str= test_strerror(test_return_t(__actual));
125
126 libtest::stream::make_cerr(file, line, func) << "Expected \""
127 << expected_str
128 << "\" got \""
129 << got_str
130 << "\""
131 << " Additionally: \"" << __hint << "\"";
132 }
133 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
134 else if (typeid(__expected) == typeid(memcached_return_t))
135 {
136 libtest::stream::make_cerr(file, line, func) << "Expected \""
137 << memcached_strerror(NULL, memcached_return_t(__expected))
138 << "\" got \""
139 << memcached_strerror(NULL, memcached_return_t(__actual)) << "\""
140 << " Additionally: \"" << __hint << "\"";
141 }
142 #endif
143 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
144 else if (typeid(__expected) == typeid(gearman_return_t))
145 {
146 libtest::stream::make_cerr(file, line, func) << "Expected \""
147 << gearman_strerror(gearman_return_t(__expected))
148 << "\" got \""
149 << gearman_strerror(gearman_return_t(__actual)) << "\""
150 << " Additionally: \"" << __hint << "\"";
151 }
152 #endif
153 else
154 {
155 libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\""
156 << " Additionally: \"" << __hint << "\"";
157 }
158 return false;
159 }
160
161 return true;
162 }
163
164 } // namespace libtest