Tests: add StrBufStripAllBut and compare it to stripallbut
[citadel] / libcitadel / tests / stripallbut_test.c
1 /*
2  *  CUnit - A Unit testing framework library for C.
3  *  Copyright (C) 2001  Anil Kumar
4  *  
5  *  This library is open source software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Library General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Library General Public License for more details.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdarg.h>
20
21     
22 #ifndef CUNIT_AUTOMATED_H_SEEN
23 #define CUNIT_AUTOMATED_H_SEEN
24
25 #include <CUnit/CUnit.h>
26 #include <CUnit/Basic.h>
27 #include <CUnit/TestDB.h>
28
29
30 CU_EXPORT void         CU_automated_run_tests(void);
31 CU_EXPORT CU_ErrorCode CU_list_tests_to_file(void);
32 CU_EXPORT void         CU_set_output_filename(const char* szFilenameRoot);
33
34
35 #endif  /*  CUNIT_AUTOMATED_H_SEEN  */
36
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <ctype.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <assert.h>
43
44 #include "../lib/libcitadel.h"
45
46 static void TestStripAllBut(void) {
47         int i;
48         long l;
49
50         char *teststrings[] = {
51                 "Nmm <fghjk> tyutyu",
52                 "Sdd <> ghhjjkk",
53                 "<>",
54                 "",
55                 "Sdd <",
56                 "Df Ghjkl>",
57                 ">< bggt",
58                 "FSDFSDFSDFSDF<><><>bggt",
59                 "FSDFSDFSDF<><eoeo><>bggt",
60                 "Abc<QWER>",
61                 "><",
62                 "Zxcv<dy<<lkj>>>"
63         };
64
65         char *strippedstrings[] = {
66                 "fghjk",
67                 "",
68                 "",
69                 "",
70                 "Sdd <",
71                 "Df Ghjkl>",
72                 ">< bggt",
73                 "FSDFSDFSDFSDF<><><>bggt",
74                 "FSDFSDFSDF<><eoeo><>bggt",
75                 "QWER",
76                 "><",
77                 "lkj"
78         };
79
80         long strippedlens[] = {
81                 5,
82                 0,
83                 0,
84                 0,
85                 5,
86                 9,
87                 7,
88                 23,
89                 24,
90                 4,
91                 2,
92                 3
93         };
94
95         char foo[128];
96         StrBuf *Test = NewStrBuf();;
97
98         for (i=0; i<(sizeof(teststrings) / sizeof (char *)); ++i) {
99                 strcpy(foo, teststrings[i]);
100                 l = stripallbut(foo, '<', '>');
101
102                 CU_ASSERT_STRING_EQUAL(foo, strippedstrings[i]);
103                 CU_ASSERT_EQUAL(strlen(foo), strippedlens[i]);
104
105
106                 StrBufPlain(Test, teststrings[i], -1);
107                 StrBufStripAllBut(Test, '<', '>');
108
109                 CU_ASSERT_STRING_EQUAL(ChrPtr(Test), strippedstrings[i]);
110                 CU_ASSERT_EQUAL(StrLength(Test), strippedlens[i]);
111
112                 printf("[%s] -> [%s][%s][%s]\n", teststrings[i], foo, ChrPtr(Test), strippedstrings[i]);
113         }
114 }
115
116
117
118 static void AddStringToolsTests(void)
119 {
120         CU_pSuite pGroup = NULL;
121         CU_pTest pTest = NULL;
122
123         pGroup = CU_add_suite("TestStringTools", NULL, NULL);
124         pTest = CU_add_test(pGroup, "testStripAllBut", TestStripAllBut);
125 }
126
127 int main(int argc, char* argv[])
128 {
129         setvbuf(stdout, NULL, _IONBF, 0);
130
131         StartLibCitadel(8);
132
133         CU_set_output_filename("TestAutomated");
134         if (CU_initialize_registry()) {
135                 printf("\nInitialize of test Registry failed.");
136                 return 1;
137         }
138         AddStringToolsTests();
139         
140         printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
141     
142                 
143         CU_cleanup_registry();
144         
145         return 0;
146 }