chiark / gitweb /
@@@ tvec doc wip
[mLib] / struct / t / dstr-putf-test.c
1 /* -*-c-*-
2  *
3  * Test driver for universal hashing
4  *
5  * (c) 2009 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <float.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "dstr.h"
40 #include "macros.h"
41 #include "report.h"
42 #include "tvec.h"
43
44 /*----- Static variables --------------------------------------------------*/
45
46 static struct tvec_state tvstate;
47 static dstr d = DSTR_INIT;
48 static char strbuf[1024];
49
50 /*----- Utilities ---------------------------------------------------------*/
51
52 #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name)
53
54 static PRINTF_LIKE(1, 2) int format(const char *fmt, ...)
55 {
56   va_list ap;
57   int n;
58
59   va_start(ap, fmt);
60   dstr_reset(&d);
61   n = dstr_vputf(&d, fmt, &ap);
62   va_end(ap);
63   return (n);
64 }
65
66 static PRINTF_LIKE(1, 2) void prepare(const char *fmt, ...)
67 {
68   va_list ap;
69   int n;
70
71   va_start(ap, fmt);
72 #ifdef HAVE_SNPRINTF
73   n = vsnprintf(strbuf, sizeof(strbuf), fmt, ap);
74 #else
75   n = vsprintf(strbuf, fmt, ap);
76 #endif
77   assert(0 <= n && n < sizeof(strbuf));
78 }
79
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);    \
86 } while (0)
87
88 #define TEST_REF(fmtargs) do {                                          \
89   int n = format fmtargs;                                               \
90   prepare 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);            \
95 } while (0)
96
97 #define LENGTHY                                                         \
98   "This is a rather longer string than the code is expecting: will it fit?"
99
100 /*----- Main program ------------------------------------------------------*/
101
102 int main(int argc, char *argv[])
103 {
104   struct tvec_test test;
105   int argpos;
106
107   tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
108   if (argpos < argc) die(2, "no input files expected");
109   tvec_adhoc(&tvstate, &test);
110
111   TESTGROUP("basics") {
112     TEST_KNOWN(("Hello, world!"), "Hello, world!");
113     TEST_KNOWN(("just a ->%%<- sign"), "just a ->%<- sign");
114   }
115
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  <-");
124   }
125
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<-");
133   }
134
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));
138   }
139
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<-");
146   }
147
148   TESTGROUP("misc")
149     TEST_KNOWN(("%s", LENGTHY), LENGTHY);
150
151   return (tvec_end(&tvstate));
152 }
153
154 /*----- That's all, folks -------------------------------------------------*/