Merge in basic unittest for test framework.
[awesomized/libmemcached] / libtest / test.h
1 /*
2 * uTest Copyright (C) 2011 Data Differential, http://datadifferential.com/
3 *
4 * Use and distribution licensed under the BSD license. See
5 * the COPYING file in the parent directory for full text.
6 */
7
8 #pragma once
9
10 #ifndef __INTEL_COMPILER
11 #pragma GCC diagnostic ignored "-Wold-style-cast"
12 #endif
13
14 /**
15 A structure describing the test case.
16 */
17 struct test_st {
18 const char *name;
19 bool requires_flush;
20 test_callback_fn *test_fn;
21 };
22
23 LIBTEST_API
24 bool test_is_local(void);
25
26 #define test_assert_errno(A) \
27 do \
28 { \
29 if ((A)) { \
30 fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
31 perror(#A); \
32 fprintf(stderr, "\n"); \
33 create_core(); \
34 assert((A)); \
35 } \
36 } while (0)
37
38 #define test_assert(A, B) \
39 do \
40 { \
41 if ((A)) { \
42 fprintf(stderr, "\n%s:%d: Assertion failed %s, with message %s, in %s", __FILE__, __LINE__, (B), #A, __func__ );\
43 fprintf(stderr, "\n"); \
44 create_core(); \
45 assert((A)); \
46 } \
47 } while (0)
48
49 #define test_truth(A) \
50 do \
51 { \
52 if (! (A)) { \
53 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
54 create_core(); \
55 return TEST_FAILURE; \
56 } \
57 } while (0)
58
59 #define test_true(A) \
60 do \
61 { \
62 if (! (A)) { \
63 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
64 create_core(); \
65 return TEST_FAILURE; \
66 } \
67 } while (0)
68
69 #define test_true_got(A,B) \
70 do \
71 { \
72 if (! (A)) { \
73 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, received \"%s\"\n", __FILE__, __LINE__, #A, (B));\
74 create_core(); \
75 return TEST_FAILURE; \
76 } \
77 } while (0)
78
79 #define test_skip(A,B) \
80 do \
81 { \
82 if ((A) != (B)) \
83 { \
84 return TEST_SKIPPED; \
85 } \
86 } while (0)
87
88 #define test_fail(A) \
89 do \
90 { \
91 if (1) { \
92 fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
93 create_core(); \
94 return TEST_FAILURE; \
95 } \
96 } while (0)
97
98
99 #define test_false(A) \
100 do \
101 { \
102 if ((A)) { \
103 fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
104 create_core(); \
105 return TEST_FAILURE; \
106 } \
107 } while (0)
108
109 #define test_false_with(A,B) \
110 do \
111 { \
112 if ((A)) { \
113 fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
114 create_core(); \
115 return TEST_FAILURE; \
116 } \
117 } while (0)
118
119
120 #define test_compare(A,B) \
121 do \
122 { \
123 if ((A) != (B)) \
124 { \
125 fprintf(stderr, "\n%s:%d: Expected %s, got %lu\n", __FILE__, __LINE__, #A, (unsigned long)(B)); \
126 create_core(); \
127 return TEST_FAILURE; \
128 } \
129 } while (0)
130
131 #define test_compare_got(A,B,C) \
132 do \
133 { \
134 if ((A) != (B)) \
135 { \
136 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, #A, (C)); \
137 create_core(); \
138 return TEST_FAILURE; \
139 } \
140 } while (0)
141
142
143 #define test_strcmp(A,B) \
144 do \
145 { \
146 if (strcmp((A), (B))) \
147 { \
148 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, (A), (B)); \
149 create_core(); \
150 return TEST_FAILURE; \
151 } \
152 } while (0)
153
154 #define test_memcmp(A,B,C) \
155 do \
156 { \
157 if (memcmp((A), (B), (C))) \
158 { \
159 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
160 create_core(); \
161 return TEST_FAILURE; \
162 } \
163 } while (0)
164
165 #define test_return_if(__test_return_t) \
166 do \
167 { \
168 if ((__test_return_t) != TEST_SUCCESS) \
169 { \
170 return __test_return_t; \
171 } \
172 } while (0)
173