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.
15 #include <vlib/vlib.h>
16 #include <vppinfra/bitmap.h>
19 check_bitmap (const char *test_name, const uword *bm, u32 expected_len, ...)
21 clib_error_t *error = 0;
26 va_start (va, expected_len);
28 if (vec_len (bm) != expected_len)
30 error = clib_error_create ("%s failed, wrong "
31 "bitmap's size (%u != %u expected)",
32 test_name, vec_len (bm), expected_len);
36 for (i = 0; i < expected_len; ++i)
38 expected_value = va_arg (va, uword);
39 if (bm[i] != expected_value)
41 error = clib_error_create (
43 "bitmap's value at index %u (%u != %u expected)",
44 test_name, i, bm[i], expected_value);
55 test_bitmap_command_fn (vlib_main_t * vm,
56 unformat_input_t * input, vlib_cli_command_t * cmd)
58 clib_error_t *error = 0;
63 /* bm should look like:
65 * LSB |0011...11|1100...00| MSB
67 bm = clib_bitmap_set_multiple (0, 2, ~0ULL, BITS (uword));
68 error = check_bitmap ("clib_bitmap_set_multiple 1", bm, 2, ~0ULL << 2, 3);
72 /* bm2 should look like:
76 bm2 = clib_bitmap_set_multiple (0, 0, ~0ULL, BITS (uword));
77 error = check_bitmap ("clib_bitmap_set_multiple 2", bm2, 1, ~0ULL);
81 /* bm should look like:
83 * LSB |0011...1100|000...000| MSB
85 bm = clib_bitmap_set_multiple (bm, 2, pow2_mask (BITS (uword) - 3),
87 error = check_bitmap ("clib_bitmap_set_multiple 3", bm, 2,
88 pow2_mask (BITS (uword) - 3) << 2, 0);
92 /* bm2 should look like:
96 bm2 = clib_bitmap_xori (bm2, 1);
97 error = check_bitmap ("clib_bitmap_xori 1", bm2, 1, ~0ULL ^ 2);
101 /* bm should look like:
103 * LSB |0011...1100|000...001| MSB
105 bm = clib_bitmap_xori (bm, 2 * BITS (uword) - 1);
106 error = check_bitmap ("clib_bitmap_xori 2", bm, 2,
107 pow2_mask (BITS (uword) - 3) << 2,
108 1ULL << (BITS (uword) - 1));
112 /* bm should look like:
114 * LSB |00100...00|000...001| MSB
116 bm = clib_bitmap_andi (bm, 2);
118 check_bitmap ("clib_bitmap_andi", bm, 2, 4, 1ULL << (BITS (uword) - 1));
122 /* bm should look like:
124 * LSB |00100...00| MSB
126 bm = clib_bitmap_xori (bm, 2 * BITS (uword) - 1);
127 error = check_bitmap ("clib_bitmap_xori 3", bm, 1, 4);
131 /* bm and bm2 should look like:
133 * LSB |0011...11|1100...00| MSB
135 * LSB |101...111|0011...11| MSB
137 bm = clib_bitmap_set_multiple (bm, 2, ~0ULL, BITS (uword));
139 clib_bitmap_set_multiple (bm2, BITS (uword) + 2, ~0ULL, BITS (uword) - 3);
140 dup = clib_bitmap_dup_and (bm, bm2);
141 error = check_bitmap ("clib_bitmap_dup_and", dup, 1, bm[0] & bm2[0]);
145 /* bm should look like:
146 * bm[0] bm[1] ... bm[3]
147 * LSB |0011...11|11...11| ... |11...11| MSB
149 bm = clib_bitmap_set_region (bm, 5, 1, 4 * BITS (uword) - 5);
150 error = check_bitmap ("clib_bitmap_set_region 1", bm, 4, ~0ULL << 2, ~0ULL,
155 /* bm should look like:
156 * bm[0] bm[1] ... bm[3]
157 * LSB |0011...11|11...11| ... |11...1100000| MSB
159 bm = clib_bitmap_set_region (bm, 4 * BITS (uword) - 5, 0, 5);
160 error = check_bitmap ("clib_bitmap_set_region 2", bm, 4, ~0ULL << 2, ~0ULL,
161 ~0ULL, pow2_mask (BITS (uword) - 5));
174 VLIB_CLI_COMMAND (test_bitmap_command, static) = {
175 .path = "test bitmap",
176 .short_help = "Coverage test for bitmap.h",
177 .function = test_bitmap_command_fn,
182 * fd.io coding-style-patch-verification: ON
185 * eval: (c-set-style "gnu")