/[ascend]/trunk/base/generic/general/test/test_list.c
ViewVC logotype

Contents of /trunk/base/generic/general/test/test_list.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1243 - (show annotations) (download) (as text)
Fri Jan 26 12:35:17 2007 UTC (15 years, 5 months ago) by johnpye
File MIME type: text/x-csrc
File size: 61461 byte(s)
Removed header files from test_general. We can do without them.
1 /*
2 * Unit test functions for ASCEND: general/list.c
3 *
4 * Copyright (C) 2005 Jerry St.Clair
5 *
6 * This file is part of the Ascend Environment.
7 *
8 * The Ascend Environment is free software; you can redistribute it
9 * and/or modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * The Ascend Environment is distributed in hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with the program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
21 * COPYING.
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <utilities/ascConfig.h>
28 #include <utilities/ascMalloc.h>
29 #include <utilities/ascPrint.h>
30 #include <general/list.h>
31 #include "CUnit/CUnit.h"
32 #include "assertimpl.h"
33 #include <general/mathmacros.h>
34
35 /* comparison function used in test_list(). */
36 static
37 int compare_addresses(CONST VOIDPTR p1, CONST VOIDPTR p2)
38 {
39 return (int)((int*)p1 - (int*)p2);
40 }
41
42 /* comparison function used in test_list(). */
43 static
44 int compare_addresses_reverse(CONST VOIDPTR p1, CONST VOIDPTR p2)
45 {
46 return (int)((int*)p2 - (int*)p1);
47 }
48
49 /* comparison function used in test_list(). */
50 static
51 int compare_ints(CONST VOIDPTR p1, CONST VOIDPTR p2)
52 {
53 assert((NULL != p1) && (NULL != p2));
54 return *((int*)p1) - *((int*)p2);
55 }
56
57 /* transform function used in test_list(). */
58 static
59 void mult_by_2(VOIDPTR p)
60 {
61 if (NULL != p)
62 *((unsigned long*)p) = *((unsigned long*)p) * 2;
63 }
64
65 /* Returns the position [1..gl_length(list)] of a pointer in a list, or 0 if not found. */
66 static
67 unsigned long find_ptr_pos(const struct gl_list_t *list, VOIDPTR ptr)
68 {
69 unsigned long i;
70
71 assert(NULL != list);
72 for(i=0 ; i<gl_length(list) ; ++i) {
73 if (ptr == gl_fetch(list, (i+1)))
74 return (i+1);
75 }
76 return 0;
77 }
78
79 /* Returns the position [1..gl_length(list)] of a data value in a list, or 0 if not found. */
80 static
81 unsigned long find_int_pos(const struct gl_list_t *list, unsigned long value)
82 {
83 unsigned long i;
84 unsigned long* ptr;
85
86 assert(NULL != list);
87 for(i=0 ; i<gl_length(list) ; ++i) {
88 ptr = (unsigned long*)gl_fetch(list, (i+1));
89 if ((NULL != ptr) && (value == *ptr))
90 return (i+1);
91 }
92 return 0;
93 }
94
95 /* number of discarded lists to track for memory deallocation */
96 #define MAX_LISTS_TO_TRACK 50
97
98 /* Note - this function tests EITHER the pooled or unpooled versions
99 * of list.c. It tests whichever version was compiled into the ASCEND
100 * base library. This is usually the pooled version.
101 */
102 static void test_list(void)
103 {
104 /* array to hold discarded list pointers to check for deallocation */
105 struct gl_list_t *p_used_lists[MAX_LISTS_TO_TRACK];
106 unsigned long n_used_lists = 0; /* # of lists stored in same */
107
108 struct gl_list_t *p_list1;
109 struct gl_list_t *p_list2;
110 struct gl_list_t *p_list3;
111 unsigned long capacity;
112 unsigned long i;
113 unsigned long *pint_array[20];
114 int lists_were_active = FALSE;
115 int i_initialized_lists = FALSE;
116 unsigned long prior_meminuse;
117
118 prior_meminuse = ascmeminuse(); /* save meminuse() at start of function*/
119
120 #ifdef NDEBUG
121 CU_FAIL("test_list() compiled with NDEBUG - some features not tested.");
122 #endif
123 #ifndef MALLOC_DEBUG
124 CU_FAIL("test_list() compiled without MALLOC_DEBUG - memory management not tested.");
125 #endif
126
127 /* set up pooling & recycling */
128 #ifdef LISTUSESPOOL
129 CU_TEST(FALSE == gl_pool_initialized());
130 #else
131 CU_TEST(TRUE == gl_pool_initialized());
132 #endif
133
134 if (FALSE == gl_pool_initialized()) { /* initialize list system if necessary */
135 gl_init();
136 gl_init_pool();
137 i_initialized_lists = TRUE;
138 }
139 else {
140 lists_were_active = TRUE;
141 }
142
143 CU_TEST(TRUE == gl_pool_initialized());
144
145 /* NOTE: Each test section assumes that
146 * 1. pooling & recycling has been set up
147 * 2. the local gl_list_t* have been destroyed
148 * 3. pint_array[0..19] is allocated and initialized to [0..19]
149 *
150 * If a test section messes with any of these, then it must restore
151 * this state before finishing.
152 */
153
154 for (i=0 ; i<20 ; ++i) { /* create some test data */
155 pint_array[i] = (unsigned long*)ascmalloc(sizeof(unsigned long));
156 *pint_array[i] = i;
157 }
158
159 for (i=0 ; i<MAX_LISTS_TO_TRACK ; ++i) { /* initialize the array of used lists */
160 p_used_lists[i] = NULL;
161 }
162
163 /* test gl_create(), gl_destroy(), gl_free_and_destroy() */
164
165 p_list1 = gl_create(0); /* create a list having initial capacity = 0 */
166 CU_TEST(0 < gl_capacity(p_list1));
167 CU_TEST(0 == gl_length(p_list1));
168 CU_TEST(0 == gl_safe_length(p_list1));
169 CU_TEST(0 != gl_sorted(p_list1));
170 CU_TEST(0 != gl_expandable(p_list1));
171 CU_TEST(0 != gl_empty(p_list1));
172 CU_TEST(TRUE == gl_unique_list(p_list1));
173
174 capacity = gl_capacity(p_list1); /* store the minimum capacity for later use */
175 #ifdef MALLOC_DEBUG
176 CU_TEST(0 != AllocatedMemory((VOIDPTR)p_list1, capacity * sizeof(VOIDPTR)));
177 #endif
178
179 if (n_used_lists < MAX_LISTS_TO_TRACK) {
180 p_used_lists[n_used_lists++] = p_list1;
181 }
182 gl_destroy(p_list1); /* destroy the list and check for deallocation */
183
184 #ifdef MALLOC_DEBUG
185 #ifdef LISTUSESPOOL
186 CU_TEST(0 != AllocatedMemory((VOIDPTR)p_list1, capacity * sizeof(VOIDPTR)));
187 #else
188 CU_TEST(0 == AllocatedMemory((VOIDPTR)p_list1, capacity * sizeof(VOIDPTR)));
189 #endif /* LISTUSESPOOL */
190 #endif /* MALLOC_DEBUG */
191
192 p_list1 = gl_create(10); /* create a new list with capacity = 10 */
193 CU_TEST(10 <= gl_capacity(p_list1));
194 CU_TEST(0 == gl_length(p_list1));
195 CU_TEST(0 == gl_safe_length(p_list1));
196 CU_TEST(0 != gl_sorted(p_list1));
197 CU_TEST(0 != gl_expandable(p_list1));
198 CU_TEST(0 != gl_empty(p_list1));
199 CU_TEST(TRUE == gl_unique_list(p_list1));
200
201 for (i=0 ; i<10 ; ++i) { /* fill the list with data */
202 gl_append_ptr(p_list1, pint_array[i]);
203 }
204 CU_TEST(10 <= gl_capacity(p_list1));
205 CU_TEST(10 == gl_length(p_list1));
206 CU_TEST(10 == gl_safe_length(p_list1));
207 CU_TEST(0 == gl_sorted(p_list1));
208 CU_TEST(0 != gl_expandable(p_list1));
209 CU_TEST(0 == gl_empty(p_list1));
210 CU_TEST(TRUE == gl_unique_list(p_list1));
211
212 #ifdef MALLOC_DEBUG
213 for (i=0 ; i<10 ; ++i) { /* check that all pointers are still active */
214 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
215 }
216 #endif
217
218 if (n_used_lists < MAX_LISTS_TO_TRACK) {
219 p_used_lists[n_used_lists++] = p_list1;
220 }
221 gl_destroy(p_list1); /* clean up, leaving data in tact */
222
223 #ifdef MALLOC_DEBUG
224 for (i=0 ; i<10 ; ++i) { /* check that data is still in tact */
225 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
226 }
227 #endif
228
229 p_list1 = gl_create(5); /* create the list again, fill it with half the data */
230 for (i=0 ; i<5 ; ++i) {
231 gl_append_ptr(p_list1, pint_array[i]);
232 }
233
234 #ifdef MALLOC_DEBUG
235 for (i=0 ; i<20 ; ++i) { /* check that all pointers are still active */
236 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
237 }
238 #endif
239
240 if (n_used_lists < MAX_LISTS_TO_TRACK) {
241 p_used_lists[n_used_lists++] = p_list1;
242 }
243 gl_free_and_destroy(p_list1); /* clean up, deallocating data stored in list */
244
245 #ifdef MALLOC_DEBUG
246 for (i=0 ; i<20 ; ++i) { /* check that some data was deallocated */
247 if (i < 5) {
248 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
249 }
250 else {
251 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
252 }
253 }
254 #endif
255
256 for (i=5 ; i<10 ; ++i) { /* deallocate the rest of the data */
257 ascfree(pint_array[i]);
258 }
259
260 #ifdef MALLOC_DEBUG
261 for (i=0 ; i<20 ; ++i) { /* confirm that all data is now deallocated */
262 if (i < 10) {
263 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
264 }
265 else {
266 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
267 }
268 }
269 #endif
270
271 for (i=0 ; i<10 ; ++i) { /* restore test array */
272 pint_array[i] = (unsigned long*)ascmalloc(sizeof(unsigned long));
273 *pint_array[i] = i;
274 }
275
276 /* test gl_fetch() */
277
278 p_list1 = gl_create(10); /* create and fill a list */
279
280 for (i=0 ; i<10 ; ++i) {
281 gl_append_ptr(p_list1, pint_array[i]);
282 }
283
284 #ifndef ASC_NO_ASSERTIONS
285 asc_assert_catch(TRUE); /* prepare to test assertions */
286
287 asc_assert_reset();
288 if (0 == setjmp(g_asc_test_env))
289 gl_fetch(p_list1, 0); /* error if pos out of range (< 1) */
290 CU_TEST(TRUE == asc_assert_failed());
291
292 asc_assert_reset();
293 if (0 == setjmp(g_asc_test_env))
294 gl_fetch(p_list1, 11); /* error if pos out of range (> gl_length(list) */
295 CU_TEST(TRUE == asc_assert_failed());
296
297 asc_assert_reset();
298 if (0 == setjmp(g_asc_test_env))
299 gl_fetch(NULL, 1); /* error if NULL list* */
300 CU_TEST(TRUE == asc_assert_failed());
301
302 asc_assert_catch(FALSE); /* done testing assertions */
303 #endif /* !ASC_NO_ASSERTIONS */
304
305 for (i=0 ; i<10 ; ++i) {
306 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+1));
307 CU_TEST(i == *((unsigned long*)gl_fetch(p_list1, i+1)));
308 }
309
310 if (n_used_lists < MAX_LISTS_TO_TRACK) {
311 p_used_lists[n_used_lists++] = p_list1;
312 }
313 gl_destroy(p_list1); /* clean up the list, preserving data */
314
315 /* test gl_store() */
316
317 p_list1 = gl_create(10); /* create and fill a list */
318
319 for (i=0 ; i<10 ; ++i) {
320 gl_append_ptr(p_list1, pint_array[i]);
321 }
322
323 #ifndef ASC_NO_ASSERTIONS
324 asc_assert_catch(TRUE); /* prepare to test assertions */
325
326 asc_assert_reset();
327 if (0 == setjmp(g_asc_test_env))
328 gl_store(p_list1, 0, NULL); /* error if pos out of range (< 1) */
329 CU_TEST(TRUE == asc_assert_failed());
330
331 asc_assert_reset();
332 if (0 == setjmp(g_asc_test_env))
333 gl_store(p_list1, 11, NULL); /* error if pos out of range (> gl_length(list) */
334 CU_TEST(TRUE == asc_assert_failed());
335
336 asc_assert_reset();
337 if (0 == setjmp(g_asc_test_env))
338 gl_store(NULL, 1, NULL); /* error if NULL list* */
339 CU_TEST(TRUE == asc_assert_failed());
340
341 asc_assert_catch(FALSE); /* done testing assertions */
342 #endif /* !ASC_NO_ASSERTIONS */
343
344 for (i=0 ; i<10 ; ++i) { /* modify the list data & check for accuracy */
345 gl_store(p_list1, i+1, pint_array[9-i]);
346 }
347
348 for (i=10 ; i>0 ; --i) {
349 CU_TEST(pint_array[10-i] == gl_fetch(p_list1, i));
350 CU_TEST((10-i) == *((unsigned long*)gl_fetch(p_list1, i)));
351 }
352
353 for (i=5 ; i<10 ; ++i) { /* should be able to store NULL pointers also */
354 gl_store(p_list1, i+1, NULL);
355 }
356
357 for (i=10 ; i>5 ; --i) {
358 CU_TEST(NULL == gl_fetch(p_list1, i));
359 }
360
361 for (i=5 ; i>0 ; --i) {
362 CU_TEST(pint_array[10-i] == gl_fetch(p_list1, i));
363 CU_TEST((10-i) == *((unsigned long*)gl_fetch(p_list1, i)));
364 }
365
366 if (n_used_lists < MAX_LISTS_TO_TRACK) {
367 p_used_lists[n_used_lists++] = p_list1;
368 }
369 gl_destroy(p_list1); /* clean up the list, preserving data */
370
371 /* test gl_append_ptr() */
372
373 p_list1 = gl_create(0); /* create an empty list with minimal capacity */
374
375 #ifndef ASC_NO_ASSERTIONS
376 asc_assert_catch(TRUE); /* prepare to test assertions */
377
378 CU_TEST(0 != gl_expandable(p_list1));
379 gl_set_expandable(p_list1, FALSE); /* set list non-expandable */
380 asc_assert_reset();
381 if (0 == setjmp(g_asc_test_env))
382 gl_append_ptr(p_list1, NULL); /* error if list not expandable */
383 CU_TEST(TRUE == asc_assert_failed());
384 CU_TEST(0 == gl_length(p_list1));
385
386 asc_assert_reset();
387 if (0 == setjmp(g_asc_test_env))
388 gl_append_ptr(NULL, NULL); /* error if NULL list* */
389 CU_TEST(TRUE == asc_assert_failed());
390
391 asc_assert_catch(FALSE); /* done testing assertions */
392 #endif /* !ASC_NO_ASSERTIONS */
393
394 gl_set_expandable(p_list1, TRUE); /* reset list to expandable */
395
396 CU_TEST(0 == gl_length(p_list1));
397
398 for (i=0 ; i<10 ; ++i) { /* append some data to the list */
399 gl_append_ptr(p_list1, pint_array[i]);
400 CU_TEST((i+1) == gl_length(p_list1));
401 }
402
403 for (i=0 ; i<10 ; ++i) { /* confirm the data in the list */
404 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+1));
405 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+1)));
406 }
407
408 for (i=10 ; i<15 ; ++i) { /* should be able to append NULL pointers also */
409 gl_append_ptr(p_list1, NULL);
410 CU_TEST((i+1) == gl_length(p_list1));
411 }
412
413 for (i=0 ; i<10 ; ++i) { /* confirm the data in the list */
414 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+1));
415 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+1)));
416 }
417
418 for (i=10 ; i<15 ; ++i) {
419 CU_TEST(NULL == gl_fetch(p_list1, i+1));
420 }
421
422 if (n_used_lists < MAX_LISTS_TO_TRACK) {
423 p_used_lists[n_used_lists++] = p_list1;
424 }
425 gl_destroy(p_list1); /* clean up the list, preserving data */
426
427 /* test gl_fast_append_ptr() */
428
429 p_list1 = gl_create(10); /* create an emptylist */
430
431 #ifndef ASC_NO_ASSERTIONS
432 asc_assert_catch(TRUE); /* prepare to test assertions */
433
434 CU_TEST(0 != gl_expandable(p_list1));
435 gl_set_expandable(p_list1, FALSE); /* set list non-expandable */
436 CU_TEST(0 == gl_length(p_list1));
437
438 asc_assert_reset();
439 if (0 == setjmp(g_asc_test_env))
440 gl_fast_append_ptr(NULL, NULL); /* error if NULL list* */
441 CU_TEST(TRUE == asc_assert_failed());
442
443 asc_assert_catch(FALSE); /* done testing assertions */
444 #endif /* !ASC_NO_ASSERTIONS */
445
446 gl_set_expandable(p_list1, TRUE); /* reset list to expandable */
447
448 CU_TEST(0 == gl_length(p_list1));
449
450 for (i=0 ; i<8 ; ++i) { /* append some data to the list */
451 gl_fast_append_ptr(p_list1, pint_array[i]);
452 CU_TEST((i+1) == gl_length(p_list1));
453 }
454
455 for (i=0 ; i<8 ; ++i) { /* confirm the data in the list */
456 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+1));
457 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+1)));
458 }
459
460 for (i=8 ; i<10 ; ++i) { /* should be able to append NULL pointers also */
461 gl_fast_append_ptr(p_list1, NULL);
462 CU_TEST((i+1) == gl_length(p_list1));
463 }
464
465 for (i=0 ; i<8 ; ++i) { /* confirm the data in the list */
466 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+1));
467 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+1)));
468 }
469
470 for (i=8 ; i<10 ; ++i) {
471 CU_TEST(NULL == gl_fetch(p_list1, i+1));
472 }
473
474 if (n_used_lists < MAX_LISTS_TO_TRACK) {
475 p_used_lists[n_used_lists++] = p_list1;
476 }
477 gl_destroy(p_list1); /* clean up the list, preserving data */
478
479 /* test gl_append_list() */
480
481 p_list1 = gl_create(0); /* create 2 empty lists */
482 p_list2 = gl_create(0);
483
484 #ifndef ASC_NO_ASSERTIONS
485 asc_assert_catch(TRUE); /* prepare to test assertions */
486
487 CU_TEST(0 != gl_expandable(p_list1));
488 CU_TEST(0 != gl_expandable(p_list2));
489 gl_set_expandable(p_list1, FALSE); /* set list non-expandable */
490 asc_assert_reset();
491 if (0 == setjmp(g_asc_test_env))
492 gl_append_list(p_list1, p_list2); /* error if 1st list not expandable */
493 CU_TEST(TRUE == asc_assert_failed());
494
495 asc_assert_reset();
496 if (0 == setjmp(g_asc_test_env))
497 gl_append_list(p_list2, p_list1); /* ok if 2nd list not expandable */
498 CU_TEST(FALSE == asc_assert_failed());
499
500 CU_TEST(0 == gl_length(p_list1)); /* but, appending empty lists should have no effect */
501 CU_TEST(0 == gl_length(p_list2));
502
503 gl_set_expandable(p_list1, TRUE); /* reset list to expandable */
504
505 asc_assert_reset();
506 if (0 == setjmp(g_asc_test_env))
507 gl_append_list(NULL, p_list2); /* error if NULL list* */
508 CU_TEST(TRUE == asc_assert_failed());
509
510 asc_assert_reset();
511 if (0 == setjmp(g_asc_test_env))
512 gl_append_list(p_list1, NULL); /* error if NULL list* */
513 CU_TEST(TRUE == asc_assert_failed());
514
515 asc_assert_catch(FALSE); /* done testing assertions */
516 #endif /* !ASC_NO_ASSERTIONS */
517
518 for (i=0 ; i<10 ; ++i) { /* append some data to the lists */
519 gl_append_ptr(p_list1, pint_array[i]);
520 }
521 CU_TEST(10 == gl_length(p_list1));
522 CU_TEST(0 == gl_length(p_list2));
523
524 gl_append_list(p_list1, p_list2); /* appending empty list has no effect */
525 CU_TEST(10 == gl_length(p_list1));
526 CU_TEST(0 == gl_length(p_list2));
527
528 gl_append_list(p_list2, p_list1); /* appending to an empty list sould be ok */
529 CU_TEST(10 == gl_length(p_list1));
530 CU_TEST(10 == gl_length(p_list2));
531
532 for (i=0 ; i<10 ; ++i) { /* check the list contents */
533 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+1));
534 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+1)));
535 CU_TEST(pint_array[i] == gl_fetch(p_list2, i+1));
536 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list2, i+1)));
537 }
538
539 gl_append_list(p_list1, p_list2); /* appending full list should be ok */
540 CU_TEST(20 == gl_length(p_list1));
541 CU_TEST(10 == gl_length(p_list2));
542
543 for (i=0 ; i<10 ; ++i) { /* check the list contents */
544 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+1));
545 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+1)));
546 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+11));
547 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+11)));
548 CU_TEST(pint_array[i] == gl_fetch(p_list2, i+1));
549 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list2, i+1)));
550 }
551
552 if (n_used_lists < MAX_LISTS_TO_TRACK) {
553 p_used_lists[n_used_lists++] = p_list2;
554 }
555 gl_destroy(p_list2); /* create a list with NULL pointer data */
556 p_list2 = gl_create(2);
557 gl_append_ptr(p_list2, NULL);
558 gl_append_ptr(p_list2, NULL);
559
560 gl_append_list(p_list1, p_list2); /* appending list holding NULL pointers should be ok */
561 CU_TEST(22 == gl_length(p_list1));
562 CU_TEST(2 == gl_length(p_list2));
563
564 for (i=0 ; i<10 ; ++i) { /* check the list contents */
565 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+1));
566 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+1)));
567 CU_TEST(pint_array[i] == gl_fetch(p_list1, i+11));
568 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list1, i+11)));
569 }
570
571 for (i=0 ; i<2 ; ++i) { /* check the list contents */
572 CU_TEST(NULL == gl_fetch(p_list1, i+21));
573 CU_TEST(NULL == gl_fetch(p_list2, i+1));
574 }
575
576 if (n_used_lists < MAX_LISTS_TO_TRACK) {
577 p_used_lists[n_used_lists++] = p_list1;
578 }
579 gl_destroy(p_list1); /* clean up the lists, preserving data */
580 if (n_used_lists < MAX_LISTS_TO_TRACK) {
581 p_used_lists[n_used_lists++] = p_list2;
582 }
583 gl_destroy(p_list2);
584
585 /* test (sort-of) gl_init(), gl_init_pool(), gl_destroy_pool() */
586
587 #ifdef MALLOC_DEBUG
588 for (i=0 ; i<(MIN(n_used_lists, MAX_LISTS_TO_TRACK)) ; ++i) {
589 CU_TEST(0 != AllocatedMemory((VOIDPTR)p_used_lists[i], capacity * sizeof(VOIDPTR)));
590 }
591 #endif
592 gl_destroy_pool();
593 #ifdef MALLOC_DEBUG
594 for (i=0 ; i<(MIN(n_used_lists, MAX_LISTS_TO_TRACK)) ; ++i) {
595 CU_TEST(0 == AllocatedMemory((VOIDPTR)p_used_lists[i], capacity * sizeof(VOIDPTR)));
596 }
597 #endif
598
599 gl_init_pool();
600 gl_init(); /* should be able to call this again without problems */
601
602 n_used_lists = 0;
603
604 /* test gl_length(), gl_safe_length() */
605
606 p_list1 = gl_create(10); /* create a list */
607
608 #ifndef ASC_NO_ASSERTIONS
609 asc_assert_catch(TRUE); /* prepare to test assertions */
610
611 asc_assert_reset();
612 if (0 == setjmp(g_asc_test_env))
613 (void)gl_length(NULL); /* error if NULL list* */
614 CU_TEST(TRUE == asc_assert_failed());
615
616 asc_assert_reset();
617 if (0 == setjmp(g_asc_test_env))
618 CU_TEST(0 == gl_safe_length(NULL)); /* no error if NULL list* for gl_safe_length() */
619 CU_TEST(FALSE == asc_assert_failed());
620
621 asc_assert_catch(FALSE); /* done testing assertions */
622 #endif /* !ASC_NO_ASSERTIONS */
623
624 CU_TEST(0 == gl_length(p_list1));
625 CU_TEST(0 == gl_safe_length(p_list1));
626 for (i=0 ; i<10 ; ++i) {
627 gl_append_ptr(p_list1, pint_array[i]);
628 CU_TEST((i+1) == gl_length(p_list1));
629 CU_TEST((i+1) == gl_safe_length(p_list1));
630 }
631
632 if (n_used_lists < MAX_LISTS_TO_TRACK) {
633 p_used_lists[n_used_lists++] = p_list1;
634 }
635 gl_destroy(p_list1); /* clean up the list, preserving data */
636
637 /* test gl_capacity() */
638
639 p_list1 = gl_create(0); /* create a list having minimum capacity */
640 capacity = gl_capacity(p_list1);
641
642 CU_TEST(0 == gl_capacity(NULL)); /* NULL is ok for gl_capacity() */
643
644 for (i=0 ; i<=capacity ; ++i) { /* append pointers to force capacity to expand */
645 gl_append_ptr(p_list1, NULL);
646 }
647 CU_TEST(capacity < gl_capacity(p_list1));
648
649 if (n_used_lists < MAX_LISTS_TO_TRACK) {
650 p_used_lists[n_used_lists++] = p_list1;
651 }
652 gl_destroy(p_list1); /* clean up the list, preserving data */
653
654 p_list1 = gl_create(capacity+1); /* create a list having larger capacity */
655 CU_TEST(capacity < gl_capacity(p_list1));
656
657 if (n_used_lists < MAX_LISTS_TO_TRACK) {
658 p_used_lists[n_used_lists++] = p_list1;
659 }
660 gl_destroy(p_list1); /* clean up the list, preserving data */
661
662 /* test gl_sort(), gl_sorted(), gl_insert_sorted(), gl_set_sorted() */
663
664 p_list1 = gl_create(0); /* create a list having minimum capacity */
665
666 #ifndef ASC_NO_ASSERTIONS
667 asc_assert_catch(TRUE); /* prepare to test assertions */
668
669 asc_assert_reset();
670 if (0 == setjmp(g_asc_test_env))
671 (void)gl_sorted(NULL); /* error if NULL list* */
672 CU_TEST(TRUE == asc_assert_failed());
673
674 asc_assert_reset();
675 if (0 == setjmp(g_asc_test_env))
676 gl_sort(NULL, compare_addresses); /* error if NULL list* */
677 CU_TEST(TRUE == asc_assert_failed());
678
679 asc_assert_reset();
680 if (0 == setjmp(g_asc_test_env))
681 gl_sort(p_list1, NULL); /* error if NULL func */
682 CU_TEST(TRUE == asc_assert_failed());
683
684 asc_assert_reset();
685 if (0 == setjmp(g_asc_test_env))
686 gl_insert_sorted(NULL, NULL, compare_addresses); /* error if NULL list* */
687 CU_TEST(TRUE == asc_assert_failed());
688
689 asc_assert_reset();
690 if (0 == setjmp(g_asc_test_env))
691 gl_insert_sorted(p_list1, NULL, NULL); /* error if NULL func */
692 CU_TEST(TRUE == asc_assert_failed());
693
694 CU_TEST(0 != gl_expandable(p_list1));
695 gl_set_expandable(p_list1, FALSE); /* set list non-expandable */
696
697 asc_assert_reset();
698 if (0 == setjmp(g_asc_test_env))
699 gl_insert_sorted(p_list1, NULL, compare_addresses);/* error if list not expandable */
700 CU_TEST(TRUE == asc_assert_failed());
701
702 gl_set_expandable(p_list1, TRUE); /* reset list to expandable */
703
704 asc_assert_reset();
705 if (0 == setjmp(g_asc_test_env))
706 gl_set_sorted(NULL, TRUE); /* error if NULL list* */
707 CU_TEST(TRUE == asc_assert_failed());
708
709 asc_assert_catch(FALSE); /* done testing assertions */
710 #endif /* !ASC_NO_ASSERTIONS */
711
712 CU_TEST(0 != gl_sorted(p_list1));
713 gl_sort(p_list1, compare_addresses); /* sorting an empty list is ok but has no effect */
714 CU_TEST(0 != gl_sorted(p_list1));
715
716 gl_append_ptr(p_list1, NULL);
717 CU_TEST(0 != gl_sorted(p_list1));
718 gl_sort(p_list1, compare_addresses); /* sorting a list with 1 element is ok but has no effect */
719 CU_TEST(0 != gl_sorted(p_list1));
720 CU_TEST(NULL == gl_fetch(p_list1, 1));
721 gl_reset(p_list1);
722
723 gl_insert_sorted(p_list1, pint_array[5], compare_addresses);
724 CU_TEST(1 == gl_length(p_list1)); /* should be able to insert into an empty (sorted) list */
725 CU_TEST(0 != gl_sorted(p_list1));
726
727 gl_insert_sorted(p_list1, pint_array[2], compare_addresses);
728 CU_TEST(2 == gl_length(p_list1));
729 CU_TEST(0 != gl_sorted(p_list1));
730 CU_TEST(gl_fetch(p_list1, 2) >= gl_fetch(p_list1, 1));
731
732 gl_insert_sorted(p_list1, NULL, compare_addresses);
733 CU_TEST(3 == gl_length(p_list1));
734 CU_TEST(0 != gl_sorted(p_list1));
735 CU_TEST(gl_fetch(p_list1, 3) >= gl_fetch(p_list1, 2));
736 CU_TEST(gl_fetch(p_list1, 2) >= gl_fetch(p_list1, 1));
737
738 gl_reset(p_list1); /* clear list for next test */
739
740 gl_insert_sorted(p_list1, pint_array[5], compare_ints);
741 CU_TEST(1 == gl_length(p_list1)); /* should be able to insert into an empty (sorted) list */
742 CU_TEST(0 != gl_sorted(p_list1));
743
744 gl_insert_sorted(p_list1, pint_array[2], compare_ints);
745 CU_TEST(2 == gl_length(p_list1));
746 CU_TEST(0 != gl_sorted(p_list1));
747 CU_TEST(*((unsigned long*)gl_fetch(p_list1, 2)) >= *((unsigned long*)gl_fetch(p_list1, 1)));
748
749 gl_insert_sorted(p_list1, pint_array[0], compare_ints);
750 CU_TEST(3 == gl_length(p_list1));
751 CU_TEST(0 != gl_sorted(p_list1));
752 CU_TEST(*((unsigned long*)gl_fetch(p_list1, 3)) >= *((unsigned long*)gl_fetch(p_list1, 2)));
753 CU_TEST(*((unsigned long*)gl_fetch(p_list1, 2)) >= *((unsigned long*)gl_fetch(p_list1, 1)));
754
755 gl_set_sorted(p_list1, FALSE); /* make sure gl_set_sorted() works */
756 CU_TEST(0 == gl_sorted(p_list1));
757 gl_set_sorted(p_list1, TRUE);
758 CU_TEST(0 != gl_sorted(p_list1));
759 gl_set_sorted(p_list1, FALSE);
760 CU_TEST(0 == gl_sorted(p_list1));
761
762 gl_reset(p_list1); /* clear list for next test */
763
764 for (i=0 ; i<10 ; ++i) { /* fill the list with unsorted data */
765 gl_append_ptr(p_list1, pint_array[9-i]);
766 if (i < 1) {
767 CU_TEST(0 != gl_sorted(p_list1));
768 }
769 else {
770 CU_TEST(0 == gl_sorted(p_list1));
771 }
772 }
773
774 gl_sort(p_list1, compare_addresses); /* sort the list by pointer address */
775 CU_TEST(0 != gl_sorted(p_list1));
776 for (i=0 ; i<9 ; ++i) {
777 CU_TEST(gl_fetch(p_list1, i+2) >= gl_fetch(p_list1, i+1));
778 }
779
780 for (i=0 ; i<10 ; ++i) { /* fill the list with unsorted data again */
781 gl_store(p_list1, i+1, pint_array[9-i]);
782 CU_TEST(0 == gl_sorted(p_list1));
783 }
784
785 gl_sort(p_list1, compare_ints); /* sort the list by data value */
786 CU_TEST(0 != gl_sorted(p_list1));
787 for (i=0 ; i<9 ; ++i) {
788 CU_TEST(*((unsigned long*)gl_fetch(p_list1, i+2)) >= *((unsigned long*)gl_fetch(p_list1, i+1)));
789 }
790
791 for (i=0 ; i<10 ; ++i) { /* fill the list with unsorted data again */
792 gl_store(p_list1, i+1, pint_array[9-i]);
793 CU_TEST(0 == gl_sorted(p_list1));
794 }
795 gl_store(p_list1, 3, NULL); /* add a couple of NULL pointers to the list */
796 gl_store(p_list1, 8, NULL);
797
798 gl_sort(p_list1, compare_addresses); /* sort the list by pointer address (can handle NULL's) */
799 CU_TEST(0 != gl_sorted(p_list1));
800 for (i=0 ; i<9 ; ++i) {
801 CU_TEST(gl_fetch(p_list1, i+2) >= gl_fetch(p_list1, i+1));
802 }
803
804 for (i=0 ; i<10 ; ++i) { /* fill the list with unsorted data again */
805 gl_store(p_list1, i+1, pint_array[9-i]);
806 CU_TEST(0 == gl_sorted(p_list1));
807 }
808
809 CU_TEST(0 == gl_sorted(p_list1)); /* calling gl_insert_sorted() on unsorted list results in sort */
810 gl_insert_sorted(p_list1, pint_array[11], compare_addresses);
811 CU_TEST(11 == gl_length(p_list1));
812 CU_TEST(0 != gl_sorted(p_list1));
813 for (i=0 ; i<10 ; ++i) {
814 CU_TEST(gl_fetch(p_list1, i+2) >= gl_fetch(p_list1, i+1));
815 }
816
817 if (n_used_lists < MAX_LISTS_TO_TRACK) {
818 p_used_lists[n_used_lists++] = p_list1;
819 }
820 gl_destroy(p_list1); /* clean up the list, preserving data */
821
822 /* test gl_iterate() */
823
824 p_list1 = gl_create(10); /* create a list and fill with data */
825 for (i=0 ; i<10 ; ++i) {
826 gl_append_ptr(p_list1, pint_array[i]);
827 }
828
829 #ifndef ASC_NO_ASSERTIONS
830 asc_assert_catch(TRUE); /* prepare to test assertions */
831
832 asc_assert_reset();
833 if (0 == setjmp(g_asc_test_env))
834 gl_iterate(NULL, mult_by_2); /* error if NULL list* */
835 CU_TEST(TRUE == asc_assert_failed());
836
837 asc_assert_reset();
838 if (0 == setjmp(g_asc_test_env))
839 gl_iterate(p_list1, NULL); /* error if NULL func* */
840 CU_TEST(TRUE == asc_assert_failed());
841
842 asc_assert_catch(FALSE); /* done testing assertions */
843 #endif /* !ASC_NO_ASSERTIONS */
844
845 gl_iterate(p_list1, mult_by_2); /* execute function on each data element */
846
847 for (i=0 ; i<10 ; ++i) {
848 CU_TEST((2*i) == *((unsigned long*)gl_fetch(p_list1, i+1)));
849 }
850
851 if (n_used_lists < MAX_LISTS_TO_TRACK) {
852 p_used_lists[n_used_lists++] = p_list1;
853 }
854 gl_destroy(p_list1); /* clean up the list, preserving data */
855 for (i=0 ; i<20 ; ++i) /* need to restore our integer array */
856 *pint_array[i] = i;
857
858 /* test gl_ptr_search(), gl_search(), gl_search_reverse */
859
860 p_list1 = gl_create(10); /* create an empty list */
861
862 #ifndef ASC_NO_ASSERTIONS
863 asc_assert_catch(TRUE); /* prepare to test assertions */
864
865 asc_assert_reset();
866 if (0 == setjmp(g_asc_test_env))
867 gl_ptr_search(NULL, NULL, FALSE); /* error if NULL list* */
868 CU_TEST(TRUE == asc_assert_failed());
869
870 asc_assert_reset();
871 if (0 == setjmp(g_asc_test_env))
872 gl_search(NULL, NULL, compare_addresses); /* error if NULL list* */
873 CU_TEST(TRUE == asc_assert_failed());
874
875 asc_assert_reset();
876 if (0 == setjmp(g_asc_test_env))
877 gl_search(p_list1, NULL, NULL); /* error if NULL func */
878 CU_TEST(TRUE == asc_assert_failed());
879
880 asc_assert_reset();
881 if (0 == setjmp(g_asc_test_env))
882 gl_search_reverse(NULL, NULL, compare_addresses); /* error if NULL list* */
883 CU_TEST(TRUE == asc_assert_failed());
884
885 asc_assert_reset();
886 if (0 == setjmp(g_asc_test_env))
887 gl_search_reverse(p_list1, NULL, NULL); /* error if NULL func */
888 CU_TEST(TRUE == asc_assert_failed());
889
890 asc_assert_catch(FALSE); /* done testing assertions */
891 #endif /* !ASC_NO_ASSERTIONS */
892
893 CU_TEST(0 == gl_ptr_search(p_list1, NULL, FALSE)); /* search empty list */
894 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[9], FALSE));
895 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[0], FALSE));
896 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[5], FALSE));
897 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[10], FALSE));
898
899 CU_TEST(0 == gl_search(p_list1, NULL, compare_addresses));
900 CU_TEST(0 == gl_search(p_list1, pint_array[9], compare_addresses));
901 CU_TEST(0 == gl_search(p_list1, pint_array[0], compare_addresses));
902 CU_TEST(0 == gl_search(p_list1, pint_array[5], compare_addresses));
903 CU_TEST(0 == gl_search(p_list1, pint_array[10], compare_addresses));
904
905 CU_TEST(0 == gl_search_reverse(p_list1, NULL, compare_addresses));
906 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[9], compare_addresses));
907 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[0], compare_addresses));
908 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[5], compare_addresses));
909 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[10], compare_addresses));
910
911 gl_append_ptr(p_list1, pint_array[0]);
912
913 CU_TEST(0 == gl_ptr_search(p_list1, NULL, TRUE)); /* search list having 1 element*/
914 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[9], TRUE));
915 CU_TEST(1 == gl_ptr_search(p_list1, pint_array[0], TRUE));
916 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[5], TRUE));
917 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[10], TRUE));
918
919 CU_TEST(0 == gl_search(p_list1, NULL, compare_addresses));
920 CU_TEST(0 == gl_search(p_list1, pint_array[9], compare_addresses));
921 CU_TEST(1 == gl_search(p_list1, pint_array[0], compare_addresses));
922 CU_TEST(0 == gl_search(p_list1, pint_array[5], compare_addresses));
923 CU_TEST(0 == gl_search(p_list1, pint_array[10], compare_addresses));
924
925 CU_TEST(0 == gl_search_reverse(p_list1, NULL, compare_addresses));
926 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[9], compare_addresses));
927 CU_TEST(1 == gl_search_reverse(p_list1, pint_array[0], compare_addresses));
928 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[5], compare_addresses));
929 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[10], compare_addresses));
930
931 gl_reset(p_list1);
932 for (i=0 ; i<10 ; ++i) {
933 gl_append_ptr(p_list1, pint_array[9-i]);
934 }
935
936 CU_TEST(0 == gl_ptr_search(p_list1, NULL, FALSE)); /* search unsorted list */
937 CU_TEST(1 == gl_ptr_search(p_list1, pint_array[9], FALSE));
938 CU_TEST(10 == gl_ptr_search(p_list1, pint_array[0], FALSE));
939 CU_TEST(5 == gl_ptr_search(p_list1, pint_array[5], FALSE));
940 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[10], FALSE));
941
942 CU_TEST(0 == gl_search(p_list1, NULL, compare_addresses));
943 CU_TEST(1 == gl_search(p_list1, pint_array[9], compare_addresses));
944 CU_TEST(10 == gl_search(p_list1, pint_array[0], compare_addresses));
945 CU_TEST(5 == gl_search(p_list1, pint_array[5], compare_addresses));
946 CU_TEST(0 == gl_search(p_list1, pint_array[10], compare_addresses));
947
948 CU_TEST(0 == gl_search_reverse(p_list1, NULL, compare_addresses));
949 CU_TEST(1 == gl_search_reverse(p_list1, pint_array[9], compare_addresses));
950 CU_TEST(10 == gl_search_reverse(p_list1, pint_array[0], compare_addresses));
951 CU_TEST(5 == gl_search_reverse(p_list1, pint_array[5], compare_addresses));
952 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[10], compare_addresses));
953
954 gl_sort(p_list1, compare_addresses); /* sort in increasing pointer order */
955 CU_TEST(0 != gl_sorted(p_list1));
956
957 CU_TEST(0 == gl_ptr_search(p_list1, NULL, TRUE)); /* search sorted list - correct increasing */
958 CU_TEST(find_ptr_pos(p_list1, pint_array[9]) ==
959 gl_ptr_search(p_list1, pint_array[9], TRUE));
960 CU_TEST(find_ptr_pos(p_list1, pint_array[0]) ==
961 gl_ptr_search(p_list1, pint_array[0], TRUE));
962 CU_TEST(find_ptr_pos(p_list1, pint_array[5]) ==
963 gl_ptr_search(p_list1, pint_array[5], TRUE));
964 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[10], TRUE));
965
966 CU_TEST(0 == gl_ptr_search(p_list1, NULL, FALSE)); /* search sorted list - incorrect increasing */
967 CU_TEST((0 == gl_ptr_search(p_list1, pint_array[9], FALSE)) ||
968 (find_ptr_pos(p_list1, pint_array[9]) ==
969 gl_ptr_search(p_list1, pint_array[9], TRUE)));
970 CU_TEST((0 == gl_ptr_search(p_list1, pint_array[0], FALSE)) ||
971 (find_ptr_pos(p_list1, pint_array[0]) ==
972 gl_ptr_search(p_list1, pint_array[0], TRUE)));
973 CU_TEST((0 == gl_ptr_search(p_list1, pint_array[5], FALSE)) ||
974 (find_ptr_pos(p_list1, pint_array[5]) ==
975 gl_ptr_search(p_list1, pint_array[5], TRUE)));
976 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[10], FALSE));
977
978 CU_TEST(0 == gl_search(p_list1, NULL, compare_addresses));
979 CU_TEST(find_ptr_pos(p_list1, pint_array[9]) ==
980 gl_search(p_list1, pint_array[9], compare_addresses));
981 CU_TEST(find_ptr_pos(p_list1, pint_array[0]) ==
982 gl_search(p_list1, pint_array[0], compare_addresses));
983 CU_TEST(find_ptr_pos(p_list1, pint_array[5]) ==
984 gl_search(p_list1, pint_array[5], compare_addresses));
985 CU_TEST(0 == gl_search(p_list1, pint_array[10], compare_addresses));
986
987 CU_TEST(0 == gl_search_reverse(p_list1, NULL, compare_addresses));
988 CU_TEST(find_ptr_pos(p_list1, pint_array[9]) ==
989 gl_search_reverse(p_list1, pint_array[9], compare_addresses));
990 CU_TEST(find_ptr_pos(p_list1, pint_array[0]) ==
991 gl_search_reverse(p_list1, pint_array[0], compare_addresses));
992 CU_TEST(find_ptr_pos(p_list1, pint_array[5]) ==
993 gl_search_reverse(p_list1, pint_array[5], compare_addresses));
994 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[10], compare_addresses));
995
996 gl_sort(p_list1, compare_addresses_reverse); /* sort in decreasing pointer order */
997 CU_TEST(0 != gl_sorted(p_list1));
998
999 CU_TEST(0 == gl_ptr_search(p_list1, NULL, FALSE)); /* search sorted list - correct increasing */
1000 CU_TEST(find_ptr_pos(p_list1, pint_array[9]) ==
1001 gl_ptr_search(p_list1, pint_array[9], FALSE));
1002 CU_TEST(find_ptr_pos(p_list1, pint_array[0]) ==
1003 gl_ptr_search(p_list1, pint_array[0], FALSE));
1004 CU_TEST(find_ptr_pos(p_list1, pint_array[5]) ==
1005 gl_ptr_search(p_list1, pint_array[5], FALSE));
1006 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[10], FALSE));
1007
1008 CU_TEST(0 == gl_ptr_search(p_list1, NULL, TRUE)); /* search sorted list - incorrect increasing */
1009 CU_TEST((0 == gl_ptr_search(p_list1, pint_array[9], TRUE)) ||
1010 (find_ptr_pos(p_list1, pint_array[9]) ==
1011 gl_ptr_search(p_list1, pint_array[9], FALSE)));
1012 CU_TEST((0 == gl_ptr_search(p_list1, pint_array[0], TRUE)) ||
1013 (find_ptr_pos(p_list1, pint_array[0]) ==
1014 gl_ptr_search(p_list1, pint_array[0], FALSE)));
1015 CU_TEST((0 == gl_ptr_search(p_list1, pint_array[5], TRUE)) ||
1016 (find_ptr_pos(p_list1, pint_array[5]) ==
1017 gl_ptr_search(p_list1, pint_array[5], FALSE)));
1018 CU_TEST(0 == gl_ptr_search(p_list1, pint_array[10], TRUE));
1019
1020 CU_TEST(0 == gl_search(p_list1, NULL, compare_addresses_reverse));
1021 CU_TEST(find_ptr_pos(p_list1, pint_array[9]) ==
1022 gl_search(p_list1, pint_array[9], compare_addresses_reverse));
1023 CU_TEST(find_ptr_pos(p_list1, pint_array[0]) ==
1024 gl_search(p_list1, pint_array[0], compare_addresses_reverse));
1025 CU_TEST(find_ptr_pos(p_list1, pint_array[5]) ==
1026 gl_search(p_list1, pint_array[5], compare_addresses_reverse));
1027 CU_TEST(0 == gl_search(p_list1, pint_array[10], compare_addresses_reverse));
1028
1029 CU_TEST(0 == gl_search_reverse(p_list1, NULL, compare_addresses_reverse));
1030 CU_TEST(find_ptr_pos(p_list1, pint_array[9]) ==
1031 gl_search_reverse(p_list1, pint_array[9], compare_addresses_reverse));
1032 CU_TEST(find_ptr_pos(p_list1, pint_array[0]) ==
1033 gl_search_reverse(p_list1, pint_array[0], compare_addresses_reverse));
1034 CU_TEST(find_ptr_pos(p_list1, pint_array[5]) ==
1035 gl_search_reverse(p_list1, pint_array[5], compare_addresses_reverse));
1036 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[10], compare_addresses_reverse));
1037
1038 gl_sort(p_list1, compare_ints); /* sort by pointed-to integer */
1039 CU_TEST(0 != gl_sorted(p_list1));
1040
1041 CU_TEST(find_int_pos(p_list1, *pint_array[9]) ==
1042 gl_search(p_list1, pint_array[9], compare_ints));
1043 CU_TEST(find_int_pos(p_list1, *pint_array[0]) ==
1044 gl_search(p_list1, pint_array[0], compare_ints));
1045 CU_TEST(find_int_pos(p_list1, *pint_array[5]) ==
1046 gl_search(p_list1, pint_array[5], compare_ints));
1047 CU_TEST(0 == gl_search(p_list1, pint_array[10], compare_ints));
1048
1049 CU_TEST(find_int_pos(p_list1, *pint_array[9]) ==
1050 gl_search_reverse(p_list1, pint_array[9], compare_ints));
1051 CU_TEST(find_int_pos(p_list1, *pint_array[0]) ==
1052 gl_search_reverse(p_list1, pint_array[0], compare_ints));
1053 CU_TEST(find_int_pos(p_list1, *pint_array[5]) ==
1054 gl_search_reverse(p_list1, pint_array[5], compare_ints));
1055 CU_TEST(0 == gl_search_reverse(p_list1, pint_array[10], compare_ints));
1056
1057 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1058 p_used_lists[n_used_lists++] = p_list1;
1059 }
1060 gl_destroy(p_list1); /* clean up the list, preserving data */
1061
1062 /* test gl_empty() */
1063
1064 p_list1 = gl_create(10); /* create an empty list */
1065
1066 #ifndef ASC_NO_ASSERTIONS
1067 asc_assert_catch(TRUE); /* prepare to test assertions */
1068
1069 asc_assert_reset();
1070 if (0 == setjmp(g_asc_test_env))
1071 gl_empty(NULL); /* error if NULL list* */
1072 CU_TEST(TRUE == asc_assert_failed());
1073
1074 asc_assert_catch(FALSE); /* done testing assertions */
1075 #endif /* !ASC_NO_ASSERTIONS */
1076
1077 CU_TEST(TRUE == gl_empty(p_list1));
1078 gl_append_ptr(p_list1, pint_array[0]);
1079 CU_TEST(FALSE == gl_empty(p_list1));
1080 gl_reset(p_list1);
1081 CU_TEST(TRUE == gl_empty(p_list1));
1082
1083 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1084 p_used_lists[n_used_lists++] = p_list1;
1085 }
1086 gl_destroy(p_list1); /* clean up the list, preserving data */
1087
1088 /* test gl_unique_list() */
1089
1090 CU_TEST(TRUE == gl_unique_list(NULL));
1091 p_list1 = gl_create(10); /* create an empty list */
1092 CU_TEST(TRUE == gl_unique_list(p_list1));
1093 gl_append_ptr(p_list1, pint_array[0]);
1094 CU_TEST(TRUE == gl_unique_list(p_list1));
1095 gl_append_ptr(p_list1, pint_array[0]);
1096 CU_TEST(FALSE == gl_unique_list(p_list1));
1097 gl_reset(p_list1);
1098 CU_TEST(TRUE == gl_unique_list(p_list1));
1099 for (i=0 ; i<10 ; ++i) {
1100 gl_append_ptr(p_list1, pint_array[i]);
1101 }
1102 CU_TEST(TRUE == gl_unique_list(p_list1));
1103 gl_append_ptr(p_list1, pint_array[5]);
1104 CU_TEST(FALSE == gl_unique_list(p_list1));
1105
1106 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1107 p_used_lists[n_used_lists++] = p_list1;
1108 }
1109 gl_destroy(p_list1); /* clean up the list, preserving data */
1110
1111 /* test gl_delete() */
1112
1113 p_list1 = gl_create(10); /* create and fill a list */
1114 for (i=0 ; i<10 ; ++i) {
1115 gl_append_ptr(p_list1, pint_array[i]);
1116 }
1117
1118 #ifndef ASC_NO_ASSERTIONS
1119 asc_assert_catch(TRUE); /* prepare to test assertions */
1120
1121 asc_assert_reset();
1122 if (0 == setjmp(g_asc_test_env))
1123 gl_delete(NULL, 1, 0); /* error if NULL list* */
1124 CU_TEST(TRUE == asc_assert_failed());
1125
1126 asc_assert_reset();
1127 if (0 == setjmp(g_asc_test_env))
1128 gl_delete(p_list1, 11, 0); /* error if pos > gl_length(p_list1) */
1129 CU_TEST(TRUE == asc_assert_failed());
1130
1131 asc_assert_catch(FALSE); /* done testing assertions */
1132 #endif /* !ASC_NO_ASSERTIONS */
1133
1134 gl_delete(p_list1, 0, 0); /* pos == 0 is tolerated */
1135 CU_TEST(10 == gl_length(p_list1));
1136 #ifdef MALLOC_DEBUG
1137 for (i=0 ; i<20 ; ++i) { /* all pointers should still be active */
1138 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1139 }
1140 #endif
1141
1142 gl_delete(p_list1, 1, 0); /* deleting 1st pos in list, keep data */
1143 CU_TEST(9 == gl_length(p_list1));
1144 for (i=1 ; i<10 ; ++i) {
1145 CU_TEST(i == *((unsigned long*)gl_fetch(p_list1, i)));
1146 }
1147 #ifdef MALLOC_DEBUG
1148 for (i=0 ; i<20 ; ++i) { /* all pointers should still be active */
1149 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1150 }
1151 #endif
1152
1153 gl_delete(p_list1, 9, 0); /* deleting last pos in list, keep data */
1154 CU_TEST(8 == gl_length(p_list1));
1155 for (i=1 ; i<9 ; ++i) {
1156 CU_TEST(i == *((unsigned long*)gl_fetch(p_list1, i)));
1157 }
1158 #ifdef MALLOC_DEBUG
1159 for (i=0 ; i<20 ; ++i) { /* all pointers should still be active */
1160 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1161 }
1162 #endif
1163
1164 gl_delete(p_list1, 4, 0); /* delete something in the middle, keep data */
1165 CU_TEST(7 == gl_length(p_list1));
1166 CU_TEST(1 == *((unsigned long*)gl_fetch(p_list1, 1)));
1167 CU_TEST(2 == *((unsigned long*)gl_fetch(p_list1, 2)));
1168 CU_TEST(3 == *((unsigned long*)gl_fetch(p_list1, 3)));
1169 CU_TEST(5 == *((unsigned long*)gl_fetch(p_list1, 4)));
1170 CU_TEST(6 == *((unsigned long*)gl_fetch(p_list1, 5)));
1171 CU_TEST(7 == *((unsigned long*)gl_fetch(p_list1, 6)));
1172 CU_TEST(8 == *((unsigned long*)gl_fetch(p_list1, 7)));
1173 #ifdef MALLOC_DEBUG
1174 for (i=0 ; i<20 ; ++i) { /* all pointers should still be active */
1175 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1176 }
1177 #endif
1178
1179 gl_delete(p_list1, 1, 1); /* deleting 1st pos in list, dispose data */
1180 CU_TEST(6 == gl_length(p_list1));
1181 CU_TEST(2 == *((unsigned long*)gl_fetch(p_list1, 1)));
1182 CU_TEST(3 == *((unsigned long*)gl_fetch(p_list1, 2)));
1183 CU_TEST(5 == *((unsigned long*)gl_fetch(p_list1, 3)));
1184 CU_TEST(6 == *((unsigned long*)gl_fetch(p_list1, 4)));
1185 CU_TEST(7 == *((unsigned long*)gl_fetch(p_list1, 5)));
1186 CU_TEST(8 == *((unsigned long*)gl_fetch(p_list1, 6)));
1187 #ifdef MALLOC_DEBUG
1188 for (i=0 ; i<20 ; ++i) { /* all other pointers should still be active */
1189 if (i != 1) {
1190 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1191 }
1192 else {
1193 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1194 }
1195 }
1196 #endif
1197
1198 gl_delete(p_list1, 6, 1); /* deleting last pos in list, dispose data */
1199 CU_TEST(5 == gl_length(p_list1));
1200 CU_TEST(2 == *((unsigned long*)gl_fetch(p_list1, 1)));
1201 CU_TEST(3 == *((unsigned long*)gl_fetch(p_list1, 2)));
1202 CU_TEST(5 == *((unsigned long*)gl_fetch(p_list1, 3)));
1203 CU_TEST(6 == *((unsigned long*)gl_fetch(p_list1, 4)));
1204 CU_TEST(7 == *((unsigned long*)gl_fetch(p_list1, 5)));
1205 #ifdef MALLOC_DEBUG
1206 for (i=0 ; i<20 ; ++i) { /* all other pointers should still be active */
1207 if ((i != 1) && (i != 8)) {
1208 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1209 }
1210 else {
1211 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1212 }
1213 }
1214 #endif
1215
1216 gl_delete(p_list1, 3, 1); /* delete something in the middle, dispose data */
1217 CU_TEST(4 == gl_length(p_list1));
1218 CU_TEST(2 == *((unsigned long*)gl_fetch(p_list1, 1)));
1219 CU_TEST(3 == *((unsigned long*)gl_fetch(p_list1, 2)));
1220 CU_TEST(6 == *((unsigned long*)gl_fetch(p_list1, 3)));
1221 CU_TEST(7 == *((unsigned long*)gl_fetch(p_list1, 4)));
1222 #ifdef MALLOC_DEBUG
1223 for (i=0 ; i<20 ; ++i) { /* all other pointers should still be active */
1224 if ((i != 1) && (i != 8) && (i != 5)) {
1225 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1226 }
1227 else {
1228 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1229 }
1230 }
1231 #endif
1232
1233 for (i=0 ; i<4 ; ++i) {
1234 gl_delete(p_list1, 1, 0); /* delete rest of list members, keeping data */
1235 }
1236 CU_TEST(0 == gl_length(p_list1));
1237 #ifdef MALLOC_DEBUG
1238 for (i=0 ; i<20 ; ++i) { /* all other pointers should still be active */
1239 if ((i != 1) && (i != 8) && (i != 5)) {
1240 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1241 }
1242 else {
1243 CU_TEST(0 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1244 }
1245 }
1246 #endif
1247
1248 pint_array[1] = (unsigned long*)ascmalloc(sizeof(unsigned long));
1249 *pint_array[1] = 1;
1250 pint_array[5] = (unsigned long*)ascmalloc(sizeof(unsigned long));
1251 *pint_array[5] = 5;
1252 pint_array[8] = (unsigned long*)ascmalloc(sizeof(unsigned long));
1253 *pint_array[8] = 8;
1254
1255 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1256 p_used_lists[n_used_lists++] = p_list1;
1257 }
1258 gl_destroy(p_list1); /* clean up the list, preserving data */
1259
1260 /* test gl_reverse() */
1261
1262 gl_reverse(NULL); /* reversing a NULL list is ok, although no effect */
1263
1264 p_list1 = gl_create(20); /* create an empty list */
1265
1266 gl_reverse(p_list1); /* reversing an empty list is ok, although no effect */
1267 CU_TEST(0 == gl_length(p_list1));
1268 CU_TEST(0 != gl_sorted(p_list1));
1269
1270 gl_append_ptr(p_list1, pint_array[10]);
1271
1272 gl_reverse(p_list1); /* reversing a list with 1 element is ok, although no effect */
1273 CU_TEST(1 == gl_length(p_list1));
1274 CU_TEST(1 == gl_length(p_list1));
1275 CU_TEST(10 == *((unsigned long*)gl_fetch(p_list1, 1)));
1276 CU_TEST(0 != gl_sorted(p_list1));
1277
1278 gl_append_ptr(p_list1, pint_array[8]);
1279
1280 gl_reverse(p_list1); /* reversing a list with 2 elements is ok */
1281 CU_TEST(2 == gl_length(p_list1));
1282 CU_TEST(8 == *((unsigned long*)gl_fetch(p_list1, 1)));
1283 CU_TEST(10 == *((unsigned long*)gl_fetch(p_list1, 2)));
1284 CU_TEST(0 == gl_sorted(p_list1));
1285
1286 gl_reset(p_list1);
1287
1288 for (i=0 ; i<10 ; ++i) {
1289 gl_append_ptr(p_list1, pint_array[9-i]);
1290 }
1291
1292 gl_reverse(p_list1); /* reversing a longer unsorted list */
1293 CU_TEST(10 == gl_length(p_list1));
1294 for (i=0 ; i<10 ; ++i) {
1295 CU_TEST(i == *((unsigned long*)gl_fetch(p_list1, i+1)));
1296 }
1297 CU_TEST(0 == gl_sorted(p_list1));
1298
1299 gl_sort(p_list1, compare_ints);
1300
1301 gl_reverse(p_list1); /* reversing a longer sorted list */
1302 CU_TEST(10 == gl_length(p_list1));
1303 for (i=0 ; i<10 ; ++i) {
1304 CU_TEST((9-i) == *((unsigned long*)gl_fetch(p_list1, i+1)));
1305 }
1306 CU_TEST(0 != gl_sorted(p_list1));
1307
1308 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1309 p_used_lists[n_used_lists++] = p_list1;
1310 }
1311 gl_destroy(p_list1); /* clean up the list, preserving data */
1312
1313 /* test gl_reset() */
1314
1315 p_list1 = gl_create(10); /* create and fill a list */
1316 for (i=0 ; i<10 ; ++i) {
1317 gl_append_ptr(p_list1, pint_array[i]);
1318 }
1319
1320 #ifndef ASC_NO_ASSERTIONS
1321 asc_assert_catch(TRUE); /* prepare to test assertions */
1322
1323 asc_assert_reset();
1324 if (0 == setjmp(g_asc_test_env))
1325 gl_reset(NULL); /* error if NULL list* */
1326 CU_TEST(TRUE == asc_assert_failed());
1327
1328 asc_assert_catch(FALSE); /* done testing assertions */
1329 #endif /* !ASC_NO_ASSERTIONS */
1330
1331 gl_reset(p_list1); /* reset an unsorted, expandable list */
1332 CU_TEST(0 == gl_length(p_list1));
1333 CU_TEST(0 != gl_sorted(p_list1));
1334 CU_TEST(0 != gl_expandable(p_list1));
1335 #ifdef MALLOC_DEBUG
1336 for (i=0 ; i<20 ; ++i) { /* all pointers should still be active */
1337 CU_TEST(2 == AllocatedMemory((VOIDPTR)pint_array[i], sizeof(unsigned long)));
1338 }
1339 #endif
1340
1341 for (i=0 ; i<10 ; ++i) {
1342 gl_append_ptr(p_list1, pint_array[i]);
1343 }
1344 gl_set_sorted(p_list1, TRUE);
1345 gl_set_expandable(p_list1, FALSE); /* set list non-expandable */
1346
1347 gl_reset(p_list1); /* reset a sorted, unexpandable list */
1348 CU_TEST(0 == gl_length(p_list1));
1349 CU_TEST(0 != gl_sorted(p_list1));
1350 CU_TEST(0 != gl_expandable(p_list1));
1351
1352 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1353 p_used_lists[n_used_lists++] = p_list1;
1354 }
1355 gl_destroy(p_list1); /* clean up the list, preserving data */
1356
1357 /* test gl_copy() */
1358
1359 p_list1 = gl_create(10); /* create and fill a list */
1360
1361 #ifndef ASC_NO_ASSERTIONS
1362 asc_assert_catch(TRUE); /* prepare to test assertions */
1363
1364 asc_assert_reset();
1365 if (0 == setjmp(g_asc_test_env))
1366 gl_copy(NULL); /* error if NULL list* */
1367 CU_TEST(TRUE == asc_assert_failed());
1368
1369 asc_assert_catch(FALSE); /* done testing assertions */
1370 #endif /* !ASC_NO_ASSERTIONS */
1371
1372 p_list2= gl_copy(p_list1); /* copying an empty list should be ok */
1373 CU_TEST(0 == gl_length(p_list2));
1374 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1375 p_used_lists[n_used_lists++] = p_list2;
1376 }
1377 gl_destroy(p_list2);
1378
1379 gl_append_ptr(p_list1, pint_array[5]);
1380
1381 p_list2= gl_copy(p_list1); /* copying a list having 1 element should be ok */
1382 CU_TEST(1 == gl_length(p_list2));
1383 CU_TEST(5 == *((unsigned long*)gl_fetch(p_list1, 1)));
1384 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1385 p_used_lists[n_used_lists++] = p_list2;
1386 }
1387 gl_destroy(p_list2);
1388
1389 gl_reset(p_list1);
1390 for (i=0 ; i<10 ; ++i) {
1391 gl_append_ptr(p_list1, pint_array[i]);
1392 }
1393
1394 p_list2= gl_copy(p_list1); /* copying a longer, unsorted list */
1395 CU_TEST(10 == gl_length(p_list2));
1396 CU_TEST(0 == gl_sorted(p_list2));
1397 for (i=0 ; i<10 ; ++i) {
1398 CU_TEST(*((unsigned long*)gl_fetch(p_list1, i+1)) == *((unsigned long*)gl_fetch(p_list2, i+1)));
1399 }
1400 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1401 p_used_lists[n_used_lists++] = p_list2;
1402 }
1403 gl_destroy(p_list2);
1404
1405 gl_sort(p_list1, compare_addresses_reverse);
1406
1407 p_list2= gl_copy(p_list1); /* copying a longer, sorted list */
1408 CU_TEST(10 == gl_length(p_list2));
1409 CU_TEST(0 != gl_sorted(p_list2));
1410 for (i=0 ; i<10 ; ++i) {
1411 CU_TEST(*((unsigned long*)gl_fetch(p_list1, i+1)) == *((unsigned long*)gl_fetch(p_list2, i+1)));
1412 }
1413
1414 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1415 p_used_lists[n_used_lists++] = p_list2;
1416 }
1417 gl_free_and_destroy(p_list2); /* shared data should be destroyed also */
1418 #ifdef MALLOC_DEBUG
1419 for (i=0 ; i<10 ; ++i) {
1420 CU_TEST(0 == AllocatedMemory(gl_fetch(p_list1, i+1), sizeof(unsigned long)));
1421 }
1422 #endif
1423
1424 for (i=0 ; i<10 ; ++i) {
1425 pint_array[i] = (unsigned long*)ascmalloc(sizeof(unsigned long));
1426 *pint_array[i] = i;
1427 }
1428
1429 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1430 p_used_lists[n_used_lists++] = p_list1;
1431 }
1432 gl_destroy(p_list1); /* clean up the list, preserving data */
1433
1434 /* test gl_concat() */
1435
1436 p_list1 = gl_create(10); /* create 2 empty lists */
1437 p_list2 = gl_create(10);
1438
1439 #ifndef ASC_NO_ASSERTIONS
1440 asc_assert_catch(TRUE); /* prepare to test assertions */
1441
1442 asc_assert_reset();
1443 if (0 == setjmp(g_asc_test_env))
1444 p_list3 = gl_concat(NULL, p_list2); /* error if NULL list* */
1445 CU_TEST(TRUE == asc_assert_failed());
1446
1447 asc_assert_reset();
1448 if (0 == setjmp(g_asc_test_env))
1449 p_list3 = gl_concat(p_list1, NULL); /* error if NULL list* */
1450 CU_TEST(TRUE == asc_assert_failed());
1451
1452 asc_assert_catch(FALSE); /* done testing assertions */
1453 #endif /* !ASC_NO_ASSERTIONS */
1454
1455 p_list3 = gl_concat(p_list1, p_list2); /* concatenating empty list should be ok */
1456 CU_TEST(0 == gl_length(p_list3));
1457
1458 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1459 p_used_lists[n_used_lists++] = p_list3;
1460 }
1461 gl_destroy(p_list3);
1462
1463 gl_append_ptr(p_list1, pint_array[3]);
1464
1465 p_list3 = gl_concat(p_list1, p_list2); /* concatenating a 1-element list with empty list */
1466 CU_TEST(1 == gl_length(p_list3));
1467 CU_TEST(*pint_array[3] == *((unsigned long*)gl_fetch(p_list3, 1)));
1468
1469 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1470 p_used_lists[n_used_lists++] = p_list3;
1471 }
1472 gl_destroy(p_list3);
1473
1474 p_list3 = gl_concat(p_list2, p_list1); /* concatenating empty list with a 1-element list */
1475 CU_TEST(1 == gl_length(p_list3));
1476 CU_TEST(*pint_array[3] == *((unsigned long*)gl_fetch(p_list3, 1)));
1477
1478 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1479 p_used_lists[n_used_lists++] = p_list3;
1480 }
1481 gl_destroy(p_list3);
1482
1483 p_list3 = gl_concat(p_list1, p_list1); /* concatenating two 1-element lists */
1484 CU_TEST(2 == gl_length(p_list3));
1485 CU_TEST(*pint_array[3] == *((unsigned long*)gl_fetch(p_list3, 1)));
1486 CU_TEST(*pint_array[3] == *((unsigned long*)gl_fetch(p_list3, 2)));
1487
1488 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1489 p_used_lists[n_used_lists++] = p_list3;
1490 }
1491 gl_destroy(p_list3);
1492
1493 gl_reset(p_list1);
1494 for (i=0 ; i<10 ; ++i) {
1495 gl_append_ptr(p_list1, pint_array[i]);
1496 }
1497
1498 p_list3 = gl_concat(p_list1, p_list2); /* concatenating a list with an empty list */
1499 CU_TEST(10 == gl_length(p_list3));
1500 for (i=0 ; i<10 ; ++i) {
1501 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list3, i+1)));
1502 }
1503
1504 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1505 p_used_lists[n_used_lists++] = p_list3;
1506 }
1507 gl_destroy(p_list3);
1508
1509 p_list3 = gl_concat(p_list2, p_list1); /* concatenating empty list with a 1-element list */
1510 CU_TEST(10 == gl_length(p_list3));
1511 for (i=0 ; i<10 ; ++i) {
1512 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list3, i+1)));
1513 }
1514
1515 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1516 p_used_lists[n_used_lists++] = p_list3;
1517 }
1518 gl_destroy(p_list3);
1519
1520 for (i=0 ; i<10 ; ++i) {
1521 gl_append_ptr(p_list2, pint_array[i+10]);
1522 }
1523
1524 p_list3 = gl_concat(p_list1, p_list2); /* concatenating two lists */
1525 CU_TEST(20 == gl_length(p_list3));
1526 for (i=0 ; i<20 ; ++i) {
1527 CU_TEST(*pint_array[i] == *((unsigned long*)gl_fetch(p_list3, i+1)));
1528 }
1529
1530 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1531 p_used_lists[n_used_lists++] = p_list1;
1532 }
1533 gl_destroy(p_list1); /* clean up the list, preserving data */
1534 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1535 p_used_lists[n_used_lists++] = p_list2;
1536 }
1537 gl_destroy(p_list2);
1538 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1539 p_used_lists[n_used_lists++] = p_list3;
1540 }
1541 gl_destroy(p_list3);
1542
1543 /* test gl_compare_ptrs() */
1544
1545 p_list1 = gl_create(10); /* create 2 empty lists */
1546 p_list2 = gl_create(10);
1547
1548 CU_TEST(0 == gl_compare_ptrs(p_list1, p_list2)); /* compare 2 empty lists */
1549 CU_TEST(0 == gl_compare_ptrs(p_list1, p_list1)); /* compare empty list with itself */
1550
1551 gl_append_ptr(p_list1, pint_array[9]);
1552
1553 CU_TEST(0 < gl_compare_ptrs(p_list1, p_list2)); /* compare 1-element with empty list */
1554 CU_TEST(0 > gl_compare_ptrs(p_list2, p_list1)); /* compare empty with 1-element list */
1555 CU_TEST(0 == gl_compare_ptrs(p_list1, p_list1)); /* compare 1-element list with itself */
1556
1557 gl_append_ptr(p_list2, pint_array[9]);
1558
1559 CU_TEST(0 == gl_compare_ptrs(p_list1, p_list2)); /* compare equal 1-element lists */
1560 CU_TEST(0 == gl_compare_ptrs(p_list2, p_list1));
1561
1562 gl_reset(p_list2);
1563 gl_append_ptr(p_list2, pint_array[3]);
1564
1565 if (pint_array[9] > pint_array[3]) {
1566 CU_TEST(0 < gl_compare_ptrs(p_list1, p_list2));
1567 }
1568 else {
1569 CU_TEST(0 > gl_compare_ptrs(p_list1, p_list2));
1570 }
1571
1572 gl_reset(p_list1);
1573 gl_reset(p_list2);
1574
1575 for (i=0 ; i<20 ; ++i) {
1576 gl_append_ptr(p_list1, pint_array[i]);
1577 gl_append_ptr(p_list2, pint_array[i]);
1578 }
1579
1580 CU_TEST(0 == gl_compare_ptrs(p_list1, p_list2)); /* compare equal lists */
1581 CU_TEST(0 == gl_compare_ptrs(p_list2, p_list1)); /* compare equal lists */
1582
1583 gl_sort(p_list1, compare_addresses);
1584 gl_sort(p_list2, compare_addresses_reverse);
1585
1586 CU_TEST(0 > gl_compare_ptrs(p_list1, p_list2)); /* compare equal lists */
1587 CU_TEST(0 < gl_compare_ptrs(p_list2, p_list1)); /* compare equal lists */
1588
1589 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1590 p_used_lists[n_used_lists++] = p_list1;
1591 }
1592 gl_destroy(p_list1); /* clean up the list, preserving data */
1593 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1594 p_used_lists[n_used_lists++] = p_list2;
1595 }
1596 gl_destroy(p_list2);
1597
1598 /* test gl_expandable(), gl_set_expandable() */
1599
1600 p_list1 = gl_create(10); /* create an empty list */
1601
1602 #ifndef ASC_NO_ASSERTIONS
1603 asc_assert_catch(TRUE); /* prepare to test assertions */
1604
1605 asc_assert_reset();
1606 if (0 == setjmp(g_asc_test_env))
1607 (void)gl_expandable(NULL); /* error if NULL list* */
1608 CU_TEST(TRUE == asc_assert_failed());
1609
1610 asc_assert_reset();
1611 if (0 == setjmp(g_asc_test_env))
1612 gl_set_expandable(NULL, TRUE); /* error if NULL list* */
1613 CU_TEST(TRUE == asc_assert_failed());
1614
1615 asc_assert_catch(FALSE); /* done testing assertions */
1616 #endif /* !ASC_NO_ASSERTIONS */
1617
1618 CU_TEST(0 != gl_expandable(p_list1)); /* make sure functions work */
1619 gl_set_expandable(p_list1, FALSE);
1620 CU_TEST(0 == gl_expandable(p_list1));
1621 gl_set_expandable(p_list1, TRUE);
1622 CU_TEST(0 != gl_expandable(p_list1));
1623 gl_set_expandable(p_list1, FALSE);
1624 CU_TEST(0 == gl_expandable(p_list1));
1625
1626 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1627 p_used_lists[n_used_lists++] = p_list1;
1628 }
1629 gl_destroy(p_list1); /* clean up the list, preserving data */
1630
1631 /* test gl_fetchaddr() */
1632
1633 p_list1 = gl_create(10); /* create an empty list */
1634
1635 #ifndef ASC_NO_ASSERTIONS
1636 asc_assert_catch(TRUE); /* prepare to test assertions */
1637
1638 asc_assert_reset();
1639 if (0 == setjmp(g_asc_test_env))
1640 gl_fetchaddr(p_list1, 0); /* error if pos out of range (< 1) */
1641 CU_TEST(TRUE == asc_assert_failed());
1642
1643 asc_assert_reset();
1644 if (0 == setjmp(g_asc_test_env))
1645 gl_fetchaddr(p_list1, 11); /* error if pos out of range (> gl_length(list) */
1646 CU_TEST(TRUE == asc_assert_failed());
1647
1648 asc_assert_reset();
1649 if (0 == setjmp(g_asc_test_env))
1650 gl_fetchaddr(NULL, 1); /* error if NULL list* */
1651 CU_TEST(TRUE == asc_assert_failed());
1652
1653 asc_assert_catch(FALSE); /* done testing assertions */
1654 #endif /* !ASC_NO_ASSERTIONS */
1655
1656 gl_append_ptr(p_list1, pint_array[10]);
1657
1658 CU_TEST(&p_list1->data[0] == gl_fetchaddr(p_list1, 1));
1659
1660 gl_append_ptr(p_list1, pint_array[10]);
1661 gl_append_ptr(p_list1, pint_array[10]);
1662
1663 CU_TEST(&p_list1->data[0] == gl_fetchaddr(p_list1, 1));
1664 CU_TEST(&p_list1->data[1] == gl_fetchaddr(p_list1, 2));
1665 CU_TEST(&p_list1->data[2] == gl_fetchaddr(p_list1, 3));
1666
1667 if (n_used_lists < MAX_LISTS_TO_TRACK) {
1668 p_used_lists[n_used_lists++] = p_list1;
1669 }
1670 gl_destroy(p_list1); /* clean up the list, preserving data */
1671
1672 /* gl_report_pool - not tested */
1673
1674 /* clean up and exit */
1675 for (i=0 ; i<20 ; ++i)
1676 ascfree(pint_array[i]);
1677
1678 #ifdef MALLOC_DEBUG
1679 for (i=0 ; i<(MIN(n_used_lists, MAX_LISTS_TO_TRACK)) ; ++i) {
1680 CU_TEST(0 != AllocatedMemory((VOIDPTR)p_used_lists[i], capacity * sizeof(VOIDPTR)));
1681 }
1682 #endif
1683 gl_destroy_pool();
1684 #ifdef MALLOC_DEBUG
1685 for (i=0 ; i<(MIN(n_used_lists, MAX_LISTS_TO_TRACK)) ; ++i) {
1686 CU_TEST(0 == AllocatedMemory((VOIDPTR)p_used_lists[i], capacity * sizeof(VOIDPTR)));
1687 }
1688 #endif
1689
1690 #ifdef LISTUSESPOOL
1691 CU_TEST(FALSE == gl_pool_initialized());
1692 #else
1693 CU_TEST(TRUE == gl_pool_initialized());
1694 #endif
1695
1696 if (TRUE == lists_were_active) { /* if list was already initialized, restore it */
1697 gl_init_pool();
1698 }
1699
1700 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
1701 }
1702
1703 /*===========================================================================*/
1704 /* Registration information */
1705
1706 static CU_TestInfo list_test_list[] = {
1707 {"test_list", test_list},
1708 CU_TEST_INFO_NULL
1709 };
1710
1711 static CU_SuiteInfo suites[] = {
1712 {"test_general_list", NULL, NULL, list_test_list},
1713 CU_SUITE_INFO_NULL
1714 };
1715
1716 /*-------------------------------------------------------------------*/
1717 CU_ErrorCode test_register_general_list(void)
1718 {
1719 return CU_register_suites(suites);
1720 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22