LCOV - code coverage report
Current view: top level - libs/mem/tests/src - test_libmem_0016.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 65 65
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 Verify fixed-size string copy and append helpers
       5                 :             :  *
       6                 :             :  * Copies a fixed-size `alpha` string into a string descriptor, then
       7                 :             :  * appends the fixed-size `-beta` suffix. Fixed-size mode trusts that
       8                 :             :  * the final element inside each source array is the terminator, so this
       9                 :             :  * test checks the resulting text and both cached string counters
      10                 :             :  *
      11                 :             :  * @return Return describing success or failure
      12                 :             :  */
      13                 :           1 : static Return test_libmem_0016_1(void)
      14                 :             : {
      15                 :           1 :         INITTEST;
      16                 :             : 
      17                 :           1 :         m_create(char,string_buffer,MEMORY_STRING);
      18                 :             : 
      19                 :           1 :         const char alpha_text[] = "alpha";
      20                 :           1 :         const char beta_suffix[] = "-beta";
      21                 :             :         static const char expected[] = "alpha-beta";
      22                 :             : 
      23                 :             :         /* Replace the empty descriptor with one fixed-size source */
      24                 :           1 :         ASSERT(SUCCESS == m_copy_fixed_string(string_buffer,sizeof(alpha_text),alpha_text));
      25                 :           1 :         ASSERT(string_buffer->string_length == sizeof(alpha_text) - 1);
      26                 :           1 :         ASSERT(string_buffer->length == sizeof(alpha_text));
      27                 :           1 :         ASSERT(0 == strcmp(m_text(string_buffer),alpha_text));
      28                 :             : 
      29                 :             :         /* Append another fixed-size source and verify the complete string view */
      30                 :           1 :         ASSERT(SUCCESS == m_concat_fixed_string(string_buffer,sizeof(beta_suffix),beta_suffix));
      31                 :           1 :         ASSERT(string_buffer->string_length == sizeof(expected) - 1);
      32                 :           1 :         ASSERT(string_buffer->length == sizeof(expected));
      33                 :           1 :         ASSERT(string_buffer->is_string == true);
      34                 :           1 :         ASSERT(0 == strcmp(m_text(string_buffer),expected));
      35                 :             : 
      36                 :           1 :         call(m_del(string_buffer));
      37                 :             : 
      38                 :           1 :         RETURN_STATUS;
      39                 :             : }
      40                 :             : 
      41                 :             : /**
      42                 :             :  * @brief Verify bounded string copy stops at the first terminator
      43                 :             :  *
      44                 :             :  * Copies from a bounded source buffer that contains `-gamma`, a zero
      45                 :             :  * terminator, and one extra byte after that terminator. The copied
      46                 :             :  * descriptor must expose only the visible `-gamma` text and must not
      47                 :             :  * include the trailing byte in its logical string length
      48                 :             :  *
      49                 :             :  * @return Return describing success or failure
      50                 :             :  */
      51                 :           1 : static Return test_libmem_0016_2(void)
      52                 :             : {
      53                 :           1 :         INITTEST;
      54                 :             : 
      55                 :           1 :         m_create(char,extra_buffer,MEMORY_STRING);
      56                 :             : 
      57                 :           1 :         const char bounded_gamma[] = {'-','g','a','m','m','a','\0','x'};
      58                 :             :         static const char expected[] = "-gamma";
      59                 :             : 
      60                 :             :         /* The three-argument m_copy_string form routes to the bounded scanner */
      61                 :           1 :         ASSERT(SUCCESS == m_copy_string(extra_buffer,sizeof(bounded_gamma),bounded_gamma));
      62                 :           1 :         ASSERT(extra_buffer->string_length == sizeof(expected) - 1);
      63                 :           1 :         ASSERT(extra_buffer->length == sizeof(expected));
      64                 :           1 :         ASSERT(extra_buffer->is_string == true);
      65                 :           1 :         ASSERT(0 == strcmp(m_text(extra_buffer),expected));
      66                 :             : 
      67                 :           1 :         call(m_del(extra_buffer));
      68                 :             : 
      69                 :           1 :         RETURN_STATUS;
      70                 :             : }
      71                 :             : 
      72                 :             : /**
      73                 :             :  * @brief Verify descriptor-to-descriptor append uses cached source string length
      74                 :             :  *
      75                 :             :  * Appends a managed string descriptor into another managed string
      76                 :             :  * descriptor. The source is deliberately grown after receiving
      77                 :             :  * `+delta`, so its logical descriptor length is larger than the
      78                 :             :  * visible payload. m_concat_strings must append only the cached visible
      79                 :             :  * source prefix plus one terminator, ignoring any reserved tail
      80                 :             :  *
      81                 :             :  * @return Return describing success or failure
      82                 :             :  */
      83                 :           1 : static Return test_libmem_0016_3(void)
      84                 :             : {
      85                 :           1 :         INITTEST;
      86                 :             : 
      87                 :           1 :         m_create(char,string_buffer,MEMORY_STRING);
      88                 :           1 :         m_create(char,extra_buffer,MEMORY_STRING);
      89                 :             : 
      90                 :             :         static const char alpha_beta_text[] = "alpha-beta";
      91                 :             :         static const char plus_delta_text[] = "+delta";
      92                 :             :         static const char expected[] = "alpha-beta+delta";
      93                 :             : 
      94                 :             :         /* Prepare the destination and source as managed string descriptors */
      95                 :           1 :         ASSERT(SUCCESS == m_copy_fixed_string(string_buffer,sizeof(alpha_beta_text),alpha_beta_text));
      96                 :           1 :         ASSERT(SUCCESS == m_copy_fixed_string(extra_buffer,sizeof(plus_delta_text),plus_delta_text));
      97                 :             : 
      98                 :             :         /* Grow the source descriptor to prove concat uses string_length rather than source length */
      99                 :           1 :         ASSERT(SUCCESS == m_resize(extra_buffer,16));
     100                 :           1 :         ASSERT(extra_buffer->string_length == sizeof(plus_delta_text) - 1);
     101                 :           1 :         ASSERT(extra_buffer->length == 16);
     102                 :             : 
     103                 :             :         /* Append from descriptor to descriptor and verify that the source reserve tail was ignored */
     104                 :           1 :         ASSERT(SUCCESS == m_concat_strings(string_buffer,extra_buffer));
     105                 :           1 :         ASSERT(string_buffer->string_length == sizeof(expected) - 1);
     106                 :           1 :         ASSERT(string_buffer->length == sizeof(expected));
     107                 :           1 :         ASSERT(string_buffer->is_string == true);
     108                 :           1 :         ASSERT(0 == strcmp(m_text(string_buffer),expected));
     109                 :             : 
     110                 :           1 :         call(m_del(string_buffer));
     111                 :           1 :         call(m_del(extra_buffer));
     112                 :             : 
     113                 :           1 :         RETURN_STATUS;
     114                 :             : }
     115                 :             : 
     116                 :             : /**
     117                 :             :  * @brief Verify bounded string append stops at the first terminator
     118                 :             :  *
     119                 :             :  * Starts from the combined `alpha-beta-gamma+delta` text and appends a
     120                 :             :  * bounded `-epsilon` source that has two extra bytes after its
     121                 :             :  * terminator. The final descriptor must contain only the visible
     122                 :             :  * bounded suffix and keep length/string_length coherent
     123                 :             :  *
     124                 :             :  * @return Return describing success or failure
     125                 :             :  */
     126                 :           1 : static Return test_libmem_0016_4(void)
     127                 :             : {
     128                 :           1 :         INITTEST;
     129                 :             : 
     130                 :           1 :         m_create(char,string_buffer,MEMORY_STRING);
     131                 :             : 
     132                 :             :         static const char prefix[] = "alpha-beta-gamma+delta";
     133                 :           1 :         const char bounded_suffix[] = {'-','e','p','s','i','l','o','n','\0','x','x'};
     134                 :             :         static const char expected[] = "alpha-beta-gamma+delta-epsilon";
     135                 :             : 
     136                 :             :         /* Seed the descriptor with the prefix that the bounded append should extend */
     137                 :           1 :         ASSERT(SUCCESS == m_copy_fixed_string(string_buffer,sizeof(prefix),prefix));
     138                 :           1 :         ASSERT(string_buffer->string_length == sizeof(prefix) - 1);
     139                 :           1 :         ASSERT(string_buffer->length == sizeof(prefix));
     140                 :             : 
     141                 :             :         /* The three-argument m_concat_string form routes to the bounded scanner */
     142                 :           1 :         ASSERT(SUCCESS == m_concat_string(string_buffer,sizeof(bounded_suffix),bounded_suffix));
     143                 :           1 :         ASSERT(string_buffer->string_length == sizeof(expected) - 1);
     144                 :           1 :         ASSERT(string_buffer->length == sizeof(expected));
     145                 :           1 :         ASSERT(string_buffer->is_string == true);
     146                 :           1 :         ASSERT(0 == strcmp(m_text(string_buffer),expected));
     147                 :             : 
     148                 :           1 :         call(m_del(string_buffer));
     149                 :             : 
     150                 :           1 :         RETURN_STATUS;
     151                 :             : }
     152                 :             : 
     153                 :             : /**
     154                 :             :  * @brief Verify string copy and concatenation helper combinations
     155                 :             :  *
     156                 :             :  * In plain terms, this suite checks the string-transfer helpers used to
     157                 :             :  * build the example text `alpha-beta-gamma+delta-epsilon`. The nested
     158                 :             :  * tests keep each source mode separate: fixed-size copy and append,
     159                 :             :  * bounded copy that ignores bytes after the first terminator,
     160                 :             :  * descriptor-to-descriptor append that trusts cached string_length, and
     161                 :             :  * bounded append that also ignores trailing bytes after a terminator.
     162                 :             :  * Each step verifies both the visible text and the descriptor metadata
     163                 :             :  * that makes later string operations safe
     164                 :             :  *
     165                 :             :  * @return Return describing success or failure
     166                 :             :  */
     167                 :           1 : Return test_libmem_0016(void)
     168                 :             : {
     169                 :           1 :         INITTEST;
     170                 :             : 
     171                 :           1 :         TEST(test_libmem_0016_1,"Fixed-size string copy and append build alpha-beta");
     172                 :           1 :         TEST(test_libmem_0016_2,"Bounded string copy ignores bytes after the terminator");
     173                 :           1 :         TEST(test_libmem_0016_3,"Descriptor string append uses cached source length");
     174                 :           1 :         TEST(test_libmem_0016_4,"Bounded string append ignores bytes after the terminator");
     175                 :             : 
     176                 :           1 :         RETURN_STATUS;
     177                 :             : }
        

Generated by: LCOV version 2.0-1