12 static int win
= 0, lose
= 0;
13 static dstr d
= DSTR_INIT
;
14 static char buf
[1024];
16 static void check(const char *what
, const char *want
)
18 if (strcmp(want
, d
.buf
) == 0)
22 fprintf(stderr
, "test failed: %s\n expected: %s\n found: %s\n",
27 static void PRINTF_LIKE(1, 2) format(const char *fmt
, ...)
32 dstr_vputf(&d
, fmt
, &ap
);
36 static void PRINTF_LIKE(1, 2) prepare(const char *fmt
, ...)
43 n
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
45 n
= vsprintf(buf
, fmt
, ap
);
47 assert(0 <= n
&& n
< sizeof(buf
));
50 #define TEST1(fmtargs) do { \
53 check(#fmtargs, buf); \
56 #define TEST2(fmtargs, want) do { \
58 check(#fmtargs, want); \
62 "This is a rather longer string than the code is expecting: will it fit?"
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
);
82 TEST1(("big float: ->%f<- and integer %d\n", DBL_MAX
, 42));
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<-");
90 if (!lose
) printf("All tests successful.\n");
91 else printf("FAILED %d of %d tests.\n", lose
, win
+ lose
);