]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testarray.c
2 * Array test program for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "string-private.h"
21 #include "debug-private.h"
22 #include "array-private.h"
30 static double get_seconds(void);
31 static int load_words(const char *filename
, cups_array_t
*array
);
35 * 'main()' - Main entry.
38 int /* O - Exit status */
41 int i
; /* Looping var */
42 cups_array_t
*array
, /* Test array */
43 *dup_array
; /* Duplicate array */
44 int status
; /* Exit status */
45 char *text
; /* Text from array */
46 char word
[256]; /* Word from file */
47 double start
, /* Start time */
49 cups_dir_t
*dir
; /* Current directory */
50 cups_dentry_t
*dent
; /* Directory entry */
51 char *saved
[32]; /* Saved entries */
52 void *data
; /* User data for arrays */
65 fputs("cupsArrayNew: ", stdout
);
67 data
= (void *)"testarray";
68 array
= cupsArrayNew((cups_array_func_t
)strcmp
, data
);
74 puts("FAIL (returned NULL, expected pointer)");
82 fputs("cupsArrayUserData: ", stdout
);
83 if (cupsArrayUserData(array
) == data
)
87 printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array
),
96 fputs("cupsArrayAdd: ", stdout
);
98 if (!cupsArrayAdd(array
, strdup("One Fish")))
100 puts("FAIL (\"One Fish\")");
105 if (!cupsArrayAdd(array
, strdup("Two Fish")))
107 puts("FAIL (\"Two Fish\")");
112 if (!cupsArrayAdd(array
, strdup("Red Fish")))
114 puts("FAIL (\"Red Fish\")");
119 if (!cupsArrayAdd(array
, strdup("Blue Fish")))
121 puts("FAIL (\"Blue Fish\")");
134 fputs("cupsArrayCount: ", stdout
);
135 if (cupsArrayCount(array
) == 4)
139 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array
));
147 fputs("cupsArrayFirst: ", stdout
);
148 if ((text
= (char *)cupsArrayFirst(array
)) != NULL
&&
149 !strcmp(text
, "Blue Fish"))
153 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text
);
161 fputs("cupsArrayNext: ", stdout
);
162 if ((text
= (char *)cupsArrayNext(array
)) != NULL
&&
163 !strcmp(text
, "One Fish"))
167 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
175 fputs("cupsArrayLast: ", stdout
);
176 if ((text
= (char *)cupsArrayLast(array
)) != NULL
&&
177 !strcmp(text
, "Two Fish"))
181 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text
);
189 fputs("cupsArrayPrev: ", stdout
);
190 if ((text
= (char *)cupsArrayPrev(array
)) != NULL
&&
191 !strcmp(text
, "Red Fish"))
195 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text
);
203 fputs("cupsArrayFind: ", stdout
);
204 if ((text
= (char *)cupsArrayFind(array
, (void *)"One Fish")) != NULL
&&
205 !strcmp(text
, "One Fish"))
209 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
217 fputs("cupsArrayCurrent: ", stdout
);
218 if ((text
= (char *)cupsArrayCurrent(array
)) != NULL
&&
219 !strcmp(text
, "One Fish"))
223 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
231 fputs("cupsArrayDup: ", stdout
);
232 if ((dup_array
= cupsArrayDup(array
)) != NULL
&&
233 cupsArrayCount(dup_array
) == 4)
237 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
238 dup_array
, cupsArrayCount(dup_array
));
246 fputs("cupsArrayRemove: ", stdout
);
247 if (cupsArrayRemove(array
, (void *)"One Fish") &&
248 cupsArrayCount(array
) == 3)
252 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
253 cupsArrayCount(array
));
261 fputs("cupsArrayClear: ", stdout
);
262 cupsArrayClear(array
);
263 if (cupsArrayCount(array
) == 0)
267 printf("FAIL (%d elements, expected 0 elements)\n",
268 cupsArrayCount(array
));
273 * Now load this source file and grab all of the unique words...
276 fputs("Load unique words: ", stdout
);
279 start
= get_seconds();
281 if ((dir
= cupsDirOpen(".")) == NULL
)
283 puts("FAIL (cupsDirOpen failed)");
288 while ((dent
= cupsDirRead(dir
)) != NULL
)
290 i
= (int)strlen(dent
->filename
) - 2;
292 if (i
> 0 && dent
->filename
[i
] == '.' &&
293 (dent
->filename
[i
+ 1] == 'c' ||
294 dent
->filename
[i
+ 1] == 'h'))
295 load_words(dent
->filename
, array
);
302 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array
),
303 end
- start
, cupsArrayCount(array
) / (end
- start
));
306 for (text
= (char *)cupsArrayFirst(array
); text
;)
309 * Copy this word to the word buffer (safe because we strdup'd from
310 * the same buffer in the first place... :)
313 strlcpy(word
, text
, sizeof(word
));
316 * Grab the next word and compare...
319 if ((text
= (char *)cupsArrayNext(array
)) == NULL
)
322 if (strcmp(word
, text
) >= 0)
328 printf("FAIL (\"%s\" >= \"%s\"!)\n", word
, text
);
336 * Test deleting with iteration...
339 fputs("Delete While Iterating: ", stdout
);
341 text
= (char *)cupsArrayFirst(array
);
342 cupsArrayRemove(array
, text
);
345 text
= (char *)cupsArrayNext(array
);
348 puts("FAIL (cupsArrayNext returned NULL!)");
355 * Test save/restore...
358 fputs("cupsArraySave: ", stdout
);
360 for (i
= 0, text
= (char *)cupsArrayFirst(array
);
362 i
++, text
= (char *)cupsArrayNext(array
))
366 if (!cupsArraySave(array
))
371 printf("FAIL (depth = %d)\n", i
);
375 fputs("cupsArrayRestore: ", stdout
);
381 text
= cupsArrayRestore(array
);
382 if (text
!= saved
[i
])
387 printf("FAIL (depth = %d)\n", i
);
392 * Delete the arrays...
395 cupsArrayDelete(array
);
396 cupsArrayDelete(dup_array
);
399 * Test the array with string functions...
402 fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout
);
403 array
= _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' ');
407 puts("FAIL (unable to create array)");
409 else if (cupsArrayCount(array
) != 4)
412 printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array
));
414 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
417 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
419 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
422 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
424 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
427 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
429 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
432 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
437 fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout
);
438 _cupsArrayAddStrings(array
, "foo2,bar2", ',');
440 if (cupsArrayCount(array
) != 6)
443 printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array
));
445 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
448 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
450 else if (strcmp(text
= (char *)cupsArrayNext(array
), "bar2"))
453 printf("FAIL (first element \"%s\", expected \"bar2\")\n", text
);
455 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
458 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
460 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
463 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
465 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
468 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
470 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo2"))
473 printf("FAIL (first element \"%s\", expected \"foo2\")\n", text
);
478 cupsArrayDelete(array
);
481 * Summarize the results and return...
485 puts("\nALL TESTS PASSED!");
487 printf("\n%d TEST(S) FAILED!\n", status
);
494 * 'get_seconds()' - Get the current time in seconds...
498 # include <windows.h>
506 # include <sys/time.h>
512 struct timeval curtime
; /* Current time */
515 gettimeofday(&curtime
, NULL
);
516 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
522 * 'load_words()' - Load words from a file.
525 static int /* O - 1 on success, 0 on failure */
526 load_words(const char *filename
, /* I - File to load */
527 cups_array_t
*array
) /* I - Array to add to */
529 FILE *fp
; /* Test file */
530 char word
[256]; /* Word from file */
533 if ((fp
= fopen(filename
, "r")) == NULL
)
539 while (fscanf(fp
, "%255s", word
) == 1)
541 if (!cupsArrayFind(array
, word
))
542 cupsArrayAdd(array
, strdup(word
));