]>
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", (void *)dup_array
, cupsArrayCount(dup_array
));
239 fputs("cupsArrayRemove: ", stdout
);
240 if (cupsArrayRemove(array
, (void *)"One Fish") &&
241 cupsArrayCount(array
) == 3)
245 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
246 cupsArrayCount(array
));
254 fputs("cupsArrayClear: ", stdout
);
255 cupsArrayClear(array
);
256 if (cupsArrayCount(array
) == 0)
260 printf("FAIL (%d elements, expected 0 elements)\n",
261 cupsArrayCount(array
));
266 * Now load this source file and grab all of the unique words...
269 fputs("Load unique words: ", stdout
);
272 start
= get_seconds();
274 if ((dir
= cupsDirOpen(".")) == NULL
)
276 puts("FAIL (cupsDirOpen failed)");
281 while ((dent
= cupsDirRead(dir
)) != NULL
)
283 i
= (int)strlen(dent
->filename
) - 2;
285 if (i
> 0 && dent
->filename
[i
] == '.' &&
286 (dent
->filename
[i
+ 1] == 'c' ||
287 dent
->filename
[i
+ 1] == 'h'))
288 load_words(dent
->filename
, array
);
295 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array
),
296 end
- start
, cupsArrayCount(array
) / (end
- start
));
299 for (text
= (char *)cupsArrayFirst(array
); text
;)
302 * Copy this word to the word buffer (safe because we strdup'd from
303 * the same buffer in the first place... :)
306 strlcpy(word
, text
, sizeof(word
));
309 * Grab the next word and compare...
312 if ((text
= (char *)cupsArrayNext(array
)) == NULL
)
315 if (strcmp(word
, text
) >= 0)
321 printf("FAIL (\"%s\" >= \"%s\"!)\n", word
, text
);
329 * Test deleting with iteration...
332 fputs("Delete While Iterating: ", stdout
);
334 text
= (char *)cupsArrayFirst(array
);
335 cupsArrayRemove(array
, text
);
338 text
= (char *)cupsArrayNext(array
);
341 puts("FAIL (cupsArrayNext returned NULL!)");
348 * Test save/restore...
351 fputs("cupsArraySave: ", stdout
);
353 for (i
= 0, text
= (char *)cupsArrayFirst(array
);
355 i
++, text
= (char *)cupsArrayNext(array
))
359 if (!cupsArraySave(array
))
364 printf("FAIL (depth = %d)\n", i
);
368 fputs("cupsArrayRestore: ", stdout
);
374 text
= cupsArrayRestore(array
);
375 if (text
!= saved
[i
])
380 printf("FAIL (depth = %d)\n", i
);
385 * Delete the arrays...
388 cupsArrayDelete(array
);
389 cupsArrayDelete(dup_array
);
392 * Test the array with string functions...
395 fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout
);
396 array
= _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' ');
400 puts("FAIL (unable to create array)");
402 else if (cupsArrayCount(array
) != 4)
405 printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array
));
407 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
410 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
412 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
415 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
417 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
420 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
422 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
425 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
430 fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout
);
431 _cupsArrayAddStrings(array
, "foo2,bar2", ',');
433 if (cupsArrayCount(array
) != 6)
436 printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array
));
438 else if (strcmp(text
= (char *)cupsArrayFirst(array
), "bar"))
441 printf("FAIL (first element \"%s\", expected \"bar\")\n", text
);
443 else if (strcmp(text
= (char *)cupsArrayNext(array
), "bar2"))
446 printf("FAIL (first element \"%s\", expected \"bar2\")\n", text
);
448 else if (strcmp(text
= (char *)cupsArrayNext(array
), "boo"))
451 printf("FAIL (first element \"%s\", expected \"boo\")\n", text
);
453 else if (strcmp(text
= (char *)cupsArrayNext(array
), "far"))
456 printf("FAIL (first element \"%s\", expected \"far\")\n", text
);
458 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo"))
461 printf("FAIL (first element \"%s\", expected \"foo\")\n", text
);
463 else if (strcmp(text
= (char *)cupsArrayNext(array
), "foo2"))
466 printf("FAIL (first element \"%s\", expected \"foo2\")\n", text
);
471 cupsArrayDelete(array
);
474 * Summarize the results and return...
478 puts("\nALL TESTS PASSED!");
480 printf("\n%d TEST(S) FAILED!\n", status
);
487 * 'get_seconds()' - Get the current time in seconds...
491 # include <windows.h>
499 # include <sys/time.h>
505 struct timeval curtime
; /* Current time */
508 gettimeofday(&curtime
, NULL
);
509 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
515 * 'load_words()' - Load words from a file.
518 static int /* O - 1 on success, 0 on failure */
519 load_words(const char *filename
, /* I - File to load */
520 cups_array_t
*array
) /* I - Array to add to */
522 FILE *fp
; /* Test file */
523 char word
[256]; /* Word from file */
526 if ((fp
= fopen(filename
, "r")) == NULL
)
532 while (fscanf(fp
, "%255s", word
) == 1)
534 if (!cupsArrayFind(array
, word
))
535 cupsArrayAdd(array
, strdup(word
));