vnet: export header files to build the plugins
[vpp.git] / src / plugins / unittest / bitmap_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 <stdbool.h>
16 #include <vlib/vlib.h>
17 #include <vppinfra/bitmap.h>
18
19 static clib_error_t *
20 check_bitmap (const char *test_name, const uword *bm, u32 expected_len, ...)
21 {
22   clib_error_t *error = 0;
23   u32 i;
24   uword expected_value;
25
26   va_list va;
27   va_start (va, expected_len);
28
29   if (vec_len (bm) != expected_len)
30     {
31       error = clib_error_create ("%s failed, wrong "
32                                  "bitmap's size (%u != %u expected)",
33                                  test_name, vec_len (bm), expected_len);
34       goto done;
35     }
36
37   for (i = 0; i < expected_len; ++i)
38     {
39       expected_value = va_arg (va, uword);
40       if (bm[i] != expected_value)
41         {
42           error = clib_error_create (
43             "%s failed, wrong "
44             "bitmap's value at index %u (%u != %u expected)",
45             test_name, i, bm[i], expected_value);
46           break;
47         }
48     }
49
50 done:
51   va_end (va);
52   return error;
53 }
54
55 static clib_error_t *
56 check_bitmap_will_expand (const char *test_name, uword **bm, uword index,
57                           bool expected_will_expand)
58 {
59   uword max_bytes = vec_max_bytes (*bm);
60   bool result;
61
62   result = clib_bitmap_will_expand (*bm, index);
63   if (result != expected_will_expand)
64     {
65       return clib_error_create (
66         "%s failed, wrong "
67         "bitmap's expansion before set (%u != %u expected)",
68         test_name, result, expected_will_expand);
69     }
70
71   *bm = clib_bitmap_set (*bm, index, 1);
72   result = vec_max_bytes (*bm) > max_bytes;
73   if (result != expected_will_expand)
74     {
75       return clib_error_create (
76         "%s failed, wrong "
77         "bitmap's expansion after set (%u != %u expected)",
78         test_name, result, expected_will_expand);
79     }
80
81   return 0;
82 }
83
84 static clib_error_t *
85 test_bitmap_command_fn (vlib_main_t * vm,
86                         unformat_input_t * input, vlib_cli_command_t * cmd)
87 {
88   clib_error_t *error = 0;
89   uword *bm = 0;
90   uword *bm2 = 0;
91   uword *bm3 = 0;
92   uword *dup = 0;
93
94   /*  bm should look like:
95    *          bm[0]     bm[1]
96    *  LSB |0011...11|1100...00| MSB
97    */
98   bm = clib_bitmap_set_multiple (0, 2, ~0ULL, BITS (uword));
99   error = check_bitmap ("clib_bitmap_set_multiple 1", bm, 2, ~0ULL << 2, 3);
100   if (error != 0)
101     goto done;
102
103   /*  bm2 should look like:
104    *        bm2[0]
105    *  LSB |11...11| MSB
106    */
107   bm2 = clib_bitmap_set_multiple (0, 0, ~0ULL, BITS (uword));
108   error = check_bitmap ("clib_bitmap_set_multiple 2", bm2, 1, ~0ULL);
109   if (error != 0)
110     goto done;
111
112   /*  bm should look like:
113    *          bm[0]      bm[1]
114    *  LSB |0011...1100|000...000| MSB
115    */
116   bm = clib_bitmap_set_multiple (bm, 2, pow2_mask (BITS (uword) - 3),
117                                  BITS (uword));
118   error = check_bitmap ("clib_bitmap_set_multiple 3", bm, 2,
119                         pow2_mask (BITS (uword) - 3) << 2, 0);
120   if (error != 0)
121     goto done;
122
123   /*  bm2 should look like:
124    *         bm2[0]
125    *  LSB |101...111| MSB
126    */
127   bm2 = clib_bitmap_xori (bm2, 1);
128   error = check_bitmap ("clib_bitmap_xori 1", bm2, 1, ~0ULL ^ 2);
129   if (error != 0)
130     goto done;
131
132   /*  bm should look like:
133    *           bm[0]      bm[1]
134    *  LSB |0011...1100|000...001| MSB
135    */
136   bm = clib_bitmap_xori (bm, 2 * BITS (uword) - 1);
137   error = check_bitmap ("clib_bitmap_xori 2", bm, 2,
138                         pow2_mask (BITS (uword) - 3) << 2,
139                         1ULL << (BITS (uword) - 1));
140   if (error != 0)
141     goto done;
142
143   /*  bm should look like:
144    *         bm[0]      bm[1]
145    *  LSB |00100...00|000...001| MSB
146    */
147   bm = clib_bitmap_andi (bm, 2);
148   error =
149     check_bitmap ("clib_bitmap_andi", bm, 2, 4, 1ULL << (BITS (uword) - 1));
150   if (error != 0)
151     goto done;
152
153   /*  bm should look like:
154    *         bm[0]
155    *  LSB |00100...00| MSB
156    */
157   bm = clib_bitmap_xori (bm, 2 * BITS (uword) - 1);
158   error = check_bitmap ("clib_bitmap_xori 3", bm, 1, 4);
159   if (error != 0)
160     goto done;
161
162   /*  bm and bm2 should look like:
163    *         bm[0]     bm[1]
164    *  LSB |0011...11|1100...00| MSB
165    *         bm2[0]    bm2[1]
166    *  LSB |101...111|0011...11| MSB
167    */
168   bm = clib_bitmap_set_multiple (bm, 2, ~0ULL, BITS (uword));
169   bm2 =
170     clib_bitmap_set_multiple (bm2, BITS (uword) + 2, ~0ULL, BITS (uword) - 3);
171   dup = clib_bitmap_dup_and (bm, bm2);
172   error = check_bitmap ("clib_bitmap_dup_and", dup, 1, bm[0] & bm2[0]);
173   if (error != 0)
174     goto done;
175
176   /*  bm should look like:
177    *         bm[0]    bm[1]   ...   bm[3]
178    *  LSB |0011...11|11...11| ... |11...11| MSB
179    */
180   bm = clib_bitmap_set_region (bm, 5, 1, 4 * BITS (uword) - 5);
181   error = check_bitmap ("clib_bitmap_set_region 1", bm, 4, ~0ULL << 2, ~0ULL,
182                         ~0ULL, ~0ULL);
183   if (error != 0)
184     goto done;
185
186   /*  bm should look like:
187    *         bm[0]    bm[1]   ...      bm[3]
188    *  LSB |0011...11|11...11| ... |11...1100000| MSB
189    */
190   bm = clib_bitmap_set_region (bm, 4 * BITS (uword) - 5, 0, 5);
191   error = check_bitmap ("clib_bitmap_set_region 2", bm, 4, ~0ULL << 2, ~0ULL,
192                         ~0ULL, pow2_mask (BITS (uword) - 5));
193   if (error != 0)
194     goto done;
195
196   error = check_bitmap_will_expand ("clib_bitmap_will_expand 1", &bm, 0, 0);
197   if (error != 0)
198     goto done;
199
200   error = check_bitmap_will_expand ("clib_bitmap_will_expand 2", &bm,
201                                     vec_max_len (bm) * BITS (uword) - 1, 0);
202   if (error != 0)
203     goto done;
204
205   error = check_bitmap_will_expand ("clib_bitmap_will_expand 3", &bm,
206                                     vec_max_len (bm) * BITS (uword), 1);
207   if (error != 0)
208     goto done;
209
210   error = check_bitmap_will_expand ("clib_bitmap_will_expand 4", &bm3, 0, 1);
211   if (error != 0)
212     goto done;
213
214 done:
215   vec_free (bm);
216   vec_free (bm2);
217   vec_free (bm3);
218   vec_free (dup);
219
220   return error;
221 }
222
223 VLIB_CLI_COMMAND (test_bitmap_command, static) = {
224   .path = "test bitmap",
225   .short_help = "Coverage test for bitmap.h",
226   .function = test_bitmap_command_fn,
227 };
228
229 /*
230  * fd.io coding-style-patch-verification: ON
231  *
232  * Local Variables:
233  * eval: (c-set-style "gnu")
234  * End:
235  */