2 * Copyright (c) 2020 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.
15 #include <vppinfra/mem.h>
16 #include <vlib/vlib.h>
18 #define MB_TEST_I(_cond, _comment, _args...) \
20 int _evald = (_cond); \
23 fformat (stderr, "FAIL:%d: " _comment "\n", __LINE__, ##_args); \
27 fformat (stderr, "PASS:%d: " _comment "\n", __LINE__, ##_args); \
32 #define MB_TEST(_cond, _comment, _args...) \
34 if (!MB_TEST_I (_cond, _comment, ##_args)) \
40 typedef struct test_struct_
46 mem_bulk_test_basic (vlib_main_t *vm, unformat_input_t *input)
48 int __clib_unused verbose, i, rv, n_iter = 1000;
49 test_struct_t *elt, **elts = 0;
50 clib_mem_bulk_handle_t mb;
52 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
54 if (unformat (input, "verbose"))
58 vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
64 mb = clib_mem_bulk_init (sizeof (test_struct_t), 0, 0);
66 for (i = 0; i < n_iter; i++)
68 elt = clib_mem_bulk_alloc (mb);
72 for (i = 0; i < n_iter; i++)
75 for (i = 0; i < n_iter; i++)
76 if (elts[i]->data != i)
77 MB_TEST (0, "data corrupted");
79 for (i = 0; i < n_iter; i++)
80 clib_mem_bulk_free (mb, elts[i]);
85 for (i = 0; i < n_iter; i++)
87 elt = clib_mem_bulk_alloc (mb);
91 for (i = n_iter - 1; i >= 0; i--)
94 for (i = n_iter - 1; i >= 0; i--)
95 if (elts[i]->data != i)
96 MB_TEST (0, "data corrupted");
98 for (i = 0; i < n_iter; i++)
99 clib_mem_bulk_free (mb, elts[i]);
101 clib_mem_bulk_destroy (mb);
107 static clib_error_t *
108 mem_bulk_test (vlib_main_t *vm, unformat_input_t *input,
109 vlib_cli_command_t *cmd_arg)
112 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
114 if (unformat (input, "basic"))
116 res = mem_bulk_test_basic (vm, input);
118 else if (unformat (input, "all"))
120 if ((res = mem_bulk_test_basic (vm, input)))
129 return clib_error_return (0, "llist unit test failed");
133 VLIB_CLI_COMMAND (mem_bulk_test_command, static) = {
134 .path = "test membulk",
135 .short_help = "internal membulk unit tests",
136 .function = mem_bulk_test,
140 * fd.io coding-style-patch-verification: ON
143 * eval: (c-set-style "gnu")