]>
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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "string-private.h"
15 #include "debug-private.h"
16 #include "array-private.h"
24 static double get_seconds(void);
25 static int load_words(const char *filename
, cups_array_t
*array
);
29 * 'main()' - Main entry.
32 int /* O - Exit status */
35 int i
; /* Looping var */
36 cups_array_t
*array
, /* Test array */
37 *dup_array
; /* Duplicate array */
38 int status
; /* Exit status */
39 char *text
; /* Text from array */
40 char word
[256]; /* Word from file */
41 double start
, /* Start time */
43 cups_dir_t
*dir
; /* Current directory */
44 cups_dentry_t
*dent
; /* Directory entry */
45 char *saved
[32]; /* Saved entries */
46 void *data
; /* User data for arrays */
59 fputs("cupsArrayNew: ", stdout
);
61 data
= (void *)"testarray";
62 array
= cupsArrayNew((cups_array_func_t
)strcmp
, data
);
68 puts("FAIL (returned NULL, expected pointer)");
76 fputs("cupsArrayUserData: ", stdout
);
77 if (cupsArrayUserData(array
) == data
)
81 printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array
),
90 fputs("cupsArrayAdd: ", stdout
);
92 if (!cupsArrayAdd(array
, strdup("One Fish")))
94 puts("FAIL (\"One Fish\")");
99 if (!cupsArrayAdd(array
, strdup("Two Fish")))
101 puts("FAIL (\"Two Fish\")");
106 if (!cupsArrayAdd(array
, strdup("Red Fish")))
108 puts("FAIL (\"Red Fish\")");
113 if (!cupsArrayAdd(array
, strdup("Blue Fish")))
115 puts("FAIL (\"Blue Fish\")");
128 fputs("cupsArrayCount: ", stdout
);
129 if (cupsArrayCount(array
) == 4)
133 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array
));
141 fputs("cupsArrayFirst: ", stdout
);
142 if ((text
= (char *)cupsArrayFirst(array
)) != NULL
&&
143 !strcmp(text
, "Blue Fish"))
147 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text
);
155 fputs("cupsArrayNext: ", stdout
);
156 if ((text
= (char *)cupsArrayNext(array
)) != NULL
&&
157 !strcmp(text
, "One Fish"))
161 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
169 fputs("cupsArrayLast: ", stdout
);
170 if ((text
= (char *)cupsArrayLast(array
)) != NULL
&&
171 !strcmp(text
, "Two Fish"))
175 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text
);
183 fputs("cupsArrayPrev: ", stdout
);
184 if ((text
= (char *)cupsArrayPrev(array
)) != NULL
&&
185 !strcmp(text
, "Red Fish"))
189 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text
);
197 fputs("cupsArrayFind: ", stdout
);
198 if ((text
= (char *)cupsArrayFind(array
, (void *)"One Fish")) != NULL
&&
199 !strcmp(text
, "One Fish"))
203 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
211 fputs("cupsArrayCurrent: ", stdout
);
212 if ((text
= (char *)cupsArrayCurrent(array
)) != NULL
&&
213 !strcmp(text
, "One Fish"))
217 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
225 fputs("cupsArrayDup: ", stdout
);
226 if ((dup_array
= cupsArrayDup(array
)) != NULL
&&
227 cupsArrayCount(dup_array
) == 4)
231 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
232 dup_array
, cupsArrayCount(dup_array
));
240 fputs("cupsArrayRemove: ", stdout
);
241 if (cupsArrayRemove(array
, (void *)"One Fish") &&
242 cupsArrayCount(array
) == 3)
246 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
247 cupsArrayCount(array
));
255 fputs("cupsArrayClear: ", stdout
);
256 cupsArrayClear(array
);
257 if (cupsArrayCount(array
) == 0)
261 printf("FAIL (%d elements, expected 0 elements)\n",
262 cupsArrayCount(array
));
267 * Now load this source file and grab all of the unique words...
270 fputs("Load unique words: ", stdout
);
273 start
= get_seconds();
275 if ((dir
= cupsDirOpen(".")) == NULL
)
277 puts("FAIL (cupsDirOpen failed)");
282 while ((dent
= cupsDirRead(dir
)) != NULL
)
284 i
= (int)strlen(dent
->filename
) - 2;
286 if (i
> 0 && dent
->filename
[i
] == '.' &&
287 (dent
->filename
[i
+ 1] == 'c' ||
288 dent
->filename
[i
+ 1] == 'h'))
289 load_words(dent
->filename
, array
);
296 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array
),
297 end
- start
, cupsArrayCount(array
) / (end
- start
));
300 for (text
= (char *)cupsArrayFirst(array
); text
;)
303 * Copy this word to the word buffer (safe because we strdup'd from
304 * the same buffer in the first place... :)
307 strlcpy(word
, text
, sizeof(word
));
310 * Grab the next word and compare...
313 if ((text
= (char *)cupsArrayNext(array
)) == NULL
)
316 if (strcmp(word
, text
) >= 0)
322 printf("FAIL (\"%s\" >= \"%s\"!)\n", word
, text
);
330 * Test deleting with iteration...
333 fputs("Delete While Iterating: ", stdout
);
335 text
= (char *)cupsArrayFirst(array
);
336 cupsArrayRemove(array
, text
);
339 text
= (char *)cupsArrayNext(array
);
342 puts("FAIL (cupsArrayNext returned NULL!)");
349 * Test save/restore...
352 fputs("cupsArraySave: ", stdout
);
354 for (i
= 0, text
= (char *)cupsArrayFirst(array
);
356 i
++, text
= (char *)cupsArrayNext(array
))
360 if (!cupsArraySave(array
))
365 printf("FAIL (depth = %d)\n", i
);
369 fputs("cupsArrayRestore: ", stdout
);
375 text
= cupsArrayRestore(array
);
376 if (text
!= saved
[i
])
381 printf("FAIL (depth = %d)\n", i
);
386 * Delete the arrays...
389 cupsArrayDelete(array
);
390 cupsArrayDelete(dup_array
);
393 * Test the array with string functions...
396 fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout
);
397 array
= _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' ');
401 puts("FAIL (unable to create array)");
403 else if (cupsArrayCount(array
) != 4)
406 printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array
));
408 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
411 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
413 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
416 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
418 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
421 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
423 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
426 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
431 fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout
);
432 _cupsArrayAddStrings(array
, "foo2,bar2", ',');
434 if (cupsArrayCount(array
) != 6)
437 printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array
));
439 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
442 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
444 else if (strcmp(text
= (char *)cupsArrayNext(array
), "bar2"))
447 printf("FAIL (first element \"%s\", expected \"bar2\")\n", text
);
449 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
452 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
454 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
457 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
459 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
462 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
464 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo2"))
467 printf("FAIL (first element \"%s\", expected \"foo2\")\n", text
);
472 cupsArrayDelete(array
);
475 * Summarize the results and return...
479 puts("\nALL TESTS PASSED!");
481 printf("\n%d TEST(S) FAILED!\n", status
);
488 * 'get_seconds()' - Get the current time in seconds...
492 # include <windows.h>
500 # include <sys/time.h>
506 struct timeval curtime
; /* Current time */
509 gettimeofday(&curtime
, NULL
);
510 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
516 * 'load_words()' - Load words from a file.
519 static int /* O - 1 on success, 0 on failure */
520 load_words(const char *filename
, /* I - File to load */
521 cups_array_t
*array
) /* I - Array to add to */
523 FILE *fp
; /* Test file */
524 char word
[256]; /* Word from file */
527 if ((fp
= fopen(filename
, "r")) == NULL
)
533 while (fscanf(fp
, "%255s", word
) == 1)
535 if (!cupsArrayFind(array
, word
))
536 cupsArrayAdd(array
, strdup(word
));