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