LCOV - code coverage report
Current view: top level - src/tests - string.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 87 87 100.0 %
Date: 2025-03-29 09:04:10 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 109 220 49.5 %

           Branch data     Line data    Source code
       1                 :            : #include "utils.h"
       2                 :            : 
       3                 :            : static const pl_str null = {0};
       4                 :            : static const pl_str test = PL_STR0("test");
       5                 :            : static const pl_str empty = PL_STR0("");
       6                 :            : 
       7                 :            : static inline bool is_null(pl_str str)
       8                 :            : {
       9   [ +  -  -  +  :          7 :     return !str.len && !str.buf;
          +  -  -  +  +  
          -  -  +  +  -  
          -  +  +  -  -  
          +  -  +  +  -  
                   -  + ]
      10                 :            : }
      11                 :            : 
      12                 :            : static inline bool is_empty(pl_str str)
      13                 :            : {
      14                 :            :     return !str.len;
      15                 :            : }
      16                 :            : 
      17                 :          1 : int main()
      18                 :            : {
      19                 :          1 :     void *tmp = pl_tmp(NULL);
      20                 :            : 
      21                 :            :     REQUIRE(is_null(pl_str0(NULL)));
      22                 :            :     REQUIRE(is_null(pl_strdup(tmp, null)));
      23                 :            :     char *empty0 = pl_strdup0(tmp, null);
      24   [ +  -  -  + ]:          1 :     REQUIRE(empty0 && !empty0[0]);
      25         [ -  + ]:          1 :     REQUIRE(pl_str_equals0(empty, empty0));
      26                 :            : 
      27                 :          1 :     pl_str buf = {0};
      28                 :          1 :     pl_str_append(tmp, &buf, null);
      29         [ -  + ]:          1 :     REQUIRE(is_empty(buf));
      30                 :          1 :     pl_str_append_asprintf(tmp, &buf, "%.*s", PL_STR_FMT(test));
      31         [ -  + ]:          1 :     REQUIRE(pl_str_equals(buf, test));
      32                 :            : 
      33                 :          1 :     pl_str_append_asprintf_c(tmp, &buf, "%d %f %f %f %lld %zu %.*sx %hx %hx %hx %hx",
      34                 :            :         1, 1.0f, 4294967295.56, 83224965647295.65, 0xFFll, (size_t) 0, PL_STR_FMT(empty),
      35                 :            :         (unsigned short) 0xCAFEu, (unsigned short) 0x1, (unsigned short) 0,
      36                 :            :         (unsigned short) 0xFFFFu);
      37                 :            :     const char *expected = "test1 1 4294967295.56 83224965647295.66 255 0 x cafe 1 0 ffff";
      38                 :            :     // fallback can produce different precision
      39                 :            :     const char *expected2 = "test1 1 4294967295.55999994277954102 83224965647295.65625 255 0 x cafe 1 0 ffff";
      40   [ -  +  -  - ]:          1 :     REQUIRE(pl_str_equals0(buf, expected) || pl_str_equals0(buf, expected2));
      41                 :            : 
      42         [ -  + ]:          1 :     REQUIRE_CMP(pl_strchr(null, ' '), <, 0, "d");
      43         [ -  + ]:          1 :     REQUIRE_CMP((int) pl_strspn(null, " "), ==, 0, "d");
      44         [ -  + ]:          1 :     REQUIRE_CMP((int) pl_strcspn(null, " "), ==, 0, "d");
      45         [ -  + ]:          2 :     REQUIRE(is_null(pl_str_strip(null)));
      46                 :            : 
      47         [ -  + ]:          1 :     REQUIRE_CMP(pl_strchr(test, 's'), ==, 2, "d");
      48         [ -  + ]:          1 :     REQUIRE_CMP((int) pl_strspn(test, "et"), ==, 2, "d");
      49         [ -  + ]:          1 :     REQUIRE_CMP((int) pl_strcspn(test, "xs"), ==, 2, "d");
      50                 :            : 
      51                 :            :     REQUIRE(is_null(pl_str_take(null, 10)));
      52                 :            :     REQUIRE(is_empty(pl_str_take(test, 0)));
      53                 :            :     REQUIRE(is_null(pl_str_drop(null, 10)));
      54                 :            :     REQUIRE(is_null(pl_str_drop(test, test.len)));
      55         [ -  + ]:          1 :     REQUIRE(pl_str_equals(pl_str_drop(test, 0), test));
      56                 :            : 
      57         [ -  + ]:          1 :     REQUIRE_CMP(pl_str_find(null, test), <, 0, "d");
      58         [ -  + ]:          1 :     REQUIRE_CMP(pl_str_find(null, null), ==, 0, "d");
      59         [ -  + ]:          1 :     REQUIRE_CMP(pl_str_find(test, null), ==, 0, "d");
      60         [ -  + ]:          1 :     REQUIRE_CMP(pl_str_find(test, test), ==, 0, "d");
      61                 :            : 
      62                 :            :     pl_str rest;
      63   [ +  -  -  + ]:          3 :     REQUIRE(is_null(pl_str_split_char(null, ' ', &rest)) && is_null(rest));
      64   [ +  -  -  + ]:          3 :     REQUIRE(is_null(pl_str_split_str(null, test, &rest)) && is_null(rest));
      65   [ +  -  -  + ]:          1 :     REQUIRE(is_empty(pl_str_split_str(test, test, &rest)) && is_empty(rest));
      66   [ +  -  +  -  :          3 :     REQUIRE(is_null(pl_str_getline(null, &rest)) && is_null(rest));
                   -  + ]
      67                 :            : 
      68                 :          1 :     pl_str right, left = pl_str_split_char(pl_str0("left right"), ' ', &right);
      69         [ -  + ]:          1 :     REQUIRE(pl_str_equals0(left, "left"));
      70         [ -  + ]:          1 :     REQUIRE(pl_str_equals0(right, "right"));
      71                 :            : 
      72                 :          1 :     left = pl_str_split_str0(pl_str0("leftTESTright"), "TEST", &right);
      73         [ -  + ]:          1 :     REQUIRE(pl_str_equals0(left, "left"));
      74         [ -  + ]:          1 :     REQUIRE(pl_str_equals0(right, "right"));
      75                 :            : 
      76                 :            :     pl_str out;
      77   [ +  -  -  + ]:          1 :     REQUIRE(pl_str_decode_hex(tmp, null, &out) && is_empty(out));
      78         [ -  + ]:          1 :     REQUIRE(!pl_str_decode_hex(tmp, pl_str0("invalid"), &out));
      79                 :            : 
      80         [ -  + ]:          1 :     REQUIRE(pl_str_equals(null, null));
      81         [ -  + ]:          1 :     REQUIRE(pl_str_equals(null, empty));
      82                 :            :     REQUIRE(pl_str_startswith(null, null));
      83                 :            :     REQUIRE(pl_str_startswith(test, null));
      84                 :            :     REQUIRE(pl_str_startswith(test, test));
      85         [ -  + ]:          1 :     REQUIRE(pl_str_endswith(null, null));
      86         [ -  + ]:          1 :     REQUIRE(pl_str_endswith(test, null));
      87         [ -  + ]:          1 :     REQUIRE(pl_str_endswith(test, test));
      88                 :            : 
      89                 :            :     double d;
      90                 :            :     float f;
      91                 :            :     int i;
      92                 :            :     unsigned u;
      93                 :            :     int64_t i64;
      94                 :            :     uint64_t u64;
      95                 :            : 
      96   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_double(pl_str0("4294967295.56"), &d));      REQUIRE_FEQ(d, 4294967295.56, 1e-20);
      97   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_double(pl_str0("-4294967295.56"), &d));     REQUIRE_FEQ(d, -4294967295.56, 1e-20);
      98   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_double(pl_str0("83224965647295.65"), &d));  REQUIRE_FEQ(d, 83224965647295.65, 1e-20);
      99   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_double(pl_str0("-83224965647295.65"), &d)); REQUIRE_FEQ(d, -83224965647295.65, 1e-20);
     100   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("4294967295.56"), &f));       REQUIRE_FEQ(f, 4294967295.56f, 1e-8);
     101   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("-4294967295.56"), &f));      REQUIRE_FEQ(f, -4294967295.56f, 1e-8);
     102   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("83224965647295.65"), &f));   REQUIRE_FEQ(f, 83224965647295.65f, 1e-8);
     103   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("-83224965647295.65"), &f));  REQUIRE_FEQ(f, -83224965647295.65f, 1e-8);
     104   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("1.3984"), &f));     REQUIRE_FEQ(f, 1.3984f, 1e-8);
     105   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("-8.9100083"), &f)); REQUIRE_FEQ(f, -8.9100083f, 1e-8);
     106   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("-0"), &f));         REQUIRE_FEQ(f, 0.0f, 1e-8);
     107   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("-3.14e20"), &f));   REQUIRE_FEQ(f, -3.14e20f, 1e-8);
     108   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("0.5e-5"), &f));     REQUIRE_FEQ(f, 0.5e-5f, 1e-8);
     109   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_float(pl_str0("0.5e+5"), &f));     REQUIRE_FEQ(f, 0.5e+5f, 1e-8);
     110   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_int(pl_str0("64239"), &i));        REQUIRE_CMP(i, ==, 64239, "d");
     111   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_int(pl_str0("-102"), &i));         REQUIRE_CMP(i, ==, -102, "d");
     112   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_int(pl_str0("1"), &i));            REQUIRE_CMP(i, ==, 1, "d");
     113   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_int(pl_str0("-0"), &i));           REQUIRE_CMP(i, ==, 0, "d");
     114   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_uint(pl_str0("64239"), &u));       REQUIRE_CMP(u, ==, 64239, "u");
     115   [ -  +  -  + ]:          1 :     REQUIRE(pl_str_parse_uint(pl_str0("1"), &u));           REQUIRE_CMP(u, ==, 1, "u");
     116         [ -  + ]:          1 :     REQUIRE(pl_str_parse_int64(pl_str0("9223372036854775799"), &i64));
     117         [ -  + ]:          1 :     REQUIRE_CMP(i64, ==, 9223372036854775799LL, PRIi64);
     118         [ -  + ]:          1 :     REQUIRE(pl_str_parse_int64(pl_str0("-9223372036854775799"), &i64));
     119         [ -  + ]:          1 :     REQUIRE_CMP(i64, ==, -9223372036854775799LL, PRIi64);
     120         [ -  + ]:          1 :     REQUIRE(pl_str_parse_uint64(pl_str0("18446744073709551609"), &u64));
     121         [ -  + ]:          1 :     REQUIRE_CMP(u64, ==, 18446744073709551609LLU, PRIu64);
     122         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_float(null, &f));
     123         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_float(test, &f));
     124         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_float(empty, &f));
     125         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_int(null, &i));
     126         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_int(test, &i));
     127         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_int(empty, &i));
     128         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_uint(null, &u));
     129         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_uint(test, &u));
     130         [ -  + ]:          1 :     REQUIRE(!pl_str_parse_uint(empty, &u));
     131                 :            : 
     132                 :          1 :     pl_str_builder builder = pl_str_builder_alloc(tmp);
     133                 :          1 :     pl_str_builder_const_str(builder, "hello");
     134                 :          1 :     pl_str_builder_str(builder, pl_str0("world"));
     135                 :          1 :     pl_str res = pl_str_builder_exec(builder);
     136         [ -  + ]:          1 :     REQUIRE(pl_str_equals0(res, "helloworld"));
     137                 :            : 
     138                 :          1 :     pl_str_builder_reset(builder);
     139                 :          1 :     pl_str_builder_printf_c(builder, "foo %d bar %u bat %s baz %lld",
     140                 :            :             123, 56u, "quack", 0xDEADBEEFll);
     141                 :          1 :     pl_str_builder_printf_c(builder, " %.*s", PL_STR_FMT(pl_str0("test123")));
     142                 :          1 :     res = pl_str_builder_exec(builder);
     143         [ -  + ]:          1 :     REQUIRE(pl_str_equals0(res, "foo 123 bar 56 bat quack baz 3735928559 test123"));
     144                 :            : 
     145                 :          1 :     pl_free(tmp);
     146                 :            :     return 0;
     147                 :            : }

Generated by: LCOV version 1.16