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