* fix Next-tokenizer
[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
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "stringbuf_test.h"
26 #include "../lib/libcitadel.h"
27
28 static int success_init(void) { return 0; }
29 static int success_clean(void) { return 0; }
30
31 static void testSuccess1(void) { CU_ASSERT(1); }
32 static void testSuccess2(void) { CU_ASSERT(1); }
33 static void testSuccess3(void) { CU_ASSERT(1); }
34
35 static int group_failure_init(void) { return 1;}
36 static int group_failure_clean(void) { return 1; }
37
38 static void testGroupFailure1(void) { CU_ASSERT(0); }
39 static void testGroupFailure2(void) { CU_ASSERT(2); }
40
41 static void testfailure1(void) { CU_ASSERT(12 <= 10); }
42 static void testfailure2(void) { CU_ASSERT(2); }
43 static void testfailure3(void) { CU_ASSERT(3); }
44 /*
45 static void test1(void)
46 {
47         CU_ASSERT((char *)2 != "THis is positive test.");
48         CU_ASSERT((char *)2 == "THis is negative test. test 1");
49 }
50
51 static void test2(void)
52 {
53         CU_ASSERT((char *)2 != "THis is positive test.");
54         CU_ASSERT((char *)3 == "THis is negative test. test 2");
55 }
56 */
57 static void TestCreateBuf(void)
58 {
59         StrBuf *Buf;
60         StrBuf *Buf2;
61         long len;
62         long i;
63
64         Buf = NewStrBuf();
65         CU_ASSERT(Buf != NULL);
66         FreeStrBuf(&Buf);
67
68         CU_ASSERT(Buf == NULL);
69         Buf = NewStrBufPlain(HKEY("ABC"));
70         CU_ASSERT(StrLength(Buf) == 3);
71         CU_ASSERT_NSTRING_EQUAL("ABC", ChrPtr(Buf), 3);
72
73         len = StrLength(Buf);
74         for (i=0; i< 500; i ++)
75         {
76                 StrBufAppendBufPlain(Buf, HKEY("ABC"), 0);
77                 len += 3;
78                 CU_ASSERT(StrLength(Buf) == len);               
79         }       
80         StrBufShrinkToFit(Buf, 1);
81         FreeStrBuf(&Buf);
82         CU_ASSERT(Buf == NULL);
83         
84         Buf = NewStrBufPlain(HKEY("ABC"));
85         len = StrLength(Buf);
86         for (i=0; i< 500; i ++)
87         {
88                 StrBufAppendPrintf(Buf, "%s", "ABC");
89                 len += 3;
90                 CU_ASSERT(StrLength(Buf) == len);               
91         }
92         StrBufShrinkToFit(Buf, 1);
93
94         Buf2 = NewStrBufDup(Buf);
95         CU_ASSERT(StrLength(Buf) == StrLength(Buf2));           
96         
97         CU_ASSERT_NSTRING_EQUAL(ChrPtr(Buf2), ChrPtr(Buf), StrLength(Buf2));
98         
99         CU_ASSERT(StrBufIsNumber(Buf) == 0);
100
101         FlushStrBuf(Buf2);
102         CU_ASSERT(StrLength(Buf2) == 0);
103
104         FLUSHStrBuf(Buf);
105         CU_ASSERT(StrLength(Buf) == 0);
106
107         FreeStrBuf(&Buf);
108         FreeStrBuf(&Buf2);
109         CU_ASSERT(Buf == NULL);
110         CU_ASSERT(Buf2 == NULL);
111
112
113         Buf = NewStrBufPlain(HKEY("123456"));
114 ///     CU_ASSERT(StrBufIsNumber(Buf) == 1); Todo: this is buggy.
115         FreeStrBuf(&Buf);
116         
117 }
118
119 static void NextTokenizerIterateBuf(StrBuf *Buf, int NTokens)
120 {
121         const char *pCh = NULL;
122         StrBuf *Buf2;
123         long CountTokens = 0;
124         long HaveNextToken;
125         long HaveNextTokenF;
126
127         printf("\n\nTemplate: >%s<\n", ChrPtr(Buf));
128                              
129         Buf2 = NewStrBuf();
130         while (HaveNextToken = StrBufHaveNextToken(Buf, &pCh),
131                HaveNextTokenF = StrBufExtract_NextToken(Buf2, Buf, &pCh, ','),
132                (HaveNextTokenF>= 0))
133         {
134                 CountTokens++;
135                 
136                 printf("Token: >%s< >%s< %ld:%ld\n", ChrPtr(Buf2), pCh, HaveNextToken, HaveNextTokenF);
137                 CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0));
138                 
139                 CU_ASSERT(CountTokens <= NTokens);
140         } 
141         CU_ASSERT(HaveNextToken == (HaveNextTokenF >= 0));
142 }
143
144 static void TestNextTokenizer1(void)
145 {
146         StrBuf *Buf;
147
148         Buf = NewStrBufPlain(HKEY("abc,abc, 1, ,,"));
149         NextTokenizerIterateBuf(Buf, 7);
150         FreeStrBuf(&Buf);
151 }
152
153 static void TestNextTokenizer2(void)
154 {
155         StrBuf *Buf;
156
157         Buf = NewStrBufPlain(HKEY(",cde,abc, 1, ,,bbb"));
158         NextTokenizerIterateBuf(Buf, 8);
159         FreeStrBuf(&Buf);
160 }
161
162
163
164 static void testSuccessAssertTrue(void)
165 {
166         CU_ASSERT_TRUE(CU_TRUE);
167         CU_ASSERT_TRUE(!CU_FALSE);
168 }
169
170 static void testSuccessAssertFalse(void)
171 {
172         CU_ASSERT_FALSE(CU_FALSE);
173         CU_ASSERT_FALSE(!CU_TRUE);
174 }
175
176 static void testSuccessAssertEqual(void)
177 {
178         CU_ASSERT_EQUAL(10, 10);
179         CU_ASSERT_EQUAL(0, 0);
180         CU_ASSERT_EQUAL(0, -0);
181         CU_ASSERT_EQUAL(-12, -12);
182 }
183
184 static void testSuccessAssertNotEqual(void)
185 {
186         CU_ASSERT_NOT_EQUAL(10, 11);
187         CU_ASSERT_NOT_EQUAL(0, -1);
188         CU_ASSERT_NOT_EQUAL(-12, -11);
189 }
190
191 static void testSuccessAssertPtrEqual(void)
192 {
193         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
194 }
195
196 static void testSuccessAssertPtrNotEqual(void)
197 {
198         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
199 }
200
201 static void testSuccessAssertPtrNull(void)
202 {
203         CU_ASSERT_PTR_NULL(NULL);
204         CU_ASSERT_PTR_NULL(0x0);
205 }
206
207 static void testSuccessAssertPtrNotNull(void)
208 {
209         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
210 }
211
212 static void testSuccessAssertStringEqual(void)
213 {
214         char str1[] = "test" ;
215         char str2[] = "test" ;
216
217         CU_ASSERT_STRING_EQUAL(str1, str2);
218 }
219
220 static void testSuccessAssertStringNotEqual(void)
221 {
222         char str1[] = "test" ;
223         char str2[] = "testtsg" ;
224
225         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
226 }
227
228 static void testSuccessAssertNStringEqual(void)
229 {
230         char str1[] = "test" ;
231         char str2[] = "testgfsg" ;
232
233         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
234         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
235         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
236 }
237
238 static void testSuccessAssertNStringNotEqual(void)
239 {
240         char str1[] = "test" ;
241         char str2[] = "teet" ;
242         char str3[] = "testgfsg" ;
243
244         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
245         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
246 }
247
248 static void testSuccessAssertDoubleEqual(void)
249 {
250         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
251         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
252         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
253         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
254 }
255
256 static void testSuccessAssertDoubleNotEqual(void)
257 {
258         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
259         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
260         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
261         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
262 }
263
264 static void AddTests(void)
265 {
266         CU_pSuite pGroup = NULL;
267         CU_pTest pTest = NULL;
268
269         pGroup = CU_add_suite("Sucess", success_init, success_clean);
270         pTest = CU_add_test(pGroup, "testSuccess1", testSuccess1);
271         pTest = CU_add_test(pGroup, "testSuccess2", testSuccess2);
272         pTest = CU_add_test(pGroup, "testSuccess3", testSuccess3);
273         
274         pGroup = CU_add_suite("failure", NULL, NULL);
275         pTest = CU_add_test(pGroup, "testfailure1", testfailure1);
276         pTest = CU_add_test(pGroup, "testfailure2", testfailure2);
277         pTest = CU_add_test(pGroup, "testfailure3", testfailure3);
278
279         pGroup = CU_add_suite("group_failure", group_failure_init, group_failure_clean);
280         pTest = CU_add_test(pGroup, "testGroupFailure1", testGroupFailure1);
281         pTest = CU_add_test(pGroup, "testGroupFailure2", testGroupFailure2);
282 }
283
284 static void AddStrBufSimlpeTests(void)
285 {
286         CU_pSuite pGroup = NULL;
287         CU_pTest pTest = NULL;
288
289         pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
290         pTest = CU_add_test(pGroup, "testCreateBuf", TestCreateBuf);
291
292         pGroup = CU_add_suite("TestStringTokenizer", NULL, NULL);
293         pTest = CU_add_test(pGroup, "testNextTokenizer_1", TestNextTokenizer1);
294         pTest = CU_add_test(pGroup, "testNextTokenizer_2", TestNextTokenizer2);
295
296
297 /*
298         pGroup = CU_add_suite("TestBooleanAssert", NULL, NULL);
299         pTest = CU_add_test(pGroup, "testSuccessAssertTrue", testSuccessAssertTrue);
300         pTest = CU_add_test(pGroup, "testSuccessAssertFalse", testSuccessAssertFalse);
301
302         pGroup = CU_add_suite("TestEqualityAssert", NULL, NULL);
303         pTest = CU_add_test(pGroup, "testSuccessAssertEqual", testSuccessAssertEqual);
304         pTest = CU_add_test(pGroup, "testSuccessAssertNotEqual", testSuccessAssertNotEqual);
305
306         pGroup = CU_add_suite("TestPointerAssert", NULL, NULL);
307         pTest = CU_add_test(pGroup, "testSuccessAssertPtrEqual", testSuccessAssertPtrEqual);
308         pTest = CU_add_test(pGroup, "testSuccessAssertPtrNotEqual", testSuccessAssertPtrNotEqual);
309
310         pGroup = CU_add_suite("TestNullnessAssert", NULL, NULL);
311         pTest = CU_add_test(pGroup, "testSuccessAssertPtrNull", testSuccessAssertPtrNull);
312         pTest = CU_add_test(pGroup, "testSuccessAssertPtrNotNull", testSuccessAssertPtrNotNull);
313
314         pGroup = CU_add_suite("TestStringAssert", NULL, NULL);
315         pTest = CU_add_test(pGroup, "testSuccessAssertStringEqual", testSuccessAssertStringEqual);
316         pTest = CU_add_test(pGroup, "testSuccessAssertStringNotEqual", testSuccessAssertStringNotEqual);
317
318         pGroup = CU_add_suite("TestNStringAssert", NULL, NULL);
319         pTest = CU_add_test(pGroup, "testSuccessAssertNStringEqual", testSuccessAssertNStringEqual);
320         pTest = CU_add_test(pGroup, "testSuccessAssertNStringNotEqual", testSuccessAssertNStringNotEqual);
321
322         pGroup = CU_add_suite("TestDoubleAssert", NULL, NULL);
323         pTest = CU_add_test(pGroup, "testSuccessAssertDoubleEqual", testSuccessAssertDoubleEqual);
324         pTest = CU_add_test(pGroup, "testSuccessAssertDoubleNotEqual", testSuccessAssertDoubleNotEqual);
325 */
326 }
327
328
329 int main(int argc, char* argv[])
330 {
331         setvbuf(stdout, NULL, _IONBF, 0);
332
333         StartLibCitadel(8);
334         if (argc > 1) {
335                 CU_BOOL Run = CU_FALSE ;
336
337                 CU_set_output_filename("TestAutomated");
338                 if (CU_initialize_registry()) {
339                         printf("\nInitialize of test Registry failed.");
340                 }
341
342                 if (!strcmp("--test", argv[1])) {
343                         Run = CU_TRUE ;
344                         AddTests();
345                 }
346                 else if (!strcmp("--atest", argv[1])) {
347                         Run = CU_TRUE ;
348                         AddStrBufSimlpeTests();
349                 }
350                 else if (!strcmp("--alltest", argv[1])) {
351                         Run = CU_TRUE ;
352                         AddTests();
353 //                      AddAssertTests();
354                 }
355                 
356                 if (CU_TRUE == Run) {
357                         //CU_console_run_tests();
358     printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
359
360                         ///CU_automated_run_tests();
361                 }
362
363                 CU_cleanup_registry();
364         }
365
366         return 0;
367 }