Update docs, and syncronize libtest.
[m6w6/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 /**
11 A structure describing the test case.
12 */
13 struct test_st {
14 const char *name;
15 bool requires_flush;
16 test_callback_fn *test_fn;
17 };
18
19 #define TEST_STRINGIFY(x) #x
20 #define TEST_TOSTRING(x) TEST_STRINGIFY(x)
21 #define TEST_AT __FILE__ ":" TEST_TOSTRING(__LINE__)
22
23
24 #define test_assert_errno(A) \
25 do \
26 { \
27 if ((A)) { \
28 fprintf(stderr, "\nAssertion failed at %s:%d: ", __FILE__, __LINE__);\
29 perror(#A); \
30 fprintf(stderr, "\n"); \
31 create_core(); \
32 assert((A)); \
33 } \
34 } while (0)
35
36 #define test_assert(A, B) \
37 do \
38 { \
39 if ((A)) { \
40 fprintf(stderr, "\nAssertion, %s(%s), failed at %s:%d: ", (B), #A, __FILE__, __LINE__);\
41 fprintf(stderr, "\n"); \
42 create_core(); \
43 assert((A)); \
44 } \
45 } while (0)
46
47 #define test_truth(A) \
48 do \
49 { \
50 if (! (A)) { \
51 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
52 create_core(); \
53 return TEST_FAILURE; \
54 } \
55 } while (0)
56
57 #define test_true(A) \
58 do \
59 { \
60 if (! (A)) { \
61 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
62 create_core(); \
63 return TEST_FAILURE; \
64 } \
65 } while (0)
66
67 #define test_true_got(A,B) \
68 do \
69 { \
70 if (! (A)) { \
71 fprintf(stderr, "\nAssertion failed at %s:%d: \"%s\" received \"%s\"\n", __FILE__, __LINE__, #A, (B));\
72 create_core(); \
73 return TEST_FAILURE; \
74 } \
75 } while (0)
76
77 #define test_skip(A,B) \
78 do \
79 { \
80 if ((A) != (B)) \
81 { \
82 return TEST_SKIPPED; \
83 } \
84 } while (0)
85
86 #define test_fail(A) \
87 do \
88 { \
89 if (1) { \
90 fprintf(stderr, "\nFailed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
91 create_core(); \
92 return TEST_FAILURE; \
93 } \
94 } while (0)
95
96
97 #define test_false(A) \
98 do \
99 { \
100 if ((A)) { \
101 fprintf(stderr, "\nAssertion failed in %s:%d: %s\n", __FILE__, __LINE__, #A);\
102 create_core(); \
103 return TEST_FAILURE; \
104 } \
105 } while (0)
106
107 #define test_false_with(A,B) \
108 do \
109 { \
110 if ((A)) { \
111 fprintf(stderr, "\nAssertion failed at %s:%d: %s with %s\n", __FILE__, __LINE__, #A, (B));\
112 create_core(); \
113 return TEST_FAILURE; \
114 } \
115 } while (0)
116
117
118 #define test_compare(A,B) \
119 do \
120 { \
121 if ((A) != (B)) \
122 { \
123 fprintf(stderr, "\n%s:%d: Expected %s, got %lu\n", __FILE__, __LINE__, #A, (unsigned long)(B)); \
124 create_core(); \
125 return TEST_FAILURE; \
126 } \
127 } while (0)
128
129 #define test_compare_got(A,B,C) \
130 do \
131 { \
132 if ((A) != (B)) \
133 { \
134 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, #A, (C)); \
135 create_core(); \
136 return TEST_FAILURE; \
137 } \
138 } while (0)
139
140
141 #define test_strcmp(A,B) \
142 do \
143 { \
144 if (strcmp((A), (B))) \
145 { \
146 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, (A), (B)); \
147 create_core(); \
148 return TEST_FAILURE; \
149 } \
150 } while (0)
151
152 #define test_memcmp(A,B,C) \
153 do \
154 { \
155 if (memcmp((A), (B), (C))) \
156 { \
157 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
158 create_core(); \
159 return TEST_FAILURE; \
160 } \
161 } while (0)
162