1/*
2 * "$Id: testarray.c 11093 2013-07-03 20:48:42Z msweet $"
3 *
4 *   Array test program for CUPS.
5 *
6 *   Copyright 2007-2012 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-private.h"
31#include "dir.h"
32
33
34/*
35 * Local functions...
36 */
37
38static double	get_seconds(void);
39static int	load_words(const char *filename, cups_array_t *array);
40
41
42/*
43 * 'main()' - Main entry.
44 */
45
46int					/* O - Exit status */
47main(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      strlcpy(word, text, sizeof(word));
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  * Test the array with string functions...
409  */
410
411  fputs("_cupsArrayNewStrings(\" \\t\\nfoo bar\\tboo\\nfar\", ' '): ", stdout);
412  array = _cupsArrayNewStrings(" \t\nfoo bar\tboo\nfar", ' ');
413  if (!array)
414  {
415    status = 1;
416    puts("FAIL (unable to create array)");
417  }
418  else if (cupsArrayCount(array) != 4)
419  {
420    status = 1;
421    printf("FAIL (got %d elements, expected 4)\n", cupsArrayCount(array));
422  }
423  else if (strcmp(text = (char *)cupsArrayFirst(array), "bar"))
424  {
425    status = 1;
426    printf("FAIL (first element \"%s\", expected \"bar\")\n", text);
427  }
428  else if (strcmp(text = (char *)cupsArrayNext(array), "boo"))
429  {
430    status = 1;
431    printf("FAIL (first element \"%s\", expected \"boo\")\n", text);
432  }
433  else if (strcmp(text = (char *)cupsArrayNext(array), "far"))
434  {
435    status = 1;
436    printf("FAIL (first element \"%s\", expected \"far\")\n", text);
437  }
438  else if (strcmp(text = (char *)cupsArrayNext(array), "foo"))
439  {
440    status = 1;
441    printf("FAIL (first element \"%s\", expected \"foo\")\n", text);
442  }
443  else
444    puts("PASS");
445
446  fputs("_cupsArrayAddStrings(array, \"foo2,bar2\", ','): ", stdout);
447  _cupsArrayAddStrings(array, "foo2,bar2", ',');
448
449  if (cupsArrayCount(array) != 6)
450  {
451    status = 1;
452    printf("FAIL (got %d elements, expected 6)\n", cupsArrayCount(array));
453  }
454  else if (strcmp(text = (char *)cupsArrayFirst(array), "bar"))
455  {
456    status = 1;
457    printf("FAIL (first element \"%s\", expected \"bar\")\n", text);
458  }
459  else if (strcmp(text = (char *)cupsArrayNext(array), "bar2"))
460  {
461    status = 1;
462    printf("FAIL (first element \"%s\", expected \"bar2\")\n", text);
463  }
464  else if (strcmp(text = (char *)cupsArrayNext(array), "boo"))
465  {
466    status = 1;
467    printf("FAIL (first element \"%s\", expected \"boo\")\n", text);
468  }
469  else if (strcmp(text = (char *)cupsArrayNext(array), "far"))
470  {
471    status = 1;
472    printf("FAIL (first element \"%s\", expected \"far\")\n", text);
473  }
474  else if (strcmp(text = (char *)cupsArrayNext(array), "foo"))
475  {
476    status = 1;
477    printf("FAIL (first element \"%s\", expected \"foo\")\n", text);
478  }
479  else if (strcmp(text = (char *)cupsArrayNext(array), "foo2"))
480  {
481    status = 1;
482    printf("FAIL (first element \"%s\", expected \"foo2\")\n", text);
483  }
484  else
485    puts("PASS");
486
487  cupsArrayDelete(array);
488
489 /*
490  * Summarize the results and return...
491  */
492
493  if (!status)
494    puts("\nALL TESTS PASSED!");
495  else
496    printf("\n%d TEST(S) FAILED!\n", status);
497
498  return (status);
499}
500
501
502/*
503 * 'get_seconds()' - Get the current time in seconds...
504 */
505
506#ifdef WIN32
507#  include <windows.h>
508
509
510static double
511get_seconds(void)
512{
513}
514#else
515#  include <sys/time.h>
516
517
518static double
519get_seconds(void)
520{
521  struct timeval	curtime;	/* Current time */
522
523
524  gettimeofday(&curtime, NULL);
525  return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
526}
527#endif /* WIN32 */
528
529
530/*
531 * 'load_words()' - Load words from a file.
532 */
533
534static int				/* O - 1 on success, 0 on failure */
535load_words(const char   *filename,	/* I - File to load */
536           cups_array_t *array)		/* I - Array to add to */
537{
538  FILE		*fp;			/* Test file */
539  char		word[256];		/* Word from file */
540
541
542  if ((fp = fopen(filename, "r")) == NULL)
543  {
544    perror(filename);
545    return (0);
546  }
547
548  while (fscanf(fp, "%255s", word) == 1)
549  {
550    if (!cupsArrayFind(array, word))
551      cupsArrayAdd(array, strdup(word));
552  }
553
554  fclose(fp);
555
556  return (1);
557}
558
559
560/*
561 * End of "$Id: testarray.c 11093 2013-07-03 20:48:42Z msweet $".
562 */
563