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

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