crypto crypto-openssl: support hashing operations
[vpp.git] / src / plugins / unittest / mem_bulk_test.c
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 mem_bulk_test_basic (vlib_main_t *vm, unformat_input_t *input)
47 {
48   int __clib_unused verbose, i, rv, n_iter = 1000;
49   test_struct_t *elt, **elts = 0;
50   clib_mem_bulk_handle_t mb;
51
52   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
53     {
54       if (unformat (input, "verbose"))
55         verbose = 1;
56       else
57         {
58           vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
59                            input);
60           return -1;
61         }
62     }
63
64   mb = clib_mem_bulk_init (sizeof (test_struct_t), 0, 0);
65
66   for (i = 0; i < n_iter; i++)
67     {
68       elt = clib_mem_bulk_alloc (mb);
69       vec_add1 (elts, elt);
70     }
71
72   for (i = 0; i < n_iter; i++)
73     elts[i]->data = i;
74
75   for (i = 0; i < n_iter; i++)
76     if (elts[i]->data != i)
77       MB_TEST (0, "data corrupted");
78
79   for (i = 0; i < n_iter; i++)
80     clib_mem_bulk_free (mb, elts[i]);
81
82   /*
83    * realloc all
84    */
85   for (i = 0; i < n_iter; i++)
86     {
87       elt = clib_mem_bulk_alloc (mb);
88       vec_add1 (elts, elt);
89     }
90
91   for (i = n_iter - 1; i >= 0; i--)
92     elts[i]->data = i;
93
94   for (i = n_iter - 1; i >= 0; i--)
95     if (elts[i]->data != i)
96       MB_TEST (0, "data corrupted");
97
98   for (i = 0; i < n_iter; i++)
99     clib_mem_bulk_free (mb, elts[i]);
100
101   clib_mem_bulk_destroy (mb);
102   vec_free (elts);
103
104   return 0;
105 }
106
107 static clib_error_t *
108 mem_bulk_test (vlib_main_t *vm, unformat_input_t *input,
109                vlib_cli_command_t *cmd_arg)
110 {
111   int res = 0;
112   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
113     {
114       if (unformat (input, "basic"))
115         {
116           res = mem_bulk_test_basic (vm, input);
117         }
118       else if (unformat (input, "all"))
119         {
120           if ((res = mem_bulk_test_basic (vm, input)))
121             goto done;
122         }
123       else
124         break;
125     }
126
127 done:
128   if (res)
129     return clib_error_return (0, "llist unit test failed");
130   return 0;
131 }
132
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,
137 };
138
139 /*
140  * fd.io coding-style-patch-verification: ON
141  *
142  * Local Variables:
143  * eval: (c-set-style "gnu")
144  * End:
145  */