* fix it some, but not all.
[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);
115
116         
117 }
118
119
120 static void TestNextTokenizer(void)
121 {
122         const char *pCh = NULL;
123         StrBuf *Buf;
124         StrBuf *Buf2;
125         long CountTokens = 0;
126         long HaveNextToken;
127         long HaveNextTokenF;
128
129         Buf = NewStrBufPlain(HKEY("abc,abc, 1, ,,"));
130         printf("\nTemplate: >%s<\n", ChrPtr(Buf));
131                              
132         Buf2 = NewStrBuf();
133         do
134         {
135                 HaveNextTokenF = StrBufExtract_NextToken(Buf2, Buf, &pCh, ',');
136                 printf("Token: >%s< >%s<\n", ChrPtr(Buf2), pCh);
137                 CountTokens++;
138                 HaveNextToken = StrBufHaveNextToken(Buf2, &pCh);
139                 CU_ASSERT(HaveNextToken == 1);
140                 
141                 CU_ASSERT(CountTokens < 7);
142         } 
143         while (HaveNextTokenF);
144 }
145
146
147
148 static void testSuccessAssertTrue(void)
149 {
150         CU_ASSERT_TRUE(CU_TRUE);
151         CU_ASSERT_TRUE(!CU_FALSE);
152 }
153
154 static void testSuccessAssertFalse(void)
155 {
156         CU_ASSERT_FALSE(CU_FALSE);
157         CU_ASSERT_FALSE(!CU_TRUE);
158 }
159
160 static void testSuccessAssertEqual(void)
161 {
162         CU_ASSERT_EQUAL(10, 10);
163         CU_ASSERT_EQUAL(0, 0);
164         CU_ASSERT_EQUAL(0, -0);
165         CU_ASSERT_EQUAL(-12, -12);
166 }
167
168 static void testSuccessAssertNotEqual(void)
169 {
170         CU_ASSERT_NOT_EQUAL(10, 11);
171         CU_ASSERT_NOT_EQUAL(0, -1);
172         CU_ASSERT_NOT_EQUAL(-12, -11);
173 }
174
175 static void testSuccessAssertPtrEqual(void)
176 {
177         CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
178 }
179
180 static void testSuccessAssertPtrNotEqual(void)
181 {
182         CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
183 }
184
185 static void testSuccessAssertPtrNull(void)
186 {
187         CU_ASSERT_PTR_NULL(NULL);
188         CU_ASSERT_PTR_NULL(0x0);
189 }
190
191 static void testSuccessAssertPtrNotNull(void)
192 {
193         CU_ASSERT_PTR_NOT_NULL((void*)0x23);
194 }
195
196 static void testSuccessAssertStringEqual(void)
197 {
198         char str1[] = "test" ;
199         char str2[] = "test" ;
200
201         CU_ASSERT_STRING_EQUAL(str1, str2);
202 }
203
204 static void testSuccessAssertStringNotEqual(void)
205 {
206         char str1[] = "test" ;
207         char str2[] = "testtsg" ;
208
209         CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
210 }
211
212 static void testSuccessAssertNStringEqual(void)
213 {
214         char str1[] = "test" ;
215         char str2[] = "testgfsg" ;
216
217         CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
218         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
219         CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
220 }
221
222 static void testSuccessAssertNStringNotEqual(void)
223 {
224         char str1[] = "test" ;
225         char str2[] = "teet" ;
226         char str3[] = "testgfsg" ;
227
228         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
229         CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
230 }
231
232 static void testSuccessAssertDoubleEqual(void)
233 {
234         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
235         CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
236         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
237         CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
238 }
239
240 static void testSuccessAssertDoubleNotEqual(void)
241 {
242         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
243         CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
244         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
245         CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
246 }
247
248 static void AddTests(void)
249 {
250         CU_pSuite pGroup = NULL;
251         CU_pTest pTest = NULL;
252
253         pGroup = CU_add_suite("Sucess", success_init, success_clean);
254         pTest = CU_add_test(pGroup, "testSuccess1", testSuccess1);
255         pTest = CU_add_test(pGroup, "testSuccess2", testSuccess2);
256         pTest = CU_add_test(pGroup, "testSuccess3", testSuccess3);
257         
258         pGroup = CU_add_suite("failure", NULL, NULL);
259         pTest = CU_add_test(pGroup, "testfailure1", testfailure1);
260         pTest = CU_add_test(pGroup, "testfailure2", testfailure2);
261         pTest = CU_add_test(pGroup, "testfailure3", testfailure3);
262
263         pGroup = CU_add_suite("group_failure", group_failure_init, group_failure_clean);
264         pTest = CU_add_test(pGroup, "testGroupFailure1", testGroupFailure1);
265         pTest = CU_add_test(pGroup, "testGroupFailure2", testGroupFailure2);
266 }
267
268 static void AddStrBufSimlpeTests(void)
269 {
270         CU_pSuite pGroup = NULL;
271         CU_pTest pTest = NULL;
272
273         pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
274         pTest = CU_add_test(pGroup, "testCreateBuf", TestCreateBuf);
275
276         pGroup = CU_add_suite("TestStringTokenizer", NULL, NULL);
277         pTest = CU_add_test(pGroup, "testNextTokenizer", TestNextTokenizer);
278
279
280 /*
281         pGroup = CU_add_suite("TestBooleanAssert", NULL, NULL);
282         pTest = CU_add_test(pGroup, "testSuccessAssertTrue", testSuccessAssertTrue);
283         pTest = CU_add_test(pGroup, "testSuccessAssertFalse", testSuccessAssertFalse);
284
285         pGroup = CU_add_suite("TestEqualityAssert", NULL, NULL);
286         pTest = CU_add_test(pGroup, "testSuccessAssertEqual", testSuccessAssertEqual);
287         pTest = CU_add_test(pGroup, "testSuccessAssertNotEqual", testSuccessAssertNotEqual);
288
289         pGroup = CU_add_suite("TestPointerAssert", NULL, NULL);
290         pTest = CU_add_test(pGroup, "testSuccessAssertPtrEqual", testSuccessAssertPtrEqual);
291         pTest = CU_add_test(pGroup, "testSuccessAssertPtrNotEqual", testSuccessAssertPtrNotEqual);
292
293         pGroup = CU_add_suite("TestNullnessAssert", NULL, NULL);
294         pTest = CU_add_test(pGroup, "testSuccessAssertPtrNull", testSuccessAssertPtrNull);
295         pTest = CU_add_test(pGroup, "testSuccessAssertPtrNotNull", testSuccessAssertPtrNotNull);
296
297         pGroup = CU_add_suite("TestStringAssert", NULL, NULL);
298         pTest = CU_add_test(pGroup, "testSuccessAssertStringEqual", testSuccessAssertStringEqual);
299         pTest = CU_add_test(pGroup, "testSuccessAssertStringNotEqual", testSuccessAssertStringNotEqual);
300
301         pGroup = CU_add_suite("TestNStringAssert", NULL, NULL);
302         pTest = CU_add_test(pGroup, "testSuccessAssertNStringEqual", testSuccessAssertNStringEqual);
303         pTest = CU_add_test(pGroup, "testSuccessAssertNStringNotEqual", testSuccessAssertNStringNotEqual);
304
305         pGroup = CU_add_suite("TestDoubleAssert", NULL, NULL);
306         pTest = CU_add_test(pGroup, "testSuccessAssertDoubleEqual", testSuccessAssertDoubleEqual);
307         pTest = CU_add_test(pGroup, "testSuccessAssertDoubleNotEqual", testSuccessAssertDoubleNotEqual);
308 */
309 }
310
311
312 int main(int argc, char* argv[])
313 {
314         setvbuf(stdout, NULL, _IONBF, 0);
315
316         StartLibCitadel(8);
317         if (argc > 1) {
318                 CU_BOOL Run = CU_FALSE ;
319
320                 CU_set_output_filename("TestAutomated");
321                 if (CU_initialize_registry()) {
322                         printf("\nInitialize of test Registry failed.");
323                 }
324
325                 if (!strcmp("--test", argv[1])) {
326                         Run = CU_TRUE ;
327                         AddTests();
328                 }
329                 else if (!strcmp("--atest", argv[1])) {
330                         Run = CU_TRUE ;
331                         AddStrBufSimlpeTests();
332                 }
333                 else if (!strcmp("--alltest", argv[1])) {
334                         Run = CU_TRUE ;
335                         AddTests();
336 //                      AddAssertTests();
337                 }
338                 
339                 if (CU_TRUE == Run) {
340                         //CU_console_run_tests();
341     printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
342
343                         ///CU_automated_run_tests();
344                 }
345
346                 CU_cleanup_registry();
347         }
348
349         return 0;
350 }