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 | 41 | #include "report.h" |
b1a20bee | 42 | |
3efcfd2d | 43 | #include "tvec.h" |
b1a20bee MW |
44 | #include "tvec-adhoc.h" |
45 | #include "tvec-types.h" | |
eff136f6 | 46 | |
c81c35df MW |
47 | /*----- Static variables --------------------------------------------------*/ |
48 | ||
3efcfd2d | 49 | static struct tvec_state tvstate; |
eff136f6 | 50 | static dstr d = DSTR_INIT; |
3efcfd2d | 51 | static char strbuf[1024]; |
eff136f6 | 52 | |
c81c35df MW |
53 | /*----- Utilities ---------------------------------------------------------*/ |
54 | ||
3efcfd2d | 55 | #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name) |
eff136f6 | 56 | |
31d0247c | 57 | static PRINTF_LIKE(1, 2) int format(const char *fmt, ...) |
eff136f6 MW |
58 | { |
59 | va_list ap; | |
912f6431 MW |
60 | int n; |
61 | ||
eff136f6 MW |
62 | va_start(ap, fmt); |
63 | dstr_reset(&d); | |
912f6431 | 64 | n = dstr_vputf(&d, fmt, &ap); |
eff136f6 | 65 | va_end(ap); |
912f6431 | 66 | return (n); |
eff136f6 MW |
67 | } |
68 | ||
31d0247c | 69 | static PRINTF_LIKE(1, 2) void prepare(const char *fmt, ...) |
eff136f6 MW |
70 | { |
71 | va_list ap; | |
72 | int n; | |
73 | ||
74 | va_start(ap, fmt); | |
75 | #ifdef HAVE_SNPRINTF | |
3efcfd2d | 76 | n = vsnprintf(strbuf, sizeof(strbuf), fmt, ap); |
eff136f6 | 77 | #else |
3efcfd2d | 78 | n = vsprintf(strbuf, fmt, ap); |
eff136f6 | 79 | #endif |
3efcfd2d | 80 | assert(0 <= n && n < sizeof(strbuf)); |
eff136f6 MW |
81 | } |
82 | ||
3efcfd2d | 83 | #define TEST_KNOWN(fmtargs, want) do { \ |
912f6431 MW |
84 | int n; n = format fmtargs; \ |
85 | tvec_claimeq_int(&tvstate, n, strlen(want), \ | |
86 | __FILE__, __LINE__, "format " #fmtargs); \ | |
c81c35df MW |
87 | tvec_claimeq_text(&tvstate, d.buf, d.len, want, strlen(want), \ |
88 | __FILE__, __LINE__, "format " #fmtargs); \ | |
eff136f6 MW |
89 | } while (0) |
90 | ||
3efcfd2d | 91 | #define TEST_REF(fmtargs) do { \ |
912f6431 | 92 | int n = format fmtargs; \ |
3efcfd2d | 93 | prepare fmtargs; \ |
912f6431 MW |
94 | tvec_claimeq_int(&tvstate, n, strlen(strbuf), \ |
95 | __FILE__, __LINE__, "format " #fmtargs); \ | |
c81c35df MW |
96 | tvec_claimeq_text(&tvstate, d.buf, d.len, strbuf, strlen(strbuf), \ |
97 | __FILE__, __LINE__, "format " #fmtargs); \ | |
eff136f6 MW |
98 | } while (0) |
99 | ||
c81c35df | 100 | #define LENGTHY \ |
eff136f6 MW |
101 | "This is a rather longer string than the code is expecting: will it fit?" |
102 | ||
c81c35df MW |
103 | /*----- Main program ------------------------------------------------------*/ |
104 | ||
3efcfd2d | 105 | int main(int argc, char *argv[]) |
eff136f6 | 106 | { |
3efcfd2d MW |
107 | int argpos; |
108 | ||
109 | tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig); | |
110 | if (argpos < argc) die(2, "no input files expected"); | |
b1a20bee | 111 | tvec_adhoc(&tvstate); |
3efcfd2d MW |
112 | |
113 | TESTGROUP("basics") { | |
114 | TEST_KNOWN(("Hello, world!"), "Hello, world!"); | |
115 | TEST_KNOWN(("just a ->%%<- sign"), "just a ->%<- sign"); | |
116 | } | |
117 | ||
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 <-"); | |
126 | } | |
127 | ||
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<-"); | |
135 | } | |
136 | ||
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)); | |
140 | } | |
141 | ||
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<-"); | |
148 | } | |
149 | ||
150 | TESTGROUP("misc") | |
151 | TEST_KNOWN(("%s", LENGTHY), LENGTHY); | |
152 | ||
153 | return (tvec_end(&tvstate)); | |
eff136f6 | 154 | } |
c81c35df MW |
155 | |
156 | /*----- That's all, folks -------------------------------------------------*/ |