Line data Source code
1 : /*
2 : INI LIBRARY
3 :
4 : Unit test for the comment object.
5 :
6 : Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
7 :
8 : INI Library is free software: you can redistribute it and/or modify
9 : it under the terms of the GNU Lesser General Public License as published by
10 : the Free Software Foundation, either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : INI Library is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU Lesser General Public License for more details.
17 :
18 : You should have received a copy of the GNU Lesser General Public License
19 : along with INI Library. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "config.h"
23 : #include <stdio.h>
24 : #include <string.h>
25 : #include <stdlib.h>
26 : #define TRACE_HOME
27 : #include "trace.h"
28 : #include "ini_comment.h"
29 :
30 : int verbose = 0;
31 :
32 : #define INIOUT(foo) \
33 : do { \
34 : if (verbose) foo; \
35 : } while(0)
36 :
37 : typedef int (*test_fn)(void);
38 :
39 1 : static int file_test(void)
40 : {
41 1 : int error = EOK;
42 1 : struct ini_comment *ic = NULL;
43 :
44 2 : if ((error = ini_comment_create(&ic)) ||
45 2 : (error = ini_comment_build(ic, ";Line 0")) ||
46 2 : (error = ini_comment_build(ic, ";Line 1")) ||
47 1 : (error = ini_comment_build(ic, ";Line 2"))) {
48 0 : printf("Failed to create comment object %d\n",
49 : error);
50 0 : ini_comment_destroy(ic);
51 0 : return error;
52 : }
53 :
54 1 : INIOUT(printf("<==== Comment ====>\n"));
55 1 : INIOUT(ini_comment_print(ic, stdout));
56 1 : INIOUT(printf("<=================>\n"));
57 1 : ini_comment_destroy(ic);
58 :
59 1 : return error;
60 : }
61 :
62 :
63 1 : static int alter_test(void)
64 : {
65 1 : int error = EOK;
66 1 : struct ini_comment *ic = NULL;
67 1 : uint32_t i, num = 0;
68 1 : char *line = NULL;
69 1 : const char *expected[] = { ";Line 0 inserted",
70 : ";Line 1 inserted",
71 : "",
72 : "",
73 : ";Line 3 replaced",
74 : "",
75 : ";Line 4" };
76 :
77 :
78 2 : if ((error = ini_comment_create(&ic)) ||
79 2 : (error = ini_comment_build(ic, ";Line 0")) ||
80 2 : (error = ini_comment_build(ic, NULL)) ||
81 2 : (error = ini_comment_build(ic, ";Line 2")) ||
82 2 : (error = ini_comment_build(ic, ";Line 3")) ||
83 1 : (error = ini_comment_build(ic, ""))) {
84 0 : printf("Failed to create comment object\n");
85 0 : ini_comment_destroy(ic);
86 0 : return error;
87 : }
88 :
89 1 : INIOUT(printf("<==== Comment ====>\n"));
90 1 : INIOUT(ini_comment_print(ic, stdout));
91 1 : INIOUT(printf("<=================>\n"));
92 :
93 2 : if ((error = ini_comment_append(ic, ";Line 4")) ||
94 2 : (error = ini_comment_clear(ic, 2)) ||
95 2 : (error = ini_comment_replace(ic, 3, ";Line 3 replaced")) ||
96 2 : (error = ini_comment_remove(ic, 0)) ||
97 2 : (error = ini_comment_insert(ic, 0, ";Line 0 inserted")) ||
98 1 : (error = ini_comment_insert(ic, 1, ";Line 1 inserted"))) {
99 0 : printf("Failed to create comment object\n");
100 0 : ini_comment_destroy(ic);
101 0 : return error;
102 : }
103 :
104 1 : INIOUT(printf("<==== Comment ====>\n"));
105 1 : INIOUT(ini_comment_print(ic, stdout));
106 1 : INIOUT(printf("<=================>\n"));
107 :
108 1 : error = ini_comment_get_numlines(ic, &num);
109 1 : if (error) {
110 0 : printf("Failed to get number of lines.\n");
111 0 : ini_comment_destroy(ic);
112 0 : return error;
113 : }
114 :
115 7 : for (i = 0; i < num; i++) {
116 7 : error = ini_comment_get_line(ic, i, &line, NULL);
117 7 : if (error) {
118 0 : printf("Failed to get line.\n");
119 0 : ini_comment_destroy(ic);
120 0 : return error;
121 : }
122 7 : if (strcmp(line, expected[i]) != 0) {
123 0 : printf("Lines do not match.\n");
124 0 : printf("GOT: %s\n", line);
125 0 : printf("EXP: %s\n", expected[i]);
126 0 : ini_comment_destroy(ic);
127 0 : return error;
128 : }
129 : }
130 :
131 1 : INIOUT(printf("\n\nSwap test\n\n"));
132 :
133 2 : if ((error = ini_comment_swap(ic, 0 , 6)) ||
134 2 : (error = ini_comment_swap(ic, 1 , 5)) ||
135 1 : (error = ini_comment_swap(ic, 2 , 4))) {
136 0 : printf("Failed to swap lines.\n");
137 0 : ini_comment_destroy(ic);
138 0 : return error;
139 : }
140 :
141 7 : for (i = 0; i < num; i++) {
142 7 : error = ini_comment_get_line(ic, i, &line, NULL);
143 7 : if (error) {
144 0 : printf("Failed to get line.\n");
145 0 : ini_comment_destroy(ic);
146 0 : return error;
147 : }
148 7 : if (strcmp(line, expected[6 - i]) != 0) {
149 0 : printf("Lines do not match.\n");
150 0 : printf("GOT: %s\n", line);
151 0 : printf("EXP: %s\n", expected[6 - i]);
152 0 : ini_comment_destroy(ic);
153 0 : return error;
154 : }
155 : }
156 :
157 1 : ini_comment_destroy(ic);
158 1 : return error;
159 : }
160 :
161 1 : static int copy_test(void)
162 : {
163 1 : int error = EOK;
164 1 : struct ini_comment *ic = NULL;
165 1 : struct ini_comment *ic_copy = NULL;
166 1 : char *line = NULL;
167 1 : char *line_copy = NULL;
168 1 : uint32_t i, num = 0;
169 :
170 1 : INIOUT(printf("\n\nCopy test\n\n"));
171 :
172 2 : if ((error = ini_comment_create(&ic)) ||
173 2 : (error = ini_comment_build(ic, ";Line 0")) ||
174 2 : (error = ini_comment_build(ic, ";Line 1")) ||
175 1 : (error = ini_comment_build(ic, ";Line 2"))) {
176 0 : printf("Failed to create comment object %d\n",
177 : error);
178 0 : ini_comment_destroy(ic);
179 0 : return error;
180 : }
181 :
182 1 : INIOUT(printf("<==== Comment ====>\n"));
183 1 : INIOUT(ini_comment_print(ic, stdout));
184 1 : INIOUT(printf("<=================>\n"));
185 :
186 1 : if ((error = ini_comment_copy(ic, &ic_copy))) {
187 0 : printf("Failed to create comment object %d\n",
188 : error);
189 0 : ini_comment_destroy(ic);
190 0 : return error;
191 : }
192 :
193 1 : INIOUT(printf("<==== Comment Copy====>\n"));
194 1 : INIOUT(ini_comment_print(ic_copy, stdout));
195 1 : INIOUT(printf("<=================>\n"));
196 :
197 1 : error = ini_comment_get_numlines(ic, &num);
198 1 : if (error) {
199 0 : printf("Failed to get number of lines.\n");
200 0 : ini_comment_destroy(ic);
201 0 : ini_comment_destroy(ic_copy);
202 0 : return error;
203 : }
204 :
205 3 : for (i = 0; i < num; i++) {
206 3 : error = ini_comment_get_line(ic, i, &line, NULL);
207 3 : if (error) {
208 0 : printf("Failed to get line.\n");
209 0 : ini_comment_destroy(ic);
210 0 : ini_comment_destroy(ic_copy);
211 0 : return error;
212 : }
213 3 : error = ini_comment_get_line(ic_copy, i, &line_copy, NULL);
214 3 : if (error) {
215 0 : printf("Failed to get line.\n");
216 0 : ini_comment_destroy(ic);
217 0 : ini_comment_destroy(ic_copy);
218 0 : return error;
219 : }
220 3 : if (strcmp(line, line_copy) != 0) {
221 0 : printf("Lines do not match.\n");
222 0 : printf("Source: %s\n", line);
223 0 : printf("Copy: %s\n", line_copy);
224 0 : ini_comment_destroy(ic);
225 0 : ini_comment_destroy(ic_copy);
226 0 : return -1;
227 : }
228 : }
229 :
230 1 : ini_comment_destroy(ic);
231 1 : ini_comment_destroy(ic_copy);
232 :
233 1 : return error;
234 : }
235 :
236 1 : static int add_test(void)
237 : {
238 1 : int error = EOK;
239 1 : struct ini_comment *ic = NULL;
240 1 : struct ini_comment *ic_to_add = NULL;
241 1 : struct ini_comment *ic_cmp = NULL;
242 1 : uint32_t i, num1 = 0, num2 = 0;
243 1 : char *line1 = NULL;
244 1 : char *line2 = NULL;
245 :
246 :
247 1 : INIOUT(printf("\n\nAdd test\n\n"));
248 :
249 2 : if ((error = ini_comment_create(&ic)) ||
250 2 : (error = ini_comment_build(ic, ";Line 0")) ||
251 2 : (error = ini_comment_build(ic, ";Line 1")) ||
252 1 : (error = ini_comment_build(ic, ";Line 2"))) {
253 0 : printf("Failed to create comment object %d\n",
254 : error);
255 0 : ini_comment_destroy(ic);
256 0 : return error;
257 : }
258 :
259 1 : INIOUT(printf("<==== Comment ====>\n"));
260 1 : INIOUT(ini_comment_print(ic, stdout));
261 1 : INIOUT(printf("<=================>\n"));
262 :
263 2 : if ((error = ini_comment_create(&ic_to_add)) ||
264 2 : (error = ini_comment_build(ic_to_add, ";Line 3")) ||
265 2 : (error = ini_comment_build(ic_to_add, ";Line 4")) ||
266 1 : (error = ini_comment_build(ic_to_add, ";Line 5"))) {
267 0 : printf("Failed to create comment object %d\n",
268 : error);
269 0 : ini_comment_destroy(ic);
270 0 : return error;
271 : }
272 :
273 1 : INIOUT(printf("<==== Comment To Add ====>\n"));
274 1 : INIOUT(ini_comment_print(ic_to_add, stdout));
275 1 : INIOUT(printf("<=================>\n"));
276 :
277 1 : error = ini_comment_add(ic_to_add, ic);
278 1 : if (error) {
279 0 : printf("Failed to add one comment to another.\n");
280 0 : ini_comment_destroy(ic);
281 0 : ini_comment_destroy(ic_to_add);
282 0 : return error;
283 : }
284 :
285 1 : INIOUT(printf("<==== Merged Comment ====>\n"));
286 1 : INIOUT(ini_comment_print(ic, stdout));
287 1 : INIOUT(printf("<=================>\n"));
288 :
289 2 : if ((error = ini_comment_create(&ic_cmp)) ||
290 2 : (error = ini_comment_build(ic_cmp, ";Line 0")) ||
291 2 : (error = ini_comment_build(ic_cmp, ";Line 1")) ||
292 2 : (error = ini_comment_build(ic_cmp, ";Line 2")) ||
293 2 : (error = ini_comment_build(ic_cmp, ";Line 3")) ||
294 2 : (error = ini_comment_build(ic_cmp, ";Line 4")) ||
295 1 : (error = ini_comment_build(ic_cmp, ";Line 5"))) {
296 0 : printf("Failed to create comment object %d\n",
297 : error);
298 0 : ini_comment_destroy(ic_cmp);
299 0 : return error;
300 : }
301 :
302 1 : ini_comment_destroy(ic_to_add);
303 :
304 1 : error = ini_comment_get_numlines(ic, &num1);
305 1 : if (error) {
306 0 : printf("Failed to get number of lines.\n");
307 0 : ini_comment_destroy(ic);
308 0 : ini_comment_destroy(ic_cmp);
309 0 : return error;
310 : }
311 :
312 1 : error = ini_comment_get_numlines(ic, &num2);
313 1 : if (error) {
314 0 : printf("Failed to get number of lines.\n");
315 0 : ini_comment_destroy(ic);
316 0 : ini_comment_destroy(ic_cmp);
317 0 : return error;
318 : }
319 :
320 1 : if (num1 != num2) {
321 0 : printf("Sizes are different.\n");
322 0 : ini_comment_destroy(ic);
323 0 : ini_comment_destroy(ic_cmp);
324 0 : return -1;
325 : }
326 :
327 6 : for (i = 0; i < num1; i++) {
328 6 : line1 = NULL;
329 6 : error = ini_comment_get_line(ic, i, &line1, NULL);
330 6 : if (error) {
331 0 : printf("Failed to get line.\n");
332 0 : ini_comment_destroy(ic);
333 0 : ini_comment_destroy(ic_cmp);
334 0 : return error;
335 : }
336 6 : line2 = NULL;
337 6 : error = ini_comment_get_line(ic_cmp, i, &line2, NULL);
338 6 : if (error) {
339 0 : printf("Failed to get line.\n");
340 0 : ini_comment_destroy(ic);
341 0 : ini_comment_destroy(ic_cmp);
342 0 : return error;
343 : }
344 6 : if (strcmp(line1, line2) != 0) {
345 0 : printf("Lines do not match.\n");
346 0 : printf("1st: %s\n", line1);
347 0 : printf("2nd: %s\n", line2);
348 0 : ini_comment_destroy(ic);
349 0 : ini_comment_destroy(ic_cmp);
350 0 : return -1;
351 : }
352 : }
353 :
354 1 : ini_comment_destroy(ic);
355 1 : ini_comment_destroy(ic_cmp);
356 :
357 1 : return error;
358 : }
359 :
360 :
361 1 : int main(int argc, char *argv[])
362 : {
363 1 : int error = EOK;
364 1 : test_fn tests[] = { file_test,
365 : alter_test,
366 : copy_test,
367 : add_test,
368 : NULL };
369 : test_fn t;
370 1 : int i = 0;
371 : char *var;
372 :
373 1 : if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
374 : else {
375 1 : var = getenv("COMMON_TEST_VERBOSE");
376 1 : if (var) verbose = 1;
377 : }
378 :
379 1 : INIOUT(printf("Start\n"));
380 :
381 5 : while ((t = tests[i++])) {
382 4 : error = t();
383 4 : if (error) {
384 0 : INIOUT(printf("Failed with error %d!\n", error));
385 0 : return error;
386 : }
387 : }
388 :
389 1 : INIOUT(printf("Success!\n"));
390 : return 0;
391 : }
|