LCOV - code coverage report
Current view: top level - libs/mem/tests/src - test_libmem_0017.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.8 % 48 46
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 1 1
Branches: 83.3 % 6 5

             Branch data     Line data    Source code
       1                 :             : #include "test_libmem_all.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * @brief Check string resizing with ZERO_NEW_MEMORY, shrink, and zero-release
       5                 :             :  *
       6                 :             :  * Builds a string descriptor, temporarily exposes extra tail slots, and
       7                 :             :  * fills them with non-zero marker bytes while leaving the existing string
       8                 :             :  * terminator intact. After hiding that tail inside retained storage, the
       9                 :             :  * test grows the descriptor again with ZERO_NEW_MEMORY and verifies that
      10                 :             :  * every re-exposed marker byte was cleared. It then verifies visible-string
      11                 :             :  * shrink behavior, zero-length resize with retained storage, and final
      12                 :             :  * release of that storage through RELEASE_UNUSED
      13                 :             :  *
      14                 :             :  * @return Return describing success or failure
      15                 :             :  */
      16                 :           1 : Return test_libmem_0017(void)
      17                 :             : {
      18                 :           1 :         INITTEST;
      19                 :             : 
      20                 :           1 :         m_create(char,string_buffer);
      21                 :             : 
      22                 :             :         static const char expected[] = "alpha-beta-gamma+delta-epsilon";
      23                 :           1 :         const size_t previous_length = sizeof(expected);
      24                 :           1 :         const size_t expanded_length = previous_length + 8U;
      25                 :             : 
      26                 :           1 :         ASSERT(SUCCESS == m_to_string(string_buffer));
      27                 :           1 :         ASSERT(SUCCESS == m_copy_fixed_string(string_buffer,sizeof(expected),expected));
      28                 :             : 
      29                 :             :         /* Prepare a deliberately dirty hidden tail before testing ZERO_NEW_MEMORY.
      30                 :             :            A newly allocated buffer may already happen to contain zero bytes, so
      31                 :             :            checking it directly would not prove that the flag cleared anything.
      32                 :             :            These marker bytes make a missing zero-fill immediately visible while
      33                 :             :            leaving the real string and its terminator unchanged */
      34                 :           1 :         ASSERT(SUCCESS == m_resize(string_buffer,expanded_length));
      35                 :             : 
      36                 :           1 :         char *tail_writer = m_data(char,string_buffer);
      37                 :           1 :         ASSERT(tail_writer != NULL);
      38                 :             : 
      39                 :           1 :         IF(tail_writer != NULL)
      40                 :             :         {
      41         [ +  + ]:           9 :                 for(size_t index = previous_length; index < expanded_length; ++index)
      42                 :             :                 {
      43                 :           8 :                         tail_writer[index] = 'X';
      44                 :             :                 }
      45                 :             :         }
      46                 :             : 
      47                 :           1 :         ASSERT(SUCCESS == m_resize(string_buffer,previous_length));
      48                 :           1 :         ASSERT(0 == strcmp(m_text(string_buffer),expected));
      49                 :             : 
      50                 :             :         /* Re-expose the hidden marker bytes: ZERO_NEW_MEMORY must turn them into zeros */
      51                 :           1 :         ASSERT(SUCCESS == m_resize(string_buffer,expanded_length,ZERO_NEW_MEMORY));
      52                 :             : 
      53                 :           1 :         const char *expanded_view = m_data_ro(char,string_buffer);
      54                 :           1 :         ASSERT(expanded_view != NULL);
      55                 :             : 
      56                 :           1 :         IF(expanded_view != NULL)
      57                 :             :         {
      58                 :           1 :                 bool zero_tail = true;
      59                 :             : 
      60         [ +  + ]:           9 :                 for(size_t index = previous_length; index < expanded_length; ++index)
      61                 :             :                 {
      62         [ -  + ]:           8 :                         if(expanded_view[index] != '\0')
      63                 :             :                         {
      64                 :           0 :                                 zero_tail = false;
      65                 :           0 :                                 break;
      66                 :             :                         }
      67                 :             :                 }
      68                 :             : 
      69                 :           1 :                 ASSERT(zero_tail == true);
      70                 :             :         }
      71                 :             : 
      72                 :           1 :         ASSERT(string_buffer->string_length == sizeof(expected) - 1);
      73                 :           1 :         ASSERT(string_buffer->is_string == true);
      74                 :           1 :         ASSERT(SUCCESS == m_resize(string_buffer,6,RELEASE_UNUSED));
      75                 :           1 :         ASSERT(string_buffer->length == 6);
      76                 :           1 :         ASSERT(string_buffer->string_length == 5);
      77                 :           1 :         ASSERT(string_buffer->is_string == true);
      78                 :           1 :         ASSERT(0 == strcmp(m_text(string_buffer),"alpha"));
      79                 :             : 
      80                 :           1 :         void *const retained_data = string_buffer->data;
      81                 :           1 :         const size_t retained_bytes = string_buffer->actually_allocated_bytes;
      82                 :             : 
      83                 :           1 :         ASSERT(SUCCESS == m_resize(string_buffer,0));
      84                 :           1 :         ASSERT(string_buffer->data == retained_data);
      85                 :           1 :         ASSERT(string_buffer->actually_allocated_bytes == retained_bytes);
      86                 :           1 :         ASSERT(string_buffer->length == 0);
      87                 :           1 :         ASSERT(string_buffer->string_length == 0);
      88                 :           1 :         ASSERT(string_buffer->is_string == true);
      89                 :             : 
      90                 :           1 :         ASSERT(SUCCESS == m_resize(string_buffer,0,RELEASE_UNUSED));
      91                 :           1 :         ASSERT(string_buffer->data == NULL);
      92                 :           1 :         ASSERT(string_buffer->actually_allocated_bytes == 0);
      93                 :           1 :         ASSERT(string_buffer->length == 0);
      94                 :           1 :         ASSERT(string_buffer->string_length == 0);
      95                 :           1 :         ASSERT(string_buffer->is_string == true);
      96                 :           1 :         call(m_del(string_buffer));
      97                 :             : 
      98                 :           1 :         RETURN_STATUS;
      99                 :             : }
        

Generated by: LCOV version 2.0-1