LCOV - code coverage report
Current view: top level - plugins/unittest - bitmap_test.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 76 96 79.2 %
Date: 2023-07-05 22:20:52 Functions: 5 5 100.0 %

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

Generated by: LCOV version 1.14