2 * "$Id: testarray.c 9099 2010-04-11 07:16:05Z mike $"
4 * Array test program for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry.
20 * get_seconds() - Get the current time in seconds...
21 * load_words() - Load words from a file.
25 * Include necessary headers...
28 #include "string-private.h"
29 #include "debug-private.h"
38 static double get_seconds(void);
39 static int load_words(const char *filename, cups_array_t *array);
43 * 'main()' - Main entry.
46 int /* O - Exit status */
47 main(int argc, /* I - Number of command-line arguments */
48 char *argv[]) /* I - Command-line arguments */
50 int i; /* Looping var */
51 cups_array_t *array, /* Test array */
52 *dup_array; /* Duplicate array */
53 int status; /* Exit status */
54 char *text; /* Text from array */
55 char word[256]; /* Word from file */
56 double start, /* Start time */
58 cups_dir_t *dir; /* Current directory */
59 cups_dentry_t *dent; /* Directory entry */
60 char *saved[32]; /* Saved entries */
61 void *data; /* User data for arrays */
74 fputs("cupsArrayNew: ", stdout);
76 data = (void *)"testarray";
77 array = cupsArrayNew((cups_array_func_t)strcmp, data);
83 puts("FAIL (returned NULL, expected pointer)");
91 fputs("cupsArrayUserData: ", stdout);
92 if (cupsArrayUserData(array) == data)
96 printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array),
105 fputs("cupsArrayAdd: ", stdout);
107 if (!cupsArrayAdd(array, strdup("One Fish")))
109 puts("FAIL (\"One Fish\")");
114 if (!cupsArrayAdd(array, strdup("Two Fish")))
116 puts("FAIL (\"Two Fish\")");
121 if (!cupsArrayAdd(array, strdup("Red Fish")))
123 puts("FAIL (\"Red Fish\")");
128 if (!cupsArrayAdd(array, strdup("Blue Fish")))
130 puts("FAIL (\"Blue Fish\")");
143 fputs("cupsArrayCount: ", stdout);
144 if (cupsArrayCount(array) == 4)
148 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array));
156 fputs("cupsArrayFirst: ", stdout);
157 if ((text = (char *)cupsArrayFirst(array)) != NULL &&
158 !strcmp(text, "Blue Fish"))
162 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text);
170 fputs("cupsArrayNext: ", stdout);
171 if ((text = (char *)cupsArrayNext(array)) != NULL &&
172 !strcmp(text, "One Fish"))
176 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
184 fputs("cupsArrayLast: ", stdout);
185 if ((text = (char *)cupsArrayLast(array)) != NULL &&
186 !strcmp(text, "Two Fish"))
190 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text);
198 fputs("cupsArrayPrev: ", stdout);
199 if ((text = (char *)cupsArrayPrev(array)) != NULL &&
200 !strcmp(text, "Red Fish"))
204 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text);
212 fputs("cupsArrayFind: ", stdout);
213 if ((text = (char *)cupsArrayFind(array, (void *)"One Fish")) != NULL &&
214 !strcmp(text, "One Fish"))
218 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
226 fputs("cupsArrayCurrent: ", stdout);
227 if ((text = (char *)cupsArrayCurrent(array)) != NULL &&
228 !strcmp(text, "One Fish"))
232 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
240 fputs("cupsArrayDup: ", stdout);
241 if ((dup_array = cupsArrayDup(array)) != NULL &&
242 cupsArrayCount(dup_array) == 4)
246 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
247 dup_array, cupsArrayCount(dup_array));
255 fputs("cupsArrayRemove: ", stdout);
256 if (cupsArrayRemove(array, (void *)"One Fish") &&
257 cupsArrayCount(array) == 3)
261 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
262 cupsArrayCount(array));
270 fputs("cupsArrayClear: ", stdout);
271 cupsArrayClear(array);
272 if (cupsArrayCount(array) == 0)
276 printf("FAIL (%d elements, expected 0 elements)\n",
277 cupsArrayCount(array));
282 * Now load this source file and grab all of the unique words...
285 fputs("Load unique words: ", stdout);
288 start = get_seconds();
290 if ((dir = cupsDirOpen(".")) == NULL)
292 puts("FAIL (cupsDirOpen failed)");
297 while ((dent = cupsDirRead(dir)) != NULL)
299 i = strlen(dent->filename) - 2;
301 if (i > 0 && dent->filename[i] == '.' &&
302 (dent->filename[i + 1] == 'c' ||
303 dent->filename[i + 1] == 'h'))
304 load_words(dent->filename, array);
311 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array),
312 end - start, cupsArrayCount(array) / (end - start));
315 for (text = (char *)cupsArrayFirst(array); text;)
318 * Copy this word to the word buffer (safe because we strdup'd from
319 * the same buffer in the first place... :)
325 * Grab the next word and compare...
328 if ((text = (char *)cupsArrayNext(array)) == NULL)
331 if (strcmp(word, text) >= 0)
337 printf("FAIL (\"%s\" >= \"%s\"!)\n", word, text);
345 * Test deleting with iteration...
348 fputs("Delete While Iterating: ", stdout);
350 text = (char *)cupsArrayFirst(array);
351 cupsArrayRemove(array, text);
354 text = (char *)cupsArrayNext(array);
357 puts("FAIL (cupsArrayNext returned NULL!)");
364 * Test save/restore...
367 fputs("cupsArraySave: ", stdout);
369 for (i = 0, text = (char *)cupsArrayFirst(array);
371 i ++, text = (char *)cupsArrayNext(array))
375 if (!cupsArraySave(array))
380 printf("FAIL (depth = %d)\n", i);
384 fputs("cupsArrayRestore: ", stdout);
390 text = cupsArrayRestore(array);
391 if (text != saved[i])
396 printf("FAIL (depth = %d)\n", i);
401 * Delete the arrays...
404 cupsArrayDelete(array);
405 cupsArrayDelete(dup_array);
408 * Summarize the results and return...
412 puts("\nALL TESTS PASSED!");
414 printf("\n%d TEST(S) FAILED!\n", status);
421 * 'get_seconds()' - Get the current time in seconds...
425 # include <windows.h>
433 # include <sys/time.h>
439 struct timeval curtime; /* Current time */
442 gettimeofday(&curtime, NULL);
443 return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
449 * 'load_words()' - Load words from a file.
452 static int /* O - 1 on success, 0 on failure */
453 load_words(const char *filename, /* I - File to load */
454 cups_array_t *array) /* I - Array to add to */
456 FILE *fp; /* Test file */
457 char word[256]; /* Word from file */
460 if ((fp = fopen(filename, "r")) == NULL)
466 while (fscanf(fp, "%255s", word) == 1)
468 if (!cupsArrayFind(array, word))
469 cupsArrayAdd(array, strdup(word));
479 * End of "$Id: testarray.c 9099 2010-04-11 07:16:05Z mike $".