LCOV - code coverage report
Current view: top level - test - test_string_view.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 63 63 100.0 %
Date: 2026-11-26 02:32:31 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 39 78 50.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2021-2024 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #undef NDEBUG
       5                 :            : 
       6                 :            : #include "failing_allocator.h"
       7                 :            : 
       8                 :            : #include <zix/allocator.h>
       9                 :            : #include <zix/string_view.h>
      10                 :            : 
      11                 :            : #include <assert.h>
      12                 :            : #include <string.h>
      13                 :            : 
      14                 :            : static void
      15                 :          1 : test_static_init(void)
      16                 :            : {
      17                 :            :   static const ZixStringView a  = ZIX_STATIC_STRING("a");
      18                 :            :   static const ZixStringView ab = ZIX_STATIC_STRING("ab");
      19                 :            : 
      20         [ -  + ]:          1 :   assert(a.length == 1U);
      21         [ -  + ]:          1 :   assert(a.data);
      22         [ -  + ]:          1 :   assert(a.data[0] == 'a');
      23         [ -  + ]:          1 :   assert(a.data[1] == '\0');
      24                 :            : 
      25         [ -  + ]:          1 :   assert(ab.length == 2U);
      26         [ -  + ]:          1 :   assert(ab.data[0] == 'a');
      27         [ -  + ]:          1 :   assert(ab.data[1] == 'b');
      28         [ -  + ]:          1 :   assert(ab.data[2] == '\0');
      29                 :          1 : }
      30                 :            : 
      31                 :            : static void
      32                 :          1 : test_empty(void)
      33                 :            : {
      34                 :            :   const ZixStringView empty = zix_empty_string();
      35                 :            : 
      36         [ -  + ]:          1 :   assert(!empty.length);
      37         [ -  + ]:          1 :   assert(empty.data);
      38         [ -  + ]:          1 :   assert(empty.data[0] == '\0');
      39                 :          1 : }
      40                 :            : 
      41                 :            : static void
      42                 :          1 : test_string(void)
      43                 :            : {
      44                 :            :   const ZixStringView nodata = zix_string(NULL);
      45                 :            :   const ZixStringView empty  = zix_string("");
      46                 :            : 
      47         [ -  + ]:          1 :   assert(!nodata.length);
      48         [ -  + ]:          1 :   assert(nodata.data);
      49         [ -  + ]:          1 :   assert(nodata.data[0] == '\0');
      50                 :            : 
      51         [ -  + ]:          1 :   assert(!empty.length);
      52         [ -  + ]:          1 :   assert(empty.data);
      53         [ -  + ]:          1 :   assert(empty.data[0] == '\0');
      54                 :          1 : }
      55                 :            : 
      56                 :            : static void
      57                 :          1 : test_equals(void)
      58                 :            : {
      59                 :            :   static const char* const prefix_str = "prefix";
      60                 :            : 
      61         [ +  - ]:          1 :   const ZixStringView prefix  = zix_string(prefix_str);
      62                 :          1 :   const ZixStringView pre     = zix_substring(prefix_str, 3U);
      63                 :          1 :   const ZixStringView fix     = zix_substring(prefix_str + 3U, 3U);
      64                 :            :   const ZixStringView suffix1 = zix_substring("suffix_1", 6U);
      65                 :            :   const ZixStringView suffix2 = zix_substring("suffix_2", 6U);
      66                 :            : 
      67         [ -  + ]:          1 :   assert(prefix.length == 6U);
      68         [ -  + ]:          1 :   assert(pre.length == 3U);
      69         [ -  + ]:          1 :   assert(fix.length == 3U);
      70         [ -  + ]:          1 :   assert(suffix1.length == 6U);
      71         [ -  + ]:          1 :   assert(suffix2.length == 6U);
      72                 :            : 
      73         [ -  + ]:          1 :   assert(zix_string_view_equals(prefix, zix_string("prefix")));
      74         [ -  + ]:          1 :   assert(zix_string_view_equals(pre, zix_string("pre")));
      75         [ -  + ]:          1 :   assert(zix_string_view_equals(fix, zix_string("fix")));
      76         [ -  + ]:          1 :   assert(zix_string_view_equals(suffix1, zix_string("suffix")));
      77         [ -  + ]:          1 :   assert(zix_string_view_equals(suffix2, zix_string("suffix")));
      78                 :            : 
      79         [ -  + ]:          1 :   assert(zix_string_view_equals(prefix, prefix));
      80         [ -  + ]:          1 :   assert(zix_string_view_equals(suffix1, suffix2));
      81                 :            : 
      82         [ -  + ]:          1 :   assert(!zix_string_view_equals(prefix, pre));
      83         [ -  + ]:          1 :   assert(!zix_string_view_equals(pre, prefix));
      84         [ -  + ]:          1 :   assert(!zix_string_view_equals(pre, fix));
      85         [ -  + ]:          1 :   assert(!zix_string_view_equals(fix, prefix));
      86         [ -  + ]:          1 :   assert(!zix_string_view_equals(suffix1, prefix));
      87         [ -  + ]:          1 :   assert(!zix_string_view_equals(prefix, suffix1));
      88                 :          1 : }
      89                 :            : 
      90                 :            : static void
      91                 :          1 : test_copy(void)
      92                 :            : {
      93                 :            :   static const ZixStringView orig = ZIX_STATIC_STRING("string");
      94                 :            : 
      95                 :          1 :   ZixFailingAllocator allocator = zix_failing_allocator();
      96                 :            : 
      97                 :            :   // Copying a string takes exactly one allocation
      98                 :          1 :   zix_failing_allocator_reset(&allocator, 1U);
      99                 :            : 
     100                 :          1 :   char* const copy = zix_string_view_copy(&allocator.base, orig);
     101         [ -  + ]:          1 :   assert(copy);
     102         [ -  + ]:          1 :   assert(!strcmp(copy, "string"));
     103                 :          1 :   zix_free(&allocator.base, copy);
     104                 :            : 
     105                 :            :   // Check that allocation failure is handled gracefully
     106                 :          1 :   zix_failing_allocator_reset(&allocator, 0U);
     107         [ -  + ]:          1 :   assert(!zix_string_view_copy(&allocator.base, orig));
     108                 :          1 : }
     109                 :            : 
     110                 :            : int
     111                 :          1 : main(void)
     112                 :            : {
     113                 :          1 :   test_static_init();
     114                 :          1 :   test_empty();
     115                 :          1 :   test_string();
     116                 :          1 :   test_equals();
     117                 :          1 :   test_copy();
     118                 :          1 :   return 0;
     119                 :            : }

Generated by: LCOV version 1.16