Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 41 | static u8 *test_vec; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 43 | static u8 * |
| 44 | format_test1 (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 51 | static int |
| 52 | expectation (const char *exp, char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 61 | vec_add1 (test_vec, 0); |
| 62 | if (strcmp (exp, (char *) test_vec)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 70 | vec_delete (test_vec, vec_len (test_vec), 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | return ret; |
| 72 | } |
| 73 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 74 | int |
| 75 | test_format_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | { |
| 77 | int ret = 0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 78 | u8 *food = format (0, "food"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | |
| 80 | ret |= expectation ("foo", "foo"); |
| 81 | ret |= expectation ("foo", "%s", "foo"); |
| 82 | ret |= expectation ("9876", "%d", 9876); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 83 | ret |= expectation ("-9876", "%wd", (word) - 9876); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 108 | typedef struct |
| 109 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | int a, b; |
| 111 | } foo_t; |
| 112 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 113 | static u8 * |
| 114 | format_foo (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 116 | foo_t *foo = va_arg (*va, foo_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | return format (s, "{a %d, b %d}", foo->a, foo->b); |
| 118 | } |
| 119 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 120 | static uword |
| 121 | unformat_foo (unformat_input_t * i, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 123 | foo_t *foo = va_arg (*va, foo_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | return unformat (i, "{%D,%D}", |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 125 | sizeof (foo->a), &foo->a, sizeof (foo->b), &foo->b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 128 | int |
| 129 | test_unformat_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | { |
| 131 | u32 v[8]; |
| 132 | long l; |
| 133 | long long ll; |
| 134 | f64 f; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 135 | u8 *s; |
| 136 | foo_t foo = {.a = ~0,.b = ~0 }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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", |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 166 | v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | else |
| 168 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 169 | clib_warning ("unknown input `%U'\n", format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | return 1; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 178 | int |
| 179 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | { |
| 181 | unformat_input_t i; |
| 182 | |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 183 | clib_mem_init (0, 3ULL << 30); |
| 184 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * fd.io coding-style-patch-verification: ON |
| 197 | * |
| 198 | * Local Variables: |
| 199 | * eval: (c-set-style "gnu") |
| 200 | * End: |
| 201 | */ |