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) 2005 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 | #include <vppinfra/random.h> |
| 40 | #include <vppinfra/serialize.h> |
| 41 | #include <vppinfra/os.h> |
| 42 | |
| 43 | #define foreach_my_vector_type \ |
| 44 | _ (u8, a8) \ |
| 45 | _ (u16, a16) \ |
| 46 | _ (u32, a32) |
| 47 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 48 | typedef struct |
| 49 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | #define _(t,f) t f; |
| 51 | foreach_my_vector_type |
| 52 | #undef _ |
| 53 | } my_vector_type_t; |
| 54 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | static void |
| 56 | serialize_my_vector_type_single (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 58 | my_vector_type_t *v = va_arg (*va, my_vector_type_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | u32 n = va_arg (*va, u32); |
| 60 | u32 i; |
| 61 | |
| 62 | for (i = 0; i < n; i++) |
| 63 | { |
| 64 | #define _(t,f) serialize_integer (m, v[i].f, sizeof (v[i].f)); |
| 65 | foreach_my_vector_type; |
| 66 | } |
| 67 | #undef _ |
| 68 | } |
| 69 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 70 | static void |
| 71 | unserialize_my_vector_type_single (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 73 | my_vector_type_t *v = va_arg (*va, my_vector_type_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | u32 n = va_arg (*va, u32); |
| 75 | u32 i; |
| 76 | |
| 77 | for (i = 0; i < n; i++) |
| 78 | { |
| 79 | #define _(t,f) { u32 tmp; unserialize_integer (m, &tmp, sizeof (v[i].f)); v[i].f = tmp; } |
| 80 | foreach_my_vector_type; |
| 81 | #undef _ |
| 82 | } |
| 83 | } |
| 84 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 85 | static void |
| 86 | serialize_my_vector_type_multiple (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | my_vector_type_t *v = va_arg (*va, my_vector_type_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | u32 n = va_arg (*va, u32); |
| 90 | |
| 91 | #define _(t,f) \ |
| 92 | serialize_multiple \ |
| 93 | (m, \ |
| 94 | &v[0].f, \ |
| 95 | STRUCT_SIZE_OF (my_vector_type_t, f), \ |
| 96 | STRUCT_STRIDE_OF (my_vector_type_t, f), \ |
| 97 | n); |
| 98 | |
| 99 | foreach_my_vector_type; |
| 100 | |
| 101 | #undef _ |
| 102 | } |
| 103 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 104 | static void |
| 105 | unserialize_my_vector_type_multiple (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 107 | my_vector_type_t *v = va_arg (*va, my_vector_type_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | u32 n = va_arg (*va, u32); |
| 109 | |
| 110 | #define _(t,f) \ |
| 111 | unserialize_multiple \ |
| 112 | (m, \ |
| 113 | &v[0].f, \ |
| 114 | STRUCT_SIZE_OF (my_vector_type_t, f), \ |
| 115 | STRUCT_STRIDE_OF (my_vector_type_t, f), \ |
| 116 | n); |
| 117 | |
| 118 | foreach_my_vector_type; |
| 119 | |
| 120 | #undef _ |
| 121 | } |
| 122 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 123 | typedef struct |
| 124 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | u32 n_iter; |
| 126 | u32 seed; |
| 127 | u32 verbose; |
| 128 | u32 multiple; |
| 129 | u32 max_len; |
| 130 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | my_vector_type_t **test_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 133 | char *dump_file; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
| 135 | serialize_main_t serialize_main; |
| 136 | serialize_main_t unserialize_main; |
| 137 | } test_serialize_main_t; |
| 138 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 139 | int |
| 140 | test_serialize_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 142 | clib_error_t *error = 0; |
| 143 | test_serialize_main_t _tm, *tm = &_tm; |
| 144 | serialize_main_t *sm = &tm->serialize_main; |
| 145 | serialize_main_t *um = &tm->unserialize_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | uword i; |
| 147 | |
| 148 | memset (tm, 0, sizeof (tm[0])); |
| 149 | tm->n_iter = 100; |
| 150 | tm->seed = 1; |
| 151 | tm->max_len = 128; |
| 152 | tm->verbose = 0; |
| 153 | tm->multiple = 1; |
| 154 | |
| 155 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 156 | { |
| 157 | if (unformat (input, "iter %d", &tm->n_iter)) |
| 158 | ; |
| 159 | else if (unformat (input, "seed %d", &tm->seed)) |
| 160 | ; |
| 161 | else if (unformat (input, "file %s", &tm->dump_file)) |
| 162 | ; |
| 163 | else if (unformat (input, "max-len %d", &tm->max_len)) |
| 164 | ; |
| 165 | else if (unformat (input, "multiple %=", &tm->multiple, 1)) |
| 166 | ; |
| 167 | else if (unformat (input, "single %=", &tm->multiple, 0)) |
| 168 | ; |
| 169 | else if (unformat (input, "verbose %=", &tm->verbose, 1)) |
| 170 | ; |
| 171 | else |
| 172 | { |
| 173 | error = clib_error_create ("unknown input `%U'\n", |
| 174 | format_unformat_error, input); |
| 175 | goto done; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (tm->seed == 0) |
| 180 | tm->seed = random_default_seed (); |
| 181 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 182 | clib_warning ("iter %d seed %d max-len %d", tm->n_iter, tm->seed, |
| 183 | tm->max_len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | |
| 185 | #ifdef CLIB_UNIX |
| 186 | if (tm->dump_file) |
| 187 | serialize_open_unix_file (sm, tm->dump_file); |
| 188 | else |
| 189 | #endif |
| 190 | serialize_open_vector (sm, 0); |
| 191 | |
| 192 | vec_resize (tm->test_vectors, tm->n_iter); |
| 193 | for (i = 0; i < tm->n_iter; i++) |
| 194 | { |
| 195 | uword l = 1 + (random_u32 (&tm->seed) % tm->max_len); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 196 | my_vector_type_t *mv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | |
| 198 | vec_resize (tm->test_vectors[i], l); |
| 199 | vec_foreach (mv, tm->test_vectors[i]) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 200 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | #define _(t,f) mv->f = random_u32 (&tm->seed) & pow2_mask (31); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 202 | foreach_my_vector_type; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | #undef _ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 204 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
| 206 | vec_serialize (sm, tm->test_vectors[i], |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 207 | tm->multiple ? serialize_my_vector_type_multiple : |
| 208 | serialize_my_vector_type_single); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (tm->verbose) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 212 | clib_warning ("overflow vector max bytes %d", |
| 213 | vec_max_len (sm->stream.overflow_buffer)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | |
| 215 | serialize_close (sm); |
| 216 | |
| 217 | #ifdef CLIB_UNIX |
| 218 | if (tm->dump_file) |
| 219 | { |
| 220 | if ((error = unserialize_open_unix_file (um, tm->dump_file))) |
| 221 | goto done; |
| 222 | } |
| 223 | else |
| 224 | #endif |
| 225 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 226 | u8 *v = serialize_close_vector (sm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | unserialize_open_data (um, v, vec_len (v)); |
| 228 | } |
| 229 | |
| 230 | for (i = 0; i < tm->n_iter; i++) |
| 231 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 232 | my_vector_type_t *mv0; |
| 233 | my_vector_type_t *mv1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | |
| 235 | vec_unserialize (um, &mv0, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 236 | tm->multiple ? unserialize_my_vector_type_multiple : |
| 237 | unserialize_my_vector_type_single); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | mv1 = tm->test_vectors[i]; |
| 239 | |
| 240 | if (vec_len (mv0) != vec_len (mv1)) |
| 241 | os_panic (); |
| 242 | if (memcmp (mv0, mv1, vec_len (mv0) * sizeof (mv0[0]))) |
| 243 | os_panic (); |
| 244 | |
| 245 | vec_free (mv0); |
| 246 | } |
| 247 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 248 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | if (error) |
| 250 | clib_error_report (error); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 255 | int |
| 256 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | { |
| 258 | unformat_input_t i; |
| 259 | int r; |
| 260 | |
| 261 | unformat_init_command_line (&i, argv); |
| 262 | r = test_serialize_main (&i); |
| 263 | unformat_free (&i); |
| 264 | return r; |
| 265 | } |
| 266 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * fd.io coding-style-patch-verification: ON |
| 270 | * |
| 271 | * Local Variables: |
| 272 | * eval: (c-set-style "gnu") |
| 273 | * End: |
| 274 | */ |