chiark / gitweb /
@@@ tty mess
[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
43 #include "tvec.h"
44 #include "tvec-adhoc.h"
45 #include "tvec-types.h"
46
47 /*----- Static variables --------------------------------------------------*/
48
49 static struct tvec_state tvstate;
50 static dstr d = DSTR_INIT;
51 static char strbuf[1024];
52
53 /*----- Utilities ---------------------------------------------------------*/
54
55 #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name)
56
57 static PRINTF_LIKE(1, 2) int format(const char *fmt, ...)
58 {
59   va_list ap;
60   int n;
61
62   va_start(ap, fmt);
63   dstr_reset(&d);
64   n = dstr_vputf(&d, fmt, &ap);
65   va_end(ap);
66   return (n);
67 }
68
69 static PRINTF_LIKE(1, 2) void prepare(const char *fmt, ...)
70 {
71   va_list ap;
72   int n;
73
74   va_start(ap, fmt);
75 #ifdef HAVE_SNPRINTF
76   n = vsnprintf(strbuf, sizeof(strbuf), fmt, ap);
77 #else
78   n = vsprintf(strbuf, fmt, ap);
79 #endif
80   assert(0 <= n && n < sizeof(strbuf));
81 }
82
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);    \
89 } while (0)
90
91 #define TEST_REF(fmtargs) do {                                          \
92   int n = format fmtargs;                                               \
93   prepare 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);            \
98 } while (0)
99
100 #define LENGTHY                                                         \
101   "This is a rather longer string than the code is expecting: will it fit?"
102
103 /*----- Main program ------------------------------------------------------*/
104
105 int main(int argc, char *argv[])
106 {
107   int argpos;
108
109   tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
110   if (argpos < argc) die(2, "no input files expected");
111
112   TESTGROUP("basics") {
113     TEST_KNOWN(("Hello, world!"), "Hello, world!");
114     TEST_KNOWN(("just a ->%%<- sign"), "just a ->%<- sign");
115   }
116
117   TESTGROUP("integers") {
118     TEST_KNOWN(("Testing, testing, %d, %d, %d.", 1, 2, 3),
119                "Testing, testing, 1, 2, 3.");
120     TEST_KNOWN(("->%5d<-", 138), "->  138<-");
121     TEST_KNOWN(("->%*d<-", 5, 138), "->  138<-");
122     TEST_KNOWN(("->%-*d<-", 5, 138), "->138  <-");
123     TEST_KNOWN(("->%*d<-", -5, 138), "->138  <-");
124     TEST_KNOWN(("->%-*d<-", -5, 138), "->138  <-");
125   }
126
127   TESTGROUP("strings") {
128     TEST_KNOWN(("->%.*s<-", 5, "truncate me"), "->trunc<-");
129     TEST_KNOWN(("->%.*s<-", -5, "don't truncate me"),
130                "->don't truncate me<-");
131     TEST_KNOWN(("Truncation indirect: ->%.*s<-", 10,
132                 "a long string to be chopped"),
133                "Truncation indirect: ->a long str<-");
134   }
135
136   TESTGROUP("combinations") {
137     TEST_KNOWN(("%08lx:%s", 0x65604204ul, "tripe-ec"), "65604204:tripe-ec");
138     TEST_REF(("big float: ->%f<- and integer %d\n", DBL_MAX, 42));
139   }
140
141   TESTGROUP("argument-order") {
142     TEST_KNOWN(("Testing, testing, %3$d, %2$d, %1$d.", 3, 2, 1),
143                "Testing, testing, 1, 2, 3.");
144     TEST_KNOWN(("Truncation indirect: ->%1$.*2$s<-",
145                 "a long string to be chopped", 10),
146                "Truncation indirect: ->a long str<-");
147   }
148
149   TESTGROUP("misc")
150     TEST_KNOWN(("%s", LENGTHY), LENGTHY);
151
152   return (tvec_end(&tvstate));
153 }
154
155 /*----- That's all, folks -------------------------------------------------*/