forked from awslabs/aws-c-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_test.c
269 lines (225 loc) · 8.19 KB
/
assert_test.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/** This standalone test harness tests that the asserts themselves function properly */
#define AWS_TESTING_REPORT_FD test_filedes
#include <stdio.h>
#include <string.h>
FILE *test_filedes;
#include <aws/testing/aws_test_harness.h>
#define NO_MORE_TESTS 12345
#define BAILED_OUT 98765
#ifdef _MSC_VER
/* disable warning about fopen() this is just a test */
#pragma warning(disable:4996)
/* disable warning about unreferenced formal parameter */
#pragma warning(disable:4100)
#endif
const char *test_filename;
int cur_line;
int expected_return;
int bail_out;
#define TEST_SUCCESS(name) \
if (bail_out) return BAILED_OUT; \
if (begin_test(index, #name, __FILE__, __LINE__, 0))
#define TEST_FAILURE(name) \
if (bail_out) return BAILED_OUT; \
if (begin_test(index, #name, __FILE__, __LINE__, -1))
const char *cur_testname, *cur_file;
int begin_test(int *index, const char *testname, const char *file, int line, int expected) {
if (*index <= line) {
*index = line;
cur_testname = testname;
cur_file = file;
cur_line = line;
expected_return = expected == 0 ? BAILED_OUT : expected;
bail_out = 1;
return 1;
}
return 0;
}
static int side_effect_ctr = 0;
int side_effect() {
if (side_effect_ctr++) {
fprintf(stderr, "***FAILURE*** Side effects triggered multiple times, after %s:%d (%s)", cur_file, cur_line, cur_testname);
abort();
}
return 0;
}
int test_asserts(int *index) {
TEST_SUCCESS(null_test) {}
TEST_FAILURE(null_failure_test) {
fprintf(AWS_TESTING_REPORT_FD, "***FAILURE*** test\n");
return FAILURE;
}
TEST_FAILURE(basic_fail_1) {
FAIL("Failed: %d", 42);
}
TEST_FAILURE(assert_bool) {
ASSERT_TRUE(0);
}
TEST_FAILURE(assert_bool) {
ASSERT_TRUE(0, "foo %d", 42);
}
TEST_FAILURE(assert_bool) {
ASSERT_FALSE(1);
}
TEST_FAILURE(assert_bool) {
ASSERT_FALSE(1, "foo %d", 42);
}
TEST_SUCCESS(assert_bool) {
ASSERT_TRUE(1);
}
TEST_SUCCESS(assert_bool) {
ASSERT_TRUE(2);
}
TEST_SUCCESS(assert_bool) {
ASSERT_FALSE(0);
}
TEST_SUCCESS(assert_success) { ASSERT_SUCCESS(AWS_OP_SUCCESS); }
TEST_SUCCESS(assert_success) { ASSERT_SUCCESS(AWS_OP_SUCCESS, "foo"); }
TEST_FAILURE(assert_success) { ASSERT_SUCCESS(aws_raise_error(AWS_ERROR_OOM), "foo"); }
TEST_SUCCESS(assert_fails) { ASSERT_FAILS(aws_raise_error(AWS_ERROR_OOM)); }
TEST_SUCCESS(assert_fails) { ASSERT_FAILS(aws_raise_error(AWS_ERROR_OOM), "foo"); }
TEST_FAILURE(assert_fails) { ASSERT_FAILS(AWS_OP_SUCCESS, "foo"); }
TEST_SUCCESS(assert_error) { ASSERT_ERROR(AWS_ERROR_OOM, aws_raise_error(AWS_ERROR_OOM)); }
TEST_SUCCESS(assert_error_side_effect) { ASSERT_ERROR((side_effect(), AWS_ERROR_OOM), aws_raise_error(AWS_ERROR_OOM)); }
TEST_SUCCESS(assert_error_side_effect) { ASSERT_ERROR(AWS_ERROR_OOM, (side_effect(), aws_raise_error(AWS_ERROR_OOM))); }
TEST_SUCCESS(assert_error) { ASSERT_ERROR(AWS_ERROR_OOM, aws_raise_error(AWS_ERROR_OOM), "foo"); }
TEST_FAILURE(assert_error) { ASSERT_ERROR(AWS_ERROR_CLOCK_FAILURE, aws_raise_error(AWS_ERROR_OOM), "foo"); }
aws_raise_error(AWS_ERROR_CLOCK_FAILURE); // set last error
TEST_FAILURE(assert_error) { ASSERT_ERROR(AWS_ERROR_CLOCK_FAILURE, AWS_OP_SUCCESS, "foo"); }
TEST_SUCCESS(assert_null) { ASSERT_NULL(NULL); }
{
const struct forward_decl *nullp2 = NULL;
TEST_SUCCESS(assert_null) { void *nullp = NULL; ASSERT_NULL(nullp); }
TEST_SUCCESS(assert_null) { ASSERT_NULL(nullp2); }
TEST_SUCCESS(assert_null_sideeffects) { ASSERT_NULL((side_effect(), nullp2)); }
}
TEST_SUCCESS(assert_null) { ASSERT_NULL(0, "foo"); }
TEST_FAILURE(assert_null) { ASSERT_NULL("hello world", "foo"); }
TEST_SUCCESS(inteq) { ASSERT_INT_EQUALS(4321, 4321); }
TEST_SUCCESS(inteq) { ASSERT_INT_EQUALS(4321, 4321, "foo"); }
TEST_SUCCESS(inteq_side_effects) {
int increment = 4321;
ASSERT_INT_EQUALS(4321, increment++, "foo");
ASSERT_INT_EQUALS(4322, increment++, "foo");
}
TEST_FAILURE(inteq_difference) { ASSERT_INT_EQUALS(0, 1, "foo"); }
// UINT/PTR/BYTE_HEX/HEX are the same backend, so just test that the format string doesn't break
TEST_FAILURE(uinteq) { ASSERT_UINT_EQUALS(0, 1, "Foo"); }
TEST_FAILURE(ptreq) { ASSERT_PTR_EQUALS("x", "y", "Foo"); }
TEST_FAILURE(bytehex) { ASSERT_BYTE_HEX_EQUALS('a', 'b'); }
TEST_FAILURE(hex) { ASSERT_HEX_EQUALS((uint64_t)-1, 0); }
TEST_SUCCESS(streq) {
ASSERT_STR_EQUALS((side_effect(), "x"), "x");
}
TEST_SUCCESS(streq) {
char str_x[2] = "x";
ASSERT_STR_EQUALS("x", (side_effect(), str_x), "foo");
}
TEST_FAILURE(streq) {
ASSERT_STR_EQUALS("x", "xy", "bar");
}
TEST_FAILURE(streq) {
ASSERT_STR_EQUALS("xy", "x");
}
uint8_t bin1[] = { 0, 1, 2 };
uint8_t bin2[] = { 0, 1, 2 };
TEST_SUCCESS(bineq) {
ASSERT_BIN_ARRAYS_EQUALS((side_effect(), bin1), 3, bin2, 3);
side_effect_ctr = 0;
ASSERT_BIN_ARRAYS_EQUALS(bin1, (side_effect(), 3), bin2, 3);
side_effect_ctr = 0;
ASSERT_BIN_ARRAYS_EQUALS(bin1, 3, (side_effect(), bin2), 3);
side_effect_ctr = 0;
ASSERT_BIN_ARRAYS_EQUALS(bin1, 3, bin2, (side_effect(), 3));
}
TEST_FAILURE(bineq_samesize) {
uint8_t bin3[] = { 0, 1, 3 };
ASSERT_BIN_ARRAYS_EQUALS(bin1, 3, bin3, 3, "foo");
}
TEST_FAILURE(bineq_diffsize) {
ASSERT_BIN_ARRAYS_EQUALS(bin1, 3, bin2, 2);
}
TEST_FAILURE(bineq_diffsize) {
ASSERT_BIN_ARRAYS_EQUALS(bin1, 2, bin2, 3);
}
TEST_SUCCESS(bineq_empty) {
ASSERT_BIN_ARRAYS_EQUALS(bin1, 0, bin2, 0, "foo");
}
TEST_SUCCESS(bineq_same) {
ASSERT_BIN_ARRAYS_EQUALS(bin1, 3, bin1, 3);
}
return NO_MORE_TESTS;
}
void reset() {
cur_testname = "UNKNOWN";
cur_file = "UNKNOWN";
bail_out = 0;
if (test_filedes) {
fclose(test_filedes);
}
test_filedes = fopen(test_filename, "w");
if (!test_filedes) {
perror("***INTERNAL ERROR*** Failed to open temporary file");
abort();
}
side_effect_ctr = 0;
}
int check_failure_output(const char *expected) {
fclose(test_filedes);
test_filedes = NULL;
FILE *readfd = fopen(test_filename, "r");
static char tmpbuf[256];
char *rv = fgets(tmpbuf, sizeof(tmpbuf), readfd);
fclose(readfd);
if (!expected) {
return rv == NULL;
} else {
if (!rv) return 0;
return !strncmp(tmpbuf, expected, strlen(expected));
}
}
int main(int argc, char **argv) {
int index = 0;
test_filename = argv[1];
// Suppress unused function warnings
(void)mem_acquire_malloc;
(void)mem_release_free;
(void)aws_run_test_case;
// Sanity checks for our own test macros
reset();
if (test_asserts(&index) != BAILED_OUT) {
fprintf(stderr, "***FAILURE*** Initial case did not succeed; stopped at %s:%d (%s)\n", cur_file, index, cur_testname);
return 1;
}
index++;
reset();
if (test_asserts(&index) != FAILURE) {
fprintf(stderr, "***FAILURE*** Second case did not fail; stopped at %s:%d (%s)\n", cur_file, index, cur_testname);
return 1;
}
index = 0;
for (;;) {
reset();
int rv = test_asserts(&index);
if (rv == NO_MORE_TESTS) {
break;
}
if (rv != expected_return) {
fprintf(stderr, "***FAILURE*** Wrong result (%d expected, %d got) after %s:%d (%s)\n", expected_return, rv, cur_file, index, cur_testname);
return 1;
}
if (expected_return == FAILURE) {
if (!check_failure_output("***FAILURE*** ")) {
fprintf(stderr, "***FAILURE*** Output did not start with ***FAILURE*** after %s:%d (%s)\n", cur_file, index, cur_testname);
return 1;
}
} else {
if (!check_failure_output(NULL)) {
fprintf(stderr, "***FAILURE*** Output was not empty after %s:%d (%s)\n", cur_file, index, cur_testname);
return 1;
}
}
index++;
}
return 0;
}