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) 2005 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>
39 #include <vppinfra/random.h>
40 #include <vppinfra/serialize.h>
41 #include <vppinfra/os.h>
43 #define foreach_my_vector_type \
51 foreach_my_vector_type
56 serialize_my_vector_type_single (serialize_main_t * m, va_list * va)
58 my_vector_type_t *v = va_arg (*va, my_vector_type_t *);
59 u32 n = va_arg (*va, u32);
62 for (i = 0; i < n; i++)
64 #define _(t,f) serialize_integer (m, v[i].f, sizeof (v[i].f));
65 foreach_my_vector_type;
71 unserialize_my_vector_type_single (serialize_main_t * m, va_list * va)
73 my_vector_type_t *v = va_arg (*va, my_vector_type_t *);
74 u32 n = va_arg (*va, u32);
77 for (i = 0; i < n; i++)
79 #define _(t,f) { u32 tmp; unserialize_integer (m, &tmp, sizeof (v[i].f)); v[i].f = tmp; }
80 foreach_my_vector_type;
86 serialize_my_vector_type_multiple (serialize_main_t * m, va_list * va)
88 my_vector_type_t *v = va_arg (*va, my_vector_type_t *);
89 u32 n = va_arg (*va, u32);
95 STRUCT_SIZE_OF (my_vector_type_t, f), \
96 STRUCT_STRIDE_OF (my_vector_type_t, f), \
99 foreach_my_vector_type;
105 unserialize_my_vector_type_multiple (serialize_main_t * m, va_list * va)
107 my_vector_type_t *v = va_arg (*va, my_vector_type_t *);
108 u32 n = va_arg (*va, u32);
111 unserialize_multiple \
114 STRUCT_SIZE_OF (my_vector_type_t, f), \
115 STRUCT_STRIDE_OF (my_vector_type_t, f), \
118 foreach_my_vector_type;
131 my_vector_type_t **test_vectors;
135 serialize_main_t serialize_main;
136 serialize_main_t unserialize_main;
137 } test_serialize_main_t;
148 test_serialize_not_inline_double_vector_expand (void)
150 serialize_main_t _m, *m = &_m;
156 vec_validate (test_pattern, 1023);
158 for (i = 0; i < vec_len (test_pattern); i++)
159 test_pattern[i] = i & 0xff;
161 serialize_open_vector (m, serialized);
162 p = serialize_get (m, 61);
163 clib_memcpy_fast (p, test_pattern, 61);
164 serialize_integer (m, 0xDEADBEEFFEEDFACEULL, 8);
165 p = serialize_get (m, vec_len (test_pattern) - 62);
166 clib_memcpy_fast (p, test_pattern + 61, vec_len (test_pattern) - 62);
167 serialized = serialize_close_vector (m);
169 magic = (u64 *) (serialized + 61);
171 if (*magic != clib_net_to_host_u64 (0xDEADBEEFFEEDFACEULL))
173 fformat (stderr, "BUG!\n");
180 test_serialize_main (unformat_input_t * input)
182 clib_error_t *error = 0;
183 test_serialize_main_t _tm, *tm = &_tm;
184 serialize_main_t *sm = &tm->serialize_main;
185 serialize_main_t *um = &tm->unserialize_main;
188 clib_memset (tm, 0, sizeof (tm[0]));
195 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
197 if (unformat (input, "iter %d", &tm->n_iter))
199 else if (unformat (input, "seed %d", &tm->seed))
201 else if (unformat (input, "file %s", &tm->dump_file))
203 else if (unformat (input, "max-len %d", &tm->max_len))
205 else if (unformat (input, "multiple %=", &tm->multiple, 1))
207 else if (unformat (input, "single %=", &tm->multiple, 0))
209 else if (unformat (input, "verbose %=", &tm->verbose, 1))
211 else if (unformat (input, "double-expand"))
213 test_serialize_not_inline_double_vector_expand ();
214 clib_warning ("serialize_not_inline double vector expand OK");
219 error = clib_error_create ("unknown input `%U'\n",
220 format_unformat_error, input);
226 tm->seed = random_default_seed ();
228 clib_warning ("iter %d seed %d max-len %d", tm->n_iter, tm->seed,
233 serialize_open_clib_file (sm, tm->dump_file);
236 serialize_open_vector (sm, 0);
238 vec_resize (tm->test_vectors, tm->n_iter);
239 for (i = 0; i < tm->n_iter; i++)
241 uword l = 1 + (random_u32 (&tm->seed) % tm->max_len);
242 my_vector_type_t *mv;
244 vec_resize (tm->test_vectors[i], l);
245 vec_foreach (mv, tm->test_vectors[i])
247 #define _(t,f) mv->f = random_u32 (&tm->seed) & pow2_mask (31);
248 foreach_my_vector_type;
252 vec_serialize (sm, tm->test_vectors[i],
253 tm->multiple ? serialize_my_vector_type_multiple :
254 serialize_my_vector_type_single);
258 clib_warning ("overflow vector max bytes %d",
259 vec_max_len (sm->stream.overflow_buffer));
261 serialize_close (sm);
266 if ((error = unserialize_open_clib_file (um, tm->dump_file)))
272 u8 *v = serialize_close_vector (sm);
273 unserialize_open_data (um, v, vec_len (v));
276 for (i = 0; i < tm->n_iter; i++)
278 my_vector_type_t *mv0;
279 my_vector_type_t *mv1;
281 vec_unserialize (um, &mv0,
282 tm->multiple ? unserialize_my_vector_type_multiple :
283 unserialize_my_vector_type_single);
284 mv1 = tm->test_vectors[i];
286 if (vec_len (mv0) != vec_len (mv1))
288 if (memcmp (mv0, mv1, vec_len (mv0) * sizeof (mv0[0])))
296 clib_error_report (error);
302 main (int argc, char *argv[])
307 clib_mem_init (0, 64ULL << 20);
309 unformat_init_command_line (&i, argv);
310 r = test_serialize_main (&i);
317 * fd.io coding-style-patch-verification: ON
320 * eval: (c-set-style "gnu")