Reorganize source tree to use single autotools instance
[vpp.git] / src / vppinfra / test_format.c
1 /*
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
40 static int verbose;
41 static u8 *test_vec;
42
43 static u8 *
44 format_test1 (u8 * s, va_list * va)
45 {
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
51 static int
52 expectation (const char *exp, char *fmt, ...)
53 {
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
61   vec_add1 (test_vec, 0);
62   if (strcmp (exp, (char *) test_vec))
63     {
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);
70   vec_delete (test_vec, vec_len (test_vec), 0);
71   return ret;
72 }
73
74 int
75 test_format_main (unformat_input_t * input)
76 {
77   int ret = 0;
78   u8 *food = format (0, "food");
79
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");
103   vec_free (food);
104   vec_free (test_vec);
105   return ret;
106 }
107
108 typedef struct
109 {
110   int a, b;
111 } foo_t;
112
113 static u8 *
114 format_foo (u8 * s, va_list * va)
115 {
116   foo_t *foo = va_arg (*va, foo_t *);
117   return format (s, "{a %d, b %d}", foo->a, foo->b);
118 }
119
120 static uword
121 unformat_foo (unformat_input_t * i, va_list * va)
122 {
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);
126 }
127
128 int
129 test_unformat_main (unformat_input_t * input)
130 {
131   u32 v[8];
132   long l;
133   long long ll;
134   f64 f;
135   u8 *s;
136   foo_t foo = {.a = ~0,.b = ~0 };
137
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",
166                  v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
167       else
168         {
169           clib_warning ("unknown input `%U'\n", format_unformat_error, input);
170           return 1;
171         }
172     }
173
174   return 0;
175 }
176
177 #ifdef CLIB_UNIX
178 int
179 main (int argc, char *argv[])
180 {
181   unformat_input_t i;
182
183   verbose = (argc > 1);
184   unformat_init_command_line (&i, argv);
185
186   if (unformat (&i, "unformat"))
187     return test_unformat_main (&i);
188   else
189     return test_format_main (&i);
190 }
191 #endif
192
193 /*
194  * fd.io coding-style-patch-verification: ON
195  *
196  * Local Variables:
197  * eval: (c-set-style "gnu")
198  * End:
199  */