Debian Codegraph @

ini/ini_comment_ut.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Unit test for the comment object.
5
6 Copyright (C) Dmitri Pal <[email protected]> 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 static int file_test(void)
40 {
41 int error = EOK;
42 struct ini_comment *ic = NULL;
43
44 if ((error = ini_comment_create(&ic)) ||
45 (error = ini_comment_build(ic, ";Line 0")) ||
46 (error = ini_comment_build(ic, ";Line 1")) ||
47 (error = ini_comment_build(ic, ";Line 2"))) {
48 printf("Failed to create comment object %d\n",
49 error);
50 ini_comment_destroy(ic);
51 return error;
52 }
53
54 INIOUT(printf("<==== Comment ====>\n"));
55 INIOUT(ini_comment_print(ic, stdout));
56 INIOUT(printf("<=================>\n"));
57 ini_comment_destroy(ic);
58
59 return error;
60 }
61
62 static int construct_test(void)
63 {
64 int error = EOK;
65 struct ini_comment *ic = NULL;
66 const char *comments[] = { ";Line1", ";Line2", ";Line3", NULL };
67
68 error = ini_comment_construct(comments,
69 0,
70 &ic);
71 if (error) {
72 printf("Failed to create comment object %d\n",
73 error);
74 return error;
75 }
76
77 INIOUT(printf("<==== Constructed Comment 1 ====>\n"));
78 INIOUT(ini_comment_print(ic, stdout));
79 INIOUT(printf("<=================>\n"));
80 ini_comment_destroy(ic);
81
82 error = ini_comment_construct(comments,
83 2,
84 &ic);
85 if (error) {
86 printf("Failed to create comment object %d\n",
87 error);
88 return error;
89 }
90
91 INIOUT(printf("<==== Constructed Comment 2 ====>\n"));
92 INIOUT(ini_comment_print(ic, stdout));
93 INIOUT(printf("<=================>\n"));
94 ini_comment_destroy(ic);
95
96 return error;
97 }
98
99
100 static int alter_test(void)
101 {
102 int error = EOK;
103 struct ini_comment *ic = NULL;
104 uint32_t i, num = 0;
105 char *line = NULL;
106 const char *expected[] = { ";Line 0 inserted",
107 ";Line 1 inserted",
108 "",
109 "",
110 ";Line 3 replaced",
111 "",
112 ";Line 4" };
113
114
115 if ((error = ini_comment_create(&ic)) ||
116 (error = ini_comment_build(ic, ";Line 0")) ||
117 (error = ini_comment_build(ic, NULL)) ||
118 (error = ini_comment_build(ic, ";Line 2")) ||
119 (error = ini_comment_build(ic, ";Line 3")) ||
120 (error = ini_comment_build(ic, ""))) {
121 printf("Failed to create comment object\n");
122 ini_comment_destroy(ic);
123 return error;
124 }
125
126 INIOUT(printf("<==== Comment ====>\n"));
127 INIOUT(ini_comment_print(ic, stdout));
128 INIOUT(printf("<=================>\n"));
129
130 if ((error = ini_comment_append(ic, ";Line 4")) ||
131 (error = ini_comment_clear(ic, 2)) ||
132 (error = ini_comment_replace(ic, 3, ";Line 3 replaced")) ||
133 (error = ini_comment_remove(ic, 0)) ||
134 (error = ini_comment_insert(ic, 0, ";Line 0 inserted")) ||
135 (error = ini_comment_insert(ic, 1, ";Line 1 inserted"))) {
136 printf("Failed to create comment object\n");
137 ini_comment_destroy(ic);
138 return error;
139 }
140
141 INIOUT(printf("<==== Comment ====>\n"));
142 INIOUT(ini_comment_print(ic, stdout));
143 INIOUT(printf("<=================>\n"));
144
145 error = ini_comment_get_numlines(ic, &num);
146 if (error) {
147 printf("Failed to get number of lines.\n");
148 ini_comment_destroy(ic);
149 return error;
150 }
151
152 for (i = 0; i < num; i++) {
153 error = ini_comment_get_line(ic, i, &line, NULL);
154 if (error) {
155 printf("Failed to get line.\n");
156 ini_comment_destroy(ic);
157 return error;
158 }
159 if (strcmp(line, expected[i]) != 0) {
160 printf("Lines do not match.\n");
161 printf("GOT: %s\n", line);
162 printf("EXP: %s\n", expected[i]);
163 ini_comment_destroy(ic);
164 return error;
165 }
166 }
167
168 INIOUT(printf("\n\nSwap test\n\n"));
169
170 if ((error = ini_comment_swap(ic, 0 , 6)) ||
171 (error = ini_comment_swap(ic, 1 , 5)) ||
172 (error = ini_comment_swap(ic, 2 , 4))) {
173 printf("Failed to swap lines.\n");
174 ini_comment_destroy(ic);
175 return error;
176 }
177
178 for (i = 0; i < num; i++) {
179 error = ini_comment_get_line(ic, i, &line, NULL);
180 if (error) {
181 printf("Failed to get line.\n");
182 ini_comment_destroy(ic);
183 return error;
184 }
185 if (strcmp(line, expected[6 - i]) != 0) {
186 printf("Lines do not match.\n");
187 printf("GOT: %s\n", line);
188 printf("EXP: %s\n", expected[6 - i]);
189 ini_comment_destroy(ic);
190 return error;
191 }
192 }
193
194 ini_comment_destroy(ic);
195 return error;
196 }
197
198 static int copy_test(void)
199 {
200 int error = EOK;
201 struct ini_comment *ic = NULL;
202 struct ini_comment *ic_copy = NULL;
203 char *line = NULL;
204 char *line_copy = NULL;
205 uint32_t i, num = 0;
206
207 INIOUT(printf("\n\nCopy test\n\n"));
208
209 if ((error = ini_comment_create(&ic)) ||
210 (error = ini_comment_build(ic, ";Line 0")) ||
211 (error = ini_comment_build(ic, ";Line 1")) ||
212 (error = ini_comment_build(ic, ";Line 2"))) {
213 printf("Failed to create comment object %d\n",
214 error);
215 ini_comment_destroy(ic);
216 return error;
217 }
218
219 INIOUT(printf("<==== Comment ====>\n"));
220 INIOUT(ini_comment_print(ic, stdout));
221 INIOUT(printf("<=================>\n"));
222
223 if ((error = ini_comment_copy(ic, &ic_copy))) {
224 printf("Failed to create comment object %d\n",
225 error);
226 ini_comment_destroy(ic);
227 return error;
228 }
229
230 INIOUT(printf("<==== Comment Copy====>\n"));
231 INIOUT(ini_comment_print(ic_copy, stdout));
232 INIOUT(printf("<=================>\n"));
233
234 error = ini_comment_get_numlines(ic, &num);
235 if (error) {
236 printf("Failed to get number of lines.\n");
237 ini_comment_destroy(ic);
238 ini_comment_destroy(ic_copy);
239 return error;
240 }
241
242 for (i = 0; i < num; i++) {
243 error = ini_comment_get_line(ic, i, &line, NULL);
244 if (error) {
245 printf("Failed to get line.\n");
246 ini_comment_destroy(ic);
247 ini_comment_destroy(ic_copy);
248 return error;
249 }
250 error = ini_comment_get_line(ic_copy, i, &line_copy, NULL);
251 if (error) {
252 printf("Failed to get line.\n");
253 ini_comment_destroy(ic);
254 ini_comment_destroy(ic_copy);
255 return error;
256 }
257 if (strcmp(line, line_copy) != 0) {
258 printf("Lines do not match.\n");
259 printf("Source: %s\n", line);
260 printf("Copy: %s\n", line_copy);
261 ini_comment_destroy(ic);
262 ini_comment_destroy(ic_copy);
263 return -1;
264 }
265 }
266
267 ini_comment_destroy(ic);
268 ini_comment_destroy(ic_copy);
269
270 return error;
271 }
272
273 static int add_test(void)
274 {
275 int error = EOK;
276 struct ini_comment *ic = NULL;
277 struct ini_comment *ic_to_add = NULL;
278 struct ini_comment *ic_cmp = NULL;
279 uint32_t i, num1 = 0, num2 = 0;
280 char *line1 = NULL;
281 char *line2 = NULL;
282
283
284 INIOUT(printf("\n\nAdd test\n\n"));
285
286 if ((error = ini_comment_create(&ic)) ||
287 (error = ini_comment_build(ic, ";Line 0")) ||
288 (error = ini_comment_build(ic, ";Line 1")) ||
289 (error = ini_comment_build(ic, ";Line 2"))) {
290 printf("Failed to create comment object %d\n",
291 error);
292 ini_comment_destroy(ic);
293 return error;
294 }
295
296 INIOUT(printf("<==== Comment ====>\n"));
297 INIOUT(ini_comment_print(ic, stdout));
298 INIOUT(printf("<=================>\n"));
299
300 if ((error = ini_comment_create(&ic_to_add)) ||
301 (error = ini_comment_build(ic_to_add, ";Line 3")) ||
302 (error = ini_comment_build(ic_to_add, ";Line 4")) ||
303 (error = ini_comment_build(ic_to_add, ";Line 5"))) {
304 printf("Failed to create comment object %d\n",
305 error);
306 ini_comment_destroy(ic);
307 return error;
308 }
309
310 INIOUT(printf("<==== Comment To Add ====>\n"));
311 INIOUT(ini_comment_print(ic_to_add, stdout));
312 INIOUT(printf("<=================>\n"));
313
314 error = ini_comment_add(ic_to_add, ic);
315 if (error) {
316 printf("Failed to add one comment to another.\n");
317 ini_comment_destroy(ic);
318 ini_comment_destroy(ic_to_add);
319 return error;
320 }
321
322 INIOUT(printf("<==== Merged Comment ====>\n"));
323 INIOUT(ini_comment_print(ic, stdout));
324 INIOUT(printf("<=================>\n"));
325
326 if ((error = ini_comment_create(&ic_cmp)) ||
327 (error = ini_comment_build(ic_cmp, ";Line 0")) ||
328 (error = ini_comment_build(ic_cmp, ";Line 1")) ||
329 (error = ini_comment_build(ic_cmp, ";Line 2")) ||
330 (error = ini_comment_build(ic_cmp, ";Line 3")) ||
331 (error = ini_comment_build(ic_cmp, ";Line 4")) ||
332 (error = ini_comment_build(ic_cmp, ";Line 5"))) {
333 printf("Failed to create comment object %d\n",
334 error);
335 ini_comment_destroy(ic_cmp);
336 return error;
337 }
338
339 ini_comment_destroy(ic_to_add);
340
341 error = ini_comment_get_numlines(ic, &num1);
342 if (error) {
343 printf("Failed to get number of lines.\n");
344 ini_comment_destroy(ic);
345 ini_comment_destroy(ic_cmp);
346 return error;
347 }
348
349 error = ini_comment_get_numlines(ic, &num2);
350 if (error) {
351 printf("Failed to get number of lines.\n");
352 ini_comment_destroy(ic);
353 ini_comment_destroy(ic_cmp);
354 return error;
355 }
356
357 if (num1 != num2) {
358 printf("Sizes are different.\n");
359 ini_comment_destroy(ic);
360 ini_comment_destroy(ic_cmp);
361 return -1;
362 }
363
364 for (i = 0; i < num1; i++) {
365 line1 = NULL;
366 error = ini_comment_get_line(ic, i, &line1, NULL);
367 if (error) {
368 printf("Failed to get line.\n");
369 ini_comment_destroy(ic);
370 ini_comment_destroy(ic_cmp);
371 return error;
372 }
373 line2 = NULL;
374 error = ini_comment_get_line(ic_cmp, i, &line2, NULL);
375 if (error) {
376 printf("Failed to get line.\n");
377 ini_comment_destroy(ic);
378 ini_comment_destroy(ic_cmp);
379 return error;
380 }
381 if (strcmp(line1, line2) != 0) {
382 printf("Lines do not match.\n");
383 printf("1st: %s\n", line1);
384 printf("2nd: %s\n", line2);
385 ini_comment_destroy(ic);
386 ini_comment_destroy(ic_cmp);
387 return -1;
388 }
389 }
390
391 ini_comment_destroy(ic);
392 ini_comment_destroy(ic_cmp);
393
394 return error;
395 }
396
397
398 int main(int argc, char *argv[])
399 {
400 int error = EOK;
401 test_fn tests[] = { file_test,
402 alter_test,
403 copy_test,
404 add_test,
405 construct_test,
406 NULL };
407 test_fn t;
408 int i = 0;
409 char *var;
410
411 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
412 else {
413 var = getenv("COMMON_TEST_VERBOSE");
414 if (var) verbose = 1;
415 }
416
417 INIOUT(printf("Start\n"));
418
419 while ((t = tests[i++])) {
420 error = t();
421 if (error) {
422 INIOUT(printf("Failed with error %d!\n", error));
423 return error;
424 }
425 }
426
427 INIOUT(printf("Success!\n"));
428 return 0;
429 }