13 static int win
= 0, lose
= 0;
14 static dstr d
= DSTR_INIT
;
15 static char buf
[1024];
17 static void check(const char *what
, const char *want
)
19 if (STRCMP(want
, ==, d
.buf
))
23 fprintf(stderr
, "test failed: %s\n expected: %s\n found: %s\n",
28 static void PRINTF_LIKE(1, 2) format(const char *fmt
, ...)
33 dstr_vputf(&d
, fmt
, &ap
);
37 static void PRINTF_LIKE(1, 2) prepare(const char *fmt
, ...)
44 n
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
46 n
= vsprintf(buf
, fmt
, ap
);
48 assert(0 <= n
&& n
< sizeof(buf
));
51 #define TEST1(fmtargs) do { \
54 check(#fmtargs, buf); \
57 #define TEST2(fmtargs, want) do { \
59 check(#fmtargs, want); \
63 "This is a rather longer string than the code is expecting: will it fit?"
67 TEST2(("Hello, world!"), "Hello, world!");
68 TEST2(("just a ->%%<- sign"), "just a ->%<- sign");
69 TEST2(("Testing, testing, %d, %d, %d.", 1, 2, 3),
70 "Testing, testing, 1, 2, 3.");
71 TEST2(("->%5d<-", 138), "-> 138<-");
72 TEST2(("->%*d<-", 5, 138), "-> 138<-");
73 TEST2(("->%-*d<-", 5, 138), "->138 <-");
74 TEST2(("->%*d<-", -5, 138), "->138 <-");
75 TEST2(("->%-*d<-", -5, 138), "->138 <-");
76 TEST2(("->%.*s<-", 5, "truncate me"), "->trunc<-");
77 TEST2(("->%.*s<-", -5, "don't truncate me"), "->don't truncate me<-");
78 TEST2(("Truncation indirect: ->%.*s<-", 10, "a long string to be chopped"),
79 "Truncation indirect: ->a long str<-");
80 TEST2(("%08lx:%s", 0x65604204ul
, "tripe-ec"), "65604204:tripe-ec");
81 TEST2(("%s", LENGTHY
), LENGTHY
);
83 TEST1(("big float: ->%f<- and integer %d\n", DBL_MAX
, 42));
85 TEST2(("Testing, testing, %3$d, %2$d, %1$d.", 3, 2, 1),
86 "Testing, testing, 1, 2, 3.");
87 TEST2(("Truncation indirect: ->%1$.*2$s<-",
88 "a long string to be chopped", 10),
89 "Truncation indirect: ->a long str<-");
91 if (!lose
) printf("All tests successful.\n");
92 else printf("FAILED %d of %d tests.\n", lose
, win
+ lose
);