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 #include "tvec-adhoc.h"
45 #include "tvec-types.h"
47 /*----- Static variables --------------------------------------------------*/
49 static struct tvec_state tvstate;
50 static dstr d = DSTR_INIT;
51 static char strbuf[1024];
53 /*----- Utilities ---------------------------------------------------------*/
55 #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name)
57 static PRINTF_LIKE(1, 2) int format(const char *fmt, ...)
64 n = dstr_vputf(&d, fmt, &ap);
69 static PRINTF_LIKE(1, 2) void prepare(const char *fmt, ...)
76 n = vsnprintf(strbuf, sizeof(strbuf), fmt, ap);
78 n = vsprintf(strbuf, fmt, ap);
80 assert(0 <= n && n < sizeof(strbuf));
83 #define TEST_KNOWN(fmtargs, want) do { \
84 int n; n = format fmtargs; \
85 tvec_claimeq_int(&tvstate, n, strlen(want), \
86 __FILE__, __LINE__, "format " #fmtargs); \
87 tvec_claimeq_text(&tvstate, d.buf, d.len, want, strlen(want), \
88 __FILE__, __LINE__, "format " #fmtargs); \
91 #define TEST_REF(fmtargs) do { \
92 int n = format fmtargs; \
94 tvec_claimeq_int(&tvstate, n, strlen(strbuf), \
95 __FILE__, __LINE__, "format " #fmtargs); \
96 tvec_claimeq_text(&tvstate, d.buf, d.len, strbuf, strlen(strbuf), \
97 __FILE__, __LINE__, "format " #fmtargs); \
101 "This is a rather longer string than the code is expecting: will it fit?"
103 /*----- Main program ------------------------------------------------------*/
105 int main(int argc, char *argv[])
109 tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
110 if (argpos < argc) die(2, "no input files expected");
111 tvec_adhoc(&tvstate);
113 TESTGROUP("basics") {
114 TEST_KNOWN(("Hello, world!"), "Hello, world!");
115 TEST_KNOWN(("just a ->%%<- sign"), "just a ->%<- sign");
118 TESTGROUP("integers") {
119 TEST_KNOWN(("Testing, testing, %d, %d, %d.", 1, 2, 3),
120 "Testing, testing, 1, 2, 3.");
121 TEST_KNOWN(("->%5d<-", 138), "-> 138<-");
122 TEST_KNOWN(("->%*d<-", 5, 138), "-> 138<-");
123 TEST_KNOWN(("->%-*d<-", 5, 138), "->138 <-");
124 TEST_KNOWN(("->%*d<-", -5, 138), "->138 <-");
125 TEST_KNOWN(("->%-*d<-", -5, 138), "->138 <-");
128 TESTGROUP("strings") {
129 TEST_KNOWN(("->%.*s<-", 5, "truncate me"), "->trunc<-");
130 TEST_KNOWN(("->%.*s<-", -5, "don't truncate me"),
131 "->don't truncate me<-");
132 TEST_KNOWN(("Truncation indirect: ->%.*s<-", 10,
133 "a long string to be chopped"),
134 "Truncation indirect: ->a long str<-");
137 TESTGROUP("combinations") {
138 TEST_KNOWN(("%08lx:%s", 0x65604204ul, "tripe-ec"), "65604204:tripe-ec");
139 TEST_REF(("big float: ->%f<- and integer %d\n", DBL_MAX, 42));
142 TESTGROUP("argument-order") {
143 TEST_KNOWN(("Testing, testing, %3$d, %2$d, %1$d.", 3, 2, 1),
144 "Testing, testing, 1, 2, 3.");
145 TEST_KNOWN(("Truncation indirect: ->%1$.*2$s<-",
146 "a long string to be chopped", 10),
147 "Truncation indirect: ->a long str<-");
151 TEST_KNOWN(("%s", LENGTHY), LENGTHY);
153 return (tvec_end(&tvstate));
156 /*----- That's all, folks -------------------------------------------------*/