1 /* Test pstring library.
2 * By Richard W.M. Jones <rich@annexia.org>
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.
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.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: test_pstring.c,v 1.11 2003/02/09 16:48:46 rich Exp $
31 const int i_one = 1, i_two = 2, i_three = 3, i_four = 4,
32 i_15 = 15, i_31 = 31, i_63 = 63, i_127 = 127;
33 const double d_one = 1, d_two = 2, d_three = 3, d_four = 4;
34 const char *s_one = "one", *s_two = "two", *s_three = "three",
37 const char *stra[] = { "one", "two", "three", "four" };
39 const char *file1[] = { "line1\r\n",
42 const char *file2[] = { "line1\n",
45 const char *file3[] = { "line1\n",
49 const char *file4[] = { "line1\\\n",
52 const char *file5[] = { "line1\n",
56 const char *res1[] = { "line1", "line2", "line3", 0 };
57 const char *res2[] = { "line1line2", "line3", 0 };
60 my_strcmp (const char **p1, const char **p2)
62 return strcmp (*p1, *p2);
70 vector v1, v2, v3, v4, v5;
81 s1 = pstrdup (pool, "sheep");
82 s2 = pstrdup (pool, s1);
86 assert (strcmp (s2, "sheep") == 0);
89 s1 = pstrndup (pool, "sheep", 3);
90 s2 = pstrndup (pool, "sheep", 6);
92 assert (strcmp (s1, "she") == 0);
93 assert (strcmp (s2, "sheep") == 0);
96 s1 = pmemdup (pool, "waves", 6);
98 assert (strcmp (s1, "waves") == 0);
101 v1 = pstrsplit (pool, "one", "--");
102 v2 = pstrsplit (pool, "--one--two", "--");
103 v3 = pstrsplit (pool, "one--two--three", "--");
104 v4 = pstrsplit (pool, "one--two--three--four--", "--");
106 v5 = new_vector (pool, char *);
107 vector_push_back (v5, s_one);
108 assert (vector_compare (v1, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
109 vector_push_back (v5, s_two);
110 assert (vector_compare (v2, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
111 vector_push_back (v5, s_three);
112 assert (vector_compare (v3, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
113 vector_push_back (v5, s_four);
114 assert (vector_compare (v4, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
117 v1 = pstrcsplit (pool, "one", ',');
118 v2 = pstrcsplit (pool, ",one,two", ',');
119 v3 = pstrcsplit (pool, "one,two,three,", ',');
120 v4 = pstrcsplit (pool, "one,two,three,four", ',');
122 v5 = new_vector (pool, char *);
123 vector_push_back (v5, s_one);
124 assert (vector_compare (v1, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
125 vector_push_back (v5, s_two);
126 assert (vector_compare (v2, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
127 vector_push_back (v5, s_three);
128 assert (vector_compare (v3, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
129 vector_push_back (v5, s_four);
130 assert (vector_compare (v4, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
133 re = pcre_compile ("[ \t]+", 0, &errptr, &erroffset, 0);
135 v1 = pstrresplit (pool, "one", re);
136 v2 = pstrresplit (pool, " one \ttwo", re);
137 v3 = pstrresplit (pool, " one\ttwo\tthree ", re);
138 v4 = pstrresplit (pool, " one two \t three four ", re);
140 v5 = new_vector (pool, char *);
141 vector_push_back (v5, s_one);
142 assert (vector_compare (v1, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
143 vector_push_back (v5, s_two);
144 assert (vector_compare (v2, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
145 vector_push_back (v5, s_three);
146 assert (vector_compare (v3, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
147 vector_push_back (v5, s_four);
148 assert (vector_compare (v4, v5, (int (*)(const void *,const void *))my_strcmp) == 0);
152 v1 = new_vector (pool, char *);
153 vector_push_back (v1, s_one);
154 vector_push_back (v1, s_two);
155 vector_push_back (v1, s_three);
156 vector_push_back (v1, s_four);
157 s1 = pconcat (pool, v1);
158 assert (strcmp (s1, "onetwothreefour") == 0);
159 s1 = pjoin (pool, v1, "");
160 assert (strcmp (s1, "onetwothreefour") == 0);
161 s1 = pjoin (pool, v1, ",");
162 assert (strcmp (s1, "one,two,three,four") == 0);
163 s1 = pjoin (pool, v1, ", ");
164 assert (strcmp (s1, "one, two, three, four") == 0);
166 /* pstrresplit2 -> pconcat = original string */
167 re = pcre_compile ("[ \t]+", 0, &errptr, &erroffset, 0);
168 s1 = "the quick brown fox";
169 v1 = pstrresplit2 (pool, s1, re);
170 s2 = pconcat (pool, v1);
171 assert (strcmp (s1, s2) == 0);
173 s1 = " the quick \tbrown fox";
174 v1 = pstrresplit2 (pool, s1, re);
175 s2 = pconcat (pool, v1);
176 assert (strcmp (s1, s2) == 0);
178 s1 = "the quick brown \tfox\t";
179 v1 = pstrresplit2 (pool, s1, re);
180 s2 = pconcat (pool, v1);
181 assert (strcmp (s1, s2) == 0);
183 s1 = " \tthe quick brown \tfox\t";
184 v1 = pstrresplit2 (pool, s1, re);
185 s2 = pconcat (pool, v1);
186 assert (strcmp (s1, s2) == 0);
190 v1 = new_vector (pool, char *);
191 vector_push_back (v1, s_one);
192 vector_push_back (v1, s_two);
193 vector_push_back (v1, s_three);
194 vector_push_back (v1, s_four);
195 psort (v1, my_strcmp);
196 s1 = pconcat (pool, v1);
197 assert (strcmp (s1, "fouronethreetwo") == 0);
200 s1 = pstrdup (pool, "sheep");
202 assert (strcmp (s1, "sheep") == 0);
203 s1 = pstrdup (pool, "sheep\r");
205 assert (strcmp (s1, "sheep") == 0);
206 s1 = pstrdup (pool, "sheep\r\n");
208 assert (strcmp (s1, "sheep") == 0);
209 s1 = pstrdup (pool, "sheep\n");
211 assert (strcmp (s1, "sheep") == 0);
214 s1 = pchrs (pool, 'x', 20);
215 assert (strcmp (s1, "xxxxxxxxxxxxxxxxxxxx") == 0);
216 s1 = pchrs (pool, 'x', 0);
217 assert (strcmp (s1, "") == 0);
218 s1 = pstrs (pool, "x", 20);
219 assert (strcmp (s1, "xxxxxxxxxxxxxxxxxxxx") == 0);
220 s1 = pstrs (pool, "xyz", 10);
221 assert (strcmp (s1, "xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz") == 0);
222 s1 = pstrs (pool, "xyz", 0);
223 assert (strcmp (s1, "") == 0);
224 s1 = pstrs (pool, "", 100);
225 assert (strcmp (s1, "") == 0);
227 /* psprintf (also implicitly tests pvsprintf) */
228 s1 = psprintf (pool, "%d %s %s %s %s",
229 4, "one", "two", "three", "four");
230 assert (strcmp (s1, "4 one two three four") == 0);
231 for (i = 250; i < 270; ++i)
233 s1 = pchrs (pool, 'x', i);
234 s2 = psprintf (pool, "%s", s1);
235 assert (strcmp (s1, s2) == 0);
238 /* pvector, pvectora */
239 s1 = pjoin (pool, pvector (pool, "one", "two", "three", "four", 0), ",");
240 assert (strcmp (s1, "one,two,three,four") == 0);
241 s1 = pjoin (pool, pvectora (pool, stra, 4), ",");
242 assert (strcmp (s1, "one,two,three,four") == 0);
244 /* pitoa, pdtoa, pxtoa */
245 assert (strcmp (pitoa (pool, 1), "1") == 0);
246 assert (strcmp (pdtoa (pool, 2.1), "2.100000") == 0);
247 assert (strcmp (pxtoa (pool, 15), "f") == 0);
249 /* pvitostr, pvdtostr, pvxtostr */
250 v1 = new_vector (pool, int);
251 vector_push_back (v1, i_one);
252 vector_push_back (v1, i_two);
253 vector_push_back (v1, i_three);
254 vector_push_back (v1, i_four);
255 s1 = pjoin (pool, pvitostr (pool, v1), ",");
256 assert (strcmp (s1, "1,2,3,4") == 0);
258 v1 = new_vector (pool, double);
259 vector_push_back (v1, d_one);
260 vector_push_back (v1, d_two);
261 vector_push_back (v1, d_three);
262 vector_push_back (v1, d_four);
263 s1 = pjoin (pool, pvdtostr (pool, v1), ",");
264 assert (strcmp (s1, "1.000000,2.000000,3.000000,4.000000") == 0);
266 v1 = new_vector (pool, unsigned);
267 vector_push_back (v1, i_15);
268 vector_push_back (v1, i_31);
269 vector_push_back (v1, i_63);
270 vector_push_back (v1, i_127);
271 s1 = pjoin (pool, pvxtostr (pool, v1), ",");
272 assert (strcmp (s1, "f,1f,3f,7f") == 0);
275 s1 = pstrdup (pool, "one");
276 s1 = pstrcat (pool, s1, ",two");
277 assert (strcmp (s1, "one,two") == 0);
280 s1 = pstrdup (pool, "one");
281 s1 = pstrncat (pool, s1, ",two,three", 4);
282 assert (strcmp (s1, "one,two") == 0);
285 s1 = psubstr (pool, "sheep", 1, 2);
286 assert (strcmp (s1, "he") == 0);
287 s1 = psubstr (pool, "sheep", 1, -1);
288 assert (strcmp (s1, "heep") == 0);
290 /* pstrupr, pstrlwr */
291 s1 = pstrdup (pool, "sheep");
293 assert (strcmp (s1, "SHEEP") == 0);
295 assert (strcmp (s1, "sheep") == 0);
297 /* pgetline, pgetlinex, pgetlinec */
298 #define TEST_PGETL(getfn,input,expect) s1 = pstrdup (pool, ""); fp = tmpfile (); for (sp = input; *sp; ++sp) fputs (*sp, fp); rewind (fp); for (sp = expect; *sp; ++sp) { s1 = getfn (pool, fp, s1); assert (strcmp (s1, *sp) == 0); } assert (getfn (pool, fp, s1) == 0); fclose (fp);
300 TEST_PGETL (pgetline, file1, res1);
301 TEST_PGETL (pgetline, file2, res1);
302 TEST_PGETL (pgetlinec, file3, res1);
303 TEST_PGETL (pgetlinec, file4, res2);
304 TEST_PGETL (pgetlinec, file5, res1);
306 /* ptrim, ptrimfront, ptrimback */
307 s1 = pstrdup (pool, " sheep\t\t\t");
309 assert (strcmp (s1, "sheep") == 0);
310 s1 = pstrdup (pool, "sheep ");
312 assert (strcmp (s1, "sheep") == 0);
313 s1 = pstrdup (pool, " sheep");
315 assert (strcmp (s1, "sheep") == 0);
316 s1 = pstrdup (pool, "sheep");
318 assert (strcmp (s1, "sheep") == 0);
319 s1 = pstrdup (pool, "");
321 assert (strcmp (s1, "") == 0);
323 s1 = pstrdup (pool, " sheep");
325 assert (strcmp (s1, "sheep") == 0);
326 s1 = pstrdup (pool, "sheep ");
328 assert (strcmp (s1, "sheep ") == 0);
329 s1 = pstrdup (pool, "sheep");
331 assert (strcmp (s1, "sheep") == 0);
332 s1 = pstrdup (pool, "");
334 assert (strcmp (s1, "") == 0);
336 s1 = pstrdup (pool, " sheep");
338 assert (strcmp (s1, " sheep") == 0);
339 s1 = pstrdup (pool, "sheep ");
341 assert (strcmp (s1, "sheep") == 0);
342 s1 = pstrdup (pool, "sheep");
344 assert (strcmp (s1, "sheep") == 0);
345 s1 = pstrdup (pool, "");
347 assert (strcmp (s1, "") == 0);