3 * Test driver for universal hashing
5 * (c) 2009 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
44 /*----- Static variables --------------------------------------------------*/
46 static struct tvec_state tvstate;
47 static dstr d = DSTR_INIT;
48 static char strbuf[1024];
50 /*----- Utilities ---------------------------------------------------------*/
52 #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name)
54 static PRINTF_LIKE(1, 2) int format(const char *fmt, ...)
61 n = dstr_vputf(&d, fmt, &ap);
66 static PRINTF_LIKE(1, 2) void prepare(const char *fmt, ...)
73 n = vsnprintf(strbuf, sizeof(strbuf), fmt, ap);
75 n = vsprintf(strbuf, fmt, ap);
77 assert(0 <= n && n < sizeof(strbuf));
80 #define TEST_KNOWN(fmtargs, want) do { \
81 int n; n = format fmtargs; \
82 tvec_claimeq_int(&tvstate, n, strlen(want), \
83 __FILE__, __LINE__, "format " #fmtargs); \
84 tvec_claimeq_text(&tvstate, d.buf, d.len, want, strlen(want), \
85 __FILE__, __LINE__, "format " #fmtargs); \
88 #define TEST_REF(fmtargs) do { \
89 int n = format fmtargs; \
91 tvec_claimeq_int(&tvstate, n, strlen(strbuf), \
92 __FILE__, __LINE__, "format " #fmtargs); \
93 tvec_claimeq_text(&tvstate, d.buf, d.len, strbuf, strlen(strbuf), \
94 __FILE__, __LINE__, "format " #fmtargs); \
98 "This is a rather longer string than the code is expecting: will it fit?"
100 /*----- Main program ------------------------------------------------------*/
102 int main(int argc, char *argv[])
104 struct tvec_test test;
107 tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
108 if (argpos < argc) die(2, "no input files expected");
109 tvec_adhoc(&tvstate, &test);
111 TESTGROUP("basics") {
112 TEST_KNOWN(("Hello, world!"), "Hello, world!");
113 TEST_KNOWN(("just a ->%%<- sign"), "just a ->%<- sign");
116 TESTGROUP("integers") {
117 TEST_KNOWN(("Testing, testing, %d, %d, %d.", 1, 2, 3),
118 "Testing, testing, 1, 2, 3.");
119 TEST_KNOWN(("->%5d<-", 138), "-> 138<-");
120 TEST_KNOWN(("->%*d<-", 5, 138), "-> 138<-");
121 TEST_KNOWN(("->%-*d<-", 5, 138), "->138 <-");
122 TEST_KNOWN(("->%*d<-", -5, 138), "->138 <-");
123 TEST_KNOWN(("->%-*d<-", -5, 138), "->138 <-");
126 TESTGROUP("strings") {
127 TEST_KNOWN(("->%.*s<-", 5, "truncate me"), "->trunc<-");
128 TEST_KNOWN(("->%.*s<-", -5, "don't truncate me"),
129 "->don't truncate me<-");
130 TEST_KNOWN(("Truncation indirect: ->%.*s<-", 10,
131 "a long string to be chopped"),
132 "Truncation indirect: ->a long str<-");
135 TESTGROUP("combinations") {
136 TEST_KNOWN(("%08lx:%s", 0x65604204ul, "tripe-ec"), "65604204:tripe-ec");
137 TEST_REF(("big float: ->%f<- and integer %d\n", DBL_MAX, 42));
140 TESTGROUP("argument-order") {
141 TEST_KNOWN(("Testing, testing, %3$d, %2$d, %1$d.", 3, 2, 1),
142 "Testing, testing, 1, 2, 3.");
143 TEST_KNOWN(("Truncation indirect: ->%1$.*2$s<-",
144 "a long string to be chopped", 10),
145 "Truncation indirect: ->a long str<-");
149 TEST_KNOWN(("%s", LENGTHY), LENGTHY);
151 return (tvec_end(&tvstate));
154 /*----- That's all, folks -------------------------------------------------*/