]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testarray.c
4 * Array test program for CUPS.
6 * Copyright 2007-2012 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"
30 #include "array-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... :)
322 strlcpy(word
, text
, sizeof(word
));
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 * Test the array with string functions...
411 fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout
);
412 array
= _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' ');
416 puts("FAIL (unable to create array)");
418 else if (cupsArrayCount(array
) != 4)
421 printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array
));
423 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
426 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
428 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
431 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
433 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
436 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
438 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
441 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
446 fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout
);
447 _cupsArrayAddStrings(array
, "foo2,bar2", ',');
449 if (cupsArrayCount(array
) != 6)
452 printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array
));
454 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
457 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
459 else if (strcmp(text
= (char *)cupsArrayNext(array
), "bar2"))
462 printf("FAIL (first element \"%s\", expected \"bar2\")\n", text
);
464 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
467 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
469 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
472 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
474 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
477 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
479 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo2"))
482 printf("FAIL (first element \"%s\", expected \"foo2\")\n", text
);
487 cupsArrayDelete(array
);
490 * Summarize the results and return...
494 puts("\nALL TESTS PASSED!");
496 printf("\n%d TEST(S) FAILED!\n", status
);
503 * 'get_seconds()' - Get the current time in seconds...
507 # include <windows.h>
515 # include <sys/time.h>
521 struct timeval curtime
; /* Current time */
524 gettimeofday(&curtime
, NULL
);
525 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
531 * 'load_words()' - Load words from a file.
534 static int /* O - 1 on success, 0 on failure */
535 load_words(const char *filename
, /* I - File to load */
536 cups_array_t
*array
) /* I - Array to add to */
538 FILE *fp
; /* Test file */
539 char word
[256]; /* Word from file */
542 if ((fp
= fopen(filename
, "r")) == NULL
)
548 while (fscanf(fp
, "%255s", word
) == 1)
550 if (!cupsArrayFind(array
, word
))
551 cupsArrayAdd(array
, strdup(word
));