15 static struct tvec_state tvstate;
16 static dstr d = DSTR_INIT;
17 static char strbuf[1024];
19 #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name)
21 static PRINTF_LIKE(1, 2) int format(const char *fmt, ...)
28 n = dstr_vputf(&d, fmt, &ap);
33 static PRINTF_LIKE(1, 2) void prepare(const char *fmt, ...)
40 n = vsnprintf(strbuf, sizeof(strbuf), fmt, ap);
42 n = vsprintf(strbuf, fmt, ap);
44 assert(0 <= n && n < sizeof(strbuf));
47 #define TEST_KNOWN(fmtargs, want) do { \
48 int n; n = format fmtargs; \
49 tvec_claimeq_int(&tvstate, n, strlen(want), \
50 __FILE__, __LINE__, "format " #fmtargs); \
51 tvec_claimeq_string(&tvstate, d.buf, d.len, want, strlen(want), \
52 __FILE__, __LINE__, "format " #fmtargs); \
55 #define TEST_REF(fmtargs) do { \
56 int n = format fmtargs; \
58 tvec_claimeq_int(&tvstate, n, strlen(strbuf), \
59 __FILE__, __LINE__, "format " #fmtargs); \
60 tvec_claimeq_string(&tvstate, d.buf, d.len, strbuf, strlen(strbuf), \
61 __FILE__, __LINE__, "format " #fmtargs); \
65 "This is a rather longer string than the code is expecting: will it fit?"
67 int main(int argc, char *argv[])
69 struct tvec_test test;
72 tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
73 if (argpos < argc) die(2, "no input files expected");
74 tvec_adhoc(&tvstate, &test);
77 TEST_KNOWN(("Hello, world!"), "Hello, world!");
78 TEST_KNOWN(("just a ->%%<- sign"), "just a ->%<- sign");
81 TESTGROUP("integers") {
82 TEST_KNOWN(("Testing, testing, %d, %d, %d.", 1, 2, 3),
83 "Testing, testing, 1, 2, 3.");
84 TEST_KNOWN(("->%5d<-", 138), "-> 138<-");
85 TEST_KNOWN(("->%*d<-", 5, 138), "-> 138<-");
86 TEST_KNOWN(("->%-*d<-", 5, 138), "->138 <-");
87 TEST_KNOWN(("->%*d<-", -5, 138), "->138 <-");
88 TEST_KNOWN(("->%-*d<-", -5, 138), "->138 <-");
91 TESTGROUP("strings") {
92 TEST_KNOWN(("->%.*s<-", 5, "truncate me"), "->trunc<-");
93 TEST_KNOWN(("->%.*s<-", -5, "don't truncate me"),
94 "->don't truncate me<-");
95 TEST_KNOWN(("Truncation indirect: ->%.*s<-", 10,
96 "a long string to be chopped"),
97 "Truncation indirect: ->a long str<-");
100 TESTGROUP("combinations") {
101 TEST_KNOWN(("%08lx:%s", 0x65604204ul, "tripe-ec"), "65604204:tripe-ec");
102 TEST_REF(("big float: ->%f<- and integer %d\n", DBL_MAX, 42));
105 TESTGROUP("argument-order") {
106 TEST_KNOWN(("Testing, testing, %3$d, %2$d, %1$d.", 3, 2, 1),
107 "Testing, testing, 1, 2, 3.");
108 TEST_KNOWN(("Truncation indirect: ->%1$.*2$s<-",
109 "a long string to be chopped", 10),
110 "Truncation indirect: ->a long str<-");
114 TEST_KNOWN(("%s", LENGTHY), LENGTHY);
116 return (tvec_end(&tvstate));