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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #include <vppinfra/format.h>
44 format_test1 (u8 * s, va_list * va)
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);
52 expectation (const char *exp, char *fmt, ...)
58 test_vec = va_format (test_vec, fmt, &va);
61 vec_add1 (test_vec, 0);
62 if (strcmp (exp, (char *) test_vec))
64 fformat (stdout, "FAIL: %s (expected vs. result)\n\"%s\"\n\"%v\"\n",
69 fformat (stdout, "PASS: %s\n", fmt);
70 vec_delete (test_vec, vec_len (test_vec), 0);
75 test_format_main (unformat_input_t * input)
78 u8 *food = format (0, "food");
80 ret |= expectation ("foo", "foo");
81 ret |= expectation ("foo", "%s", "foo");
82 ret |= expectation ("9876", "%d", 9876);
83 ret |= expectation ("-9876", "%wd", (word) - 9876);
84 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");
114 format_foo (u8 * s, va_list * va)
116 foo_t *foo = va_arg (*va, foo_t *);
117 return format (s, "{a %d, b %d}", foo->a, foo->b);
121 unformat_foo (unformat_input_t * i, va_list * va)
123 foo_t *foo = va_arg (*va, foo_t *);
124 return unformat (i, "{%D,%D}",
125 sizeof (foo->a), &foo->a, sizeof (foo->b), &foo->b);
129 test_unformat_main (unformat_input_t * input)
136 foo_t foo = {.a = ~0,.b = ~0 };
140 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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",
166 v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
169 clib_warning ("unknown input `%U'\n", format_unformat_error, input);
179 main (int argc, char *argv[])
183 verbose = (argc > 1);
184 unformat_init_command_line (&i, argv);
186 if (unformat (&i, "unformat"))
187 return test_unformat_main (&i);
189 return test_format_main (&i);
194 * fd.io coding-style-patch-verification: ON
197 * eval: (c-set-style "gnu")