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