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