]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testarray.c
4 * Array test program for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include "string-private.h"
23 #include "debug-private.h"
24 #include "array-private.h"
32 static double get_seconds(void);
33 static int load_words(const char *filename
, cups_array_t
*array
);
37 * 'main()' - Main entry.
40 int /* O - Exit status */
43 int i
; /* Looping var */
44 cups_array_t
*array
, /* Test array */
45 *dup_array
; /* Duplicate array */
46 int status
; /* Exit status */
47 char *text
; /* Text from array */
48 char word
[256]; /* Word from file */
49 double start
, /* Start time */
51 cups_dir_t
*dir
; /* Current directory */
52 cups_dentry_t
*dent
; /* Directory entry */
53 char *saved
[32]; /* Saved entries */
54 void *data
; /* User data for arrays */
67 fputs("cupsArrayNew: ", stdout
);
69 data
= (void *)"testarray";
70 array
= cupsArrayNew((cups_array_func_t
)strcmp
, data
);
76 puts("FAIL (returned NULL, expected pointer)");
84 fputs("cupsArrayUserData: ", stdout
);
85 if (cupsArrayUserData(array
) == data
)
89 printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array
),
98 fputs("cupsArrayAdd: ", stdout
);
100 if (!cupsArrayAdd(array
, strdup("One Fish")))
102 puts("FAIL (\"One Fish\")");
107 if (!cupsArrayAdd(array
, strdup("Two Fish")))
109 puts("FAIL (\"Two Fish\")");
114 if (!cupsArrayAdd(array
, strdup("Red Fish")))
116 puts("FAIL (\"Red Fish\")");
121 if (!cupsArrayAdd(array
, strdup("Blue Fish")))
123 puts("FAIL (\"Blue Fish\")");
136 fputs("cupsArrayCount: ", stdout
);
137 if (cupsArrayCount(array
) == 4)
141 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array
));
149 fputs("cupsArrayFirst: ", stdout
);
150 if ((text
= (char *)cupsArrayFirst(array
)) != NULL
&&
151 !strcmp(text
, "Blue Fish"))
155 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text
);
163 fputs("cupsArrayNext: ", stdout
);
164 if ((text
= (char *)cupsArrayNext(array
)) != NULL
&&
165 !strcmp(text
, "One Fish"))
169 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
177 fputs("cupsArrayLast: ", stdout
);
178 if ((text
= (char *)cupsArrayLast(array
)) != NULL
&&
179 !strcmp(text
, "Two Fish"))
183 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text
);
191 fputs("cupsArrayPrev: ", stdout
);
192 if ((text
= (char *)cupsArrayPrev(array
)) != NULL
&&
193 !strcmp(text
, "Red Fish"))
197 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text
);
205 fputs("cupsArrayFind: ", stdout
);
206 if ((text
= (char *)cupsArrayFind(array
, (void *)"One Fish")) != NULL
&&
207 !strcmp(text
, "One Fish"))
211 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
219 fputs("cupsArrayCurrent: ", stdout
);
220 if ((text
= (char *)cupsArrayCurrent(array
)) != NULL
&&
221 !strcmp(text
, "One Fish"))
225 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
233 fputs("cupsArrayDup: ", stdout
);
234 if ((dup_array
= cupsArrayDup(array
)) != NULL
&&
235 cupsArrayCount(dup_array
) == 4)
239 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
240 dup_array
, cupsArrayCount(dup_array
));
248 fputs("cupsArrayRemove: ", stdout
);
249 if (cupsArrayRemove(array
, (void *)"One Fish") &&
250 cupsArrayCount(array
) == 3)
254 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
255 cupsArrayCount(array
));
263 fputs("cupsArrayClear: ", stdout
);
264 cupsArrayClear(array
);
265 if (cupsArrayCount(array
) == 0)
269 printf("FAIL (%d elements, expected 0 elements)\n",
270 cupsArrayCount(array
));
275 * Now load this source file and grab all of the unique words...
278 fputs("Load unique words: ", stdout
);
281 start
= get_seconds();
283 if ((dir
= cupsDirOpen(".")) == NULL
)
285 puts("FAIL (cupsDirOpen failed)");
290 while ((dent
= cupsDirRead(dir
)) != NULL
)
292 i
= (int)strlen(dent
->filename
) - 2;
294 if (i
> 0 && dent
->filename
[i
] == '.' &&
295 (dent
->filename
[i
+ 1] == 'c' ||
296 dent
->filename
[i
+ 1] == 'h'))
297 load_words(dent
->filename
, array
);
304 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array
),
305 end
- start
, cupsArrayCount(array
) / (end
- start
));
308 for (text
= (char *)cupsArrayFirst(array
); text
;)
311 * Copy this word to the word buffer (safe because we strdup'd from
312 * the same buffer in the first place... :)
315 strlcpy(word
, text
, sizeof(word
));
318 * Grab the next word and compare...
321 if ((text
= (char *)cupsArrayNext(array
)) == NULL
)
324 if (strcmp(word
, text
) >= 0)
330 printf("FAIL (\"%s\" >= \"%s\"!)\n", word
, text
);
338 * Test deleting with iteration...
341 fputs("Delete While Iterating: ", stdout
);
343 text
= (char *)cupsArrayFirst(array
);
344 cupsArrayRemove(array
, text
);
347 text
= (char *)cupsArrayNext(array
);
350 puts("FAIL (cupsArrayNext returned NULL!)");
357 * Test save/restore...
360 fputs("cupsArraySave: ", stdout
);
362 for (i
= 0, text
= (char *)cupsArrayFirst(array
);
364 i
++, text
= (char *)cupsArrayNext(array
))
368 if (!cupsArraySave(array
))
373 printf("FAIL (depth = %d)\n", i
);
377 fputs("cupsArrayRestore: ", stdout
);
383 text
= cupsArrayRestore(array
);
384 if (text
!= saved
[i
])
389 printf("FAIL (depth = %d)\n", i
);
394 * Delete the arrays...
397 cupsArrayDelete(array
);
398 cupsArrayDelete(dup_array
);
401 * Test the array with string functions...
404 fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout
);
405 array
= _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' ');
409 puts("FAIL (unable to create array)");
411 else if (cupsArrayCount(array
) != 4)
414 printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array
));
416 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
419 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
421 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
424 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
426 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
429 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
431 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
434 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
439 fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout
);
440 _cupsArrayAddStrings(array
, "foo2,bar2", ',');
442 if (cupsArrayCount(array
) != 6)
445 printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array
));
447 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
450 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
452 else if (strcmp(text
= (char *)cupsArrayNext(array
), "bar2"))
455 printf("FAIL (first element \"%s\", expected \"bar2\")\n", text
);
457 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
460 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
462 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
465 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
467 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
470 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
472 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo2"))
475 printf("FAIL (first element \"%s\", expected \"foo2\")\n", text
);
480 cupsArrayDelete(array
);
483 * Summarize the results and return...
487 puts("\nALL TESTS PASSED!");
489 printf("\n%d TEST(S) FAILED!\n", status
);
496 * 'get_seconds()' - Get the current time in seconds...
500 # include <windows.h>
508 # include <sys/time.h>
514 struct timeval curtime
; /* Current time */
517 gettimeofday(&curtime
, NULL
);
518 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
524 * 'load_words()' - Load words from a file.
527 static int /* O - 1 on success, 0 on failure */
528 load_words(const char *filename
, /* I - File to load */
529 cups_array_t
*array
) /* I - Array to add to */
531 FILE *fp
; /* Test file */
532 char word
[256]; /* Word from file */
535 if ((fp
= fopen(filename
, "r")) == NULL
)
541 while (fscanf(fp
, "%255s", word
) == 1)
543 if (!cupsArrayFind(array
, word
))
544 cupsArrayAdd(array
, strdup(word
));