]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testarray.c
2 * "$Id: testarray.c 6649 2007-07-11 21:46:42Z mike $"
4 * Array test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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...
30 #include <cups/string.h>
41 static double get_seconds(void);
42 static int load_words(const char *filename
, cups_array_t
*array
);
46 * 'main()' - Main entry.
49 int /* O - Exit status */
50 main(int argc
, /* I - Number of command-line arguments */
51 char *argv
[]) /* I - Command-line arguments */
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 */
61 cups_dir_t
*dir
; /* Current directory */
62 cups_dentry_t
*dent
; /* Directory entry */
63 char *saved
[32]; /* Saved entries */
64 void *data
; /* User data for arrays */
77 fputs("cupsArrayNew: ", stdout
);
79 data
= (void *)"testarray";
80 array
= cupsArrayNew((cups_array_func_t
)strcmp
, data
);
86 puts("FAIL (returned NULL, expected pointer)");
94 fputs("cupsArrayUserData: ", stdout
);
95 if (cupsArrayUserData(array
) == data
)
99 printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array
),
108 fputs("cupsArrayAdd: ", stdout
);
110 if (!cupsArrayAdd(array
, strdup("One Fish")))
112 puts("FAIL (\"One Fish\")");
119 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
121 text
= (char *)cupsArrayNext(array
), i
++)
122 printf(" #1 array[%d]=\"%s\"\n", i
, text
);
125 if (!cupsArrayAdd(array
, strdup("Two Fish")))
127 puts("FAIL (\"Two Fish\")");
133 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
135 text
= (char *)cupsArrayNext(array
), i
++)
136 printf(" #2 array[%d]=\"%s\"\n", i
, text
);
139 if (!cupsArrayAdd(array
, strdup("Red Fish")))
141 puts("FAIL (\"Red Fish\")");
147 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
149 text
= (char *)cupsArrayNext(array
), i
++)
150 printf(" #3 array[%d]=\"%s\"\n", i
, text
);
153 if (!cupsArrayAdd(array
, strdup("Blue Fish")))
155 puts("FAIL (\"Blue Fish\")");
161 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
163 text
= (char *)cupsArrayNext(array
), i
++)
164 printf(" #4 array[%d]=\"%s\"\n", i
, text
);
177 fputs("cupsArrayCount: ", stdout
);
178 if (cupsArrayCount(array
) == 4)
182 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array
));
190 fputs("cupsArrayFirst: ", stdout
);
191 if ((text
= (char *)cupsArrayFirst(array
)) != NULL
&&
192 !strcmp(text
, "Blue Fish"))
196 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text
);
204 fputs("cupsArrayNext: ", stdout
);
205 if ((text
= (char *)cupsArrayNext(array
)) != NULL
&&
206 !strcmp(text
, "One Fish"))
210 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
218 fputs("cupsArrayLast: ", stdout
);
219 if ((text
= (char *)cupsArrayLast(array
)) != NULL
&&
220 !strcmp(text
, "Two Fish"))
224 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text
);
232 fputs("cupsArrayPrev: ", stdout
);
233 if ((text
= (char *)cupsArrayPrev(array
)) != NULL
&&
234 !strcmp(text
, "Red Fish"))
238 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text
);
246 fputs("cupsArrayFind: ", stdout
);
247 if ((text
= (char *)cupsArrayFind(array
, (void *)"One Fish")) != NULL
&&
248 !strcmp(text
, "One Fish"))
252 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
260 fputs("cupsArrayCurrent: ", stdout
);
261 if ((text
= (char *)cupsArrayCurrent(array
)) != NULL
&&
262 !strcmp(text
, "One Fish"))
266 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
274 fputs("cupsArrayDup: ", stdout
);
275 if ((dup_array
= cupsArrayDup(array
)) != NULL
&&
276 cupsArrayCount(dup_array
) == 4)
280 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
281 dup_array
, cupsArrayCount(dup_array
));
289 fputs("cupsArrayRemove: ", stdout
);
290 if (cupsArrayRemove(array
, (void *)"One Fish") &&
291 cupsArrayCount(array
) == 3)
295 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
296 cupsArrayCount(array
));
304 fputs("cupsArrayClear: ", stdout
);
305 cupsArrayClear(array
);
306 if (cupsArrayCount(array
) == 0)
310 printf("FAIL (%d elements, expected 0 elements)\n",
311 cupsArrayCount(array
));
316 * Now load this source file and grab all of the unique words...
319 fputs("Load unique words: ", stdout
);
322 start
= get_seconds();
324 if ((dir
= cupsDirOpen(".")) == NULL
)
326 puts("FAIL (cupsDirOpen failed)");
331 while ((dent
= cupsDirRead(dir
)) != NULL
)
333 i
= strlen(dent
->filename
) - 2;
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
);
345 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array
),
346 end
- start
, cupsArrayCount(array
) / (end
- start
));
349 for (text
= (char *)cupsArrayFirst(array
); text
;)
352 * Copy this word to the word buffer (safe because we strdup'd from
353 * the same buffer in the first place... :)
359 * Grab the next word and compare...
362 if ((text
= (char *)cupsArrayNext(array
)) == NULL
)
365 if (strcmp(word
, text
) >= 0)
371 printf("FAIL (\"%s\" >= \"%s\"!)\n", word
, text
);
379 * Test deleting with iteration...
382 fputs("Delete While Iterating: ", stdout
);
384 text
= (char *)cupsArrayFirst(array
);
385 cupsArrayRemove(array
, text
);
388 text
= (char *)cupsArrayNext(array
);
391 puts("FAIL (cupsArrayNext returned NULL!)");
398 * Test save/restore...
401 fputs("cupsArraySave: ", stdout
);
403 for (i
= 0, text
= (char *)cupsArrayFirst(array
);
405 i
++, text
= (char *)cupsArrayNext(array
))
409 if (!cupsArraySave(array
))
414 printf("FAIL (depth = %d)\n", i
);
418 fputs("cupsArrayRestore: ", stdout
);
424 text
= cupsArrayRestore(array
);
425 if (text
!= saved
[i
])
430 printf("FAIL (depth = %d)\n", i
);
435 * Delete the arrays...
438 cupsArrayDelete(array
);
439 cupsArrayDelete(dup_array
);
442 * Summarize the results and return...
446 puts("\nALL TESTS PASSED!");
448 printf("\n%d TEST(S) FAILED!\n", status
);
455 * 'get_seconds()' - Get the current time in seconds...
459 # include <windows.h>
467 # include <sys/time.h>
473 struct timeval curtime
; /* Current time */
476 gettimeofday(&curtime
, NULL
);
477 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
483 * 'load_words()' - Load words from a file.
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 */
490 FILE *fp
; /* Test file */
491 char word
[256]; /* Word from file */
494 DEBUG_printf((" Loading \"%s\"...\n", filename
));
496 if ((fp
= fopen(filename
, "r")) == NULL
)
502 while (fscanf(fp
, "%255s", word
) == 1)
504 if (!cupsArrayFind(array
, word
))
506 DEBUG_printf((" Adding \"%s\"...\n", word
));
508 cupsArrayAdd(array
, strdup(word
));
519 * End of "$Id: testarray.c 6649 2007-07-11 21:46:42Z mike $".