LCOV - code coverage report
Current view: top level - src/replacements/unittests - strsep.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 35 35 100.0 %
Date: 2025-04-23 10:28:42 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2018  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : #undef NDEBUG
      21             : #include <config.h>
      22             : 
      23             : #ifdef HAVE_STRSEP
      24             : char *tds_strsep(char **stringp, const char *delim);
      25             : #include "../strsep.c"
      26             : 
      27             : #include <freetds/utils/test_base.h>
      28             : 
      29             : #else
      30             : 
      31             : #include <freetds/utils/test_base.h>
      32             : 
      33             : #include <stdarg.h>
      34             : #include <stdio.h>
      35             : 
      36             : #if HAVE_STRING_H
      37             : #include <string.h>
      38             : #endif /* HAVE_STRING_H */
      39             : 
      40             : #include <freetds/replacements.h>
      41             : #endif
      42             : 
      43             : #include <stdarg.h>
      44             : #include <stdlib.h>
      45             : #include <assert.h>
      46             : 
      47             : /* test strsep with same separators */
      48             : static void
      49          80 : test(char *s, const char *sep, ...)
      50             : {
      51          80 :         char *copy = strdup(s);
      52             :         const char *out, *expected;
      53             :         va_list ap;
      54             : 
      55          80 :         printf("testing '%s' with '%s' separator(s)\n", s, sep);
      56             : 
      57          80 :         s = copy;
      58          80 :         va_start(ap, sep);
      59             :         do {
      60         320 :                 out = tds_strsep(&s, sep);
      61         320 :                 expected = va_arg(ap, const char *);
      62         320 :                 if (expected) {
      63         240 :                         assert(out && strcmp(out, expected) == 0);
      64             :                 } else {
      65          80 :                         assert(out == NULL);
      66             :                 }
      67         320 :         } while (expected != NULL);
      68          80 :         va_end(ap);
      69             : 
      70             :         /* should continue to give NULL */
      71          80 :         assert(tds_strsep(&s, sep) == NULL);
      72          80 :         assert(tds_strsep(&s, sep) == NULL);
      73             : 
      74          80 :         free(copy);
      75          80 : }
      76             : 
      77             : /* test with different separators */
      78             : static void
      79          10 : test2(void)
      80             : {
      81          10 :         char buf[] = "one;two=value";
      82          10 :         char *s = buf;
      83          10 :         assert(strcmp(tds_strsep(&s, ";:"), "one") == 0);
      84          10 :         assert(strcmp(tds_strsep(&s, "="), "two") == 0);
      85          10 :         assert(strcmp(tds_strsep(&s, ""), "value") == 0);
      86          10 :         assert(tds_strsep(&s, "") == NULL);
      87          10 : }
      88             : 
      89          10 : TEST_MAIN()
      90             : {
      91          10 :         test("a b c", "", "a b c", NULL);
      92          10 :         test("a b c", " ", "a", "b", "c", NULL);
      93          10 :         test("a  c", " ", "a", "", "c", NULL);
      94          10 :         test("a b\tc", " \t", "a", "b", "c", NULL);
      95          10 :         test("a b\tc ", " \t", "a", "b", "c", "", NULL);
      96          10 :         test(" a b\tc", " \t", "", "a", "b", "c", NULL);
      97          10 :         test(",,", ",", "", "", "", NULL);
      98          10 :         test(",foo,", ",", "", "foo", "", NULL);
      99          10 :         test2();
     100          10 :         return 0;
     101             : }

Generated by: LCOV version 1.13