]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testarray.c
Remove svn:keywords since they cause svn_load_dirs.pl to complain about every file.
[thirdparty/cups.git] / cups / testarray.c
1 /*
2 * "$Id: testarray.c 177 2006-06-21 00:20:03Z jlovell $"
3 *
4 * Array test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * main() - Main entry.
29 * get_seconds() - Get the current time in seconds...
30 * load_words() - Load words from a file.
31 */
32
33 /*
34 * Include necessary headers...
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <cups/string.h>
40 #include <errno.h>
41 #include "array.h"
42 #include "dir.h"
43 #include "debug.h"
44
45
46 /*
47 * Local functions...
48 */
49
50 static double get_seconds(void);
51 static int load_words(const char *filename, cups_array_t *array);
52
53
54 /*
55 * 'main()' - Main entry.
56 */
57
58 int /* O - Exit status */
59 main(int argc, /* I - Number of command-line arguments */
60 char *argv[]) /* I - Command-line arguments */
61 {
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 */
69 end; /* End 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 */
74
75
76 /*
77 * No errors so far...
78 */
79
80 status = 0;
81
82 /*
83 * cupsArrayNew()
84 */
85
86 fputs("cupsArrayNew: ", stdout);
87
88 data = (void *)"testarray";
89 array = cupsArrayNew((cups_array_func_t)strcmp, data);
90
91 if (array)
92 puts("PASS");
93 else
94 {
95 puts("FAIL (returned NULL, expected pointer)");
96 status ++;
97 }
98
99 /*
100 * cupsArrayUserData()
101 */
102
103 fputs("cupsArrayUserData: ", stdout);
104 if (cupsArrayUserData(array) == data)
105 puts("PASS");
106 else
107 {
108 printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array),
109 data);
110 status ++;
111 }
112
113 /*
114 * cupsArrayAdd()
115 */
116
117 fputs("cupsArrayAdd: ", stdout);
118
119 if (!cupsArrayAdd(array, strdup("One Fish")))
120 {
121 puts("FAIL (\"One Fish\")");
122 status ++;
123 }
124 else
125 {
126 #ifdef DEBUG
127 putchar('\n');
128 for (text = (char *)cupsArrayFirst(array), i = 0;
129 text;
130 text = (char *)cupsArrayNext(array), i ++)
131 printf(" #1 array[%d]=\"%s\"\n", i, text);
132 #endif /* DEBUG */
133
134 if (!cupsArrayAdd(array, strdup("Two Fish")))
135 {
136 puts("FAIL (\"Two Fish\")");
137 status ++;
138 }
139 else
140 {
141 #ifdef DEBUG
142 for (text = (char *)cupsArrayFirst(array), i = 0;
143 text;
144 text = (char *)cupsArrayNext(array), i ++)
145 printf(" #2 array[%d]=\"%s\"\n", i, text);
146 #endif /* DEBUG */
147
148 if (!cupsArrayAdd(array, strdup("Red Fish")))
149 {
150 puts("FAIL (\"Red Fish\")");
151 status ++;
152 }
153 else
154 {
155 #ifdef DEBUG
156 for (text = (char *)cupsArrayFirst(array), i = 0;
157 text;
158 text = (char *)cupsArrayNext(array), i ++)
159 printf(" #3 array[%d]=\"%s\"\n", i, text);
160 #endif /* DEBUG */
161
162 if (!cupsArrayAdd(array, strdup("Blue Fish")))
163 {
164 puts("FAIL (\"Blue Fish\")");
165 status ++;
166 }
167 else
168 {
169 #ifdef DEBUG
170 for (text = (char *)cupsArrayFirst(array), i = 0;
171 text;
172 text = (char *)cupsArrayNext(array), i ++)
173 printf(" #4 array[%d]=\"%s\"\n", i, text);
174 #endif /* DEBUG */
175
176 puts("PASS");
177 }
178 }
179 }
180 }
181
182 /*
183 * cupsArrayCount()
184 */
185
186 fputs("cupsArrayCount: ", stdout);
187 if (cupsArrayCount(array) == 4)
188 puts("PASS");
189 else
190 {
191 printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array));
192 status ++;
193 }
194
195 /*
196 * cupsArrayFirst()
197 */
198
199 fputs("cupsArrayFirst: ", stdout);
200 if ((text = (char *)cupsArrayFirst(array)) != NULL &&
201 !strcmp(text, "Blue Fish"))
202 puts("PASS");
203 else
204 {
205 printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text);
206 status ++;
207 }
208
209 /*
210 * cupsArrayNext()
211 */
212
213 fputs("cupsArrayNext: ", stdout);
214 if ((text = (char *)cupsArrayNext(array)) != NULL &&
215 !strcmp(text, "One Fish"))
216 puts("PASS");
217 else
218 {
219 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
220 status ++;
221 }
222
223 /*
224 * cupsArrayLast()
225 */
226
227 fputs("cupsArrayLast: ", stdout);
228 if ((text = (char *)cupsArrayLast(array)) != NULL &&
229 !strcmp(text, "Two Fish"))
230 puts("PASS");
231 else
232 {
233 printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text);
234 status ++;
235 }
236
237 /*
238 * cupsArrayPrev()
239 */
240
241 fputs("cupsArrayPrev: ", stdout);
242 if ((text = (char *)cupsArrayPrev(array)) != NULL &&
243 !strcmp(text, "Red Fish"))
244 puts("PASS");
245 else
246 {
247 printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text);
248 status ++;
249 }
250
251 /*
252 * cupsArrayFind()
253 */
254
255 fputs("cupsArrayFind: ", stdout);
256 if ((text = (char *)cupsArrayFind(array, (void *)"One Fish")) != NULL &&
257 !strcmp(text, "One Fish"))
258 puts("PASS");
259 else
260 {
261 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
262 status ++;
263 }
264
265 /*
266 * cupsArrayCurrent()
267 */
268
269 fputs("cupsArrayCurrent: ", stdout);
270 if ((text = (char *)cupsArrayCurrent(array)) != NULL &&
271 !strcmp(text, "One Fish"))
272 puts("PASS");
273 else
274 {
275 printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
276 status ++;
277 }
278
279 /*
280 * cupsArrayDup()
281 */
282
283 fputs("cupsArrayDup: ", stdout);
284 if ((dup_array = cupsArrayDup(array)) != NULL &&
285 cupsArrayCount(dup_array) == 4)
286 puts("PASS");
287 else
288 {
289 printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
290 dup_array, cupsArrayCount(dup_array));
291 status ++;
292 }
293
294 /*
295 * cupsArrayRemove()
296 */
297
298 fputs("cupsArrayRemove: ", stdout);
299 if (cupsArrayRemove(array, (void *)"One Fish") &&
300 cupsArrayCount(array) == 3)
301 puts("PASS");
302 else
303 {
304 printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
305 cupsArrayCount(array));
306 status ++;
307 }
308
309 /*
310 * cupsArrayClear()
311 */
312
313 fputs("cupsArrayClear: ", stdout);
314 cupsArrayClear(array);
315 if (cupsArrayCount(array) == 0)
316 puts("PASS");
317 else
318 {
319 printf("FAIL (%d elements, expected 0 elements)\n",
320 cupsArrayCount(array));
321 status ++;
322 }
323
324 /*
325 * Now load this source file and grab all of the unique words...
326 */
327
328 fputs("Load unique words: ", stdout);
329 fflush(stdout);
330
331 start = get_seconds();
332
333 if ((dir = cupsDirOpen(".")) == NULL)
334 {
335 puts("FAIL (cupsDirOpen failed)");
336 status ++;
337 }
338 else
339 {
340 while ((dent = cupsDirRead(dir)) != NULL)
341 {
342 i = strlen(dent->filename) - 2;
343
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);
348 }
349
350 cupsDirClose(dir);
351
352 end = get_seconds();
353
354 printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array),
355 end - start, cupsArrayCount(array) / (end - start));
356 fflush(stdout);
357
358 for (text = (char *)cupsArrayFirst(array); text;)
359 {
360 /*
361 * Copy this word to the word buffer (safe because we strdup'd from
362 * the same buffer in the first place... :)
363 */
364
365 strcpy(word, text);
366
367 /*
368 * Grab the next word and compare...
369 */
370
371 if ((text = (char *)cupsArrayNext(array)) == NULL)
372 break;
373
374 if (strcmp(word, text) >= 0)
375 break;
376 }
377
378 if (text)
379 {
380 printf("FAIL (\"%s\" >= \"%s\"!)\n", word, text);
381 status ++;
382 }
383 else
384 puts("PASS");
385 }
386
387 /*
388 * Test deleting with iteration...
389 */
390
391 fputs("Delete While Iterating: ", stdout);
392
393 text = (char *)cupsArrayFirst(array);
394 cupsArrayRemove(array, text);
395 free(text);
396
397 text = (char *)cupsArrayNext(array);
398 if (!text)
399 {
400 puts("FAIL (cupsArrayNext returned NULL!)");
401 status ++;
402 }
403 else
404 puts("PASS");
405
406 /*
407 * Test save/restore...
408 */
409
410 fputs("cupsArraySave: ", stdout);
411
412 for (i = 0, text = (char *)cupsArrayFirst(array);
413 i < 32;
414 i ++, text = (char *)cupsArrayNext(array))
415 {
416 saved[i] = text;
417
418 if (!cupsArraySave(array))
419 break;
420 }
421
422 if (i < 32)
423 printf("FAIL (depth = %d)\n", i);
424 else
425 puts("PASS");
426
427 fputs("cupsArrayRestore: ", stdout);
428
429 while (i > 0)
430 {
431 i --;
432
433 text = cupsArrayRestore(array);
434 if (text != saved[i])
435 break;
436 }
437
438 if (i)
439 printf("FAIL (depth = %d)\n", i);
440 else
441 puts("PASS");
442
443 /*
444 * Delete the arrays...
445 */
446
447 cupsArrayDelete(array);
448 cupsArrayDelete(dup_array);
449
450 /*
451 * Summarize the results and return...
452 */
453
454 if (!status)
455 puts("\nALL TESTS PASSED!");
456 else
457 printf("\n%d TEST(S) FAILED!\n", status);
458
459 return (status);
460 }
461
462
463 /*
464 * 'get_seconds()' - Get the current time in seconds...
465 */
466
467 #ifdef WIN32
468 # include <windows.h>
469
470
471 static double
472 get_seconds(void)
473 {
474 }
475 #else
476 # include <sys/time.h>
477
478
479 static double
480 get_seconds(void)
481 {
482 struct timeval curtime; /* Current time */
483
484
485 gettimeofday(&curtime, NULL);
486 return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
487 }
488 #endif /* WIN32 */
489
490
491 /*
492 * 'load_words()' - Load words from a file.
493 */
494
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 */
498 {
499 FILE *fp; /* Test file */
500 char word[256]; /* Word from file */
501
502
503 DEBUG_printf((" Loading \"%s\"...\n", filename));
504
505 if ((fp = fopen(filename, "r")) == NULL)
506 {
507 perror(filename);
508 return (0);
509 }
510
511 while (fscanf(fp, "%255s", word) == 1)
512 {
513 if (!cupsArrayFind(array, word))
514 {
515 DEBUG_printf((" Adding \"%s\"...\n", word));
516
517 cupsArrayAdd(array, strdup(word));
518 }
519 }
520
521 fclose(fp);
522
523 return (1);
524 }
525
526
527 /*
528 * End of "$Id: testarray.c 177 2006-06-21 00:20:03Z jlovell $".
529 */