Line data Source code
1 : /*
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:
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 : #include <vppinfra/mem.h>
16 : #include <vlib/vlib.h>
17 :
18 : #define MB_TEST_I(_cond, _comment, _args...) \
19 : ({ \
20 : int _evald = (_cond); \
21 : if (!(_evald)) \
22 : { \
23 : fformat (stderr, "FAIL:%d: " _comment "\n", __LINE__, ##_args); \
24 : } \
25 : else \
26 : { \
27 : fformat (stderr, "PASS:%d: " _comment "\n", __LINE__, ##_args); \
28 : } \
29 : _evald; \
30 : })
31 :
32 : #define MB_TEST(_cond, _comment, _args...) \
33 : { \
34 : if (!MB_TEST_I (_cond, _comment, ##_args)) \
35 : { \
36 : return 1; \
37 : } \
38 : }
39 :
40 : typedef struct test_struct_
41 : {
42 : u32 data;
43 : } test_struct_t;
44 :
45 : static int
46 0 : mem_bulk_test_basic (vlib_main_t *vm, unformat_input_t *input)
47 : {
48 0 : int __clib_unused verbose, i, rv, n_iter = 1000;
49 0 : test_struct_t *elt, **elts = 0;
50 : clib_mem_bulk_handle_t mb;
51 :
52 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
53 : {
54 0 : if (unformat (input, "verbose"))
55 0 : verbose = 1;
56 : else
57 : {
58 0 : vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
59 : input);
60 0 : return -1;
61 : }
62 : }
63 :
64 0 : mb = clib_mem_bulk_init (sizeof (test_struct_t), 0, 0);
65 :
66 0 : for (i = 0; i < n_iter; i++)
67 : {
68 0 : elt = clib_mem_bulk_alloc (mb);
69 0 : vec_add1 (elts, elt);
70 : }
71 :
72 0 : for (i = 0; i < n_iter; i++)
73 0 : elts[i]->data = i;
74 :
75 0 : for (i = 0; i < n_iter; i++)
76 0 : if (elts[i]->data != i)
77 0 : MB_TEST (0, "data corrupted");
78 :
79 0 : for (i = 0; i < n_iter; i++)
80 0 : clib_mem_bulk_free (mb, elts[i]);
81 :
82 : /*
83 : * realloc all
84 : */
85 0 : for (i = 0; i < n_iter; i++)
86 : {
87 0 : elt = clib_mem_bulk_alloc (mb);
88 0 : vec_add1 (elts, elt);
89 : }
90 :
91 0 : for (i = n_iter - 1; i >= 0; i--)
92 0 : elts[i]->data = i;
93 :
94 0 : for (i = n_iter - 1; i >= 0; i--)
95 0 : if (elts[i]->data != i)
96 0 : MB_TEST (0, "data corrupted");
97 :
98 0 : for (i = 0; i < n_iter; i++)
99 0 : clib_mem_bulk_free (mb, elts[i]);
100 :
101 0 : clib_mem_bulk_destroy (mb);
102 0 : vec_free (elts);
103 :
104 0 : return 0;
105 : }
106 :
107 : static clib_error_t *
108 0 : mem_bulk_test (vlib_main_t *vm, unformat_input_t *input,
109 : vlib_cli_command_t *cmd_arg)
110 : {
111 0 : int res = 0;
112 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
113 : {
114 0 : if (unformat (input, "basic"))
115 : {
116 0 : res = mem_bulk_test_basic (vm, input);
117 : }
118 0 : else if (unformat (input, "all"))
119 : {
120 0 : if ((res = mem_bulk_test_basic (vm, input)))
121 0 : goto done;
122 : }
123 : else
124 0 : break;
125 : }
126 :
127 0 : done:
128 0 : if (res)
129 0 : return clib_error_return (0, "llist unit test failed");
130 0 : return 0;
131 : }
132 :
133 16239 : VLIB_CLI_COMMAND (mem_bulk_test_command, static) = {
134 : .path = "test membulk",
135 : .short_help = "internal membulk unit tests",
136 : .function = mem_bulk_test,
137 : };
138 :
139 : /*
140 : * fd.io coding-style-patch-verification: ON
141 : *
142 : * Local Variables:
143 : * eval: (c-set-style "gnu")
144 : * End:
145 : */
|