blob: 8370be7a4609a7036ff9c0ec445b222fbe041307 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#include <vppinfra/format.h>
39
40static int verbose;
Dave Barachc3799992016-08-15 11:12:27 -040041static u8 *test_vec;
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
Dave Barachc3799992016-08-15 11:12:27 -040043static u8 *
44format_test1 (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070045{
46 uword x = va_arg (*va, uword);
47 f64 y = va_arg (*va, f64);
48 return format (s, "%12d %12f%12.4e", x, y, y);
49}
50
Dave Barachc3799992016-08-15 11:12:27 -040051static int
52expectation (const char *exp, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -070053{
54 int ret = 0;
55
56 va_list va;
57 va_start (va, fmt);
58 test_vec = va_format (test_vec, fmt, &va);
59 va_end (va);
60
Dave Barachc3799992016-08-15 11:12:27 -040061 vec_add1 (test_vec, 0);
62 if (strcmp (exp, (char *) test_vec))
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 {
64 fformat (stdout, "FAIL: %s (expected vs. result)\n\"%s\"\n\"%v\"\n",
65 fmt, exp, test_vec);
66 ret = 1;
67 }
68 else if (verbose)
69 fformat (stdout, "PASS: %s\n", fmt);
Dave Barachc3799992016-08-15 11:12:27 -040070 vec_delete (test_vec, vec_len (test_vec), 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 return ret;
72}
73
Dave Barachc3799992016-08-15 11:12:27 -040074int
75test_format_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
77 int ret = 0;
Dave Barachc3799992016-08-15 11:12:27 -040078 u8 *food = format (0, "food");
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
80 ret |= expectation ("foo", "foo");
81 ret |= expectation ("foo", "%s", "foo");
82 ret |= expectation ("9876", "%d", 9876);
Dave Barachc3799992016-08-15 11:12:27 -040083 ret |= expectation ("-9876", "%wd", (word) - 9876);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 ret |= expectation ("98765432", "%u", 98765432);
85 ret |= expectation ("1200ffee", "%x", 0x1200ffee);
86 ret |= expectation ("BABEBABE", "%X", 0xbabebabe);
87 ret |= expectation ("10%a", "%d%%%c", 10, 'a');
88 ret |= expectation ("123456789abcdef0", "%016Lx", 0x123456789abcdef0LL);
89 ret |= expectation ("00000123", "%08x", 0x123);
90 ret |= expectation (" 23 23 2.3037e1",
91 "%40U", format_test1, 23, 23.0367);
92 ret |= expectation ("left ", "%-10s", "left");
93 ret |= expectation (" center ", "%=10s", "center");
94 ret |= expectation (" right", "%+10s", "right");
95 ret |= expectation ("123456", "%.0f", 123456.);
96 ret |= expectation ("1234567.0", "%.1f", 1234567.);
97 ret |= expectation ("foo", "%.*s", 3, "food");
98 ret |= expectation ("food ", "%.*s", 10, "food ");
99 ret |= expectation ("(nil)", "%.*s", 3, (void *) 0);
100 ret |= expectation ("foo", "%.*v", 3, food);
101 ret |= expectation ("foobar", "%.*v%s", 3, food, "bar");
102 ret |= expectation ("foo bar", "%S", "foo_bar");
103 vec_free (food);
104 vec_free (test_vec);
105 return ret;
106}
107
Dave Barachc3799992016-08-15 11:12:27 -0400108typedef struct
109{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 int a, b;
111} foo_t;
112
Dave Barachc3799992016-08-15 11:12:27 -0400113static u8 *
114format_foo (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
Dave Barachc3799992016-08-15 11:12:27 -0400116 foo_t *foo = va_arg (*va, foo_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 return format (s, "{a %d, b %d}", foo->a, foo->b);
118}
119
Dave Barachc3799992016-08-15 11:12:27 -0400120static uword
121unformat_foo (unformat_input_t * i, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122{
Dave Barachc3799992016-08-15 11:12:27 -0400123 foo_t *foo = va_arg (*va, foo_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 return unformat (i, "{%D,%D}",
Dave Barachc3799992016-08-15 11:12:27 -0400125 sizeof (foo->a), &foo->a, sizeof (foo->b), &foo->b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126}
127
Dave Barachc3799992016-08-15 11:12:27 -0400128int
129test_unformat_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130{
131 u32 v[8];
132 long l;
133 long long ll;
134 f64 f;
Dave Barachc3799992016-08-15 11:12:27 -0400135 u8 *s;
136 foo_t foo = {.a = ~0,.b = ~0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
138 v[0] = v[1] = 0;
139
140 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
141 {
142 if (unformat (input, "01 %d %d", &v[0], &v[1]))
143 fformat (stdout, "got 01 %d %d\n", v[0], v[1]);
144 else if (unformat (input, "d %d", &v[0]))
145 fformat (stdout, "got it d %d\n", v[0]);
146 else if (unformat (input, "ld %ld", &l))
147 fformat (stdout, "got it ld %ld\n", l);
148 else if (unformat (input, "lld %lld", &ll))
149 fformat (stdout, "got it lld %lld\n", ll);
150 else if (unformat (input, "string %s", &s))
151 fformat (stdout, "got string `%s'\n", s);
152 else if (unformat (input, "float %f", &f))
153 fformat (stdout, "got float `%.4f'\n", f);
154 else if (unformat (input, "foo %U", unformat_foo, &foo))
155 fformat (stdout, "got a foo `%U'\n", format_foo, &foo);
156 else if (unformat (input, "ignore-me1"))
157 fformat (stdout, "got an `ignore-me1'\n");
158 else if (unformat (input, "ignore-me2"))
159 fformat (stdout, "got an `ignore-me2'\n");
160 else if (unformat (input, "gi%d_%d@-", &v[0], &v[1]))
161 fformat (stdout, "got `gi%d_%d@-'\n", v[0], v[1]);
162 else if (unformat (input, "%_%d.%d.%d.%d%_->%_%d.%d.%d.%d%_",
163 &v[0], &v[1], &v[2], &v[3],
164 &v[4], &v[5], &v[6], &v[7]))
165 fformat (stdout, "got %d.%d.%d.%d -> %d.%d.%d.%d",
Dave Barachc3799992016-08-15 11:12:27 -0400166 v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 else
168 {
Dave Barachc3799992016-08-15 11:12:27 -0400169 clib_warning ("unknown input `%U'\n", format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 return 1;
171 }
172 }
173
174 return 0;
175}
176
177#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400178int
179main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180{
181 unformat_input_t i;
182
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200183 clib_mem_init (0, 3ULL << 30);
184
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 verbose = (argc > 1);
186 unformat_init_command_line (&i, argv);
187
188 if (unformat (&i, "unformat"))
189 return test_unformat_main (&i);
190 else
191 return test_format_main (&i);
192}
193#endif
Dave Barachc3799992016-08-15 11:12:27 -0400194
195/*
196 * fd.io coding-style-patch-verification: ON
197 *
198 * Local Variables:
199 * eval: (c-set-style "gnu")
200 * End:
201 */