]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7e86f2f6 | 2 | * Array test program for CUPS. |
ef416fc2 | 3 | * |
7e86f2f6 MS |
4 | * Copyright 2007-2014 by Apple Inc. |
5 | * Copyright 1997-2006 by Easy Software Products. | |
ef416fc2 | 6 | * |
e3101897 | 7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ef416fc2 | 8 | */ |
9 | ||
10 | /* | |
11 | * Include necessary headers... | |
12 | */ | |
13 | ||
71e16022 MS |
14 | #include "string-private.h" |
15 | #include "debug-private.h" | |
a469f8a5 | 16 | #include "array-private.h" |
ef416fc2 | 17 | #include "dir.h" |
ef416fc2 | 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 */ | |
7e86f2f6 | 33 | main(void) |
ef416fc2 | 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 */ | |
e1d6a774 | 46 | void *data; /* User data for arrays */ |
ef416fc2 | 47 | |
48 | ||
49 | /* | |
50 | * No errors so far... | |
51 | */ | |
52 | ||
53 | status = 0; | |
54 | ||
55 | /* | |
56 | * cupsArrayNew() | |
57 | */ | |
58 | ||
59 | fputs("cupsArrayNew: ", stdout); | |
60 | ||
e1d6a774 | 61 | data = (void *)"testarray"; |
62 | array = cupsArrayNew((cups_array_func_t)strcmp, data); | |
ef416fc2 | 63 | |
64 | if (array) | |
65 | puts("PASS"); | |
66 | else | |
67 | { | |
68 | puts("FAIL (returned NULL, expected pointer)"); | |
69 | status ++; | |
70 | } | |
71 | ||
e1d6a774 | 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 | ||
ef416fc2 | 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 | { | |
ef416fc2 | 99 | if (!cupsArrayAdd(array, strdup("Two Fish"))) |
100 | { | |
101 | puts("FAIL (\"Two Fish\")"); | |
102 | status ++; | |
103 | } | |
104 | else | |
105 | { | |
ef416fc2 | 106 | if (!cupsArrayAdd(array, strdup("Red Fish"))) |
107 | { | |
108 | puts("FAIL (\"Red Fish\")"); | |
109 | status ++; | |
110 | } | |
111 | else | |
112 | { | |
ef416fc2 | 113 | if (!cupsArrayAdd(array, strdup("Blue Fish"))) |
114 | { | |
115 | puts("FAIL (\"Blue Fish\")"); | |
116 | status ++; | |
117 | } | |
118 | else | |
ef416fc2 | 119 | puts("PASS"); |
ef416fc2 | 120 | } |
121 | } | |
122 | } | |
123 | ||
124 | /* | |
125 | * cupsArrayCount() | |
126 | */ | |
5a9febac | 127 | |
ef416fc2 | 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", | |
232 | dup_array, cupsArrayCount(dup_array)); | |
233 | status ++; | |
234 | } | |
235 | ||
236 | /* | |
237 | * cupsArrayRemove() | |
238 | */ | |
239 | ||
240 | fputs("cupsArrayRemove: ", stdout); | |
241 | if (cupsArrayRemove(array, (void *)"One Fish") && | |
242 | cupsArrayCount(array) == 3) | |
243 | puts("PASS"); | |
244 | else | |
245 | { | |
246 | printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n", | |
247 | cupsArrayCount(array)); | |
248 | status ++; | |
249 | } | |
250 | ||
251 | /* | |
252 | * cupsArrayClear() | |
253 | */ | |
254 | ||
255 | fputs("cupsArrayClear: ", stdout); | |
256 | cupsArrayClear(array); | |
257 | if (cupsArrayCount(array) == 0) | |
258 | puts("PASS"); | |
259 | else | |
260 | { | |
261 | printf("FAIL (%d elements, expected 0 elements)\n", | |
262 | cupsArrayCount(array)); | |
263 | status ++; | |
264 | } | |
265 | ||
266 | /* | |
267 | * Now load this source file and grab all of the unique words... | |
268 | */ | |
269 | ||
270 | fputs("Load unique words: ", stdout); | |
271 | fflush(stdout); | |
272 | ||
273 | start = get_seconds(); | |
274 | ||
275 | if ((dir = cupsDirOpen(".")) == NULL) | |
276 | { | |
277 | puts("FAIL (cupsDirOpen failed)"); | |
278 | status ++; | |
279 | } | |
280 | else | |
281 | { | |
282 | while ((dent = cupsDirRead(dir)) != NULL) | |
283 | { | |
7e86f2f6 | 284 | i = (int)strlen(dent->filename) - 2; |
ef416fc2 | 285 | |
286 | if (i > 0 && dent->filename[i] == '.' && | |
287 | (dent->filename[i + 1] == 'c' || | |
288 | dent->filename[i + 1] == 'h')) | |
289 | load_words(dent->filename, array); | |
290 | } | |
291 | ||
292 | cupsDirClose(dir); | |
293 | ||
294 | end = get_seconds(); | |
295 | ||
296 | printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array), | |
297 | end - start, cupsArrayCount(array) / (end - start)); | |
298 | fflush(stdout); | |
299 | ||
300 | for (text = (char *)cupsArrayFirst(array); text;) | |
301 | { | |
302 | /* | |
303 | * Copy this word to the word buffer (safe because we strdup'd from | |
304 | * the same buffer in the first place... :) | |
305 | */ | |
306 | ||
5a9febac | 307 | strlcpy(word, text, sizeof(word)); |
ef416fc2 | 308 | |
309 | /* | |
310 | * Grab the next word and compare... | |
311 | */ | |
312 | ||
313 | if ((text = (char *)cupsArrayNext(array)) == NULL) | |
314 | break; | |
315 | ||
316 | if (strcmp(word, text) >= 0) | |
317 | break; | |
318 | } | |
319 | ||
320 | if (text) | |
321 | { | |
322 | printf("FAIL (\"%s\" >= \"%s\"!)\n", word, text); | |
323 | status ++; | |
324 | } | |
325 | else | |
326 | puts("PASS"); | |
327 | } | |
328 | ||
329 | /* | |
330 | * Test deleting with iteration... | |
331 | */ | |
332 | ||
333 | fputs("Delete While Iterating: ", stdout); | |
334 | ||
335 | text = (char *)cupsArrayFirst(array); | |
336 | cupsArrayRemove(array, text); | |
337 | free(text); | |
338 | ||
339 | text = (char *)cupsArrayNext(array); | |
340 | if (!text) | |
341 | { | |
342 | puts("FAIL (cupsArrayNext returned NULL!)"); | |
343 | status ++; | |
344 | } | |
345 | else | |
346 | puts("PASS"); | |
347 | ||
348 | /* | |
349 | * Test save/restore... | |
350 | */ | |
351 | ||
352 | fputs("cupsArraySave: ", stdout); | |
353 | ||
354 | for (i = 0, text = (char *)cupsArrayFirst(array); | |
355 | i < 32; | |
356 | i ++, text = (char *)cupsArrayNext(array)) | |
357 | { | |
358 | saved[i] = text; | |
359 | ||
360 | if (!cupsArraySave(array)) | |
361 | break; | |
362 | } | |
363 | ||
364 | if (i < 32) | |
365 | printf("FAIL (depth = %d)\n", i); | |
366 | else | |
367 | puts("PASS"); | |
368 | ||
369 | fputs("cupsArrayRestore: ", stdout); | |
370 | ||
371 | while (i > 0) | |
372 | { | |
373 | i --; | |
374 | ||
375 | text = cupsArrayRestore(array); | |
376 | if (text != saved[i]) | |
377 | break; | |
378 | } | |
379 | ||
380 | if (i) | |
381 | printf("FAIL (depth = %d)\n", i); | |
382 | else | |
383 | puts("PASS"); | |
384 | ||
385 | /* | |
386 | * Delete the arrays... | |
387 | */ | |
388 | ||
389 | cupsArrayDelete(array); | |
390 | cupsArrayDelete(dup_array); | |
391 | ||
a469f8a5 MS |
392 | /* |
393 | * Test the array with string functions... | |
394 | */ | |
395 | ||
396 | fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout); | |
397 | array = _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' '); | |
398 | if (!array) | |
399 | { | |
400 | status = 1; | |
401 | puts("FAIL (unable to create array)"); | |
402 | } | |
403 | else if (cupsArrayCount(array) != 4) | |
404 | { | |
405 | status = 1; | |
406 | printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array)); | |
407 | } | |
408 | else if (strcmp(text = (char *)cupsArrayFirst(array), "bar")) | |
409 | { | |
410 | status = 1; | |
411 | printf("FAIL (first element \"%s\", expected \"bar\")\n", text); | |
412 | } | |
413 | else if (strcmp(text = (char *)cupsArrayNext(array), "boo")) | |
414 | { | |
415 | status = 1; | |
416 | printf("FAIL (first element \"%s\", expected \"boo\")\n", text); | |
417 | } | |
418 | else if (strcmp(text = (char *)cupsArrayNext(array), "far")) | |
419 | { | |
420 | status = 1; | |
421 | printf("FAIL (first element \"%s\", expected \"far\")\n", text); | |
422 | } | |
423 | else if (strcmp(text = (char *)cupsArrayNext(array), "foo")) | |
424 | { | |
425 | status = 1; | |
426 | printf("FAIL (first element \"%s\", expected \"foo\")\n", text); | |
427 | } | |
428 | else | |
429 | puts("PASS"); | |
430 | ||
431 | fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout); | |
432 | _cupsArrayAddStrings(array, "foo2,bar2", ','); | |
433 | ||
434 | if (cupsArrayCount(array) != 6) | |
435 | { | |
436 | status = 1; | |
437 | printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array)); | |
438 | } | |
439 | else if (strcmp(text = (char *)cupsArrayFirst(array), "bar")) | |
440 | { | |
441 | status = 1; | |
442 | printf("FAIL (first element \"%s\", expected \"bar\")\n", text); | |
443 | } | |
444 | else if (strcmp(text = (char *)cupsArrayNext(array), "bar2")) | |
445 | { | |
446 | status = 1; | |
447 | printf("FAIL (first element \"%s\", expected \"bar2\")\n", text); | |
448 | } | |
449 | else if (strcmp(text = (char *)cupsArrayNext(array), "boo")) | |
450 | { | |
451 | status = 1; | |
452 | printf("FAIL (first element \"%s\", expected \"boo\")\n", text); | |
453 | } | |
454 | else if (strcmp(text = (char *)cupsArrayNext(array), "far")) | |
455 | { | |
456 | status = 1; | |
457 | printf("FAIL (first element \"%s\", expected \"far\")\n", text); | |
458 | } | |
459 | else if (strcmp(text = (char *)cupsArrayNext(array), "foo")) | |
460 | { | |
461 | status = 1; | |
462 | printf("FAIL (first element \"%s\", expected \"foo\")\n", text); | |
463 | } | |
464 | else if (strcmp(text = (char *)cupsArrayNext(array), "foo2")) | |
465 | { | |
466 | status = 1; | |
467 | printf("FAIL (first element \"%s\", expected \"foo2\")\n", text); | |
468 | } | |
469 | else | |
470 | puts("PASS"); | |
471 | ||
472 | cupsArrayDelete(array); | |
473 | ||
ef416fc2 | 474 | /* |
475 | * Summarize the results and return... | |
476 | */ | |
477 | ||
478 | if (!status) | |
479 | puts("\nALL TESTS PASSED!"); | |
480 | else | |
481 | printf("\n%d TEST(S) FAILED!\n", status); | |
482 | ||
483 | return (status); | |
484 | } | |
485 | ||
486 | ||
487 | /* | |
488 | * 'get_seconds()' - Get the current time in seconds... | |
489 | */ | |
490 | ||
491 | #ifdef WIN32 | |
492 | # include <windows.h> | |
493 | ||
494 | ||
495 | static double | |
496 | get_seconds(void) | |
497 | { | |
498 | } | |
499 | #else | |
500 | # include <sys/time.h> | |
501 | ||
502 | ||
503 | static double | |
504 | get_seconds(void) | |
505 | { | |
506 | struct timeval curtime; /* Current time */ | |
507 | ||
508 | ||
509 | gettimeofday(&curtime, NULL); | |
510 | return (curtime.tv_sec + 0.000001 * curtime.tv_usec); | |
511 | } | |
512 | #endif /* WIN32 */ | |
513 | ||
514 | ||
515 | /* | |
516 | * 'load_words()' - Load words from a file. | |
517 | */ | |
518 | ||
519 | static int /* O - 1 on success, 0 on failure */ | |
520 | load_words(const char *filename, /* I - File to load */ | |
521 | cups_array_t *array) /* I - Array to add to */ | |
522 | { | |
523 | FILE *fp; /* Test file */ | |
524 | char word[256]; /* Word from file */ | |
525 | ||
526 | ||
ef416fc2 | 527 | if ((fp = fopen(filename, "r")) == NULL) |
528 | { | |
529 | perror(filename); | |
530 | return (0); | |
531 | } | |
532 | ||
533 | while (fscanf(fp, "%255s", word) == 1) | |
534 | { | |
535 | if (!cupsArrayFind(array, word)) | |
ef416fc2 | 536 | cupsArrayAdd(array, strdup(word)); |
ef416fc2 | 537 | } |
538 | ||
539 | fclose(fp); | |
540 | ||
541 | return (1); | |
542 | } |