Add to git.
[c2lib.git] / test_pstring.c
1 /* Test pstring library.
2  * By Richard W.M. Jones <rich@annexia.org>
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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: test_pstring.c,v 1.11 2003/02/09 16:48:46 rich Exp $
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include <pcre.h>
27
28 #include "pstring.h"
29 #include "vector.h"
30
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",
35   *s_four = "four";
36
37 const char *stra[] = { "one", "two", "three", "four" };
38
39 const char *file1[] = { "line1\r\n",
40                         "line2\r\n",
41                         "line3\r\n", 0};
42 const char *file2[] = { "line1\n",
43                         "line2\n",
44                         "line3\n", 0 };
45 const char *file3[] = { "line1\n",
46                         "line2\n",
47                         "\n",
48                         "line3\n", 0 };
49 const char *file4[] = { "line1\\\n",
50                         "line2\n",
51                         "line3\n", 0 };
52 const char *file5[] = { "line1\n",
53                         "# a comment\n",
54                         "line2\n",
55                         "line3\n", 0 };
56 const char *res1[] = { "line1", "line2", "line3", 0 };
57 const char *res2[] = { "line1line2", "line3", 0 };
58
59 static int
60 my_strcmp (const char **p1, const char **p2)
61 {
62   return strcmp (*p1, *p2);
63 }
64
65 int
66 main ()
67 {
68   pool pool;
69   char *s1, *s2;
70   vector v1, v2, v3, v4, v5;
71   pcre *re;
72   int i;
73   FILE *fp;
74   const char **sp;
75   const char *errptr;
76   int erroffset;
77
78   pool = new_pool ();
79
80   /* pstrdup */
81   s1 = pstrdup (pool, "sheep");
82   s2 = pstrdup (pool, s1);
83
84   s1[1] = 'l';
85
86   assert (strcmp (s2, "sheep") == 0);
87
88   /* pstrndup */
89   s1 = pstrndup (pool, "sheep", 3);
90   s2 = pstrndup (pool, "sheep", 6);
91
92   assert (strcmp (s1, "she") == 0);
93   assert (strcmp (s2, "sheep") == 0);
94
95   /* pmemdup */
96   s1 = pmemdup (pool, "waves", 6);
97
98   assert (strcmp (s1, "waves") == 0);
99
100   /* pstrsplit */
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--", "--");
105
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);
115
116   /* pstrcsplit */
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", ',');
121
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);
131
132   /* pstrresplit */
133   re = pcre_compile ("[ \t]+", 0, &errptr, &erroffset, 0);
134   assert (re);
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);
139
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);
149   free (re);
150
151   /* pconcat, pjoin */
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);
165
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);
172
173   s1 = " the   quick \tbrown fox";
174   v1 = pstrresplit2 (pool, s1, re);
175   s2 = pconcat (pool, v1);
176   assert (strcmp (s1, s2) == 0);
177
178   s1 = "the quick brown  \tfox\t";
179   v1 = pstrresplit2 (pool, s1, re);
180   s2 = pconcat (pool, v1);
181   assert (strcmp (s1, s2) == 0);
182
183   s1 = "  \tthe quick brown  \tfox\t";
184   v1 = pstrresplit2 (pool, s1, re);
185   s2 = pconcat (pool, v1);
186   assert (strcmp (s1, s2) == 0);
187   free (re);
188
189   /* psort */
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);
198
199   /* pchomp */
200   s1 = pstrdup (pool, "sheep");
201   pchomp (s1);
202   assert (strcmp (s1, "sheep") == 0);
203   s1 = pstrdup (pool, "sheep\r");
204   pchomp (s1);
205   assert (strcmp (s1, "sheep") == 0);
206   s1 = pstrdup (pool, "sheep\r\n");
207   pchomp (s1);
208   assert (strcmp (s1, "sheep") == 0);
209   s1 = pstrdup (pool, "sheep\n");
210   pchomp (s1);
211   assert (strcmp (s1, "sheep") == 0);
212
213   /* pchrs, pstrs */
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);
226
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)
232     {
233       s1 = pchrs (pool, 'x', i);
234       s2 = psprintf (pool, "%s", s1);
235       assert (strcmp (s1, s2) == 0);
236     }
237
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);
243
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);
248
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);
257
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);
265
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);
273
274   /* pstrcat */
275   s1 = pstrdup (pool, "one");
276   s1 = pstrcat (pool, s1, ",two");
277   assert (strcmp (s1, "one,two") == 0);
278
279   /* pstrncat */
280   s1 = pstrdup (pool, "one");
281   s1 = pstrncat (pool, s1, ",two,three", 4);
282   assert (strcmp (s1, "one,two") == 0);
283
284   /* psubstr */
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);
289
290   /* pstrupr, pstrlwr */
291   s1 = pstrdup (pool, "sheep");
292   pstrupr (s1);
293   assert (strcmp (s1, "SHEEP") == 0);
294   pstrlwr (s1);
295   assert (strcmp (s1, "sheep") == 0);
296
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);
299
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);
305
306   /* ptrim, ptrimfront, ptrimback */
307   s1 = pstrdup (pool, "          sheep\t\t\t");
308   ptrim (s1);
309   assert (strcmp (s1, "sheep") == 0);
310   s1 = pstrdup (pool, "sheep   ");
311   ptrim (s1);
312   assert (strcmp (s1, "sheep") == 0);
313   s1 = pstrdup (pool, "          sheep");
314   ptrim (s1);
315   assert (strcmp (s1, "sheep") == 0);
316   s1 = pstrdup (pool, "sheep");
317   ptrim (s1);
318   assert (strcmp (s1, "sheep") == 0);
319   s1 = pstrdup (pool, "");
320   ptrim (s1);
321   assert (strcmp (s1, "") == 0);
322
323   s1 = pstrdup (pool, "          sheep");
324   ptrimfront (s1);
325   assert (strcmp (s1, "sheep") == 0);
326   s1 = pstrdup (pool, "sheep  ");
327   ptrimfront (s1);
328   assert (strcmp (s1, "sheep  ") == 0);
329   s1 = pstrdup (pool, "sheep");
330   ptrimfront (s1);
331   assert (strcmp (s1, "sheep") == 0);
332   s1 = pstrdup (pool, "");
333   ptrimfront (s1);
334   assert (strcmp (s1, "") == 0);
335
336   s1 = pstrdup (pool, "          sheep");
337   ptrimback (s1);
338   assert (strcmp (s1, "          sheep") == 0);
339   s1 = pstrdup (pool, "sheep  ");
340   ptrimback (s1);
341   assert (strcmp (s1, "sheep") == 0);
342   s1 = pstrdup (pool, "sheep");
343   ptrimback (s1);
344   assert (strcmp (s1, "sheep") == 0);
345   s1 = pstrdup (pool, "");
346   ptrimback (s1);
347   assert (strcmp (s1, "") == 0);
348
349   delete_pool (pool);
350   exit (0);
351 }