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.
16 #include <vlib/vlib.h>
17 #include <vppinfra/bitmap.h>
20 check_bitmap (const char *test_name, const uword *bm, u32 expected_len, ...)
22 clib_error_t *error = 0;
27 va_start (va, expected_len);
29 if (vec_len (bm) != expected_len)
31 error = clib_error_create ("%s failed, wrong "
32 "bitmap's size (%u != %u expected)",
33 test_name, vec_len (bm), expected_len);
37 for (i = 0; i < expected_len; ++i)
39 expected_value = va_arg (va, uword);
40 if (bm[i] != expected_value)
42 error = clib_error_create (
44 "bitmap's value at index %u (%u != %u expected)",
45 test_name, i, bm[i], expected_value);
56 check_bitmap_will_expand (const char *test_name, uword **bm, uword index,
57 bool expected_will_expand)
59 uword max_bytes = vec_max_bytes (*bm);
62 result = clib_bitmap_will_expand (*bm, index);
63 if (result != expected_will_expand)
65 return clib_error_create (
67 "bitmap's expansion before set (%u != %u expected)",
68 test_name, result, expected_will_expand);
71 *bm = clib_bitmap_set (*bm, index, 1);
72 result = vec_max_bytes (*bm) > max_bytes;
73 if (result != expected_will_expand)
75 return clib_error_create (
77 "bitmap's expansion after set (%u != %u expected)",
78 test_name, result, expected_will_expand);
85 test_bitmap_command_fn (vlib_main_t * vm,
86 unformat_input_t * input, vlib_cli_command_t * cmd)
88 clib_error_t *error = 0;
94 /* bm should look like:
96 * LSB |0011...11|1100...00| MSB
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);
103 /* bm2 should look like:
107 bm2 = clib_bitmap_set_multiple (0, 0, ~0ULL, BITS (uword));
108 error = check_bitmap ("clib_bitmap_set_multiple 2", bm2, 1, ~0ULL);
112 /* bm should look like:
114 * LSB |0011...1100|000...000| MSB
116 bm = clib_bitmap_set_multiple (bm, 2, pow2_mask (BITS (uword) - 3),
118 error = check_bitmap ("clib_bitmap_set_multiple 3", bm, 2,
119 pow2_mask (BITS (uword) - 3) << 2, 0);
123 /* bm2 should look like:
125 * LSB |101...111| MSB
127 bm2 = clib_bitmap_xori (bm2, 1);
128 error = check_bitmap ("clib_bitmap_xori 1", bm2, 1, ~0ULL ^ 2);
132 /* bm should look like:
134 * LSB |0011...1100|000...001| MSB
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));
143 /* bm should look like:
145 * LSB |00100...00|000...001| MSB
147 bm = clib_bitmap_andi (bm, 2);
149 check_bitmap ("clib_bitmap_andi", bm, 2, 4, 1ULL << (BITS (uword) - 1));
153 /* bm should look like:
155 * LSB |00100...00| MSB
157 bm = clib_bitmap_xori (bm, 2 * BITS (uword) - 1);
158 error = check_bitmap ("clib_bitmap_xori 3", bm, 1, 4);
162 /* bm and bm2 should look like:
164 * LSB |0011...11|1100...00| MSB
166 * LSB |101...111|0011...11| MSB
168 bm = clib_bitmap_set_multiple (bm, 2, ~0ULL, BITS (uword));
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]);
176 /* bm should look like:
177 * bm[0] bm[1] ... bm[3]
178 * LSB |0011...11|11...11| ... |11...11| MSB
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,
186 /* bm should look like:
187 * bm[0] bm[1] ... bm[3]
188 * LSB |0011...11|11...11| ... |11...1100000| MSB
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));
196 error = check_bitmap_will_expand ("clib_bitmap_will_expand 1", &bm, 0, 0);
200 error = check_bitmap_will_expand ("clib_bitmap_will_expand 2", &bm,
201 vec_max_len (bm) * BITS (uword) - 1, 0);
205 error = check_bitmap_will_expand ("clib_bitmap_will_expand 3", &bm,
206 vec_max_len (bm) * BITS (uword), 1);
210 error = check_bitmap_will_expand ("clib_bitmap_will_expand 4", &bm3, 0, 1);
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,
230 * fd.io coding-style-patch-verification: ON
233 * eval: (c-set-style "gnu")