* fix StrBufIsNumber; its not used right now, but it should be sane ;-)
[citadel.git] / libcitadel / tests / stringbuf_test.c
1
2 /*
3  *  CUnit - A Unit testing framework library for C.
4  *  Copyright (C) 2001  Anil Kumar
5  *  
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #define SHOW_ME_VAPPEND_PRINTF
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26
27 #include "stringbuf_test.h"
28 #include "../lib/libcitadel.h"
29
30
31 /*
32  * Stolen from wc_printf; we need to test that other printf too... 
33  */
34 static void TEST_StrBufAppendPrintf(StrBuf *WBuf, const char *format,...)
35 {
36         va_list arg_ptr;
37
38         if (WBuf == NULL)
39                 return;
40
41         va_start(arg_ptr, format);
42         StrBufVAppendPrintf(WBuf, format, arg_ptr);
43         va_end(arg_ptr);
44 }
45
46 static void TestRevalidateStrBuf(StrBuf *Buf)
47 {
48         CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
49 }
50
51
52 static void TestCreateBuf(void)
53 {
54         StrBuf *Buf;
55         StrBuf *Buf2;
56         StrBuf *Buf3;
57         long len;
58         long i;
59         char *ch;
60
61         Buf = NewStrBuf();
62         CU_ASSERT(Buf != NULL);
63         FreeStrBuf(&Buf);
64
65         Buf = NewStrBufPlain(ChrPtr(NULL), StrLength(NULL));
66         CU_ASSERT(Buf != NULL);
67         FreeStrBuf(&Buf);
68
69
70         Buf = NewStrBufDup(NULL);
71         CU_ASSERT(Buf != NULL);
72         StrBufPlain(Buf, "abc", -1);
73         TestRevalidateStrBuf(Buf);
74         StrBufPlain(Buf, HKEY("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
75         TestRevalidateStrBuf(Buf);
76         FreeStrBuf(&Buf);
77
78         FlushStrBuf(NULL);
79         FLUSHStrBuf(NULL);
80
81         CU_ASSERT(Buf == NULL);
82         Buf = NewStrBufPlain(HKEY("ABC"));
83         TestRevalidateStrBuf(Buf);
84         CU_ASSERT(StrLength(Buf) == 3);
85         CU_ASSERT_NSTRING_EQUAL("ABC", ChrPtr(Buf), 3);
86
87         len = StrLength(Buf);
88         for (i=0; i< 500; i ++)
89         {
90                 StrBufAppendBufPlain(Buf, HKEY("ABC"), 0);
91                 len += 3;
92                 CU_ASSERT(StrLength(Buf) == len);
93         }       
94         StrBufShrinkToFit(Buf, 1);
95         FlushStrBuf(Buf);
96         CU_ASSERT(StrLength(Buf) == 0);
97         ReAdjustEmptyBuf(Buf, 1, 1);
98         TestRevalidateStrBuf(Buf);
99         FreeStrBuf(&Buf);
100         CU_ASSERT(Buf == NULL);
101         
102         Buf = NewStrBufPlain(HKEY("ABC"));
103         TestRevalidateStrBuf(Buf);
104         len = StrLength(Buf);
105         for (i=0; i< 500; i ++)
106         {
107                 StrBufAppendPrintf(Buf, "%s", "ABC");
108                 len += 3;
109                 CU_ASSERT(StrLength(Buf) == len);               
110         }
111         TestRevalidateStrBuf(Buf);
112         StrBufShrinkToFit(Buf, 1);
113         TestRevalidateStrBuf(Buf);
114         FreeStrBuf(&Buf);
115
116         Buf = NewStrBufPlain(HKEY("ABC"));
117         TestRevalidateStrBuf(Buf);
118         len = StrLength(Buf);
119         for (i=0; i< 500; i ++)
120         {
121                 TEST_StrBufAppendPrintf(Buf, "%s", "ABC");
122                 len += 3;
123                 CU_ASSERT(StrLength(Buf) == len);               
124         }
125         TestRevalidateStrBuf(Buf);
126         StrBufShrinkToFit(Buf, 1);
127         TestRevalidateStrBuf(Buf);
128
129
130         Buf2 = NewStrBufDup(Buf);
131         CU_ASSERT(StrLength(Buf) == StrLength(Buf2));           
132         
133         CU_ASSERT_NSTRING_EQUAL(ChrPtr(Buf2), ChrPtr(Buf), StrLength(Buf2));
134         
135         CU_ASSERT(StrBufIsNumber(Buf) == 0);
136
137         FlushStrBuf(Buf2);
138         CU_ASSERT(StrLength(Buf2) == 0);
139
140         FLUSHStrBuf(Buf);
141         CU_ASSERT(StrLength(Buf) == 0);
142
143         HFreeStrBuf(NULL);
144         HFreeStrBuf(Buf2);
145         CU_ASSERT(Buf2 != NULL);
146
147         FreeStrBuf(&Buf);
148         CU_ASSERT(Buf == NULL);
149         
150         Buf2 = NewStrBuf();
151         Buf3 = NewStrBufPlain(HKEY("abcd"));
152         Buf = NewStrBufPlain(HKEY("123456"));
153         CU_ASSERT(StrBufIsNumber(Buf) == 1);
154         CU_ASSERT(StrBufIsNumber(NULL) == 0);
155         CU_ASSERT(StrBufIsNumber(Buf2) == 0);
156         CU_ASSERT(StrBufIsNumber(Buf3) == 0);
157
158         CU_ASSERT(StrTol(Buf) == 123456);
159         CU_ASSERT(StrTol(NULL) == 0);
160         CU_ASSERT(StrTol(Buf2) == 0);
161
162         CU_ASSERT(StrToi(Buf) == 123456);
163         CU_ASSERT(StrToi(NULL) == 0);
164         CU_ASSERT(StrToi(Buf2) == 0);
165         ch = SmashStrBuf(NULL);
166         CU_ASSERT(ch == NULL);
167         i = StrLength(Buf);
168         ch = SmashStrBuf(&Buf);
169         CU_ASSERT(strlen(ch) == i);
170         free(ch);
171         FreeStrBuf(&Buf2);
172 }
173
174 static void NextTokenizerIterateBuf(StrBuf *Buf, int NTokens)
175 {
176         const char *pCh = NULL;
177         StrBuf *Buf2;
178         long CountTokens = 0;
179         long HaveNextToken = 0;
180         long HaveNextTokenF = 0;
181
182         printf("\n\nTemplate: >%s<\n", ChrPtr(Buf));
183         TestRevalidateStrBuf(Buf);
184                              
185         Buf2 = NewStrBuf();
186         while (HaveNextToken = StrBufHaveNextToken(Buf, &pCh),
187                HaveNextTokenF = StrBufExtract_NextToken(Buf2, Buf, &pCh, ','),
188                (HaveNextTokenF>= 0))
189         {
190                 CountTokens++;
191                 
192                 printf("Token: >%s< >%s< %ld:%ld\n", 
193                        ChrPtr(Buf2), 
194                        ((pCh != NULL) && (pCh != StrBufNOTNULL))? pCh : "N/A", 
195                        HaveNextToken, 
196                        HaveNextTokenF);
197                 TestRevalidateStrBuf(Buf2);
198
199                 CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0));
200                 
201                 CU_ASSERT(CountTokens <= NTokens);
202         } 
203         CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0));
204         FreeStrBuf(&Buf2);
205 }
206
207
208
209 static void TestNextTokenizer_EndWithEmpty(void)
210 {
211         StrBuf *Buf;
212
213         Buf = NewStrBufPlain(HKEY("abc,abc, 1, ,,"));
214         NextTokenizerIterateBuf(Buf, 7);
215         FreeStrBuf(&Buf);
216 }
217
218 static void TestNextTokenizer_StartWithEmpty(void)
219 {
220         StrBuf *Buf;
221
222         Buf = NewStrBufPlain(HKEY(",cde,abc, 1, ,,bbb"));
223         NextTokenizerIterateBuf(Buf, 8);
224         FreeStrBuf(&Buf);
225 }
226
227 static void TestNextTokenizer_Empty(void)
228 {
229         StrBuf *Buf;
230
231         Buf = NewStrBufPlain(HKEY(""));
232         NextTokenizerIterateBuf(Buf, 8);
233         FreeStrBuf(&Buf);
234 }
235
236 static void TestNextTokenizer_TwoEmpty(void)
237 {
238         StrBuf *Buf;
239
240         Buf = NewStrBufPlain(HKEY(","));
241         NextTokenizerIterateBuf(Buf, 8);
242         FreeStrBuf(&Buf);
243 }
244
245 static void TestNextTokenizer_One(void)
246 {
247         StrBuf *Buf;
248
249         Buf = NewStrBufPlain(HKEY("one"));
250         NextTokenizerIterateBuf(Buf, 8);
251         FreeStrBuf(&Buf);
252 }
253
254 static void TestNextTokenizer_Sequence(void)
255 {
256         StrBuf *Buf;
257         char *teststring = "40:24524,24662,24673,27869:27935,28393,28426,31247:31258,31731,31749,31761,31778,31782,31801:31803,31813,31904,31915,33708,33935,34619,34672,34720:34723,34766,34835,37594,38854,39235,39942,40030,40142,40520,40815,40907,41201,41578,41781,41954,42292,43110,43565,43801,43998,44180,44241,44295,44401,44561,44635,44798,44861,44946,45022,45137:45148,45166,45179,45707,47114,47141:47157,47194,47314,47349,47386,47489,47496,47534:47543,54460,54601,54637:54652";
258         Buf = NewStrBufPlain(teststring, -1);
259         NextTokenizerIterateBuf(Buf, 67);
260         FreeStrBuf(&Buf);
261 }
262
263
264
265 static void NextLineterateBuf(StrBuf *Buf, int NLines)
266 {
267         int n = 0;
268         const char *pCh = NULL;
269         StrBuf *OneLine;
270         StrBuf *ConcatenatedLines;
271         long CountTokens = 0;
272         
273         TestRevalidateStrBuf(Buf);
274                              
275         OneLine = NewStrBuf();
276         ConcatenatedLines = NewStrBuf();
277
278         printf("\n");
279
280         if (StrLength(Buf) > 0) 
281                 do 
282                 {
283                         n = StrBufSipLine(OneLine, Buf, &pCh);
284                         
285                         CountTokens++;
286                         
287                         printf("Line: >%s< >%s<\n", 
288                                ChrPtr(OneLine), 
289                                ((pCh != NULL) && (pCh != StrBufNOTNULL))? pCh : "N/A");
290                         TestRevalidateStrBuf(OneLine);
291                         CU_ASSERT(CountTokens <= NLines);
292                         StrBufAppendBuf(ConcatenatedLines, OneLine, 0);
293                         
294                         if ((pCh == StrBufNOTNULL) && 
295                             (*(ChrPtr(Buf) + StrLength(Buf) - 1) != '\n'))
296                         {
297                         }
298                         else 
299                                 StrBufAppendBufPlain(ConcatenatedLines, HKEY("\n"), 0);
300                         
301                 } 
302                 while ((pCh != StrBufNOTNULL) &&
303                        (pCh != NULL));
304         
305
306         printf("\n\nTemplate: >%s<\n", ChrPtr(Buf));
307         printf("\n\nAfter: >%s<\n", ChrPtr(ConcatenatedLines));
308         CU_ASSERT_NSTRING_EQUAL(ChrPtr(ConcatenatedLines), 
309                                 ChrPtr(Buf), 
310                                 StrLength(Buf));
311
312         FreeStrBuf(&OneLine);
313         FreeStrBuf(&ConcatenatedLines);
314 }
315
316
317 static void TestNextLine_Empty(void)
318 {
319         StrBuf *Buf;
320
321         Buf = NewStrBufPlain(HKEY(""));
322         NextLineterateBuf(Buf, 0);
323         FreeStrBuf(&Buf);
324 }
325
326
327 static void TestNextLine_OneLine(void)
328 {
329         StrBuf *Buf;
330
331         Buf = NewStrBufPlain(HKEY("abc\n"));
332         NextLineterateBuf(Buf, 1);
333         FreeStrBuf(&Buf);
334 }
335
336
337 static void TestNextLine_TwoLinesMissingCR(void)
338 {
339         StrBuf *Buf;
340
341         Buf = NewStrBufPlain(HKEY("abc\ncde"));
342         NextLineterateBuf(Buf, 2);
343         FreeStrBuf(&Buf);
344 }
345
346
347 static void TestNextLine_twolines(void)
348 {
349         StrBuf *Buf;
350
351         Buf = NewStrBufPlain(HKEY("abc\ncde\n"));
352         NextLineterateBuf(Buf, 2);
353         FreeStrBuf(&Buf);
354 }
355
356 static void TestNextLine_LongLine(void)
357 {
358         StrBuf *Buf;
359
360         Buf = NewStrBufPlain(HKEY("abcde\n1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"));
361         NextLineterateBuf(Buf, 2);
362         FreeStrBuf(&Buf);
363 }
364
365
366 static void TestStrBufRemove_token_NotThere(void)
367 {
368 //      StrBuf *Test = NewStrBufPlain(HKEY(" 127.0.0.1"));
369         StrBuf *Test = NewStrBufPlain(HKEY(" 10.122.44.30, 10.122.44.30"));
370         StrBufRemove_token(Test, 0, ',');
371         TestRevalidateStrBuf(Test);
372         FreeStrBuf(&Test);
373 }
374
375
376 static void TestStrBufUrlescAppend(void)
377 {
378         const char *expect = "%20%2B%23%26%3B%60%27%7C%2A%3F%2D%7E%3C%3E%5E%28%29%5B%5D%7B%7D%2F%24%22%5C";
379         StrBuf *In = NewStrBufPlain(HKEY( " +#&;`'|*?-~<>^()[]{}/$\"\\"));
380         StrBuf *Out = NewStrBuf();
381
382         StrBufUrlescAppend (Out, In, NULL);
383         printf ("%s<\n%s<\n%s\n", ChrPtr(In), ChrPtr(Out), expect);
384         CU_ASSERT_STRING_EQUAL(ChrPtr(Out), expect);
385         FreeStrBuf(&In);
386         FreeStrBuf(&Out);
387 }
388
389 /*
390 Some samples from the original...
391         CU_ASSERT_EQUAL(10, 10);
392         CU_ASSERT_EQUAL(0, -0);
393         CU_ASSERT_EQUAL(-12, -12);
394         CU_ASSERT_NOT_EQUAL(10, 11);
395         CU_ASSERT_NOT_EQUAL(0, -1);
396         CU_ASSERT_NOT_EQUAL(-12, -11);
397         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
398         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
399         CU_ASSERT_PTR_NULL(NULL);
400         CU_ASSERT_PTR_NULL(0x0);
401         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
402         CU_ASSERT_STRING_EQUAL(str1, str2);
403         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
404         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
405         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
406         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
407         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
408         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
409         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
410         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
411         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
412         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
413         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
414         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
415         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
416         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
417 */
418
419
420
421
422
423 static void AddStrBufSimlpeTests(void)
424 {
425         CU_pSuite pGroup = NULL;
426         CU_pTest pTest = NULL;
427
428         pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
429         pTest = CU_add_test(pGroup, "testCreateBuf", TestCreateBuf);
430
431         pGroup = CU_add_suite("TestStringTokenizer", NULL, NULL);
432         pTest = CU_add_test(pGroup, "testNextTokenizer_EndWithEmpty", TestNextTokenizer_EndWithEmpty);
433         pTest = CU_add_test(pGroup, "testNextTokenizer_StartWithEmpty", TestNextTokenizer_StartWithEmpty);
434         pTest = CU_add_test(pGroup, "testNextTokenizer_StartWithEmpty", TestNextTokenizer_StartWithEmpty);
435         pTest = CU_add_test(pGroup, "testNextTokenizer_Empty", TestNextTokenizer_Empty);
436         pTest = CU_add_test(pGroup, "testNextTokenizer_TwoEmpty", TestNextTokenizer_TwoEmpty);
437         pTest = CU_add_test(pGroup, "testNextTokenizer_One", TestNextTokenizer_One);
438         pTest = CU_add_test(pGroup, "testNextTokenizer_Sequence", TestNextTokenizer_Sequence);
439
440
441         pGroup = CU_add_suite("TestStrBufSipLine", NULL, NULL);
442         pTest = CU_add_test(pGroup, "TestNextLine_Empty", TestNextLine_Empty);
443         pTest = CU_add_test(pGroup, "TestNextLine_OneLine", TestNextLine_OneLine);
444         pTest = CU_add_test(pGroup, "TestNextLine_TwoLinesMissingCR", TestNextLine_TwoLinesMissingCR);
445         pTest = CU_add_test(pGroup, "TestNextLine_twolines", TestNextLine_twolines);
446         pTest = CU_add_test(pGroup, "TestNextLine_LongLine", TestNextLine_LongLine);
447         
448         pGroup = CU_add_suite("TestStrBufRemove_token", NULL, NULL);
449         pTest = CU_add_test(pGroup, "TestStrBufRemove_token_NotThere", TestStrBufRemove_token_NotThere);
450
451         pGroup = CU_add_suite("TestStrBuf_escapers", NULL, NULL);
452         pTest = CU_add_test(pGroup, "TestStrBufUrlescAppend", TestStrBufUrlescAppend);
453 }
454
455
456 int main(int argc, char* argv[])
457 {
458         setvbuf(stdout, NULL, _IONBF, 0);
459
460         StartLibCitadel(8);
461         CU_BOOL Run = CU_FALSE ;
462         
463         CU_set_output_filename("TestAutomated");
464         if (CU_initialize_registry()) {
465                 printf("\nInitialize of test Registry failed.");
466         }
467         
468         Run = CU_TRUE ;
469         AddStrBufSimlpeTests();
470         
471         if (CU_TRUE == Run) {
472                 //CU_console_run_tests();
473     printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
474     
475     ///CU_automated_run_tests();
476         }
477         
478         CU_cleanup_registry();
479
480         return 0;
481 }