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