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-01-18 11:50:39 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          64 : test(char *s, const char *sep, ...)
      44             : {
      45          64 :         char *copy = strdup(s);
      46             :         const char *out, *expected;
      47             :         va_list ap;
      48             : 
      49          64 :         printf("testing '%s' with '%s' separator(s)\n", s, sep);
      50             : 
      51          64 :         s = copy;
      52          64 :         va_start(ap, sep);
      53             :         do {
      54         256 :                 out = tds_strsep(&s, sep);
      55         256 :                 expected = va_arg(ap, const char *);
      56         256 :                 if (expected) {
      57         192 :                         assert(out && strcmp(out, expected) == 0);
      58             :                 } else {
      59          64 :                         assert(out == NULL);
      60             :                 }
      61         256 :         } while (expected != NULL);
      62          64 :         va_end(ap);
      63             : 
      64             :         /* should continue to give NULL */
      65          64 :         assert(tds_strsep(&s, sep) == NULL);
      66          64 :         assert(tds_strsep(&s, sep) == NULL);
      67             : 
      68          64 :         free(copy);
      69          64 : }
      70             : 
      71             : /* test with different separators */
      72             : static void
      73           8 : test2(void)
      74             : {
      75           8 :         char buf[] = "one;two=value";
      76           8 :         char *s = buf;
      77           8 :         assert(strcmp(tds_strsep(&s, ";:"), "one") == 0);
      78           8 :         assert(strcmp(tds_strsep(&s, "="), "two") == 0);
      79           8 :         assert(strcmp(tds_strsep(&s, ""), "value") == 0);
      80           8 :         assert(tds_strsep(&s, "") == NULL);
      81           8 : }
      82             : 
      83             : int
      84           8 : main(void)
      85             : {
      86           8 :         test("a b c", "", "a b c", NULL);
      87           8 :         test("a b c", " ", "a", "b", "c", NULL);
      88           8 :         test("a  c", " ", "a", "", "c", NULL);
      89           8 :         test("a b\tc", " \t", "a", "b", "c", NULL);
      90           8 :         test("a b\tc ", " \t", "a", "b", "c", "", NULL);
      91           8 :         test(" a b\tc", " \t", "", "a", "b", "c", NULL);
      92           8 :         test(",,", ",", "", "", "", NULL);
      93           8 :         test(",foo,", ",", "", "foo", "", NULL);
      94           8 :         test2();
      95             :         return 0;
      96             : }

Generated by: LCOV version 1.13