forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlib_bool.c
253 lines (212 loc) · 6.19 KB
/
tlib_bool.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include "util_memory.h"
#include "util_threads.h"
#include "tlib_main.h"
/*
* The testing functions in this file follow
* unix process status conventions: 0 on success, and 1 on failure,
*/
static nrthread_mutex_t tlib_passcount_mutex = NRTHREAD_MUTEX_INITIALIZER;
int tlib_did_pass(void) {
nrt_mutex_lock(&tlib_passcount_mutex);
{ tlib_passcount += 1; }
nrt_mutex_unlock(&tlib_passcount_mutex);
return 0; /* Pass */
}
int tlib_did_fail(void) {
nrt_mutex_lock(&tlib_passcount_mutex);
{ tlib_unexpected_failcount += 1; }
nrt_mutex_unlock(&tlib_passcount_mutex);
return 1; /* Fail */
}
int tlib_pass_if_true_f(const char* what,
int val,
const char* file,
int line,
const char* cond,
const char* fmt,
...) {
va_list ap;
if (0 != val) {
return tlib_did_pass();
}
printf("FAIL [%s:%d]: TRUE check: %s\n", file, line, what);
printf(">>> Condition: %s\n", cond);
printf(">>> ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
fflush(stdout);
return tlib_did_fail();
}
int tlib_pass_if_false_f(const char* what,
int val,
const char* file,
int line,
const char* cond,
const char* fmt,
...) {
va_list ap;
if (0 == val) {
return tlib_did_pass();
}
printf("FAIL [%s:%d]: FALSE check: %s\n", file, line, what);
printf(">>> Condition: %s\n", cond);
printf(">>> ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
fflush(stdout);
return tlib_did_fail();
}
int tlib_fail_if_true_f(const char* what,
int val,
const char* file,
int line,
const char* cond,
const char* fmt,
...) {
va_list ap;
if (0 == val) {
return tlib_did_pass();
}
printf("FAIL [%s:%d]: !TRUE check: %s\n", file, line, what);
printf(">>> Condition: %s\n", cond);
printf(">>> ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
fflush(stdout);
return tlib_did_fail();
}
int tlib_fail_if_false_f(const char* what,
int val,
const char* file,
int line,
const char* cond,
const char* fmt,
...) {
va_list ap;
if (0 != val) {
return tlib_did_pass();
}
printf("FAIL [%s:%d]: !FALSE check: %s\n", file, line, what);
printf(">>> Condition: %s\n", cond);
printf(">>> ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
fflush(stdout);
return tlib_did_fail();
}
int tlib_pass_if_status_success_f(const char* what,
nr_status_t val,
const char* file,
int line,
const char* cond) {
if (NR_SUCCESS == val) {
return tlib_did_pass();
}
printf("FAIL [%s:%d]: NR_SUCCESS check: %s\n", file, line, what);
printf(">>> Condition: %s\n", cond);
printf(">>> Result: %d\n", (int)val);
fflush(stdout);
return tlib_did_fail();
}
int tlib_fail_if_status_success_f(const char* what,
nr_status_t val,
const char* file,
int line,
const char* cond) {
if (NR_SUCCESS != val) {
return tlib_did_pass();
}
printf("FAIL [%s:%d]: !NR_SUCCESS check: %s\n", file, line, what);
printf(">>> Condition: %s\n", cond);
printf(">>> Result: %d\n", (int)val);
fflush(stdout);
return tlib_did_fail();
}
/*
* Mimic the output of xxd(1) to pretty print an array of bytes.
*/
static void hexdump(const char* p, size_t len) {
size_t i;
size_t j;
const size_t bytes_per_line = 16;
i = 0;
do {
printf(">>> %07zx:", i);
/* First print bytes as hex digits. */
for (j = 0; j < bytes_per_line; j++) {
/* Put a blank every 2 bytes. */
if (j % 2 == 0) {
printf(" ");
}
if (i + j < len) {
printf("%02x", ((const uint8_t*)p)[i + j]);
} else {
printf(" ");
}
}
printf(" ");
/* Then print bytes as characters, if printable. */
for (j = 0; j < bytes_per_line; j++) {
if (i + j < len) {
if (isprint((int)((const uint8_t*)p)[i + j])) {
printf("%c", (char)((const uint8_t*)p)[i + j]);
} else {
printf(".");
}
} else {
printf(" ");
}
}
printf("\n");
i += bytes_per_line;
} while (i < len);
}
int tlib_pass_if_bytes_equal_f(const char* what,
const void* expected,
size_t expected_len,
const void* actual,
size_t actual_len,
const char* file,
int line) {
int val;
if (expected_len != actual_len) {
printf("FAIL [%s:%d]: TRUE check: %s\n", file, line, what);
printf(">>> Condition: expected_len == actual_len\n");
printf(">>> Result: %zu != %zu\n", expected_len, actual_len);
printf(">>> Expected:\n");
hexdump((const char*)expected, expected_len);
printf(">>> Actual:\n");
hexdump((const char*)actual, actual_len);
fflush(stdout);
return tlib_did_fail();
}
val = nr_memcmp(expected, actual, expected_len);
if (0 == val) {
return tlib_did_pass();
}
printf("FAIL [%s:%d]: TRUE check: %s\n", file, line, what);
printf(">>> Condition: 0 == nr_memcmp(expected, actual, expected_len)\n");
printf(">>> Result: %d\n", val);
printf(">>> Expected:\n");
hexdump((const char*)expected, expected_len);
printf(">>> Actual:\n");
hexdump((const char*)actual, actual_len);
fflush(stdout);
return tlib_did_fail();
}