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