LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/util/xor.c - xor_ut.c (source / functions) Hit Total Coverage
Test: Combined Lines: 58 58 100.0 %
Date: 2024-07-15 21:16:48 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 30 46 65.2 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2022 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "spdk_internal/cunit.h"
       9                 :            : 
      10                 :            : #include "util/xor.c"
      11                 :            : #include "common/lib/test_env.c"
      12                 :            : 
      13                 :            : #define BUF_COUNT 8
      14                 :            : #define SRC_BUF_COUNT (BUF_COUNT - 1)
      15                 :            : #define BUF_SIZE 4096
      16                 :            : 
      17                 :            : static void
      18                 :          3 : test_xor_gen(void)
      19                 :            : {
      20                 :          3 :         void *bufs[BUF_COUNT];
      21                 :          3 :         void *bufs2[SRC_BUF_COUNT];
      22                 :            :         uint8_t *ref, *dest;
      23                 :            :         int ret;
      24                 :            :         size_t i, j;
      25                 :            :         uint32_t *tmp;
      26                 :            : 
      27                 :            :         /* alloc and fill the buffers with a pattern */
      28         [ +  + ]:         27 :         for (i = 0; i < BUF_COUNT; i++) {
      29         [ -  + ]:         24 :                 ret = posix_memalign(&bufs[i], spdk_xor_get_optimal_alignment(), BUF_SIZE);
      30         [ -  + ]:         24 :                 SPDK_CU_ASSERT_FATAL(ret == 0);
      31                 :            : 
      32                 :         24 :                 tmp = bufs[i];
      33         [ +  + ]:      24600 :                 for (j = 0; j < BUF_SIZE / sizeof(*tmp); j++) {
      34                 :      24576 :                         tmp[j] = (i << 16) + j;
      35                 :            :                 }
      36                 :            :         }
      37                 :          3 :         dest = bufs[SRC_BUF_COUNT];
      38                 :            : 
      39                 :            :         /* prepare the reference buffer */
      40                 :          3 :         ref = malloc(BUF_SIZE);
      41         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(ref != NULL);
      42                 :            : 
      43         [ -  + ]:          3 :         memset(ref, 0, BUF_SIZE);
      44         [ +  + ]:         24 :         for (i = 0; i < SRC_BUF_COUNT; i++) {
      45         [ +  + ]:      86037 :                 for (j = 0; j < BUF_SIZE; j++) {
      46                 :      86016 :                         ref[j] ^= ((uint8_t *)bufs[i])[j];
      47                 :            :                 }
      48                 :            :         }
      49                 :            : 
      50                 :            :         /* generate xor, compare the dest and reference buffers */
      51                 :          3 :         ret = spdk_xor_gen(dest, bufs, SRC_BUF_COUNT, BUF_SIZE);
      52                 :          3 :         CU_ASSERT(ret == 0);
      53   [ -  +  -  + ]:          3 :         ret = memcmp(ref, dest, BUF_SIZE);
      54                 :          3 :         CU_ASSERT(ret == 0);
      55                 :            : 
      56                 :            :         /* len not multiple of alignment */
      57         [ -  + ]:          3 :         memset(dest, 0xba, BUF_SIZE);
      58                 :          3 :         ret = spdk_xor_gen(dest, bufs, SRC_BUF_COUNT, BUF_SIZE - 1);
      59                 :          3 :         CU_ASSERT(ret == 0);
      60   [ -  +  -  + ]:          3 :         ret = memcmp(ref, dest, BUF_SIZE - 1);
      61                 :          3 :         CU_ASSERT(ret == 0);
      62                 :            : 
      63                 :            :         /* unaligned buffer */
      64                 :          3 :         memcpy(bufs2, bufs, sizeof(bufs2));
      65                 :          3 :         bufs2[1] += 1;
      66                 :          3 :         bufs2[2] += 2;
      67                 :          3 :         bufs2[3] += 3;
      68                 :            : 
      69         [ -  + ]:          3 :         memset(ref, 0, BUF_SIZE);
      70         [ +  + ]:         24 :         for (i = 0; i < SRC_BUF_COUNT; i++) {
      71         [ +  + ]:      85890 :                 for (j = 0; j < BUF_SIZE - SRC_BUF_COUNT; j++) {
      72                 :      85869 :                         ref[j] ^= ((uint8_t *)bufs2[i])[j];
      73                 :            :                 }
      74                 :            :         }
      75                 :            : 
      76         [ -  + ]:          3 :         memset(dest, 0xba, BUF_SIZE);
      77                 :          3 :         ret = spdk_xor_gen(dest, bufs2, SRC_BUF_COUNT, BUF_SIZE - SRC_BUF_COUNT);
      78                 :          3 :         CU_ASSERT(ret == 0);
      79   [ -  +  -  + ]:          3 :         ret = memcmp(ref, dest, BUF_SIZE - SRC_BUF_COUNT);
      80                 :          3 :         CU_ASSERT(ret == 0);
      81                 :            : 
      82                 :            :         /* xoring a buffer with itself should result in all zeros */
      83         [ -  + ]:          3 :         memset(ref, 0, BUF_SIZE);
      84                 :          3 :         bufs2[0] = bufs[0];
      85                 :          3 :         bufs2[1] = bufs[0];
      86                 :          3 :         dest = bufs[0];
      87                 :            : 
      88                 :          3 :         ret = spdk_xor_gen(dest, bufs2, 2, BUF_SIZE);
      89                 :          3 :         CU_ASSERT(ret == 0);
      90   [ -  +  -  + ]:          3 :         ret = memcmp(ref, dest, BUF_SIZE);
      91                 :          3 :         CU_ASSERT(ret == 0);
      92                 :            : 
      93                 :            :         /* cleanup */
      94         [ +  + ]:         27 :         for (i = 0; i < BUF_COUNT; i++) {
      95                 :         24 :                 free(bufs[i]);
      96                 :            :         }
      97                 :          3 :         free(ref);
      98                 :          3 : }
      99                 :            : 
     100                 :            : int
     101                 :          3 : main(int argc, char **argv)
     102                 :            : {
     103                 :          3 :         CU_pSuite       suite = NULL;
     104                 :            :         unsigned int    num_failures;
     105                 :            : 
     106                 :          3 :         CU_initialize_registry();
     107                 :            : 
     108                 :          3 :         suite = CU_add_suite("xor", NULL, NULL);
     109                 :            : 
     110                 :          3 :         CU_ADD_TEST(suite, test_xor_gen);
     111                 :            : 
     112                 :            : 
     113                 :          3 :         num_failures = spdk_ut_run_tests(argc, argv, NULL);
     114                 :            : 
     115                 :          3 :         CU_cleanup_registry();
     116                 :            : 
     117                 :          3 :         return num_failures;
     118                 :            : }

Generated by: LCOV version 1.14