LCOV - code coverage report
Current view: top level - libs/mem/tests/src - test_libmem_0066.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 82 82
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 5 5
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : #include "test_libmem_all.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * @brief Check aliased replacement from the current terminator of the same string
       5                 :             :  *
       6                 :             :  * The test first stores "abcdef" in a string descriptor. It then takes a source pointer to the current '\0' terminator inside the same descriptor and performs bounded replacement with a size of one byte. This verifies that self-aliased replacement from an empty suffix is handled correctly and leaves the descriptor as a valid empty string
       7                 :             :  *
       8                 :             :  * @return Return describing success or failure
       9                 :             :  */
      10                 :           1 : static Return test_libmem_0066_1(void)
      11                 :             : {
      12                 :           1 :         INITTEST;
      13                 :             : 
      14                 :             :         static const char abcdef_text[] = "abcdef";
      15                 :             : 
      16                 :           1 :         m_create(char,string_buffer);
      17                 :           1 :         ASSERT(SUCCESS == m_to_string(string_buffer));
      18                 :             : 
      19                 :           1 :         ASSERT(SUCCESS == m_copy_fixed_string(string_buffer,sizeof(abcdef_text),abcdef_text));
      20                 :             : 
      21                 :           1 :         const char *aliased_string_suffix = m_text(string_buffer);
      22                 :           1 :         ASSERT(aliased_string_suffix != NULL);
      23                 :             : 
      24                 :           1 :         size_t string_buffer_length = 0U;
      25                 :           1 :         ASSERT(SUCCESS == m_string_length(string_buffer,&string_buffer_length));
      26                 :             : 
      27                 :           1 :         IF(aliased_string_suffix != NULL)
      28                 :             :         {
      29                 :           1 :                 aliased_string_suffix += string_buffer_length;
      30                 :           1 :                 ASSERT(SUCCESS == m_copy_fixed_string(string_buffer,1U,aliased_string_suffix));
      31                 :             :         }
      32                 :             : 
      33                 :           1 :         ASSERT(string_buffer->length == 1);
      34                 :           1 :         ASSERT(string_buffer->string_length == 0);
      35                 :           1 :         ASSERT(string_buffer->is_string == true);
      36                 :           1 :         ASSERT(0 == strcmp(m_text(string_buffer),""));
      37                 :           1 :         call(m_del(string_buffer));
      38                 :             : 
      39                 :           1 :         RETURN_STATUS;
      40                 :             : }
      41                 :             : 
      42                 :             : /**
      43                 :             :  * @brief Check self-copy through m_copy on a string descriptor
      44                 :             :  *
      45                 :             :  * @return Return describing success or failure
      46                 :             :  */
      47                 :           1 : static Return test_libmem_0066_2(void)
      48                 :             : {
      49                 :           1 :         INITTEST;
      50                 :             : 
      51                 :             :         static const char xyz_text[] = "xyz";
      52                 :             : 
      53                 :           1 :         m_create(char,string_buffer);
      54                 :             : 
      55                 :           1 :         ASSERT(SUCCESS == m_to_string(string_buffer));
      56                 :           1 :         ASSERT(SUCCESS == m_copy_fixed_string(string_buffer,sizeof(xyz_text),xyz_text));
      57                 :           1 :         ASSERT(SUCCESS == m_copy(string_buffer,string_buffer));
      58                 :           1 :         ASSERT(string_buffer->length == 4);
      59                 :           1 :         ASSERT(string_buffer->string_length == 3);
      60                 :           1 :         ASSERT(string_buffer->is_string == true);
      61                 :           1 :         ASSERT(0 == strcmp(m_text(string_buffer),"xyz"));
      62                 :           1 :         call(m_del(string_buffer));
      63                 :             : 
      64                 :           1 :         RETURN_STATUS;
      65                 :             : }
      66                 :             : 
      67                 :             : /**
      68                 :             :  * @brief Check aliased raw-buffer replacement and data self-copy
      69                 :             :  *
      70                 :             :  * @return Return describing success or failure
      71                 :             :  */
      72                 :           1 : static Return test_libmem_0066_3(void)
      73                 :             : {
      74                 :           1 :         INITTEST;
      75                 :             : 
      76                 :           1 :         m_create(char,raw_buffer);
      77                 :             : 
      78                 :           1 :         const unsigned char raw_bytes[] = {
      79                 :             :                 (unsigned char)'a',
      80                 :             :                 (unsigned char)'b',
      81                 :             :                 (unsigned char)'c',
      82                 :             :                 (unsigned char)'d',
      83                 :             :                 (unsigned char)'e',
      84                 :             :                 (unsigned char)'f'
      85                 :             :         };
      86                 :           1 :         const unsigned char expected_raw_tail[] = {
      87                 :             :                 (unsigned char)'c',
      88                 :             :                 (unsigned char)'d',
      89                 :             :                 (unsigned char)'e',
      90                 :             :                 (unsigned char)'f'
      91                 :             :         };
      92                 :             : 
      93                 :           1 :         ASSERT(SUCCESS == m_copy_buffer(raw_buffer,sizeof(raw_bytes),raw_bytes));
      94                 :             : 
      95                 :           1 :         const unsigned char *aliased_raw_suffix = (const unsigned char *)m_raw_data_ro(raw_buffer);
      96                 :           1 :         ASSERT(aliased_raw_suffix != NULL);
      97                 :             : 
      98                 :           1 :         IF(aliased_raw_suffix != NULL)
      99                 :             :         {
     100                 :           1 :                 aliased_raw_suffix += 2;
     101                 :           1 :                 ASSERT(SUCCESS == m_copy_buffer(raw_buffer,sizeof(raw_bytes) - 2U,aliased_raw_suffix));
     102                 :             :         }
     103                 :             : 
     104                 :           1 :         ASSERT(raw_buffer->length == sizeof(expected_raw_tail));
     105                 :           1 :         ASSERT(raw_buffer->string_length == 0);
     106                 :           1 :         ASSERT(raw_buffer->is_string == false);
     107                 :           1 :         ASSERT(SUCCESS == m_copy(raw_buffer,raw_buffer));
     108                 :             : 
     109                 :           1 :         const unsigned char *raw_view = (const unsigned char *)m_raw_data_ro(raw_buffer);
     110                 :           1 :         ASSERT(raw_view != NULL);
     111                 :             : 
     112                 :           1 :         IF(raw_view != NULL)
     113                 :             :         {
     114                 :           1 :                 ASSERT(0 == memcmp(raw_view,expected_raw_tail,sizeof(expected_raw_tail)));
     115                 :             :         }
     116                 :             : 
     117                 :           1 :         call(m_del(raw_buffer));
     118                 :             : 
     119                 :           1 :         RETURN_STATUS;
     120                 :             : }
     121                 :             : 
     122                 :             : /**
     123                 :             :  * @brief Check aliased raw-buffer core replacement and clearing
     124                 :             :  *
     125                 :             :  * @return Return describing success or failure
     126                 :             :  */
     127                 :           1 : static Return test_libmem_0066_4(void)
     128                 :             : {
     129                 :           1 :         INITTEST;
     130                 :             : 
     131                 :           1 :         m_create(char,raw_core_buffer);
     132                 :             : 
     133                 :           1 :         const unsigned char raw_bytes[] = {
     134                 :             :                 (unsigned char)'a',
     135                 :             :                 (unsigned char)'b',
     136                 :             :                 (unsigned char)'c',
     137                 :             :                 (unsigned char)'d',
     138                 :             :                 (unsigned char)'e',
     139                 :             :                 (unsigned char)'f'
     140                 :             :         };
     141                 :           1 :         const unsigned char expected_raw_core_tail[] = {
     142                 :             :                 (unsigned char)'b',
     143                 :             :                 (unsigned char)'c',
     144                 :             :                 (unsigned char)'d',
     145                 :             :                 (unsigned char)'e'
     146                 :             :         };
     147                 :             : 
     148                 :           1 :         ASSERT(SUCCESS == m_copy_buffer(raw_core_buffer,sizeof(raw_bytes),raw_bytes));
     149                 :             : 
     150                 :           1 :         const unsigned char *aliased_core_suffix = (const unsigned char *)m_raw_data_ro(raw_core_buffer);
     151                 :           1 :         ASSERT(aliased_core_suffix != NULL);
     152                 :             : 
     153                 :           1 :         IF(aliased_core_suffix != NULL)
     154                 :             :         {
     155                 :           1 :                 aliased_core_suffix += 1;
     156                 :           1 :                 ASSERT(SUCCESS == m_copy_buffer(raw_core_buffer,sizeof(raw_bytes) - 2U,aliased_core_suffix));
     157                 :             :         }
     158                 :             : 
     159                 :           1 :         ASSERT(raw_core_buffer->length == sizeof(expected_raw_core_tail));
     160                 :           1 :         ASSERT(raw_core_buffer->string_length == 0);
     161                 :           1 :         ASSERT(raw_core_buffer->is_string == false);
     162                 :             : 
     163                 :           1 :         const unsigned char *raw_core_view = (const unsigned char *)m_raw_data_ro(raw_core_buffer);
     164                 :           1 :         ASSERT(raw_core_view != NULL);
     165                 :             : 
     166                 :           1 :         IF(raw_core_view != NULL)
     167                 :             :         {
     168                 :           1 :                 ASSERT(0 == memcmp(raw_core_view,expected_raw_core_tail,sizeof(expected_raw_core_tail)));
     169                 :             :         }
     170                 :             : 
     171                 :           1 :         ASSERT(SUCCESS == m_copy_buffer(raw_core_buffer,0,NULL));
     172                 :           1 :         ASSERT(raw_core_buffer->length == 0);
     173                 :           1 :         ASSERT(raw_core_buffer->string_length == 0);
     174                 :           1 :         ASSERT(raw_core_buffer->is_string == false);
     175                 :           1 :         call(m_del(raw_core_buffer));
     176                 :             : 
     177                 :           1 :         RETURN_STATUS;
     178                 :             : }
     179                 :             : 
     180                 :             : /**
     181                 :             :  * @brief Run aliasing scenarios for string and data descriptors
     182                 :             :  *
     183                 :             :  * @return Return describing success or failure
     184                 :             :  */
     185                 :           1 : Return test_libmem_0066(void)
     186                 :             : {
     187                 :           1 :         INITTEST;
     188                 :             : 
     189                 :           1 :         TEST(test_libmem_0066_1,"Aliased string replacement from the current terminator");
     190                 :           1 :         TEST(test_libmem_0066_2,"m_copy preserves string self-copy");
     191                 :           1 :         TEST(test_libmem_0066_3,"Aliased raw-buffer replacement preserves data self-copy");
     192                 :           1 :         TEST(test_libmem_0066_4,"Aliased raw-buffer replacement supports clearing");
     193                 :             : 
     194                 :           1 :         RETURN_STATUS;
     195                 :             : }
        

Generated by: LCOV version 2.0-1