1 /* test_splitstring.c -- unit test for splitstring()
2 Copyright (C) 2011 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /* config.h must always be included first. */
25 /* gnulib headers would go here. */
28 #include "splitstring.h"
31 assertEqualFunc(const char *file, int line, const char *label,
32 size_t expected, size_t got)
35 fprintf(stderr, "%s line %d: %s: expected %lu, got %lu\n",
36 file, line, label, (unsigned long)expected, (unsigned long)got);
38 #define ASSERT_EQUAL(expected,got) \
40 assertEqualFunc(__FILE__,__LINE__,"ASSERT_EQUAL",expected,got); \
41 assert (expected == got); \
50 const char *empty = "";
52 result = splitstring (empty, ":", true, &pos, &len);
54 ASSERT_EQUAL (0, pos);
55 ASSERT_EQUAL (0, len);
56 result = splitstring (empty, ":", false, &pos, &len);
60 static void test_onefield (void)
64 const char *input = "aaa";
66 result = splitstring (input, ":", true, &pos, &len);
68 ASSERT_EQUAL (0, pos);
69 ASSERT_EQUAL (3, len);
70 result = splitstring (input, ":", false, &pos, &len);
74 static void test_not_colon (void)
78 const char *separators = "!";
79 const char *input = "aa!b";
81 result = splitstring (input, separators, true, &pos, &len);
83 ASSERT_EQUAL (0, pos);
84 ASSERT_EQUAL (2, len);
86 result = splitstring (input, separators, false, &pos, &len);
88 ASSERT_EQUAL (3, pos);
89 ASSERT_EQUAL (1, len);
91 result = splitstring (input, separators, false, &pos, &len);
95 static void test_empty_back (void)
99 const char *input = "aa:";
101 result = splitstring (input, ":", true, &pos, &len);
103 ASSERT_EQUAL (0, pos);
104 ASSERT_EQUAL (2, len);
105 result = splitstring (input, ":", false, &pos, &len);
107 ASSERT_EQUAL (3, pos);
108 ASSERT_EQUAL (0, len);
109 result = splitstring (input, ":", false, &pos, &len);
113 static void test_empty_front (void)
117 const char *input = ":aaa";
119 result = splitstring (input, ":", true, &pos, &len);
121 ASSERT_EQUAL (0, pos);
122 ASSERT_EQUAL (0, len);
123 result = splitstring (input, ":", false, &pos, &len);
125 ASSERT_EQUAL (1, pos);
126 ASSERT_EQUAL (3, len);
127 result = splitstring (input, ":", false, &pos, &len);
131 static void test_twofields (void)
135 const char *input = "aaa:bb";
137 result = splitstring (input, ":", true, &pos, &len);
139 ASSERT_EQUAL (0, pos);
140 ASSERT_EQUAL (3, len);
141 result = splitstring (input, ":", false, &pos, &len);
143 ASSERT_EQUAL (4, pos);
144 ASSERT_EQUAL (2, len);
145 result = splitstring (input, ":", false, &pos, &len);
149 static void test_twoseparators (void)
153 const char *input = "a:bb!c";
155 result = splitstring (input, ":!", true, &pos, &len);
157 ASSERT_EQUAL (0, pos);
158 ASSERT_EQUAL (1, len);
159 result = splitstring (input, ":!", false, &pos, &len);
161 ASSERT_EQUAL (2, pos);
162 ASSERT_EQUAL (2, len);
163 result = splitstring (input, ":!", false, &pos, &len);
165 ASSERT_EQUAL (5, pos);
166 ASSERT_EQUAL (1, len);
167 result = splitstring (input, ":!", false, &pos, &len);
171 static void test_consecutive_empty (void)
175 const char *input = "a::b";
176 const char *separators = ":";
178 result = splitstring (input, separators, true, &pos, &len);
180 ASSERT_EQUAL (0, pos);
181 ASSERT_EQUAL (1, len);
183 result = splitstring (input, separators, false, &pos, &len);
185 ASSERT_EQUAL (2, pos);
186 ASSERT_EQUAL (0, len);
188 result = splitstring (input, separators, false, &pos, &len);
190 ASSERT_EQUAL (3, pos);
191 ASSERT_EQUAL (1, len);
193 result = splitstring (input, separators, false, &pos, &len);
197 int main (int argc, char *argv[])
205 test_twoseparators ();
206 test_consecutive_empty ();