Skip to content

Commit 2abce72

Browse files
committed
cleaned up asprintf_fprintf testing code
1 parent 9cb861b commit 2abce72

File tree

2 files changed

+75
-50
lines changed

2 files changed

+75
-50
lines changed

test/standalone/asprintf_fprintf/src/main.c

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,46 @@
88
#include <ti/sprintf.h>
99
#include <ctype.h>
1010

11-
/**
12-
* @brief Tests the following functions/macros:
13-
* boot_sprintf
14-
* boot_snprintf
15-
* boot_asprintf
16-
* asprintf
17-
* fprintf
18-
* stpcpy
19-
* memccpy
20-
*/
11+
//------------------------------------------------------------------------------
12+
// Config
13+
//------------------------------------------------------------------------------
14+
15+
// define to 0 or 1
16+
#define DEBUG_DIAGNOSTICS 0
17+
18+
// define to 0 or 1 (prevents Clang from replacing functions with builtins)
19+
#define RENAME_FUNCTIONS 1
20+
21+
//------------------------------------------------------------------------------
22+
// Utilities
23+
//------------------------------------------------------------------------------
2124

2225
#define C(expr) if (!(expr)) { return __LINE__; }
2326

24-
#define TEST(test) { ret = test; if (ret != 0) { return ret; }}
27+
#define TEST(test) { ret = test; if (ret != 0) { return ret; } }
2528

2629
#define SINK (char*)0xE40000
2730

31+
#ifndef DEBUG_DIAGNOSTICS
32+
#error "DEBUG_DIAGNOSTICS needs to be defined to 0 or 1"
33+
#endif
34+
35+
#if DEBUG_DIAGNOSTICS
36+
#define test_printf printf
37+
#else
38+
#define test_printf(...)
39+
#endif
40+
2841
/* pass NULL into functions without triggering -Wnonnull */
29-
extern void* NULL_ptr;
42+
extern void * NULL_ptr;
43+
extern size_t ZERO_size;
44+
45+
//------------------------------------------------------------------------------
46+
// Functions
47+
//------------------------------------------------------------------------------
3048

3149
// prevents Clang from replacing function calls with builtins
32-
#if 1
50+
#if RENAME_FUNCTIONS
3351

3452
void *T_memcpy(void *__restrict dest, const void *__restrict src, size_t n)
3553
__attribute__((nonnull(1, 2)));
@@ -107,6 +125,10 @@ void T_bzero(void* s, size_t n);
107125

108126
#endif
109127

128+
//------------------------------------------------------------------------------
129+
// Globals
130+
//------------------------------------------------------------------------------
131+
110132
const char gnu_copypasta[] =
111133
"I would just like to interject for a moment. What you're referring to "\
112134
"as Linux, is in fact, GNU/Linux, or as I\'ve recently taken to calling "\
@@ -131,42 +153,46 @@ static const int pos_4 = 212;
131153
static char* buf = NULL;
132154
static FILE* file = NULL;
133155

156+
//------------------------------------------------------------------------------
157+
// Tests
158+
//------------------------------------------------------------------------------
159+
134160
int boot_sprintf_tests(void) {
135161
int pos;
136162
int len = boot_asprintf(
137163
&buf, "%+d %s%% %#o %#x %n %#X %i\n",
138164
123, "asprintf", 076543, 0x9abcd, &pos, 0xFE1, 0
139165
);
140166
if (buf == NULL || len <= 0) {
141-
printf("buf %p len %d\n", buf, len);
167+
test_printf("buf %p len %d\n", buf, len);
142168
return __LINE__;
143169
}
144170
if (buf[len] != '\0') {
145171
return __LINE__;
146172
}
147173
size_t buf_len = T_strlen(buf);
148174
if (buf_len != T_strlen(test_1) || buf_len != (size_t)len) {
149-
printf("E: %zu != %zu != %d\n", T_strlen(test_1), buf_len, len);
175+
test_printf("E: %zu != %zu != %d\n", T_strlen(test_1), buf_len, len);
150176
return __LINE__;
151177
}
152178
if (pos != pos_1) {
153-
printf("E: %d != %d\n", pos, pos_1);
179+
test_printf("E: %d != %d\n", pos, pos_1);
154180
return __LINE__;
155181
}
156182
int cmp = strcmp(buf, test_1);
157183
if (cmp != 0) {
158-
printf("cmp: %d\n", cmp);
184+
test_printf("cmp: %d\n", cmp);
159185
return __LINE__;
160186
}
161187
char append[128];
162188
int snprintf_test = boot_snprintf(append, 20, "%s", test_1);
163189
if (snprintf_test != (int)T_strlen(test_1)) {
164-
printf("sprintf_test: %d\n", snprintf_test);
190+
test_printf("sprintf_test: %d\n", snprintf_test);
165191
return __LINE__;
166192
}
167193
int len_2 = boot_snprintf(append, sizeof(append), "%s", test_1);
168194
if (len_2 != (int)T_strlen(test_1)) {
169-
printf("E: %d != %zu\n", len_2, T_strlen(test_1));
195+
test_printf("E: %d != %zu\n", len_2, T_strlen(test_1));
170196
return __LINE__;
171197
}
172198
char str2[128];
@@ -178,18 +204,18 @@ int boot_sprintf_tests(void) {
178204
return __LINE__;
179205
}
180206
if (end != &str2[pos_2]) {
181-
printf("diff %p - %p = %td\n", end, str2, (ptrdiff_t)(end - str2));
207+
test_printf("diff %p - %p = %td\n", end, str2, (ptrdiff_t)(end - str2));
182208
return __LINE__;
183209
}
184210
int cmp2 = T_strcmp(str2, test_2);
185211
if (cmp2 != 0) {
186-
printf("cmp: %d\n", cmp2);
212+
test_printf("cmp: %d\n", cmp2);
187213
return __LINE__;
188214
}
189215
char buf_3[30];
190216
int len_3 = boot_snprintf(buf_3, sizeof(buf_3), test_3);
191217
if (len_3 != pos_3) {
192-
printf("E: %d != %d\n", len_3, pos_3);
218+
test_printf("E: %d != %d\n", len_3, pos_3);
193219
return __LINE__;
194220
}
195221

@@ -222,12 +248,12 @@ int boot_sprintf_tests(void) {
222248
5, 10
223249
);
224250
if (len_4 != pos_4) {
225-
printf("E: %d != %d\n", len_4, pos_4);
251+
test_printf("E: %d != %d\n", len_4, pos_4);
226252
return __LINE__;
227253
}
228254
int len_5 = boot_snprintf(SINK, 10, "");
229255
if (len_5 != 0) {
230-
printf("E: %d != 0\n", len_5);
256+
test_printf("E: %d != 0\n", len_5);
231257
return __LINE__;
232258
}
233259

@@ -244,35 +270,35 @@ int nano_tests(void) {
244270
123, "asprintf", 076543, 0x9abcd, &pos, 0xFE1, 0
245271
);
246272
if (buf == NULL || len <= 0) {
247-
printf("buf %p len %d\n", buf, len);
273+
test_printf("buf %p len %d\n", buf, len);
248274
return __LINE__;
249275
}
250276
if (buf[len] != '\0') {
251277
return __LINE__;
252278
}
253279
size_t buf_len = T_strlen(buf);
254280
if (buf_len != T_strlen(test_1) || buf_len != (size_t)len) {
255-
printf("E: %zu != %zu != %d\n", T_strlen(test_1), buf_len, len);
281+
test_printf("E: %zu != %zu != %d\n", T_strlen(test_1), buf_len, len);
256282
return __LINE__;
257283
}
258284
if (pos != pos_1) {
259-
printf("E: %d != %d\n", pos, pos_1);
285+
test_printf("E: %d != %d\n", pos, pos_1);
260286
return __LINE__;
261287
}
262288
int cmp = strcmp(buf, test_1);
263289
if (cmp != 0) {
264-
printf("cmp: %d\n", cmp);
290+
test_printf("cmp: %d\n", cmp);
265291
return __LINE__;
266292
}
267293
char append[128];
268294
int snprintf_test = snprintf(append, 20, "%s", test_1);
269295
if (snprintf_test != (int)T_strlen(test_1)) {
270-
printf("sprintf_test: %d\n", snprintf_test);
296+
test_printf("sprintf_test: %d\n", snprintf_test);
271297
return __LINE__;
272298
}
273299
int len_2 = snprintf(append, sizeof(append), "%s", test_1);
274300
if (len_2 != (int)T_strlen(test_1)) {
275-
printf("E: %d != %zu\n", len_2, T_strlen(test_1));
301+
test_printf("E: %d != %zu\n", len_2, T_strlen(test_1));
276302
return __LINE__;
277303
}
278304
char str2[128];
@@ -284,24 +310,24 @@ int nano_tests(void) {
284310
return __LINE__;
285311
}
286312
if (end != &str2[pos_2]) {
287-
printf("diff %p - %p = %td\n", end, str2, (ptrdiff_t)(end - str2));
313+
test_printf("diff %p - %p = %td\n", end, str2, (ptrdiff_t)(end - str2));
288314
return __LINE__;
289315
}
290316
int cmp2 = T_strcmp(str2, test_2);
291317
if (cmp2 != 0) {
292-
printf("cmp: %d\n", cmp2);
318+
test_printf("cmp: %d\n", cmp2);
293319
return __LINE__;
294320
}
295321
char buf_30[30];
296322
int len_3sn = snprintf(buf_30, sizeof(buf_30), test_3);
297323
if (len_3sn != pos_3) {
298-
printf("E: %d != %d\n", len_3sn, pos_3);
324+
test_printf("E: %d != %d\n", len_3sn, pos_3);
299325
return __LINE__;
300326
}
301327

302328
int len_3s = sprintf(buf_30, test_3);
303329
if (len_3s != pos_3) {
304-
printf("E: %d != %d\n", len_3s, pos_3);
330+
test_printf("E: %d != %d\n", len_3s, pos_3);
305331
return __LINE__;
306332
}
307333

@@ -334,12 +360,12 @@ int nano_tests(void) {
334360
5, 10
335361
);
336362
if (len_4 != pos_4) {
337-
printf("E: %d != %d\n", len_4, pos_4);
363+
test_printf("E: %d != %d\n", len_4, pos_4);
338364
return __LINE__;
339365
}
340366
int len_5 = snprintf(SINK, 10, "");
341367
if (len_5 != 0) {
342-
printf("E: %d != 0\n", len_5);
368+
test_printf("E: %d != 0\n", len_5);
343369
return __LINE__;
344370
}
345371

@@ -389,7 +415,7 @@ int memccpy_tests(void) {
389415
// test zero byte case
390416
void* ptr = T_memccpy((void*)0xC0FFEE, (void*)0x123456, 123, 0);
391417
if (ptr != NULL) {
392-
printf("%p != NULL\n", ptr);
418+
test_printf("%p != NULL\n", ptr);
393419
return __LINE__;
394420
}
395421
file = fopen(file_name, "wb");
@@ -405,11 +431,10 @@ int memccpy_tests(void) {
405431
char dest[sizeof src];
406432
const char alt = '@';
407433

408-
for (size_t i = 0; i != sizeof terminal; ++i)
409-
{
434+
for (size_t i = 0; i != sizeof terminal; ++i) {
410435
void* to = T_memccpy(dest, src, terminal[i], sizeof dest);
411436

412-
fprintf(file,"Terminal '%c' (%s):\t\"", terminal[i], to ? "found" : "absent");
437+
fprintf(file, "Terminal '%c' (%s):\t\"", terminal[i], to ? "found" : "absent");
413438

414439
// if `terminal` character was not found - print the whole `dest`
415440
to = to ? to : dest + sizeof dest;
@@ -421,7 +446,6 @@ int memccpy_tests(void) {
421446
fputs("\"\n", file);
422447
}
423448

424-
425449
fprintf(file, "%c%s", '\n', "Separate star names from distances (ly):\n");
426450
const char *star_distance[] = {
427451
"Arcturus : 37", "Vega : 25", "Capella : 43", "Rigel : 860", "Procyon : 11"
@@ -430,8 +454,7 @@ int memccpy_tests(void) {
430454
char *first = names_only;
431455
char *last = names_only + sizeof names_only;
432456

433-
for (size_t t = 0; t != (sizeof star_distance) / (sizeof star_distance[0]); ++t)
434-
{
457+
for (size_t t = 0; t != (sizeof star_distance) / (sizeof star_distance[0]); ++t) {
435458
if (first) {
436459
first = T_memccpy(first, star_distance[t], ' ', last - first);
437460
} else {
@@ -490,7 +513,7 @@ int mempcpy_test(void) {
490513
// test zero byte case
491514
void* ptr = T_mempcpy((void*)0xC0FFEE, (void*)0x123456, 0);
492515
if (ptr != (void*)0xC0FFEE) {
493-
printf("%p != %p\n", ptr, (void*)0xC0FFEE);
516+
test_printf("%p != %p\n", ptr, (void*)0xC0FFEE);
494517
return __LINE__;
495518
}
496519
char data[192 + 1];
@@ -501,7 +524,7 @@ int mempcpy_test(void) {
501524
T_memset(append, 0x34, 64);
502525
char* res = T_mempcpy(&data[64], append, 64);
503526
if (res != &data[128]) {
504-
printf("%p != %p\n", res, &data[128]);
527+
test_printf("%p != %p\n", res, &data[128]);
505528
return __LINE__;
506529
}
507530

@@ -512,7 +535,7 @@ int mempcpy_test(void) {
512535

513536
int cmp = T_memcmp(data, truth, 192);
514537
if (cmp != 0) {
515-
printf("cmp: %d\n", cmp);
538+
test_printf("cmp: %d\n", cmp);
516539
return __LINE__;
517540
}
518541
return 0;
@@ -535,7 +558,7 @@ int bzero_test(void) {
535558
T_memset(&truth[ 2], 0x00, 1);
536559
T_memset(&truth[ 8], 0x00, 17);
537560
T_memset(&truth[25], 0x8F, 7);
538-
T_bzero(&data[2], 0);
561+
T_bzero(&data[2], ZERO_size);
539562
T_bzero(&data[2], 1);
540563
if (T_strlen(&data[0]) != 2) {
541564
return __LINE__;
@@ -546,11 +569,11 @@ int bzero_test(void) {
546569
if (T_strlen(&data[2]) != 0) {
547570
return __LINE__;
548571
}
549-
T_bzero(NULL, 0);
572+
T_bzero(NULL_ptr, ZERO_size);
550573
T_bzero(&data[8], 17);
551574
int cmp = T_memcmp(data, truth, 32);
552575
if (cmp != 0) {
553-
printf("cmp: %d\n", cmp);
576+
test_printf("cmp: %d\n", cmp);
554577
return __LINE__;
555578
}
556579
return 0;
@@ -996,6 +1019,7 @@ int strchrnul_test(void) {
9961019

9971020
int run_tests(void) {
9981021
int ret = 0;
1022+
9991023
/* boot_asprintf */
10001024
ret = boot_sprintf_tests();
10011025
free(buf); buf = NULL;

test/standalone/asprintf_fprintf/src/rename.asm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ _T_bzero := _bzero
2929

3030
section .rodata
3131

32-
public _NULL_ptr
32+
public _NULL_ptr, _ZERO_size
3333
_NULL_ptr:
34+
_ZERO_size:
3435
db $00, $00, $00
3536

3637
extern _memset, _memcpy, _memmove, _memcmp, _memccpy, _mempcpy, _memrchr, _memmem, _memrmem

0 commit comments

Comments
 (0)