diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/unittest/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/plugins/unittest/mem_bulk_test.c | 145 | ||||
-rw-r--r-- | src/vppinfra/mem_bulk.c | 2 |
3 files changed, 147 insertions, 1 deletions
diff --git a/src/plugins/unittest/CMakeLists.txt b/src/plugins/unittest/CMakeLists.txt index ba6a82280f6..2b358a5d906 100644 --- a/src/plugins/unittest/CMakeLists.txt +++ b/src/plugins/unittest/CMakeLists.txt @@ -35,6 +35,7 @@ add_vpp_plugin(unittest ipsec_test.c llist_test.c mactime_test.c + mem_bulk_test.c mfib_test.c mpcap_node.c punt_test.c diff --git a/src/plugins/unittest/mem_bulk_test.c b/src/plugins/unittest/mem_bulk_test.c new file mode 100644 index 00000000000..9d2f60d59a8 --- /dev/null +++ b/src/plugins/unittest/mem_bulk_test.c @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2020 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <vppinfra/mem.h> +#include <vlib/vlib.h> + +#define MB_TEST_I(_cond, _comment, _args...) \ + ({ \ + int _evald = (_cond); \ + if (!(_evald)) \ + { \ + fformat (stderr, "FAIL:%d: " _comment "\n", __LINE__, ##_args); \ + } \ + else \ + { \ + fformat (stderr, "PASS:%d: " _comment "\n", __LINE__, ##_args); \ + } \ + _evald; \ + }) + +#define MB_TEST(_cond, _comment, _args...) \ + { \ + if (!MB_TEST_I (_cond, _comment, ##_args)) \ + { \ + return 1; \ + } \ + } + +typedef struct test_struct_ +{ + u32 data; +} test_struct_t; + +static int +mem_bulk_test_basic (vlib_main_t *vm, unformat_input_t *input) +{ + int __clib_unused verbose, i, rv, n_iter = 1000; + test_struct_t *elt, **elts = 0; + clib_mem_bulk_handle_t mb; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "verbose")) + verbose = 1; + else + { + vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, + input); + return -1; + } + } + + mb = clib_mem_bulk_init (sizeof (test_struct_t), 0, 0); + + for (i = 0; i < n_iter; i++) + { + elt = clib_mem_bulk_alloc (mb); + vec_add1 (elts, elt); + } + + for (i = 0; i < n_iter; i++) + elts[i]->data = i; + + for (i = 0; i < n_iter; i++) + if (elts[i]->data != i) + MB_TEST (0, "data corrupted"); + + for (i = 0; i < n_iter; i++) + clib_mem_bulk_free (mb, elts[i]); + + /* + * realloc all + */ + for (i = 0; i < n_iter; i++) + { + elt = clib_mem_bulk_alloc (mb); + vec_add1 (elts, elt); + } + + for (i = n_iter - 1; i >= 0; i--) + elts[i]->data = i; + + for (i = n_iter - 1; i >= 0; i--) + if (elts[i]->data != i) + MB_TEST (0, "data corrupted"); + + for (i = 0; i < n_iter; i++) + clib_mem_bulk_free (mb, elts[i]); + + clib_mem_bulk_destroy (mb); + vec_free (elts); + + return 0; +} + +static clib_error_t * +mem_bulk_test (vlib_main_t *vm, unformat_input_t *input, + vlib_cli_command_t *cmd_arg) +{ + int res = 0; + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "basic")) + { + res = mem_bulk_test_basic (vm, input); + } + else if (unformat (input, "all")) + { + if ((res = mem_bulk_test_basic (vm, input))) + goto done; + } + else + break; + } + +done: + if (res) + return clib_error_return (0, "llist unit test failed"); + return 0; +} + +VLIB_CLI_COMMAND (mem_bulk_test_command, static) = { + .path = "test membulk", + .short_help = "internal membulk unit tests", + .function = mem_bulk_test, +}; + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vppinfra/mem_bulk.c b/src/vppinfra/mem_bulk.c index 76fc7585e37..2f236b4785f 100644 --- a/src/vppinfra/mem_bulk.c +++ b/src/vppinfra/mem_bulk.c @@ -173,7 +173,7 @@ clib_mem_bulk_free (clib_mem_bulk_handle_t h, void *p) u32 elt_idx = (offset - b->chunk_hdr_sz) / b->elt_sz; ASSERT (elt_idx < b->elts_per_chunk); - ASSERT (get_chunk_elt_ptr (b, c, elt_idx) != p); + ASSERT (get_chunk_elt_ptr (b, c, elt_idx) == p); c->n_free++; |