1 /* test_splitstring.c -- unit test for splitstring()
2 Copyright (C) 2011-2022 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 /* config.h must always be included first. */
19 #include <config.h>
20
21 /* system headers. */
22 #include <stdio.h>
23 #include <assert.h>
24
25 /* gnulib headers would go here. */
26
27 /* find headers. */
28 #include "splitstring.h"
29
30 static void
31 assertEqualFunc(const char *file, int line, const char *label,
32 size_t expected, size_t got)
33 {
34 if (expected != got)
35 fprintf(stderr, "%s line %d: %s: expected %lu, got %lu\n",
36 file, line, label, (unsigned long)expected, (unsigned long)got);
37 }
38 #define ASSERT_EQUAL(expected,got) \
39 do{ \
40 assertEqualFunc(__FILE__,__LINE__,"ASSERT_EQUAL",expected,got); \
41 assert (expected == got); \
42 } while (0);
43
44
45 static void
46 test_empty (void)
47 {
48 size_t len, pos;
49 bool result;
50 const char *empty = "";
51
52 result = splitstring (empty, ":", true, &pos, &len);
53 assert (result);
54 ASSERT_EQUAL (0, pos);
55 ASSERT_EQUAL (0, len);
56 result = splitstring (empty, ":", false, &pos, &len);
57 assert (!result);
58 }
59
60 static void test_onefield (void)
61 {
62 size_t len, pos;
63 bool result;
64 const char *input = "aaa";
65
66 result = splitstring (input, ":", true, &pos, &len);
67 assert (result);
68 ASSERT_EQUAL (0, pos);
69 ASSERT_EQUAL (3, len);
70 result = splitstring (input, ":", false, &pos, &len);
71 assert (!result);
72 }
73
74 static void test_not_colon (void)
75 {
76 size_t len, pos;
77 bool result;
78 const char *separators = "!";
79 const char *input = "aa!b";
80
81 result = splitstring (input, separators, true, &pos, &len);
82 assert (result);
83 ASSERT_EQUAL (0, pos);
84 ASSERT_EQUAL (2, len);
85
86 result = splitstring (input, separators, false, &pos, &len);
87 assert (result);
88 ASSERT_EQUAL (3, pos);
89 ASSERT_EQUAL (1, len);
90
91 result = splitstring (input, separators, false, &pos, &len);
92 assert (!result);
93 }
94
95 static void test_empty_back (void)
96 {
97 size_t len, pos;
98 bool result;
99 const char *input = "aa:";
100
101 result = splitstring (input, ":", true, &pos, &len);
102 assert (result);
103 ASSERT_EQUAL (0, pos);
104 ASSERT_EQUAL (2, len);
105 result = splitstring (input, ":", false, &pos, &len);
106 assert (result);
107 ASSERT_EQUAL (3, pos);
108 ASSERT_EQUAL (0, len);
109 result = splitstring (input, ":", false, &pos, &len);
110 assert (!result);
111 }
112
113 static void test_empty_front (void)
114 {
115 size_t len, pos;
116 bool result;
117 const char *input = ":aaa";
118
119 result = splitstring (input, ":", true, &pos, &len);
120 assert (result);
121 ASSERT_EQUAL (0, pos);
122 ASSERT_EQUAL (0, len);
123 result = splitstring (input, ":", false, &pos, &len);
124 assert (result);
125 ASSERT_EQUAL (1, pos);
126 ASSERT_EQUAL (3, len);
127 result = splitstring (input, ":", false, &pos, &len);
128 assert (!result);
129 }
130
131 static void test_twofields (void)
132 {
133 size_t len, pos;
134 bool result;
135 const char *input = "aaa:bb";
136
137 result = splitstring (input, ":", true, &pos, &len);
138 assert (result);
139 ASSERT_EQUAL (0, pos);
140 ASSERT_EQUAL (3, len);
141 result = splitstring (input, ":", false, &pos, &len);
142 assert (result);
143 ASSERT_EQUAL (4, pos);
144 ASSERT_EQUAL (2, len);
145 result = splitstring (input, ":", false, &pos, &len);
146 assert (!result);
147 }
148
149 static void test_twoseparators (void)
150 {
151 size_t len, pos;
152 bool result;
153 const char *input = "a:bb!c";
154
155 result = splitstring (input, ":!", true, &pos, &len);
156 assert (result);
157 ASSERT_EQUAL (0, pos);
158 ASSERT_EQUAL (1, len);
159 result = splitstring (input, ":!", false, &pos, &len);
160 assert (result);
161 ASSERT_EQUAL (2, pos);
162 ASSERT_EQUAL (2, len);
163 result = splitstring (input, ":!", false, &pos, &len);
164 assert (result);
165 ASSERT_EQUAL (5, pos);
166 ASSERT_EQUAL (1, len);
167 result = splitstring (input, ":!", false, &pos, &len);
168 assert (!result);
169 }
170
171 static void test_consecutive_empty (void)
172 {
173 size_t len, pos;
174 bool result;
175 const char *input = "a::b";
176 const char *separators = ":";
177
178 result = splitstring (input, separators, true, &pos, &len);
179 assert (result);
180 ASSERT_EQUAL (0, pos);
181 ASSERT_EQUAL (1, len);
182
183 result = splitstring (input, separators, false, &pos, &len);
184 assert (result);
185 ASSERT_EQUAL (2, pos);
186 ASSERT_EQUAL (0, len);
187
188 result = splitstring (input, separators, false, &pos, &len);
189 assert (result);
190 ASSERT_EQUAL (3, pos);
191 ASSERT_EQUAL (1, len);
192
193 result = splitstring (input, separators, false, &pos, &len);
194 assert (!result);
195 }
196
197 int main (int argc, char *argv[])
198 {
199 (void) argc; /* pacify -Werror=unused-parameter */
200 (void) argv; /* pacify -Werror=unused-parameter */
201
202 test_empty ();
203 test_onefield ();
204 test_not_colon ();
205 test_empty_back ();
206 test_empty_front ();
207 test_twofields ();
208 test_twoseparators ();
209 test_consecutive_empty ();
210 return 0;
211 }