]>
Commit | Line | Data |
---|---|---|
b9911c2e DL |
1 | /* |
2 | * printfrr() unit test | |
3 | * Copyright (C) 2019 David Lamparter | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #include "zebra.h" | |
21 | ||
22 | #include <math.h> | |
23 | ||
24 | #include "lib/printfrr.h" | |
25 | #include "lib/memory.h" | |
26 | #include "lib/prefix.h" | |
27 | ||
28 | static int errors; | |
29 | ||
afb35622 | 30 | static void printcmp(const char *fmt, ...) PRINTFRR(1, 2); |
b9911c2e DL |
31 | static void printcmp(const char *fmt, ...) |
32 | { | |
33 | va_list ap; | |
34 | char buf[256], bufrr[256], *p; | |
35 | int cmp; | |
36 | memset(bufrr, 0xcc, sizeof(bufrr)); | |
37 | ||
38 | va_start(ap, fmt); | |
39 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
40 | va_end(ap); | |
41 | ||
42 | va_start(ap, fmt); | |
43 | vsnprintfrr(bufrr, sizeof(bufrr), fmt, ap); | |
44 | va_end(ap); | |
45 | ||
46 | cmp = strcmp(buf, bufrr); | |
47 | ||
48 | /* OS dependent "+nan" vs. "nan" */ | |
49 | if (cmp && (p = strstr(bufrr, "+nan"))) { | |
50 | p[0] = ' '; | |
51 | if (!strcmp(buf, bufrr)) | |
52 | cmp = 0; | |
53 | p[0] = '+'; | |
54 | } | |
55 | printf("fmt: \"%s\"\nsys: \"%s\"\nfrr: \"%s\"\n%s\n\n", | |
56 | fmt, buf, bufrr, cmp ? "ERROR" : "ok"); | |
57 | ||
58 | if (cmp) | |
59 | errors++; | |
60 | } | |
61 | ||
afb35622 | 62 | static void printchk(const char *ref, const char *fmt, ...) PRINTFRR(2, 3); |
b9911c2e DL |
63 | static void printchk(const char *ref, const char *fmt, ...) |
64 | { | |
65 | va_list ap; | |
66 | char bufrr[256]; | |
67 | memset(bufrr, 0xcc, sizeof(bufrr)); | |
68 | ||
69 | va_start(ap, fmt); | |
70 | vsnprintfrr(bufrr, sizeof(bufrr), fmt, ap); | |
71 | va_end(ap); | |
72 | ||
73 | printf("fmt: \"%s\"\nref: \"%s\"\nfrr: \"%s\"\n%s\n\n", | |
74 | fmt, ref, bufrr, strcmp(ref, bufrr) ? "ERROR" : "ok"); | |
75 | if (strcmp(ref, bufrr)) | |
76 | errors++; | |
77 | } | |
78 | ||
79 | int main(int argc, char **argv) | |
80 | { | |
81 | size_t i; | |
82 | float flts[] = { | |
83 | 123.456789, | |
84 | 23.456789e-30, | |
85 | 3.456789e+30, | |
86 | INFINITY, | |
87 | NAN, | |
88 | }; | |
89 | uint64_t ui64 = 0xfeed1278cafef00d; | |
90 | struct in_addr ip; | |
91 | char *p; | |
92 | char buf[256]; | |
93 | ||
94 | printcmp("%d %u %d %u", 123, 123, -456, -456); | |
95 | printcmp("%lld %llu %lld %llu", 123LL, 123LL, -456LL, -456LL); | |
96 | ||
97 | printcmp("%-20s,%20s,%.20s", "test", "test", "test"); | |
98 | printcmp("%-3s,%3s,%.3s", "test", "test", "test"); | |
99 | printcmp("%-6.3s,%6.3s,%6.3s", "test", "test", "test"); | |
100 | printcmp("%*s,%*s,%.*s", -3, "test", 3, "test", 3, "test"); | |
101 | ||
102 | for (i = 0; i < array_size(flts); i++) { | |
103 | printcmp("%-6.3e,%6.3e,%+06.3e", flts[i], flts[i], flts[i]); | |
104 | printcmp("%-6.3f,%6.3f,%+06.3f", flts[i], flts[i], flts[i]); | |
105 | printcmp("%-6.3g,%6.3g,%+06.3g", flts[i], flts[i], flts[i]); | |
106 | printcmp("%-6.3a,%6.3a,%+06.3a", flts[i], flts[i], flts[i]); | |
107 | } | |
108 | ||
109 | printchk("-77385308584349683 18369358765125201933 feed1278cafef00d", | |
110 | "%Ld %Lu %Lx", ui64, ui64, ui64); | |
111 | ||
112 | inet_aton("192.168.1.2", &ip); | |
113 | printchk("192.168.1.2", "%pI4", &ip); | |
114 | printchk(" 192.168.1.2", "%20pI4", &ip); | |
115 | ||
116 | printcmp("%p", &ip); | |
117 | ||
118 | snprintfrr(buf, sizeof(buf), "test%s", "#1"); | |
119 | csnprintfrr(buf, sizeof(buf), "test%s", "#2"); | |
120 | assert(strcmp(buf, "test#1test#2") == 0); | |
121 | ||
122 | p = asnprintfrr(MTYPE_TMP, buf, sizeof(buf), "test%s", "#3"); | |
123 | assert(p == buf); | |
124 | assert(strcmp(buf, "test#3") == 0); | |
125 | ||
126 | p = asnprintfrr(MTYPE_TMP, buf, 4, "test%s", "#4"); | |
127 | assert(p != buf); | |
128 | assert(strcmp(p, "test#4") == 0); | |
129 | XFREE(MTYPE_TMP, p); | |
130 | ||
131 | p = asprintfrr(MTYPE_TMP, "test%s", "#5"); | |
132 | assert(strcmp(p, "test#5") == 0); | |
133 | XFREE(MTYPE_TMP, p); | |
134 | ||
135 | struct prefix_sg sg; | |
136 | sg.src.s_addr = INADDR_ANY; | |
137 | sg.grp.s_addr = INADDR_ANY; | |
138 | printchk("(*,*)", "%pSG4", &sg); | |
139 | ||
140 | inet_aton("192.168.1.2", &sg.src); | |
141 | printchk("(192.168.1.2,*)", "%pSG4", &sg); | |
142 | ||
143 | inet_aton("224.1.2.3", &sg.grp); | |
144 | printchk("(192.168.1.2,224.1.2.3)", "%pSG4", &sg); | |
145 | ||
146 | sg.src.s_addr = INADDR_ANY; | |
147 | printchk("(*,224.1.2.3)", "%pSG4", &sg); | |
148 | ||
149 | return !!errors; | |
150 | } |