]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testarray.c
2 * "$Id: testarray.c 5258 2006-03-09 15:40:56Z 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 */
73 void *data
; /* User data for arrays */
86 fputs("cupsArrayNew: ", stdout
);
88 data
= (void *)"testarray";
89 array
= cupsArrayNew((cups_array_func_t
)strcmp
, data
);
95 puts("FAIL (returned NULL, expected pointer)");
100 * cupsArrayUserData()
103 fputs("cupsArrayUserData: ", stdout
);
104 if (cupsArrayUserData(array
) == data
)
108 printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array
),
117 fputs("cupsArrayAdd: ", stdout
);
119 if (!cupsArrayAdd(array
, strdup("One Fish")))
121 puts("FAIL (\"One Fish\")");
128 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
130 text
= (char *)cupsArrayNext(array
), i
++)
131 printf(" #1 array[%d]=\"%s\"\n", i
, text
);
134 if (!cupsArrayAdd(array
, strdup("Two Fish")))
136 puts("FAIL (\"Two Fish\")");
142 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
144 text
= (char *)cupsArrayNext(array
), i
++)
145 printf(" #2 array[%d]=\"%s\"\n", i
, text
);
148 if (!cupsArrayAdd(array
, strdup("Red Fish")))
150 puts("FAIL (\"Red Fish\")");
156 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
158 text
= (char *)cupsArrayNext(array
), i
++)
159 printf(" #3 array[%d]=\"%s\"\n", i
, text
);
162 if (!cupsArrayAdd(array
, strdup("Blue Fish")))
164 puts("FAIL (\"Blue Fish\")");
170 for (text
= (char *)cupsArrayFirst(array
), i
= 0;
172 text
= (char *)cupsArrayNext(array
), i
++)
173 printf(" #4 array[%d]=\"%s\"\n", i
, text
);
186 fputs("cupsArrayCount: ", stdout
);
187 if (cupsArrayCount(array
) == 4)
191 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array
));
199 fputs("cupsArrayFirst: ", stdout
);
200 if ((text
= (char *)cupsArrayFirst(array
)) != NULL
&&
201 !strcmp(text
, "Blue Fish"))
205 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text
);
213 fputs("cupsArrayNext: ", stdout
);
214 if ((text
= (char *)cupsArrayNext(array
)) != NULL
&&
215 !strcmp(text
, "One Fish"))
219 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
227 fputs("cupsArrayLast: ", stdout
);
228 if ((text
= (char *)cupsArrayLast(array
)) != NULL
&&
229 !strcmp(text
, "Two Fish"))
233 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text
);
241 fputs("cupsArrayPrev: ", stdout
);
242 if ((text
= (char *)cupsArrayPrev(array
)) != NULL
&&
243 !strcmp(text
, "Red Fish"))
247 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text
);
255 fputs("cupsArrayFind: ", stdout
);
256 if ((text
= (char *)cupsArrayFind(array
, (void *)"One Fish")) != NULL
&&
257 !strcmp(text
, "One Fish"))
261 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
269 fputs("cupsArrayCurrent: ", stdout
);
270 if ((text
= (char *)cupsArrayCurrent(array
)) != NULL
&&
271 !strcmp(text
, "One Fish"))
275 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text
);
283 fputs("cupsArrayDup: ", stdout
);
284 if ((dup_array
= cupsArrayDup(array
)) != NULL
&&
285 cupsArrayCount(dup_array
) == 4)
289 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
290 dup_array
, cupsArrayCount(dup_array
));
298 fputs("cupsArrayRemove: ", stdout
);
299 if (cupsArrayRemove(array
, (void *)"One Fish") &&
300 cupsArrayCount(array
) == 3)
304 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
305 cupsArrayCount(array
));
313 fputs("cupsArrayClear: ", stdout
);
314 cupsArrayClear(array
);
315 if (cupsArrayCount(array
) == 0)
319 printf("FAIL (%d elements, expected 0 elements)\n",
320 cupsArrayCount(array
));
325 * Now load this source file and grab all of the unique words...
328 fputs("Load unique words: ", stdout
);
331 start
= get_seconds();
333 if ((dir
= cupsDirOpen(".")) == NULL
)
335 puts("FAIL (cupsDirOpen failed)");
340 while ((dent
= cupsDirRead(dir
)) != NULL
)
342 i
= strlen(dent
->filename
) - 2;
344 if (i
> 0 && dent
->filename
[i
] == '.' &&
345 (dent
->filename
[i
+ 1] == 'c' ||
346 dent
->filename
[i
+ 1] == 'h'))
347 load_words(dent
->filename
, array
);
354 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array
),
355 end
- start
, cupsArrayCount(array
) / (end
- start
));
358 for (text
= (char *)cupsArrayFirst(array
); text
;)
361 * Copy this word to the word buffer (safe because we strdup'd from
362 * the same buffer in the first place... :)
368 * Grab the next word and compare...
371 if ((text
= (char *)cupsArrayNext(array
)) == NULL
)
374 if (strcmp(word
, text
) >= 0)
380 printf("FAIL (\"%s\" >= \"%s\"!)\n", word
, text
);
388 * Test deleting with iteration...
391 fputs("Delete While Iterating: ", stdout
);
393 text
= (char *)cupsArrayFirst(array
);
394 cupsArrayRemove(array
, text
);
397 text
= (char *)cupsArrayNext(array
);
400 puts("FAIL (cupsArrayNext returned NULL!)");
407 * Test save/restore...
410 fputs("cupsArraySave: ", stdout
);
412 for (i
= 0, text
= (char *)cupsArrayFirst(array
);
414 i
++, text
= (char *)cupsArrayNext(array
))
418 if (!cupsArraySave(array
))
423 printf("FAIL (depth = %d)\n", i
);
427 fputs("cupsArrayRestore: ", stdout
);
433 text
= cupsArrayRestore(array
);
434 if (text
!= saved
[i
])
439 printf("FAIL (depth = %d)\n", i
);
444 * Delete the arrays...
447 cupsArrayDelete(array
);
448 cupsArrayDelete(dup_array
);
451 * Summarize the results and return...
455 puts("\nALL TESTS PASSED!");
457 printf("\n%d TEST(S) FAILED!\n", status
);
464 * 'get_seconds()' - Get the current time in seconds...
468 # include <windows.h>
476 # include <sys/time.h>
482 struct timeval curtime
; /* Current time */
485 gettimeofday(&curtime
, NULL
);
486 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
492 * 'load_words()' - Load words from a file.
495 static int /* O - 1 on success, 0 on failure */
496 load_words(const char *filename
, /* I - File to load */
497 cups_array_t
*array
) /* I - Array to add to */
499 FILE *fp
; /* Test file */
500 char word
[256]; /* Word from file */
503 DEBUG_printf((" Loading \"%s\"...\n", filename
));
505 if ((fp
= fopen(filename
, "r")) == NULL
)
511 while (fscanf(fp
, "%255s", word
) == 1)
513 if (!cupsArrayFind(array
, word
))
515 DEBUG_printf((" Adding \"%s\"...\n", word
));
517 cupsArrayAdd(array
, strdup(word
));
528 * End of "$Id: testarray.c 5258 2006-03-09 15:40:56Z mike $".