Commit | Line | Data |
---|---|---|
c81c35df MW |
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 | ||
eff136f6 MW |
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" | |
36188114 | 40 | #include "macros.h" |
3efcfd2d MW |
41 | #include "report.h" |
42 | #include "tvec.h" | |
eff136f6 | 43 | |
c81c35df MW |
44 | /*----- Static variables --------------------------------------------------*/ |
45 | ||
3efcfd2d | 46 | static struct tvec_state tvstate; |
eff136f6 | 47 | static dstr d = DSTR_INIT; |
3efcfd2d | 48 | static char strbuf[1024]; |
eff136f6 | 49 | |
c81c35df MW |
50 | /*----- Utilities ---------------------------------------------------------*/ |
51 | ||
3efcfd2d | 52 | #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name) |
eff136f6 | 53 | |
31d0247c | 54 | static PRINTF_LIKE(1, 2) int format(const char *fmt, ...) |
eff136f6 MW |
55 | { |
56 | va_list ap; | |
912f6431 MW |
57 | int n; |
58 | ||
eff136f6 MW |
59 | va_start(ap, fmt); |
60 | dstr_reset(&d); | |
912f6431 | 61 | n = dstr_vputf(&d, fmt, &ap); |
eff136f6 | 62 | va_end(ap); |
912f6431 | 63 | return (n); |
eff136f6 MW |
64 | } |
65 | ||
31d0247c | 66 | static PRINTF_LIKE(1, 2) void prepare(const char *fmt, ...) |
eff136f6 MW |
67 | { |
68 | va_list ap; | |
69 | int n; | |
70 | ||
71 | va_start(ap, fmt); | |
72 | #ifdef HAVE_SNPRINTF | |
3efcfd2d | 73 | n = vsnprintf(strbuf, sizeof(strbuf), fmt, ap); |
eff136f6 | 74 | #else |
3efcfd2d | 75 | n = vsprintf(strbuf, fmt, ap); |
eff136f6 | 76 | #endif |
3efcfd2d | 77 | assert(0 <= n && n < sizeof(strbuf)); |
eff136f6 MW |
78 | } |
79 | ||
3efcfd2d | 80 | #define TEST_KNOWN(fmtargs, want) do { \ |
912f6431 MW |
81 | int n; n = format fmtargs; \ |
82 | tvec_claimeq_int(&tvstate, n, strlen(want), \ | |
83 | __FILE__, __LINE__, "format " #fmtargs); \ | |
c81c35df MW |
84 | tvec_claimeq_text(&tvstate, d.buf, d.len, want, strlen(want), \ |
85 | __FILE__, __LINE__, "format " #fmtargs); \ | |
eff136f6 MW |
86 | } while (0) |
87 | ||
3efcfd2d | 88 | #define TEST_REF(fmtargs) do { \ |
912f6431 | 89 | int n = format fmtargs; \ |
3efcfd2d | 90 | prepare fmtargs; \ |
912f6431 MW |
91 | tvec_claimeq_int(&tvstate, n, strlen(strbuf), \ |
92 | __FILE__, __LINE__, "format " #fmtargs); \ | |
c81c35df MW |
93 | tvec_claimeq_text(&tvstate, d.buf, d.len, strbuf, strlen(strbuf), \ |
94 | __FILE__, __LINE__, "format " #fmtargs); \ | |
eff136f6 MW |
95 | } while (0) |
96 | ||
c81c35df | 97 | #define LENGTHY \ |
eff136f6 MW |
98 | "This is a rather longer string than the code is expecting: will it fit?" |
99 | ||
c81c35df MW |
100 | /*----- Main program ------------------------------------------------------*/ |
101 | ||
3efcfd2d | 102 | int main(int argc, char *argv[]) |
eff136f6 | 103 | { |
3efcfd2d MW |
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)); | |
eff136f6 | 152 | } |
c81c35df MW |
153 | |
154 | /*----- That's all, folks -------------------------------------------------*/ |