]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testarray.c
Migrate Windows conditional code to _WIN32 define.
[thirdparty/cups.git] / cups / testarray.c
1 /*
2 * Array test program for CUPS.
3 *
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
8 */
9
10 /*
11 * Include necessary headers...
12 */
13
14 #include "string-private.h"
15 #include "debug-private.h"
16 #include "array-private.h"
17 #include "dir.h"
18
19
20 /*
21 * Local functions...
22 */
23
24 static double get_seconds(void);
25 static int load_words(const char *filename, cups_array_t *array);
26
27
28 /*
29 * 'main()' - Main entry.
30 */
31
32 int /* O - Exit status */
33 main(void)
34 {
35 int i; /* Looping var */
36 cups_array_t *array, /* Test array */
37 *dup_array; /* Duplicate array */
38 int status; /* Exit status */
39 char *text; /* Text from array */
40 char word[256]; /* Word from file */
41 double start, /* Start time */
42 end; /* End time */
43 cups_dir_t *dir; /* Current directory */
44 cups_dentry_t *dent; /* Directory entry */
45 char *saved[32]; /* Saved entries */
46 void *data; /* User data for arrays */
47
48
49 /*
50 * No errors so far...
51 */
52
53 status = 0;
54
55 /*
56 * cupsArrayNew()
57 */
58
59 fputs("cupsArrayNew: ", stdout);
60
61 data = (void *)"testarray";
62 array = cupsArrayNew((cups_array_func_t)strcmp, data);
63
64 if (array)
65 puts("PASS");
66 else
67 {
68 puts("FAIL (returned NULL, expected pointer)");
69 status ++;
70 }
71
72 /*
73 * cupsArrayUserData()
74 */
75
76 fputs("cupsArrayUserData: ", stdout);
77 if (cupsArrayUserData(array) == data)
78 puts("PASS");
79 else
80 {
81 printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array),
82 data);
83 status ++;
84 }
85
86 /*
87 * cupsArrayAdd()
88 */
89
90 fputs("cupsArrayAdd: ", stdout);
91
92 if (!cupsArrayAdd(array, strdup("One Fish")))
93 {
94 puts("FAIL (\"One Fish\")");
95 status ++;
96 }
97 else
98 {
99 if (!cupsArrayAdd(array, strdup("Two Fish")))
100 {
101 puts("FAIL (\"Two Fish\")");
102 status ++;
103 }
104 else
105 {
106 if (!cupsArrayAdd(array, strdup("Red Fish")))
107 {
108 puts("FAIL (\"Red Fish\")");
109 status ++;
110 }
111 else
112 {
113 if (!cupsArrayAdd(array, strdup("Blue Fish")))
114 {
115 puts("FAIL (\"Blue Fish\")");
116 status ++;
117 }
118 else
119 puts("PASS");
120 }
121 }
122 }
123
124 /*
125 * cupsArrayCount()
126 */
127
128 fputs("cupsArrayCount: ", stdout);
129 if (cupsArrayCount(array) == 4)
130 puts("PASS");
131 else
132 {
133 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array));
134 status ++;
135 }
136
137 /*
138 * cupsArrayFirst()
139 */
140
141 fputs("cupsArrayFirst: ", stdout);
142 if ((text = (char *)cupsArrayFirst(array)) != NULL &&
143 !strcmp(text, "Blue Fish"))
144 puts("PASS");
145 else
146 {
147 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text);
148 status ++;
149 }
150
151 /*
152 * cupsArrayNext()
153 */
154
155 fputs("cupsArrayNext: ", stdout);
156 if ((text = (char *)cupsArrayNext(array)) != NULL &&
157 !strcmp(text, "One Fish"))
158 puts("PASS");
159 else
160 {
161 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
162 status ++;
163 }
164
165 /*
166 * cupsArrayLast()
167 */
168
169 fputs("cupsArrayLast: ", stdout);
170 if ((text = (char *)cupsArrayLast(array)) != NULL &&
171 !strcmp(text, "Two Fish"))
172 puts("PASS");
173 else
174 {
175 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text);
176 status ++;
177 }
178
179 /*
180 * cupsArrayPrev()
181 */
182
183 fputs("cupsArrayPrev: ", stdout);
184 if ((text = (char *)cupsArrayPrev(array)) != NULL &&
185 !strcmp(text, "Red Fish"))
186 puts("PASS");
187 else
188 {
189 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text);
190 status ++;
191 }
192
193 /*
194 * cupsArrayFind()
195 */
196
197 fputs("cupsArrayFind: ", stdout);
198 if ((text = (char *)cupsArrayFind(array, (void *)"One Fish")) != NULL &&
199 !strcmp(text, "One Fish"))
200 puts("PASS");
201 else
202 {
203 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
204 status ++;
205 }
206
207 /*
208 * cupsArrayCurrent()
209 */
210
211 fputs("cupsArrayCurrent: ", stdout);
212 if ((text = (char *)cupsArrayCurrent(array)) != NULL &&
213 !strcmp(text, "One Fish"))
214 puts("PASS");
215 else
216 {
217 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
218 status ++;
219 }
220
221 /*
222 * cupsArrayDup()
223 */
224
225 fputs("cupsArrayDup: ", stdout);
226 if ((dup_array = cupsArrayDup(array)) != NULL &&
227 cupsArrayCount(dup_array) == 4)
228 puts("PASS");
229 else
230 {
231 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n", (void *)dup_array, cupsArrayCount(dup_array));
232 status ++;
233 }
234
235 /*
236 * cupsArrayRemove()
237 */
238
239 fputs("cupsArrayRemove: ", stdout);
240 if (cupsArrayRemove(array, (void *)"One Fish") &&
241 cupsArrayCount(array) == 3)
242 puts("PASS");
243 else
244 {
245 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
246 cupsArrayCount(array));
247 status ++;
248 }
249
250 /*
251 * cupsArrayClear()
252 */
253
254 fputs("cupsArrayClear: ", stdout);
255 cupsArrayClear(array);
256 if (cupsArrayCount(array) == 0)
257 puts("PASS");
258 else
259 {
260 printf("FAIL (%d elements, expected 0 elements)\n",
261 cupsArrayCount(array));
262 status ++;
263 }
264
265 /*
266 * Now load this source file and grab all of the unique words...
267 */
268
269 fputs("Load unique words: ", stdout);
270 fflush(stdout);
271
272 start = get_seconds();
273
274 if ((dir = cupsDirOpen(".")) == NULL)
275 {
276 puts("FAIL (cupsDirOpen failed)");
277 status ++;
278 }
279 else
280 {
281 while ((dent = cupsDirRead(dir)) != NULL)
282 {
283 i = (int)strlen(dent->filename) - 2;
284
285 if (i > 0 && dent->filename[i] == '.' &&
286 (dent->filename[i + 1] == 'c' ||
287 dent->filename[i + 1] == 'h'))
288 load_words(dent->filename, array);
289 }
290
291 cupsDirClose(dir);
292
293 end = get_seconds();
294
295 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array),
296 end - start, cupsArrayCount(array) / (end - start));
297 fflush(stdout);
298
299 for (text = (char *)cupsArrayFirst(array); text;)
300 {
301 /*
302 * Copy this word to the word buffer (safe because we strdup'd from
303 * the same buffer in the first place... :)
304 */
305
306 strlcpy(word, text, sizeof(word));
307
308 /*
309 * Grab the next word and compare...
310 */
311
312 if ((text = (char *)cupsArrayNext(array)) == NULL)
313 break;
314
315 if (strcmp(word, text) >= 0)
316 break;
317 }
318
319 if (text)
320 {
321 printf("FAIL (\"%s\" >= \"%s\"!)\n", word, text);
322 status ++;
323 }
324 else
325 puts("PASS");
326 }
327
328 /*
329 * Test deleting with iteration...
330 */
331
332 fputs("Delete While Iterating: ", stdout);
333
334 text = (char *)cupsArrayFirst(array);
335 cupsArrayRemove(array, text);
336 free(text);
337
338 text = (char *)cupsArrayNext(array);
339 if (!text)
340 {
341 puts("FAIL (cupsArrayNext returned NULL!)");
342 status ++;
343 }
344 else
345 puts("PASS");
346
347 /*
348 * Test save/restore...
349 */
350
351 fputs("cupsArraySave: ", stdout);
352
353 for (i = 0, text = (char *)cupsArrayFirst(array);
354 i < 32;
355 i ++, text = (char *)cupsArrayNext(array))
356 {
357 saved[i] = text;
358
359 if (!cupsArraySave(array))
360 break;
361 }
362
363 if (i < 32)
364 printf("FAIL (depth = %d)\n", i);
365 else
366 puts("PASS");
367
368 fputs("cupsArrayRestore: ", stdout);
369
370 while (i > 0)
371 {
372 i --;
373
374 text = cupsArrayRestore(array);
375 if (text != saved[i])
376 break;
377 }
378
379 if (i)
380 printf("FAIL (depth = %d)\n", i);
381 else
382 puts("PASS");
383
384 /*
385 * Delete the arrays...
386 */
387
388 cupsArrayDelete(array);
389 cupsArrayDelete(dup_array);
390
391 /*
392 * Test the array with string functions...
393 */
394
395 fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout);
396 array = _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' ');
397 if (!array)
398 {
399 status = 1;
400 puts("FAIL (unable to create array)");
401 }
402 else if (cupsArrayCount(array) != 4)
403 {
404 status = 1;
405 printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array));
406 }
407 else if (strcmp(text = (char *)cupsArrayFirst(array), "bar"))
408 {
409 status = 1;
410 printf("FAIL (first element \"%s\", expected \"bar\")\n", text);
411 }
412 else if (strcmp(text = (char *)cupsArrayNext(array), "boo"))
413 {
414 status = 1;
415 printf("FAIL (first element \"%s\", expected \"boo\")\n", text);
416 }
417 else if (strcmp(text = (char *)cupsArrayNext(array), "far"))
418 {
419 status = 1;
420 printf("FAIL (first element \"%s\", expected \"far\")\n", text);
421 }
422 else if (strcmp(text = (char *)cupsArrayNext(array), "foo"))
423 {
424 status = 1;
425 printf("FAIL (first element \"%s\", expected \"foo\")\n", text);
426 }
427 else
428 puts("PASS");
429
430 fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout);
431 _cupsArrayAddStrings(array, "foo2,bar2", ',');
432
433 if (cupsArrayCount(array) != 6)
434 {
435 status = 1;
436 printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array));
437 }
438 else if (strcmp(text = (char *)cupsArrayFirst(array), "bar"))
439 {
440 status = 1;
441 printf("FAIL (first element \"%s\", expected \"bar\")\n", text);
442 }
443 else if (strcmp(text = (char *)cupsArrayNext(array), "bar2"))
444 {
445 status = 1;
446 printf("FAIL (first element \"%s\", expected \"bar2\")\n", text);
447 }
448 else if (strcmp(text = (char *)cupsArrayNext(array), "boo"))
449 {
450 status = 1;
451 printf("FAIL (first element \"%s\", expected \"boo\")\n", text);
452 }
453 else if (strcmp(text = (char *)cupsArrayNext(array), "far"))
454 {
455 status = 1;
456 printf("FAIL (first element \"%s\", expected \"far\")\n", text);
457 }
458 else if (strcmp(text = (char *)cupsArrayNext(array), "foo"))
459 {
460 status = 1;
461 printf("FAIL (first element \"%s\", expected \"foo\")\n", text);
462 }
463 else if (strcmp(text = (char *)cupsArrayNext(array), "foo2"))
464 {
465 status = 1;
466 printf("FAIL (first element \"%s\", expected \"foo2\")\n", text);
467 }
468 else
469 puts("PASS");
470
471 cupsArrayDelete(array);
472
473 /*
474 * Summarize the results and return...
475 */
476
477 if (!status)
478 puts("\nALL TESTS PASSED!");
479 else
480 printf("\n%d TEST(S) FAILED!\n", status);
481
482 return (status);
483 }
484
485
486 /*
487 * 'get_seconds()' - Get the current time in seconds...
488 */
489
490 #ifdef _WIN32
491 # include <windows.h>
492
493
494 static double
495 get_seconds(void)
496 {
497 }
498 #else
499 # include <sys/time.h>
500
501
502 static double
503 get_seconds(void)
504 {
505 struct timeval curtime; /* Current time */
506
507
508 gettimeofday(&curtime, NULL);
509 return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
510 }
511 #endif /* _WIN32 */
512
513
514 /*
515 * 'load_words()' - Load words from a file.
516 */
517
518 static int /* O - 1 on success, 0 on failure */
519 load_words(const char *filename, /* I - File to load */
520 cups_array_t *array) /* I - Array to add to */
521 {
522 FILE *fp; /* Test file */
523 char word[256]; /* Word from file */
524
525
526 if ((fp = fopen(filename, "r")) == NULL)
527 {
528 perror(filename);
529 return (0);
530 }
531
532 while (fscanf(fp, "%255s", word) == 1)
533 {
534 if (!cupsArrayFind(array, word))
535 cupsArrayAdd(array, strdup(word));
536 }
537
538 fclose(fp);
539
540 return (1);
541 }