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