LCOV - code coverage report
Current view: top level - src/replacements/unittests - strsep.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 34 34 100.0 %
Date: 2025-07-06 17:21:34 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             : #else
      27             : #include <stdarg.h>
      28             : #include <stdio.h>
      29             : 
      30             : #if HAVE_STRING_H
      31             : #include <string.h>
      32             : #endif /* HAVE_STRING_H */
      33             : 
      34             : #include <freetds/replacements.h>
      35             : #endif
      36             : 
      37             : #include <stdarg.h>
      38             : #include <stdlib.h>
      39             : #include <assert.h>
      40             : 
      41             : /* test strsep with same separators */
      42             : static void
      43          80 : test(char *s, const char *sep, ...)
      44             : {
      45          80 :         char *copy = strdup(s);
      46             :         const char *out, *expected;
      47             :         va_list ap;
      48             : 
      49          80 :         printf("testing '%s' with '%s' separator(s)\n", s, sep);
      50             : 
      51          80 :         s = copy;
      52          80 :         va_start(ap, sep);
      53             :         do {
      54         320 :                 out = tds_strsep(&s, sep);
      55         320 :                 expected = va_arg(ap, const char *);
      56         320 :                 if (expected) {
      57         240 :                         assert(out && strcmp(out, expected) == 0);
      58             :                 } else {
      59          80 :                         assert(out == NULL);
      60             :                 }
      61         320 :         } while (expected != NULL);
      62          80 :         va_end(ap);
      63             : 
      64             :         /* should continue to give NULL */
      65          80 :         assert(tds_strsep(&s, sep) == NULL);
      66          80 :         assert(tds_strsep(&s, sep) == NULL);
      67             : 
      68          80 :         free(copy);
      69          80 : }
      70             : 
      71             : /* test with different separators */
      72             : static void
      73          10 : test2(void)
      74             : {
      75          10 :         char buf[] = "one;two=value";
      76          10 :         char *s = buf;
      77          10 :         assert(strcmp(tds_strsep(&s, ";:"), "one") == 0);
      78          10 :         assert(strcmp(tds_strsep(&s, "="), "two") == 0);
      79          10 :         assert(strcmp(tds_strsep(&s, ""), "value") == 0);
      80          10 :         assert(tds_strsep(&s, "") == NULL);
      81          10 : }
      82             : 
      83             : int
      84          10 : main(void)
      85             : {
      86          10 :         test("a b c", "", "a b c", NULL);
      87          10 :         test("a b c", " ", "a", "b", "c", NULL);
      88          10 :         test("a  c", " ", "a", "", "c", NULL);
      89          10 :         test("a b\tc", " \t", "a", "b", "c", NULL);
      90          10 :         test("a b\tc ", " \t", "a", "b", "c", "", NULL);
      91          10 :         test(" a b\tc", " \t", "", "a", "b", "c", NULL);
      92          10 :         test(",,", ",", "", "", "", NULL);
      93          10 :         test(",foo,", ",", "", "foo", "", NULL);
      94          10 :         test2();
      95             :         return 0;
      96             : }

Generated by: LCOV version 1.13