forked from confluentinc/kibosh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.h
More file actions
212 lines (188 loc) · 5.49 KB
/
test.h
File metadata and controls
212 lines (188 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* Copyright 2017 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#ifndef KIBOSH_TEST_H
#define KIBOSH_TEST_H
#include "log.h" // for safe_strerror
#include <stdio.h> // for fprintf
/**
* Abort unless t is true
*
* @param t condition to check
*/
void die_unless(int t);
/**
* Abort if t is true
*
* @param t condition to check
*/
void die_if(int t);
/**
* Create a zero-size file at ${file_name}
*
* @param fname The file name
*
* @return 0 on success; error code otherwise
*/
int do_touch1(const char *fname);
/**
* Create a zero-size file at ${tempdir}/${file_name}
*
* @param dir The dir
* @param fname The file name
*
* @return 0 on success; error code otherwise
*/
int do_touch2(const char *dir, const char *fname);
/**
* Expect two strings to be equal.
*
* @param a The first string.
* @param b The second string.
*
* @return 0 on success; error code otherwise.
*/
int expect_str_equal(const char *a, const char *b);
#define EXPECT_INT_ZERO(x) \
do { \
int __my_ret__ = x; \
if (__my_ret__) { \
fprintf(stderr, "expected 0, got %d on line %d: %s\n",\
__my_ret__, __LINE__, #x); \
return -1; \
} \
} while (0);
#define EXPECT_INT_NONZERO(x) \
do { \
int __my_ret__ = x; \
if (__my_ret__ == 0) { \
fprintf(stderr, "expected non-zero, got %d on line %d: %s\n",\
__my_ret__, __LINE__, #x); \
return -1; \
} \
} while (0);
#define EXPECT_NULL(x) \
do { \
const void *__my_ret__ = x; \
if (__my_ret__) { \
fprintf(stderr, "expected NULL, got %p on line %d: %s\n",\
__my_ret__, __LINE__, #x); \
return -1; \
} \
} while (0);
#define EXPECT_NONNULL(x) \
do { \
const void *__my_ret__ = x; \
if (!__my_ret__) { \
fprintf(stderr, "expected non-NULL, got NULL on line %d: %s\n",\
__LINE__, #x); \
return -1; \
} \
} while (0);
#define EXPECT_INT_NONNEGATIVE(x) \
do { \
int __my_ret__ = x; \
if (__my_ret__ < 0) { \
fprintf(stderr, "expected non-negative, got %d on line %d: %s\n",\
__my_ret__, __LINE__, #x); \
return -1; \
} \
} while (0);
#define EXPECT_INT_EQ(x, y) \
do { \
int __my_x__ = x; \
int __my_y__ = y; \
if (__my_x__ != __my_y__) { \
fprintf(stderr, "expected %d, got %d on line %d: %s != %s\n",\
__my_x__, __my_y__, __LINE__, #x, #y); \
return -1; \
} \
} while (0);
#define EXPECT_INT_NE(x, y) \
do { \
int __my_x__ = x; \
int __my_y__ = y; \
if (__my_x__ == __my_y__) { \
fprintf(stderr, "expected %d != %d on line %d: %s == %s\n",\
__my_x__, __my_y__, __LINE__, #x, #y); \
return -1; \
} \
} while (0);
#define EXPECT_INT_LT(x, y) \
do { \
int __my_x__ = x; \
int __my_y__ = y; \
if (__my_x__ >= __my_y__) { \
fprintf(stderr, "expected %d < %d on line %d: %s >= %s\n",\
__my_x__, __my_y__, __LINE__, #x, #y); \
return -1; \
} \
} while (0);
#define EXPECT_INT_GE(x, y) \
do { \
int __my_x__ = x; \
int __my_y__ = y; \
if (__my_x__ < __my_y__) { \
fprintf(stderr, "expected %d >= %d on line %d: %s < %s\n",\
__my_x__, __my_y__, __LINE__, #x, #y); \
return -1; \
} \
} while (0);
#define EXPECT_INT_GT(x, y) \
do { \
int __my_x__ = x; \
int __my_y__ = y; \
if (__my_x__ <= __my_y__) { \
fprintf(stderr, "expected %d > %d on line %d: %s <= %s\n",\
__my_x__, __my_y__, __LINE__, #x, #y); \
return -1; \
} \
} while (0);
#define EXPECT_POSIX_SUCC(expr) \
do { \
if (expr) { \
int err = errno; \
fprintf(stderr, "error %d (%s) on line %d: %s\n", \
err, safe_strerror(err), __LINE__, #expr); \
return -1; \
} \
} while (0);
#define EXPECT_POSIX_FAIL(expr, eret) \
do { \
int err; \
\
if (expr == 0) { \
fprintf(stderr, "unexpected success on line %d: %s\n",\
__LINE__, #expr); \
return -1; \
} \
err = errno; \
if (err != eret) { \
fprintf(stderr, "unexpected error %d (%s) on line %d: %s\n",\
err, safe_strerror(err), __LINE__, #expr); \
return -1; \
} \
} while (0);
#define EXPECT_STR_EQ(a, b) \
do { \
if (expect_str_equal(a, b)) { \
fprintf(stderr, "failed on line %d: string comparison failed. " \
"strcmp(%s, %s) != 0\n", \
__LINE__, #a, #b); \
return -1; \
} \
} while (0);
#endif
// vim: ts=4:sw=4:tw=99:et