Commit | Line | Data |
---|---|---|
eff136f6 MW |
1 | #include "config.h" |
2 | ||
3 | #include <assert.h> | |
4 | #include <float.h> | |
5 | #include <stdarg.h> | |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <string.h> | |
9 | ||
10 | #include "dstr.h" | |
11 | ||
12 | static int win = 0, lose = 0; | |
13 | static dstr d = DSTR_INIT; | |
14 | static char buf[1024]; | |
15 | ||
16 | static void check(const char *what, const char *want) | |
17 | { | |
18 | if (strcmp(want, d.buf) == 0) | |
19 | win++; | |
20 | else { | |
21 | lose++; | |
22 | fprintf(stderr, "test failed: %s\n expected: %s\n found: %s\n", | |
23 | what, want, d.buf); | |
24 | } | |
25 | } | |
26 | ||
27 | static void PRINTF_LIKE(1, 2) format(const char *fmt, ...) | |
28 | { | |
29 | va_list ap; | |
30 | va_start(ap, fmt); | |
31 | dstr_reset(&d); | |
32 | dstr_vputf(&d, fmt, &ap); | |
33 | va_end(ap); | |
34 | } | |
35 | ||
36 | static void PRINTF_LIKE(1, 2) prepare(const char *fmt, ...) | |
37 | { | |
38 | va_list ap; | |
39 | int n; | |
40 | ||
41 | va_start(ap, fmt); | |
42 | #ifdef HAVE_SNPRINTF | |
43 | n = vsnprintf(buf, sizeof(buf), fmt, ap); | |
44 | #else | |
45 | n = vsprintf(buf, fmt, ap); | |
46 | #endif | |
47 | assert(0 <= n && n < sizeof(buf)); | |
48 | } | |
49 | ||
50 | #define TEST1(fmtargs) do { \ | |
51 | format fmtargs; \ | |
52 | prepare fmtargs; \ | |
53 | check(#fmtargs, buf); \ | |
54 | } while (0) | |
55 | ||
56 | #define TEST2(fmtargs, want) do { \ | |
57 | format fmtargs; \ | |
58 | check(#fmtargs, want); \ | |
59 | } while (0) | |
60 | ||
61 | #define LENGTHY \ | |
62 | "This is a rather longer string than the code is expecting: will it fit?" | |
63 | ||
64 | int main(void) | |
65 | { | |
66 | TEST2(("Hello, world!"), "Hello, world!"); | |
67 | TEST2(("just a ->%%<- sign"), "just a ->%<- sign"); | |
68 | TEST2(("Testing, testing, %d, %d, %d.", 1, 2, 3), | |
69 | "Testing, testing, 1, 2, 3."); | |
70 | TEST2(("->%5d<-", 138), "-> 138<-"); | |
71 | TEST2(("->%*d<-", 5, 138), "-> 138<-"); | |
72 | TEST2(("->%-*d<-", 5, 138), "->138 <-"); | |
73 | TEST2(("->%*d<-", -5, 138), "->138 <-"); | |
74 | TEST2(("->%-*d<-", -5, 138), "->138 <-"); | |
75 | TEST2(("->%.*s<-", 5, "truncate me"), "->trunc<-"); | |
76 | TEST2(("->%.*s<-", -5, "don't truncate me"), "->don't truncate me<-"); | |
77 | TEST2(("Truncation indirect: ->%.*s<-", 10, "a long string to be chopped"), | |
78 | "Truncation indirect: ->a long str<-"); | |
79 | TEST2(("%08lx:%s", 0x65604204ul, "tripe-ec"), "65604204:tripe-ec"); | |
80 | TEST2(("%s", LENGTHY), LENGTHY); | |
81 | ||
82 | TEST1(("big float: ->%f<- and integer %d\n", DBL_MAX, 42)); | |
83 | ||
84 | TEST2(("Testing, testing, %3$d, %2$d, %1$d.", 3, 2, 1), | |
85 | "Testing, testing, 1, 2, 3."); | |
86 | TEST2(("Truncation indirect: ->%1$.*2$s<-", | |
87 | "a long string to be chopped", 10), | |
88 | "Truncation indirect: ->a long str<-"); | |
89 | ||
90 | if (!lose) printf("All tests successful.\n"); | |
91 | else printf("FAILED %d of %d tests.\n", lose, win + lose); | |
92 | return (!!lose); | |
93 | } |