]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testarray.c
2 * "$Id: testarray.c 4903 2006-01-10 20:02:46Z mike $"
4 * Array test program for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Main entry.
29 * get_seconds() - Get the current time in seconds...
30 * load_words() - Load words from a file.
34 * Include necessary headers...
39 #include <cups/string.h>
50 static double get_seconds(void);
51 static int load_words(const char *filename
, cups_array_t
*array
);
55 * 'main()' - Main entry.
58 int /* O - Exit status */
59 main(int argc
, /* I - Number of command-line arguments */
60 char *argv
[]) /* I - Command-line arguments */
62 int i
; /* Looping var */
63 cups_array_t
*array
, /* Test array */
64 *dup_array
; /* Duplicate array */
65 int status
; /* Exit status */
66 char *text
; /* Text from array */
67 char word
[256]; /* Word from file */
68 double start
, /* Start time */
70 cups_dir_t
*dir
; /* Current directory */
71 cups_dentry_t
*dent
; /* Directory entry */
72 char *saved
[32]; /* Saved entries */
85 fputs("cupsArrayNew: ", stdout
);
87 array
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
93 puts("FAIL (returned NULL, expected pointer)");
101 fputs("cupsArrayAdd: ", stdout
);
103 if (!cupsArrayAdd(array
, strdup("One Fish")))
105 puts("FAIL (\"One Fish\")");
112 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
114 text
= (char *)cupsArrayNext(array
), i
++)
115 printf(" #1 array[%d]=\"%s\"\n", i
, text
);
118 if (!cupsArrayAdd(array
, strdup("Two Fish")))
120 puts("FAIL (\"Two Fish\")");
126 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
128 text
= (char *)cupsArrayNext(array
), i
++)
129 printf(" #2 array[%d]=\"%s\"\n", i
, text
);
132 if (!cupsArrayAdd(array
, strdup("Red Fish")))
134 puts("FAIL (\"Red Fish\")");
140 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
142 text
= (char *)cupsArrayNext(array
), i
++)
143 printf(" #3 array[%d]=\"%s\"\n", i
, text
);
146 if (!cupsArrayAdd(array
, strdup("Blue Fish")))
148 puts("FAIL (\"Blue Fish\")");
154 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
156 text
= (char *)cupsArrayNext(array
), i
++)
157 printf(" #4 array[%d]=\"%s\"\n", i
, text
);
170 fputs("cupsArrayCount: ", stdout
);
171 if (cupsArrayCount(array
) == 4)
175 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array
));
183 fputs("cupsArrayFirst: ", stdout
);
184 if ((text
= (char *)cupsArrayFirst(array
)) != NULL
&&
185 !strcmp(text
, "Blue Fish"))
189 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text
);
197 fputs("cupsArrayNext: ", stdout
);
198 if ((text
= (char *)cupsArrayNext(array
)) != NULL
&&
199 !strcmp(text
, "One Fish"))
203 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
211 fputs("cupsArrayLast: ", stdout
);
212 if ((text
= (char *)cupsArrayLast(array
)) != NULL
&&
213 !strcmp(text
, "Two Fish"))
217 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text
);
225 fputs("cupsArrayPrev: ", stdout
);
226 if ((text
= (char *)cupsArrayPrev(array
)) != NULL
&&
227 !strcmp(text
, "Red Fish"))
231 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text
);
239 fputs("cupsArrayFind: ", stdout
);
240 if ((text
= (char *)cupsArrayFind(array
, (void *)"One Fish")) != NULL
&&
241 !strcmp(text
, "One Fish"))
245 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
253 fputs("cupsArrayCurrent: ", stdout
);
254 if ((text
= (char *)cupsArrayCurrent(array
)) != NULL
&&
255 !strcmp(text
, "One Fish"))
259 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
267 fputs("cupsArrayDup: ", stdout
);
268 if ((dup_array
= cupsArrayDup(array
)) != NULL
&&
269 cupsArrayCount(dup_array
) == 4)
273 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
274 dup_array
, cupsArrayCount(dup_array
));
282 fputs("cupsArrayRemove: ", stdout
);
283 if (cupsArrayRemove(array
, (void *)"One Fish") &&
284 cupsArrayCount(array
) == 3)
288 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
289 cupsArrayCount(array
));
297 fputs("cupsArrayClear: ", stdout
);
298 cupsArrayClear(array
);
299 if (cupsArrayCount(array
) == 0)
303 printf("FAIL (%d elements, expected 0 elements)\n",
304 cupsArrayCount(array
));
309 * Now load this source file and grab all of the unique words...
312 fputs("Load unique words: ", stdout
);
315 start
= get_seconds();
317 if ((dir
= cupsDirOpen(".")) == NULL
)
319 puts("FAIL (cupsDirOpen failed)");
324 while ((dent
= cupsDirRead(dir
)) != NULL
)
326 i
= strlen(dent
->filename
) - 2;
328 if (i
> 0 && dent
->filename
[i
] == '.' &&
329 (dent
->filename
[i
+ 1] == 'c' ||
330 dent
->filename
[i
+ 1] == 'h'))
331 load_words(dent
->filename
, array
);
338 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array
),
339 end
- start
, cupsArrayCount(array
) / (end
- start
));
342 for (text
= (char *)cupsArrayFirst(array
); text
;)
345 * Copy this word to the word buffer (safe because we strdup'd from
346 * the same buffer in the first place... :)
352 * Grab the next word and compare...
355 if ((text
= (char *)cupsArrayNext(array
)) == NULL
)
358 if (strcmp(word
, text
) >= 0)
364 printf("FAIL (\"%s\" >= \"%s\"!)\n", word
, text
);
372 * Test deleting with iteration...
375 fputs("Delete While Iterating: ", stdout
);
377 text
= (char *)cupsArrayFirst(array
);
378 cupsArrayRemove(array
, text
);
381 text
= (char *)cupsArrayNext(array
);
384 puts("FAIL (cupsArrayNext returned NULL!)");
391 * Test save/restore...
394 fputs("cupsArraySave: ", stdout
);
396 for (i
= 0, text
= (char *)cupsArrayFirst(array
);
398 i
++, text
= (char *)cupsArrayNext(array
))
402 if (!cupsArraySave(array
))
407 printf("FAIL (depth = %d)\n", i
);
411 fputs("cupsArrayRestore: ", stdout
);
417 text
= cupsArrayRestore(array
);
418 if (text
!= saved
[i
])
423 printf("FAIL (depth = %d)\n", i
);
428 * Delete the arrays...
431 cupsArrayDelete(array
);
432 cupsArrayDelete(dup_array
);
435 * Summarize the results and return...
439 puts("\nALL TESTS PASSED!");
441 printf("\n%d TEST(S) FAILED!\n", status
);
448 * 'get_seconds()' - Get the current time in seconds...
452 # include <windows.h>
460 # include <sys/time.h>
466 struct timeval curtime
; /* Current time */
469 gettimeofday(&curtime
, NULL
);
470 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
476 * 'load_words()' - Load words from a file.
479 static int /* O - 1 on success, 0 on failure */
480 load_words(const char *filename
, /* I - File to load */
481 cups_array_t
*array
) /* I - Array to add to */
483 FILE *fp
; /* Test file */
484 char word
[256]; /* Word from file */
487 DEBUG_printf((" Loading \"%s\"...\n", filename
));
489 if ((fp
= fopen(filename
, "r")) == NULL
)
495 while (fscanf(fp
, "%255s", word
) == 1)
497 if (!cupsArrayFind(array
, word
))
499 DEBUG_printf((" Adding \"%s\"...\n", word
));
501 cupsArrayAdd(array
, strdup(word
));
512 * End of "$Id: testarray.c 4903 2006-01-10 20:02:46Z mike $".