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