LCOV - code coverage report
Current view: top level - app/test - test_common.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 86 109 78.9 %
Date: 2024-01-22 16:13:49 Functions: 8 8 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 91 132 68.9 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdio.h>
       6                 :            : #include <inttypes.h>
       7                 :            : #include <string.h>
       8                 :            : #include <math.h>
       9                 :            : #include <rte_common.h>
      10                 :            : #include <rte_bitops.h>
      11                 :            : #include <rte_hexdump.h>
      12                 :            : #include <rte_pause.h>
      13                 :            : 
      14                 :            : #include "test.h"
      15                 :            : 
      16                 :            : #define MAX_NUM 1 << 20
      17                 :            : 
      18                 :            : #define FAIL(x)\
      19                 :            :         {printf(x "() test failed!\n");\
      20                 :            :         return -1;}
      21                 :            : 
      22                 :            : /* this is really a sanity check */
      23                 :            : static int
      24                 :          1 : test_macros(int __rte_unused unused_parm)
      25                 :            : {
      26                 :            : #define SMALLER 0x1000U
      27                 :            : #define BIGGER 0x2000U
      28                 :            : #define PTR_DIFF BIGGER - SMALLER
      29                 :            : 
      30                 :            :         uintptr_t unused = 0;
      31                 :            :         unsigned int smaller = SMALLER, bigger  = BIGGER;
      32                 :            :         uint32_t arr[3];
      33                 :            : 
      34                 :            :         RTE_SET_USED(unused);
      35                 :            : 
      36                 :            :         RTE_SWAP(smaller, bigger);
      37                 :            :         RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
      38                 :            :                 "RTE_SWAP");
      39                 :            :         RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
      40                 :            :                 "RTE_PTR_ADD");
      41                 :            :         RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
      42                 :            :                 "RTE_PTR_SUB");
      43                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
      44                 :            :                 "RTE_PTR_DIFF");
      45                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
      46                 :            :                 "RTE_MAX");
      47                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_MIN(SMALLER, BIGGER), SMALLER,
      48                 :            :                 "RTE_MIN");
      49                 :            : 
      50         [ -  + ]:          1 :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_ADD(arr + 1, sizeof(arr[0])), &arr[2],
      51                 :            :                 "RTE_PTR_ADD(expr, x)");
      52                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_SUB(arr + 1, sizeof(arr[0])), &arr[0],
      53                 :            :                 "RTE_PTR_SUB(expr, x)");
      54                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_FLOOR(arr + 2, 4), &arr[2],
      55                 :            :                 "RTE_PTR_ALIGN_FLOOR(expr, x)");
      56         [ -  + ]:          1 :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN_CEIL(arr + 2, 4), &arr[2],
      57                 :            :                 "RTE_PTR_ALIGN_CEIL(expr, x)");
      58                 :            :         RTE_TEST_ASSERT_EQUAL(RTE_PTR_ALIGN(arr + 2, 4), &arr[2],
      59                 :            :                 "RTE_PTR_ALIGN(expr, x)");
      60                 :            : 
      61         [ -  + ]:          1 :         RTE_TEST_ASSERT_EQUAL(
      62                 :            :                 RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 1), 4), &arr[1],
      63                 :            :                 "RTE_PTR_ALIGN_FLOOR(x < y/2, y)");
      64         [ -  + ]:          1 :         RTE_TEST_ASSERT_EQUAL(
      65                 :            :                 RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(&arr[1], 3), 4), &arr[1],
      66                 :            :                 "RTE_PTR_ALIGN_FLOOR(x > y/2, y)");
      67         [ -  + ]:          1 :         RTE_TEST_ASSERT_EQUAL(
      68                 :            :                 RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 3), 4), &arr[2],
      69                 :            :                 "RTE_PTR_ALIGN_CEIL(x < y/2, y)");
      70         [ -  + ]:          1 :         RTE_TEST_ASSERT_EQUAL(
      71                 :            :                 RTE_PTR_ALIGN_CEIL(RTE_PTR_ADD(&arr[1], 1), 4), &arr[2],
      72                 :            :                 "RTE_PTR_ALIGN_CEIL(x > y/2, y)");
      73                 :            : 
      74                 :            :         RTE_TEST_ASSERT(strncmp(RTE_STR(test), "test", sizeof("test")) == 0,
      75                 :            :                 "RTE_STR");
      76                 :            : 
      77                 :            :         return 0;
      78                 :            : }
      79                 :            : 
      80                 :            : static int
      81                 :          1 : test_bsf(void)
      82                 :            : {
      83                 :            :         uint32_t shift, pos;
      84                 :            : 
      85                 :            :         /* safe versions should be able to handle 0 */
      86                 :            :         if (rte_bsf32_safe(0, &pos) != 0)
      87                 :            :                 FAIL("rte_bsf32_safe");
      88                 :            :         if (rte_bsf64_safe(0, &pos) != 0)
      89                 :            :                 FAIL("rte_bsf64_safe");
      90                 :            : 
      91         [ +  + ]:         64 :         for (shift = 0; shift < 63; shift++) {
      92                 :            :                 uint32_t val32;
      93                 :            :                 uint64_t val64;
      94                 :            : 
      95         [ -  + ]:         63 :                 val64 = 1ULL << shift;
      96         [ -  + ]:         63 :                 if ((uint32_t)rte_bsf64(val64) != shift)
      97                 :          0 :                         FAIL("rte_bsf64");
      98                 :            :                 if (rte_bsf64_safe(val64, &pos) != 1)
      99                 :            :                         FAIL("rte_bsf64_safe");
     100                 :            :                 if (pos != shift)
     101                 :            :                         FAIL("rte_bsf64_safe");
     102                 :            : 
     103         [ +  + ]:         63 :                 if (shift > 31)
     104                 :         31 :                         continue;
     105                 :            : 
     106         [ -  + ]:         32 :                 val32 = 1U << shift;
     107         [ -  + ]:         32 :                 if ((uint32_t)rte_bsf32(val32) != shift)
     108                 :          0 :                         FAIL("rte_bsf32");
     109                 :            :                 if (rte_bsf32_safe(val32, &pos) != 1)
     110                 :            :                         FAIL("rte_bsf32_safe");
     111                 :            :                 if (pos != shift)
     112                 :            :                         FAIL("rte_bsf32_safe");
     113                 :            :         }
     114                 :            : 
     115                 :            :         return 0;
     116                 :            : }
     117                 :            : 
     118                 :            : static int
     119                 :          1 : test_misc(void)
     120                 :            : {
     121                 :          1 :         char memdump[] = "memdump_test";
     122                 :            : 
     123                 :          1 :         rte_memdump(stdout, "test", memdump, sizeof(memdump));
     124                 :          1 :         rte_hexdump(stdout, "test", memdump, sizeof(memdump));
     125                 :            : 
     126                 :            :         rte_pause();
     127                 :            : 
     128                 :          1 :         return 0;
     129                 :            : }
     130                 :            : 
     131                 :            : static int
     132                 :          1 : test_align(void)
     133                 :            : {
     134                 :            : #define FAIL_ALIGN(x, i, p)\
     135                 :            :         {printf(x "() test failed: %u %u\n", i, p);\
     136                 :            :         return -1;}
     137                 :            : #define FAIL_ALIGN64(x, j, q)\
     138                 :            :         {printf(x "() test failed: %"PRIu64" %"PRIu64"\n", j, q);\
     139                 :            :         return -1; }
     140                 :            : #define ERROR_FLOOR(res, i, pow) \
     141                 :            :                 (res % pow) ||                                          /* check if not aligned */ \
     142                 :            :                 ((res / pow) != (i / pow))              /* check if correct alignment */
     143                 :            : #define ERROR_CEIL(res, i, pow) \
     144                 :            :                 (res % pow) ||                                          /* check if not aligned */ \
     145                 :            :                         ((i % pow) == 0 ?                               /* check if ceiling is invoked */ \
     146                 :            :                         val / pow != i / pow :                  /* if aligned */ \
     147                 :            :                         val / pow != (i / pow) + 1)             /* if not aligned, hence +1 */
     148                 :            : 
     149                 :            :         uint32_t i, p, val;
     150                 :            :         uint64_t j, q;
     151                 :            : 
     152         [ +  + ]:    1048577 :         for (i = 1, p = 1; i <= MAX_NUM; i ++) {
     153         [ -  + ]:    1048576 :                 if (rte_align32pow2(i) != p)
     154                 :          0 :                         FAIL_ALIGN("rte_align32pow2", i, p);
     155         [ +  + ]:    1048576 :                 if (i == p)
     156                 :         21 :                         p <<= 1;
     157                 :            :         }
     158                 :            : 
     159         [ +  + ]:    1048577 :         for (i = 1, p = 1; i <= MAX_NUM; i++) {
     160         [ -  + ]:    1048576 :                 if (rte_align32prevpow2(i) != p)
     161                 :          0 :                         FAIL_ALIGN("rte_align32prevpow2", i, p);
     162         [ +  + ]:    1048576 :                 if (rte_is_power_of_2(i + 1))
     163                 :            :                         p = i + 1;
     164                 :            :         }
     165                 :            : 
     166         [ +  + ]:    1048577 :         for (j = 1, q = 1; j <= MAX_NUM ; j++) {
     167         [ -  + ]:    1048576 :                 if (rte_align64pow2(j) != q)
     168                 :          0 :                         FAIL_ALIGN64("rte_align64pow2", j, q);
     169         [ +  + ]:    1048576 :                 if (j == q)
     170                 :         21 :                         q <<= 1;
     171                 :            :         }
     172                 :            : 
     173         [ +  + ]:    1048577 :         for (j = 1, q = 1; j <= MAX_NUM ; j++) {
     174         [ -  + ]:    1048576 :                 if (rte_align64prevpow2(j) != q)
     175                 :          0 :                         FAIL_ALIGN64("rte_align64prevpow2", j, q);
     176         [ +  + ]:    1048576 :                 if (rte_is_power_of_2(j + 1))
     177                 :         20 :                         q = j + 1;
     178                 :            :         }
     179                 :            : 
     180         [ +  + ]:         21 :         for (p = 2; p <= MAX_NUM; p <<= 1) {
     181                 :            : 
     182                 :            :                 if (!rte_is_power_of_2(p))
     183                 :          0 :                         FAIL("rte_is_power_of_2");
     184                 :            : 
     185         [ +  + ]:   20971540 :                 for (i = 1; i <= MAX_NUM; i++) {
     186                 :            :                         /* align floor */
     187         [ -  + ]:   20971520 :                         if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
     188                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
     189                 :            : 
     190                 :   20971520 :                         val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
     191   [ +  -  -  + ]:   20971520 :                         if (ERROR_FLOOR(val, i, p))
     192                 :          0 :                                 FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
     193                 :            : 
     194                 :            :                         val = RTE_ALIGN_FLOOR(i, p);
     195                 :            :                         if (ERROR_FLOOR(val, i, p))
     196                 :            :                                 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
     197                 :            : 
     198                 :            :                         /* align ceiling */
     199                 :   20971520 :                         val = RTE_PTR_ALIGN((uintptr_t) i, p);
     200   [ +  -  +  +  :   20971520 :                         if (ERROR_CEIL(val, i, p))
             -  +  -  + ]
     201                 :          0 :                                 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
     202                 :            : 
     203                 :            :                         val = RTE_ALIGN(i, p);
     204   [ +  +  -  +  :   20971520 :                         if (ERROR_CEIL(val, i, p))
                   -  + ]
     205                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN", i, p);
     206                 :            : 
     207                 :            :                         val = RTE_ALIGN_CEIL(i, p);
     208   [ +  +  -  +  :   20971520 :                         if (ERROR_CEIL(val, i, p))
                   -  + ]
     209                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
     210                 :            : 
     211                 :            :                         val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
     212   [ +  +  -  +  :   20971520 :                         if (ERROR_CEIL(val, i, p))
                   -  + ]
     213                 :          0 :                                 FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
     214                 :            : 
     215                 :            :                         /* by this point we know that val is aligned to p */
     216         [ -  + ]:   20971520 :                         if (!rte_is_aligned((void*)(uintptr_t) val, p))
     217                 :          0 :                                 FAIL("rte_is_aligned");
     218                 :            :                 }
     219                 :            :         }
     220                 :            : 
     221         [ +  + ]:       1025 :         for (p = 1; p <= MAX_NUM / 2; p++) {
     222         [ +  + ]:    1049600 :                 for (i = 1; i <= MAX_NUM / 2; i++) {
     223                 :    1048576 :                         val = RTE_ALIGN_MUL_CEIL(i, p);
     224   [ +  -  -  + ]:    1048576 :                         if (val % p != 0 || val < i)
     225                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN_MUL_CEIL", i, p);
     226                 :    1048576 :                         val = RTE_ALIGN_MUL_FLOOR(i, p);
     227   [ +  -  -  + ]:    1048576 :                         if (val % p != 0 || val > i)
     228                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
     229         [ +  + ]:    1048576 :                         val = RTE_ALIGN_MUL_NEAR(i, p);
     230         [ +  - ]:    1048576 :                         if (val % p != 0 || ((val != RTE_ALIGN_MUL_CEIL(i, p))
     231         [ -  + ]:    1048576 :                                 & (val != RTE_ALIGN_MUL_FLOOR(i, p))))
     232                 :          0 :                                 FAIL_ALIGN("RTE_ALIGN_MUL_NEAR", i, p);
     233                 :            :                 }
     234                 :            :         }
     235                 :            : 
     236                 :            :         return 0;
     237                 :            : }
     238                 :            : 
     239                 :            : static int
     240                 :          1 : test_log2(void)
     241                 :            : {
     242                 :            :         uint32_t i, base, compare;
     243                 :            :         const uint32_t max = 0x10000;
     244                 :            :         const uint32_t step = 1;
     245                 :            : 
     246                 :            :         compare = rte_log2_u32(0);
     247                 :            :         if (compare != 0) {
     248                 :            :                 printf("Wrong rte_log2_u32(0) val %x, expected 0\n", compare);
     249                 :            :                 return TEST_FAILED;
     250                 :            :         }
     251                 :            : 
     252                 :            :         compare = rte_log2_u64(0);
     253                 :            :         if (compare != 0) {
     254                 :            :                 printf("Wrong rte_log2_u64(0) val %x, expected 0\n", compare);
     255                 :            :                 return TEST_FAILED;
     256                 :            :         }
     257                 :            : 
     258         [ +  + ]:      65536 :         for (i = 1; i < max; i = i + step) {
     259                 :            :                 uint64_t i64;
     260                 :            : 
     261                 :            :                 /* extend range for 64-bit */
     262                 :      65535 :                 i64 = (uint64_t)i << 32;
     263         [ -  + ]:      65535 :                 base = (uint32_t)ceilf(log2(i64));
     264                 :            :                 compare = rte_log2_u64(i64);
     265         [ -  + ]:      65535 :                 if (base != compare) {
     266                 :            :                         printf("Wrong rte_log2_u64(%" PRIx64 ") val %x, expected %x\n",
     267                 :            :                                 i64, compare, base);
     268                 :          0 :                         return TEST_FAILED;
     269                 :            :                 }
     270                 :            : 
     271         [ -  + ]:      65535 :                 base = (uint32_t)ceilf(log2((uint32_t)i));
     272                 :            :                 compare = rte_log2_u32((uint32_t)i);
     273         [ -  + ]:      65535 :                 if (base != compare) {
     274                 :            :                         printf("Wrong rte_log2_u32(%x) val %x, expected %x\n",
     275                 :            :                                 i, compare, base);
     276                 :          0 :                         return TEST_FAILED;
     277                 :            :                 }
     278                 :            :                 compare = rte_log2_u64((uint64_t)i);
     279         [ -  + ]:      65535 :                 if (base != compare) {
     280                 :            :                         printf("Wrong rte_log2_u64(%x) val %x, expected %x\n",
     281                 :            :                                 i, compare, base);
     282                 :          0 :                         return TEST_FAILED;
     283                 :            :                 }
     284                 :            :         }
     285                 :            :         return 0;
     286                 :            : }
     287                 :            : 
     288                 :            : static int
     289                 :          1 : test_fls(void)
     290                 :            : {
     291                 :            :         struct fls_test_vector {
     292                 :            :                 uint32_t arg;
     293                 :            :                 int rc;
     294                 :            :         };
     295                 :            :         int expected, rc;
     296                 :            :         uint32_t i, arg;
     297                 :            : 
     298                 :          1 :         const struct fls_test_vector test[] = {
     299                 :            :                 {0x0, 0},
     300                 :            :                 {0x1, 1},
     301                 :            :                 {0x4000, 15},
     302                 :            :                 {0x80000000, 32},
     303                 :            :         };
     304                 :            : 
     305         [ +  + ]:          5 :         for (i = 0; i < RTE_DIM(test); i++) {
     306                 :            :                 uint64_t arg64;
     307                 :            : 
     308         [ +  + ]:          4 :                 arg = test[i].arg;
     309                 :          4 :                 rc = rte_fls_u32(arg);
     310                 :          4 :                 expected = test[i].rc;
     311         [ -  + ]:          4 :                 if (rc != expected) {
     312                 :            :                         printf("Wrong rte_fls_u32(0x%x) rc=%d, expected=%d\n",
     313                 :            :                                 arg, rc, expected);
     314                 :          0 :                         return TEST_FAILED;
     315                 :            :                 }
     316                 :            :                 /* 64-bit version */
     317                 :            :                 arg = test[i].arg;
     318         [ +  + ]:          4 :                 rc = rte_fls_u64(arg);
     319                 :            :                 expected = test[i].rc;
     320         [ -  + ]:          4 :                 if (rc != expected) {
     321                 :            :                         printf("Wrong rte_fls_u64(0x%x) rc=%d, expected=%d\n",
     322                 :            :                                 arg, rc, expected);
     323                 :          0 :                         return TEST_FAILED;
     324                 :            :                 }
     325                 :            :                 /* 64-bit version shifted by 32 bits */
     326         [ +  + ]:          4 :                 arg64 = (uint64_t)test[i].arg << 32;
     327                 :          4 :                 rc = rte_fls_u64(arg64);
     328                 :            :                 /* don't shift zero */
     329         [ +  + ]:          4 :                 expected = test[i].rc == 0 ? 0 : test[i].rc + 32;
     330         [ -  + ]:          4 :                 if (rc != expected) {
     331                 :            :                         printf("Wrong rte_fls_u64(0x%" PRIx64 ") rc=%d, expected=%d\n",
     332                 :            :                                 arg64, rc, expected);
     333                 :          0 :                         return TEST_FAILED;
     334                 :            :                 }
     335                 :            :         }
     336                 :            : 
     337                 :            :         return 0;
     338                 :            : }
     339                 :            : 
     340                 :            : static int
     341                 :          1 : test_common(void)
     342                 :            : {
     343                 :            :         int ret = 0;
     344                 :          1 :         ret |= test_align();
     345                 :          1 :         ret |= test_macros(0);
     346                 :          1 :         ret |= test_misc();
     347                 :          1 :         ret |= test_bsf();
     348                 :          1 :         ret |= test_log2();
     349                 :          1 :         ret |= test_fls();
     350                 :            : 
     351                 :          1 :         return ret;
     352                 :            : }
     353                 :            : 
     354                 :        235 : REGISTER_FAST_TEST(common_autotest, true, true, test_common);

Generated by: LCOV version 1.14