Revert manifest to default one
[external/cups.git] / cups / testarray.c
1 /*
2  * "$Id: testarray.c 9099 2010-04-11 07:16:05Z mike $"
3  *
4  *   Array test program for CUPS.
5  *
6  *   Copyright 2007-2010 by Apple Inc.
7  *   Copyright 1997-2006 by Easy Software Products.
8  *
9  *   These coded instructions, statements, and computer programs are the
10  *   property of Apple Inc. and are protected by Federal copyright
11  *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
12  *   which should have been included with this file.  If this file is
13  *   file is missing or damaged, see the license at "http://www.cups.org/".
14  *
15  *   This file is subject to the Apple OS-Developed Software exception.
16  *
17  * Contents:
18  *
19  *   main()        - Main entry.
20  *   get_seconds() - Get the current time in seconds...
21  *   load_words()  - Load words from a file.
22  */
23
24 /*
25  * Include necessary headers...
26  */
27
28 #include "string-private.h"
29 #include "debug-private.h"
30 #include "array.h"
31 #include "dir.h"
32
33
34 /*
35  * Local functions...
36  */
37
38 static double   get_seconds(void);
39 static int      load_words(const char *filename, cups_array_t *array);
40
41
42 /*
43  * 'main()' - Main entry.
44  */
45
46 int                                     /* O - Exit status */
47 main(int  argc,                         /* I - Number of command-line arguments */
48      char *argv[])                      /* I - Command-line arguments */
49 {
50   int           i;                      /* Looping var */
51   cups_array_t  *array,                 /* Test array */
52                 *dup_array;             /* Duplicate array */
53   int           status;                 /* Exit status */
54   char          *text;                  /* Text from array */
55   char          word[256];              /* Word from file */
56   double        start,                  /* Start time */
57                 end;                    /* End time */
58   cups_dir_t    *dir;                   /* Current directory */
59   cups_dentry_t *dent;                  /* Directory entry */
60   char          *saved[32];             /* Saved entries */
61   void          *data;                  /* User data for arrays */
62
63
64  /*
65   * No errors so far...
66   */
67
68   status = 0;
69
70  /*
71   * cupsArrayNew()
72   */
73
74   fputs("cupsArrayNew: ", stdout);
75
76   data  = (void *)"testarray";
77   array = cupsArrayNew((cups_array_func_t)strcmp, data);
78
79   if (array)
80     puts("PASS");
81   else
82   {
83     puts("FAIL (returned NULL, expected pointer)");
84     status ++;
85   }
86
87  /*
88   * cupsArrayUserData()
89   */
90
91   fputs("cupsArrayUserData: ", stdout);
92   if (cupsArrayUserData(array) == data)
93     puts("PASS");
94   else
95   {
96     printf("FAIL (returned %p instead of %p!)\n", cupsArrayUserData(array),
97            data);
98     status ++;
99   }
100
101  /*
102   * cupsArrayAdd()
103   */
104
105   fputs("cupsArrayAdd: ", stdout);
106
107   if (!cupsArrayAdd(array, strdup("One Fish")))
108   {
109     puts("FAIL (\"One Fish\")");
110     status ++;
111   }
112   else
113   {
114     if (!cupsArrayAdd(array, strdup("Two Fish")))
115     {
116       puts("FAIL (\"Two Fish\")");
117       status ++;
118     }
119     else
120     {
121       if (!cupsArrayAdd(array, strdup("Red Fish")))
122       {
123         puts("FAIL (\"Red Fish\")");
124         status ++;
125       }
126       else
127       {
128         if (!cupsArrayAdd(array, strdup("Blue Fish")))
129         {
130           puts("FAIL (\"Blue Fish\")");
131           status ++;
132         }
133         else
134           puts("PASS");
135       }
136     }
137   }
138
139  /*
140   * cupsArrayCount()
141   */
142  
143   fputs("cupsArrayCount: ", stdout);
144   if (cupsArrayCount(array) == 4)
145     puts("PASS");
146   else
147   {
148     printf("FAIL (returned %d, expected 4)\n", cupsArrayCount(array));
149     status ++;
150   }
151
152  /*
153   * cupsArrayFirst()
154   */
155
156   fputs("cupsArrayFirst: ", stdout);
157   if ((text = (char *)cupsArrayFirst(array)) != NULL &&
158       !strcmp(text, "Blue Fish"))
159     puts("PASS");
160   else
161   {
162     printf("FAIL (returned \"%s\", expected \"Blue Fish\")\n", text);
163     status ++;
164   }
165
166  /*
167   * cupsArrayNext()
168   */
169
170   fputs("cupsArrayNext: ", stdout);
171   if ((text = (char *)cupsArrayNext(array)) != NULL &&
172       !strcmp(text, "One Fish"))
173     puts("PASS");
174   else
175   {
176     printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
177     status ++;
178   }
179
180  /*
181   * cupsArrayLast()
182   */
183
184   fputs("cupsArrayLast: ", stdout);
185   if ((text = (char *)cupsArrayLast(array)) != NULL &&
186       !strcmp(text, "Two Fish"))
187     puts("PASS");
188   else
189   {
190     printf("FAIL (returned \"%s\", expected \"Two Fish\")\n", text);
191     status ++;
192   }
193
194  /*
195   * cupsArrayPrev()
196   */
197
198   fputs("cupsArrayPrev: ", stdout);
199   if ((text = (char *)cupsArrayPrev(array)) != NULL &&
200       !strcmp(text, "Red Fish"))
201     puts("PASS");
202   else
203   {
204     printf("FAIL (returned \"%s\", expected \"Red Fish\")\n", text);
205     status ++;
206   }
207
208  /*
209   * cupsArrayFind()
210   */
211
212   fputs("cupsArrayFind: ", stdout);
213   if ((text = (char *)cupsArrayFind(array, (void *)"One Fish")) != NULL &&
214       !strcmp(text, "One Fish"))
215     puts("PASS");
216   else
217   {
218     printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
219     status ++;
220   }
221
222  /*
223   * cupsArrayCurrent()
224   */
225
226   fputs("cupsArrayCurrent: ", stdout);
227   if ((text = (char *)cupsArrayCurrent(array)) != NULL &&
228       !strcmp(text, "One Fish"))
229     puts("PASS");
230   else
231   {
232     printf("FAIL (returned \"%s\", expected \"One Fish\")\n", text);
233     status ++;
234   }
235
236  /*
237   * cupsArrayDup()
238   */
239
240   fputs("cupsArrayDup: ", stdout);
241   if ((dup_array = cupsArrayDup(array)) != NULL &&
242       cupsArrayCount(dup_array) == 4)
243     puts("PASS");
244   else
245   {
246     printf("FAIL (returned %p with %d elements, expected pointer with 4 elements)\n",
247            dup_array, cupsArrayCount(dup_array));
248     status ++;
249   }
250
251  /*
252   * cupsArrayRemove()
253   */
254
255   fputs("cupsArrayRemove: ", stdout);
256   if (cupsArrayRemove(array, (void *)"One Fish") &&
257       cupsArrayCount(array) == 3)
258     puts("PASS");
259   else
260   {
261     printf("FAIL (returned 0 with %d elements, expected 1 with 4 elements)\n",
262            cupsArrayCount(array));
263     status ++;
264   }
265
266  /*
267   * cupsArrayClear()
268   */
269
270   fputs("cupsArrayClear: ", stdout);
271   cupsArrayClear(array);
272   if (cupsArrayCount(array) == 0)
273     puts("PASS");
274   else
275   {
276     printf("FAIL (%d elements, expected 0 elements)\n",
277            cupsArrayCount(array));
278     status ++;
279   }
280
281  /*
282   * Now load this source file and grab all of the unique words...
283   */
284
285   fputs("Load unique words: ", stdout);
286   fflush(stdout);
287
288   start = get_seconds();
289
290   if ((dir = cupsDirOpen(".")) == NULL)
291   {
292     puts("FAIL (cupsDirOpen failed)");
293     status ++;
294   }
295   else
296   {
297     while ((dent = cupsDirRead(dir)) != NULL)
298     {
299       i = strlen(dent->filename) - 2;
300
301       if (i > 0 && dent->filename[i] == '.' &&
302           (dent->filename[i + 1] == 'c' ||
303            dent->filename[i + 1] == 'h'))
304         load_words(dent->filename, array);
305     }
306
307     cupsDirClose(dir);
308
309     end = get_seconds();
310
311     printf("%d words in %.3f seconds (%.0f words/sec), ", cupsArrayCount(array),
312            end - start, cupsArrayCount(array) / (end - start));
313     fflush(stdout);
314
315     for (text = (char *)cupsArrayFirst(array); text;)
316     {
317      /*
318       * Copy this word to the word buffer (safe because we strdup'd from
319       * the same buffer in the first place... :)
320       */
321
322       strcpy(word, text);
323
324      /*
325       * Grab the next word and compare...
326       */
327
328       if ((text = (char *)cupsArrayNext(array)) == NULL)
329         break;
330
331       if (strcmp(word, text) >= 0)
332         break;
333     }
334
335     if (text)
336     {
337       printf("FAIL (\"%s\" >= \"%s\"!)\n", word, text);
338       status ++;
339     }
340     else
341       puts("PASS");
342   }
343
344  /*
345   * Test deleting with iteration...
346   */
347
348   fputs("Delete While Iterating: ", stdout);
349
350   text = (char *)cupsArrayFirst(array);
351   cupsArrayRemove(array, text);
352   free(text);
353
354   text = (char *)cupsArrayNext(array);
355   if (!text)
356   {
357     puts("FAIL (cupsArrayNext returned NULL!)");
358     status ++;
359   }
360   else
361     puts("PASS");
362
363  /*
364   * Test save/restore...
365   */
366
367   fputs("cupsArraySave: ", stdout);
368
369   for (i = 0, text = (char *)cupsArrayFirst(array);
370        i < 32;
371        i ++, text = (char *)cupsArrayNext(array))
372   {
373     saved[i] = text;
374
375     if (!cupsArraySave(array))
376       break;
377   }
378
379   if (i < 32)
380     printf("FAIL (depth = %d)\n", i);
381   else
382     puts("PASS");
383
384   fputs("cupsArrayRestore: ", stdout);
385
386   while (i > 0)
387   {
388     i --;
389
390     text = cupsArrayRestore(array);
391     if (text != saved[i])
392       break;
393   }
394
395   if (i)
396     printf("FAIL (depth = %d)\n", i);
397   else
398     puts("PASS");
399
400  /*
401   * Delete the arrays...
402   */
403
404   cupsArrayDelete(array);
405   cupsArrayDelete(dup_array);
406
407  /*
408   * Summarize the results and return...
409   */
410
411   if (!status)
412     puts("\nALL TESTS PASSED!");
413   else
414     printf("\n%d TEST(S) FAILED!\n", status);
415
416   return (status);
417 }
418
419
420 /*
421  * 'get_seconds()' - Get the current time in seconds...
422  */
423
424 #ifdef WIN32
425 #  include <windows.h>
426
427
428 static double
429 get_seconds(void)
430 {
431 }
432 #else
433 #  include <sys/time.h>
434
435
436 static double
437 get_seconds(void)
438 {
439   struct timeval        curtime;        /* Current time */
440
441
442   gettimeofday(&curtime, NULL);
443   return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
444 }
445 #endif /* WIN32 */
446
447
448 /*
449  * 'load_words()' - Load words from a file.
450  */
451
452 static int                              /* O - 1 on success, 0 on failure */
453 load_words(const char   *filename,      /* I - File to load */
454            cups_array_t *array)         /* I - Array to add to */
455 {
456   FILE          *fp;                    /* Test file */
457   char          word[256];              /* Word from file */
458
459
460   if ((fp = fopen(filename, "r")) == NULL)
461   {
462     perror(filename);
463     return (0);
464   }
465
466   while (fscanf(fp, "%255s", word) == 1)
467   {
468     if (!cupsArrayFind(array, word))
469       cupsArrayAdd(array, strdup(word));
470   }
471
472   fclose(fp);
473
474   return (1);
475 }
476
477
478 /*
479  * End of "$Id: testarray.c 9099 2010-04-11 07:16:05Z mike $".
480  */