Debian Codegraph @

texk/xdvik/tests/test_string_utils.c

raw
1 #include "xdvi-config.h"
2 #include "xdvi.h"
3 #include <stdarg.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "run_tests.h"
10
11 #include "string-utils.h"
12
13 #define DEBUG 0
14
15 static Boolean test_get_separated_list(int verbose)
16 {
17 char *test_list1[] = {
18 "This is the first line",
19 "This is the second",
20 "This, is the third",
21 "The last one",
22 NULL
23 };
24 char *test_list2[] = {
25 "This",
26 "is",
27 "another",
28 "first",
29 "line",
30 NULL
31 };
32 const char *teststring1 = "This is the first line,This is the second,\"This, is the third\",The last one";
33 const char *teststring2 = "This\nis another\tfirst line";
34
35 char **res_list1 = get_separated_list(teststring1, ",", True);
36 char **res_list2 = get_separated_list(teststring2, " \t\n", True);
37 /* TODO: deallocate? */
38 return test_str_list_equality(verbose, test_list1, res_list1) && test_str_list_equality(verbose, test_list2, res_list2);
39 }
40
41 static Boolean test_str_is_prefix(int verbose)
42 {
43 static struct stringtest {
44 char *str1;
45 char *str2;
46 Boolean case_sensitive;
47 Boolean result;
48 } tests[] = {
49 { "foo", "foobar", True, True },
50 { "foo", "Foobar", True, False },
51 { "foo", "Foobar", False, True },
52 { "foo", "FOObar", False, True },
53 { "foo", "foo", True, True },
54 { "f", "foo", True, True },
55 { "b", "foo", True, False },
56 { "foo", "b", True, False },
57 { "", "foo", True, True },
58 };
59
60 size_t i;
61 Boolean result = True;
62 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
63 if (verbose) {
64 INFO((stderr, "str_is_prefix(%s,%s,%d) -> %d == %d?\n",
65 tests[i].str1,
66 tests[i].str2,
67 tests[i].case_sensitive,
68 str_is_prefix(tests[i].str1, tests[i].str2, tests[i].case_sensitive),
69 tests[i].result));
70 }
71 if (str_is_prefix(tests[i].str1, tests[i].str2, tests[i].case_sensitive) != tests[i].result) {
72 result = False;
73 }
74 }
75 return result;
76 }
77
78 static Boolean test_str_is_suffix(int verbose)
79 {
80 static struct stringtest {
81 char *str1;
82 char *str2;
83 Boolean case_sensitive;
84 Boolean result;
85 } tests[] = {
86 { "bar", "foobar", True, True },
87 { "bar", "FooBar", True, False },
88 { "bar", "fooBAR", False, True },
89 { "bar", "fooBaR", False, True },
90 { "bar", "bar", True, True },
91 { "r", "bar", True, True },
92 { "x", "bar", True, False },
93 { "bar", "r", True, False },
94 { "", "bar", True, True },
95 { "", "", True, True },
96 };
97
98 size_t i;
99 Boolean result = True;
100 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
101 if (verbose) {
102 INFO((stderr, "str_is_suffix(%s,%s,%d) -> %d == %d?\n",
103 tests[i].str1,
104 tests[i].str2,
105 tests[i].case_sensitive,
106 str_is_suffix(tests[i].str1, tests[i].str2, tests[i].case_sensitive),
107 tests[i].result));
108 }
109 if (str_is_suffix(tests[i].str1, tests[i].str2, tests[i].case_sensitive) != tests[i].result) {
110 result = False;
111 }
112 }
113 return result;
114 }
115
116 static Boolean test_is_spaces_only(int verbose)
117 {
118 static struct stringtest {
119 char *str1;
120 Boolean result;
121 } tests[] = {
122 { "bar", False },
123 { " \t\n ", True },
124 { " a ", False },
125 { "", True },
126 };
127
128 size_t i;
129 Boolean result = True;
130 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
131 if (verbose) {
132 INFO((stderr, "is_spaces_only(%s) -> %d == %d?\n",
133 tests[i].str1,
134 is_spaces_only(tests[i].str1),
135 tests[i].result));
136 }
137 if (is_spaces_only(tests[i].str1) != tests[i].result) {
138 result = False;
139 }
140 }
141 return result;
142 }
143
144 static Boolean test_my_stristr(int verbose)
145 {
146 static struct stringtest {
147 char *str1;
148 char *str2;
149 char *result;
150 } tests[] = {
151 { "paloozah", "ooz", "oozah" },
152 { "paloozah", "pa", "paloozah" },
153 { "paloozah", "par", NULL },
154 { "paloozah", "x", NULL },
155 { "PALOOZAH", "ooz", "OOZAH" },
156 { "PALOOZAH", "ah", "AH" },
157 { "PALOOZAH", "h", "H" },
158 { "h", "h", "h" },
159 { "H", "h", "H" },
160 { "", "h", NULL },
161 };
162
163 size_t i;
164 Boolean result = True;
165 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
166 char *res_str = my_stristr(tests[i].str1, tests[i].str2);
167 /* We want to verify that my_stristr(STR1, STR2) is either NULL or
168 * a substring of STR1, but, e.g., `"oozah" == "paloozah" + 3' may
169 * or may not be true (depending on compiler optimizations). */
170 char *test_str = tests[i].result ?
171 tests[i].str1 + strlen(tests[i].str1) - strlen(tests[i].result) :
172 NULL;
173 if (verbose) {
174 INFO((stderr, "my_stristr(%s, %s) -> %s == %s?\n",
175 tests[i].str1,
176 tests[i].str2,
177 res_str,
178 test_str));
179 }
180 if (res_str != test_str) {
181 result = False;
182 }
183 }
184 return result;
185 }
186
187 static Boolean test_format_arg(int verbose)
188 {
189 static struct stringtest {
190 char *str1;
191 char *arg;
192 char *result;
193 } tests[] = {
194 { "netscape(%s, same-window)", "file.html", "netscape(file.html, same-window)" },
195 { "netscape(same-window)", "file.html", "netscape(same-window)" },
196 { "%s", "a", "a" },
197 { "", "", "" },
198 };
199
200 size_t i;
201 int tmpcnt = 0;
202 Boolean result = True;
203 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
204 if (verbose) {
205 INFO((stderr, "format_arg(%s) -> %s == %s, %d\n",
206 tests[i].str1,
207 format_arg(tests[i].str1, tests[i].arg, &tmpcnt),
208 tests[i].result,
209 tmpcnt));
210 }
211 if (!test_str_equality(verbose,
212 format_arg(tests[i].str1, tests[i].arg, &tmpcnt),
213 tests[i].result)) {
214 result = False;
215 }
216 }
217 /* tempcnt shouldn't be reset to 0 by format_arg() */
218 if (tmpcnt != 1) {
219 ERROR((stderr, "tmpcnt should be 1\n"));
220 result = False;
221 }
222 return result;
223 }
224
225 static Boolean test_escape_format_arg(int verbose)
226 {
227 static struct stringtest {
228 char *str;
229 char *result;
230 } tests[] = {
231 { "netscape(%s, same-window)", "netscape(%%s, same-window)" },
232 { "%%s", "%%%%s" },
233 { "%d%s", "%%d%%s" },
234 { "a", "a" },
235 { "", "" },
236 };
237
238 size_t i;
239 Boolean result = True;
240 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
241 if (verbose) {
242 INFO((stderr, "escape_format_arg(%s) -> %s == %s\n",
243 tests[i].str,
244 escape_format_arg(tests[i].str),
245 tests[i].result));
246 }
247 if (!test_str_equality(verbose,
248 escape_format_arg(tests[i].str),
249 tests[i].result)) {
250 result = False;
251 }
252 }
253 return result;
254 }
255
256 static Boolean test_filename_append_dvi(int verbose)
257 {
258 static struct stringtest {
259 char *str1;
260 char *str2;
261 } tests[] = {
262 { "file:///tmp/foo.bar", "/tmp/foo.bar" },
263 { "file:///tmp/foo", "/tmp/foo.dvi" },
264 /* the following two mimick firefox behaviour with double/single slashes */
265 { "file://tmp/foo", "/foo.dvi" },
266 { "file:/tmp/foo", "/tmp/foo.dvi" },
267 { "file://localhost/tmp/foo", "/tmp/foo.dvi" },
268 { "~/tmp/foo", "~/tmp/foo.dvi" },
269 };
270
271 size_t i;
272 Boolean result = True;
273 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
274 char *tmp = filename_append_dvi(tests[i].str1);
275 if (verbose) {
276 INFO((stderr, "filename_append_dvi(%s) -> %s == %s?\n",
277 tests[i].str1,
278 tmp,
279 tests[i].str2));
280 }
281 if (!test_str_equality(verbose, tmp, tests[i].str2)) {
282 result = False;
283 }
284 free(tmp);
285 }
286 return result;
287 }
288
289 static Boolean test_canonicalize_path(int verbose)
290 {
291 static struct stringtest {
292 char *str1;
293 char *str2;
294 } tests[] = {
295 { "/foo/a/bz.to/bar.dvi", "/foo/a/bz.to/bar.dvi" },
296 { "/foo/../bar.dvi", "/bar.dvi" },
297 { "/foo/../bar.dvi", "/bar.dvi" },
298 { "/foo/./roo/./bar.dvi", "/foo/roo/bar.dvi" },
299 { "/foo/./roo/../bar.dvi", "/foo/bar.dvi" },
300 };
301
302 size_t i;
303 Boolean result = True;
304 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
305 if (verbose) {
306 INFO((stderr, "canonicalize_path(%s) -> %s == %s?\n",
307 tests[i].str1,
308 canonicalize_path(tests[i].str1),
309 tests[i].str2));
310 }
311 if (!test_str_equality(verbose, canonicalize_path(tests[i].str1), tests[i].str2)) {
312 result = False;
313 }
314 }
315 return result;
316 }
317
318
319
320 static Boolean test_expand_filename(int verbose)
321 {
322 char cwd[10240];
323 char *currpath, *testpath1, *testpath2;
324
325 currpath = xstrdup(getcwd(cwd, 10240));
326 testpath1 = expand_filename(__FILE__, USE_CWD_PATH);
327 globals.dvi_file.dirname = xstrdup(cwd);
328 globals.dvi_file.dirname = xstrcat(globals.dvi_file.dirname, "/");
329 testpath2 = expand_filename(__FILE__, USE_DVI_PATH);
330
331 if (__FILE__[0] == '/') {
332 free(currpath);
333 currpath = xstrdup(__FILE__);
334 } else {
335 currpath = xstrcat(currpath, "/");
336 currpath = xstrcat(currpath, __FILE__);
337 }
338
339 return test_str_equality(verbose, testpath1, currpath) \
340 && test_str_equality(verbose, testpath2, currpath);
341 }
342
343
344 static Boolean test_get_extension(int verbose)
345 {
346 const char *test_strings[][2] = {
347 { "/dev/fd/3", NULL },
348 { "/home/user/file.dvi", ".dvi" },
349 { "/home/user/file.ps", ".ps" },
350 { "/home/user.bar/file.dvi", ".dvi" },
351 { "/home/user.bar/file", NULL },
352 { "/home/user/file", NULL },
353 { NULL, NULL }
354 };
355
356 size_t i;
357
358 for (i = 0; test_strings[i][0] != NULL; i++) {
359 const char *tmp = get_extension(test_strings[i][0]);
360 if (tmp == NULL && test_strings[i][1] == NULL) {
361 if (verbose)
362 INFO((stderr, "Checking extension of |%s| OK: <NULL> == <NULL>\n", test_strings[i][0]));
363 }
364 else if (tmp == NULL && test_strings[i][1] != NULL) {
365 if (verbose)
366 ERROR((stderr, "extension of |%s| gave <NULL> - should be |%s|\n",
367 test_strings[i][0], test_strings[i][1]));
368 return False;
369 }
370 else if (tmp != NULL && test_strings[i][1] == NULL) {
371 if (verbose)
372 ERROR((stderr, "extension of |%s| gave |%s| - should be <NULL>\n",
373 test_strings[i][0], tmp));
374 return False;
375 }
376 else {
377 if (strcmp(tmp, test_strings[i][1]) != 0) {
378 if (verbose)
379 ERROR((stderr, "ERROR: extension of |%s| gave |%s| - should be |%s|\n",
380 test_strings[i][0], tmp, test_strings[i][1]));
381 return False;
382 }
383 else {
384 if (verbose)
385 INFO((stderr, "Checking extension of |%s| OK: |%s| == |%s|\n",
386 test_strings[i][0], tmp, test_strings[i][1]));
387 }
388 }
389 }
390
391 return True;
392 }
393
394 static Boolean test_length_of_int(int verbose)
395 {
396 static struct stringtest {
397 int d;
398 int len;
399 } tests[] = {
400 { 0, 1 },
401 { 1, 1 },
402 { 11, 2 },
403 { 111, 3 },
404 { 100, 3 },
405 { 100000, 6 },
406 { 999999, 6 },
407 { 1000000, 7 },
408 { 101, 3 },
409 { 001, 1 },
410 };
411
412 size_t i;
413 Boolean result = True;
414 for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) {
415 if (verbose) {
416 INFO((stderr, "length_of_int(%d) -> %d == %d?\n",
417 tests[i].d,
418 length_of_int(tests[i].d),
419 tests[i].len));
420 }
421 if (length_of_int(tests[i].d) != tests[i].len) {
422 result = False;
423 }
424 }
425 return result;
426 }
427
428 void register_all_from_test_string_utils(void)
429 {
430 register_test(test_str_is_prefix, "str_is_prefix()");
431 register_test(test_str_is_suffix, "str_is_suffix()");
432 register_test(test_is_spaces_only, "is_spaces_only()");
433 register_test(test_my_stristr, "my_stristr()");
434 register_test(test_filename_append_dvi, "filename_append_dvi()");
435 /* TODO: expand_filename_append_dvi */
436 register_test(test_canonicalize_path, "canonicalize_path()");
437 register_test(test_format_arg, "format_arg()");
438 register_test(test_escape_format_arg, "escape_format_arg()");
439 register_test(test_expand_filename, "expand_filename()");
440 register_test(test_get_separated_list, "get_separated_list");
441 register_test(test_length_of_int, "length_of_int()");
442 register_test(test_get_extension, "Filename extensions");
443 }