1 /***************************************************************************
2     begin       : Mon Mar 01 2004
3     copyright   : (C) 2020 by Martin Preuss
4     email       : martin@libchipcard.de
5 
6  ***************************************************************************
7  *                                                                         *
8  *   This library is free software; you can redistribute it and/or         *
9  *   modify it under the terms of the GNU Lesser General Public            *
10  *   License as published by the Free Software Foundation; either          *
11  *   version 2.1 of the License, or (at your option) any later version.    *
12  *                                                                         *
13  *   This library is distributed in the 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  *   Lesser General Public License for more details.                       *
17  *                                                                         *
18  *   You should have received a copy of the GNU Lesser General Public      *
19  *   License along with this library; if not, write to the Free Software   *
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
21  *   MA  02111-1307  USA                                                   *
22  *                                                                         *
23  ***************************************************************************/
24 
25 
26 /* This file is included by "idlist64.c" */
27 
28 #include "idlist64-t.h"
29 
30 
31 #ifdef GWENHYWFAR_ENABLE_TESTCODE
32 
33 
34 
35 /* ------------------------------------------------------------------------------------------------
36  * forward declarations
37  * ------------------------------------------------------------------------------------------------
38  */
39 
40 
41 static int GWENHYWFAR_CB test1(GWEN_TEST_MODULE *mod);
42 static int GWENHYWFAR_CB test2(GWEN_TEST_MODULE *mod);
43 static int GWENHYWFAR_CB test3(GWEN_TEST_MODULE *mod);
44 static int GWENHYWFAR_CB test4(GWEN_TEST_MODULE *mod);
45 static int GWENHYWFAR_CB test5(GWEN_TEST_MODULE *mod);
46 static int GWENHYWFAR_CB test6(GWEN_TEST_MODULE *mod);
47 static int GWENHYWFAR_CB test7(GWEN_TEST_MODULE *mod);
48 static int GWENHYWFAR_CB test8(GWEN_TEST_MODULE *mod);
49 static int GWENHYWFAR_CB test9(GWEN_TEST_MODULE *mod);
50 static int GWENHYWFAR_CB test10(GWEN_TEST_MODULE *mod);
51 static int GWENHYWFAR_CB test11(GWEN_TEST_MODULE *mod);
52 
53 static int _compareList1AgainstList2(GWEN_IDLIST64 *idList1, GWEN_IDLIST64 *idList2);
54 
55 
56 
57 /* ------------------------------------------------------------------------------------------------
58  * implementations
59  * ------------------------------------------------------------------------------------------------
60  */
61 
62 
63 
GWEN_IdList64_AddTests(GWEN_TEST_MODULE * mod)64 int GWEN_IdList64_AddTests(GWEN_TEST_MODULE *mod)
65 {
66   GWEN_TEST_MODULE *newMod;
67 
68   newMod=GWEN_Test_Module_AddModule(mod, "GWEN_IdList64", NULL);
69 
70   GWEN_Test_Module_AddTest(newMod, "test1", test1, NULL);
71   GWEN_Test_Module_AddTest(newMod, "test2", test2, NULL);
72   GWEN_Test_Module_AddTest(newMod, "test3", test3, NULL);
73   GWEN_Test_Module_AddTest(newMod, "test4", test4, NULL);
74   GWEN_Test_Module_AddTest(newMod, "test5", test5, NULL);
75   GWEN_Test_Module_AddTest(newMod, "test6", test6, NULL);
76   GWEN_Test_Module_AddTest(newMod, "test7", test7, NULL);
77   GWEN_Test_Module_AddTest(newMod, "test8", test8, NULL);
78   GWEN_Test_Module_AddTest(newMod, "test9", test9, NULL);
79   GWEN_Test_Module_AddTest(newMod, "test10", test10, NULL);
80   GWEN_Test_Module_AddTest(newMod, "test11", test11, NULL);
81 
82   return 0;
83 }
84 
85 
86 
test1(GWEN_UNUSED GWEN_TEST_MODULE * mod)87 int test1(GWEN_UNUSED GWEN_TEST_MODULE *mod)
88 {
89   GWEN_IDLIST64 *idList1;
90   GWEN_IDLIST64_ITERATOR *iterator1;
91   int i;
92   uint64_t id;
93 
94   idList1=GWEN_IdList64_newWithSteps(64);
95   for (i=0; i<100; i++) {
96     int64_t rv;
97 
98     rv=GWEN_IdList64_AddId(idList1, i+1);
99     if (rv<0) {
100       DBG_ERROR(GWEN_LOGDOMAIN, "here (%d)", (int) rv);
101       GWEN_IdList64_free(idList1);
102       return (int) rv;
103     }
104     if (rv!=(int64_t) i) {
105       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected position for id at index %u (is %lu, expected %u)",
106                 i, (unsigned long) rv, i);
107       GWEN_IdList64_free(idList1);
108       return (int) rv;
109     }
110   }
111 
112   i=0;
113   iterator1=GWEN_IdList64_Iterator_new(idList1);
114   id=GWEN_IdList64_Iterator_GetFirstId(iterator1);
115   while (id) {
116     if (id!=i+1) {
117       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected id at index %lu (is %lu, expected %lu)",
118                 (unsigned long) i, (unsigned long) id, (unsigned long) i+1);
119       GWEN_IdList64_Iterator_free(iterator1);
120       GWEN_IdList64_free(idList1);
121       return GWEN_ERROR_GENERIC;
122     }
123     i++;
124     id=GWEN_IdList64_Iterator_GetNextId(iterator1);
125   }
126   GWEN_IdList64_Iterator_free(iterator1);
127   GWEN_IdList64_free(idList1);
128   return 0;
129 }
130 
131 
132 
test2(GWEN_UNUSED GWEN_TEST_MODULE * mod)133 int test2(GWEN_UNUSED GWEN_TEST_MODULE *mod)
134 {
135   GWEN_IDLIST64 *idList1;
136   GWEN_IDLIST64_ITERATOR *iterator1;
137   int i;
138   uint64_t id;
139   int rv;
140 
141   idList1=GWEN_IdList64_newWithSteps(64);
142   for (i=0; i<100; i++) {
143     int64_t rv64;
144 
145     rv64=GWEN_IdList64_AddId(idList1, i+1);
146     if (rv64<0) {
147       DBG_ERROR(GWEN_LOGDOMAIN, "here (%d)", (int) rv64);
148       GWEN_IdList64_free(idList1);
149       return (int) rv64;
150     }
151   }
152 
153   rv=GWEN_IdList64_ReverseSort(idList1);
154   if (rv<0) {
155     DBG_ERROR(GWEN_LOGDOMAIN, "here (%d)", (int) rv);
156     GWEN_IdList64_free(idList1);
157     return (int) rv;
158   }
159 
160 
161   i=0;
162   iterator1=GWEN_IdList64_Iterator_new(idList1);
163   id=GWEN_IdList64_Iterator_GetFirstId(iterator1);
164   while (id) {
165     if (id!=100-i) {
166       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected id at index %lu (is %lu, expected %lu)",
167                 (unsigned long) i, (unsigned long) id, (unsigned long) i+1);
168       GWEN_IdList64_Iterator_free(iterator1);
169       GWEN_IdList64_free(idList1);
170       return GWEN_ERROR_GENERIC;
171     }
172     i++;
173     id=GWEN_IdList64_Iterator_GetNextId(iterator1);
174   }
175   if (i!=100) {
176     DBG_ERROR(GWEN_LOGDOMAIN, "More entries than expected (is %d, expected %d)", i, 100);
177     GWEN_IdList64_Iterator_free(iterator1);
178     GWEN_IdList64_free(idList1);
179     return GWEN_ERROR_GENERIC;
180   }
181   GWEN_IdList64_Iterator_free(iterator1);
182 
183 
184   rv=GWEN_IdList64_Sort(idList1);
185   if (rv<0) {
186     DBG_ERROR(GWEN_LOGDOMAIN, "here (%d)", (int) rv);
187     GWEN_IdList64_free(idList1);
188     return (int) rv;
189   }
190 
191   i=0;
192   iterator1=GWEN_IdList64_Iterator_new(idList1);
193   id=GWEN_IdList64_Iterator_GetFirstId(iterator1);
194   while (id) {
195     if (id!=i+1) {
196       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected id at index %lu (is %lu, expected %lu)",
197                 (unsigned long) i, (unsigned long) id, (unsigned long) i+1);
198       GWEN_IdList64_Iterator_free(iterator1);
199       GWEN_IdList64_free(idList1);
200       return GWEN_ERROR_GENERIC;
201     }
202     i++;
203     id=GWEN_IdList64_Iterator_GetNextId(iterator1);
204   }
205   if (i!=100) {
206     DBG_ERROR(GWEN_LOGDOMAIN, "More entries than expected (is %d, expected %d)", i, 100);
207     GWEN_IdList64_Iterator_free(iterator1);
208     GWEN_IdList64_free(idList1);
209     return GWEN_ERROR_GENERIC;
210   }
211   GWEN_IdList64_Iterator_free(iterator1);
212 
213 
214   GWEN_IdList64_free(idList1);
215   return 0;
216 }
217 
218 
219 
test3(GWEN_UNUSED GWEN_TEST_MODULE * mod)220 int test3(GWEN_UNUSED GWEN_TEST_MODULE *mod)
221 {
222   GWEN_IDLIST64 *ptrList1;
223   int i;
224 
225   ptrList1=GWEN_IdList64_newWithSteps(256);
226 
227   for (i=0; i<2048; i++) {
228     int64_t rv;
229 
230     rv=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
231     if (rv<0) {
232       DBG_ERROR(GWEN_LOGDOMAIN,
233                 "Error on GWEN_IdList64_AddEntry(%d): %s (%d)",
234                 i, GWEN_Error_SimpleToString((int)rv), (int)rv);
235       return rv;
236     }
237     if (rv!=(int64_t)i) {
238       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv, i);
239       return GWEN_ERROR_INTERNAL;
240     }
241   }
242 
243 
244   for (i=0; i<8; i++) {
245     GWEN_IDTABLE64 *table;
246     int refCounter;
247 
248     table=GWEN_IdList64_GetTableAt(ptrList1, i);
249     refCounter=table->refCount;
250     if (refCounter!=1) {
251       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table (list1) %d: is %d, should be 1 ", i, refCounter);
252       return GWEN_ERROR_INTERNAL;
253     }
254   }
255 
256   GWEN_IdList64_free(ptrList1);
257 
258   return 0;
259 }
260 
261 
262 
test4(GWEN_UNUSED GWEN_TEST_MODULE * mod)263 int test4(GWEN_UNUSED GWEN_TEST_MODULE *mod)
264 {
265   GWEN_IDLIST64 *ptrList1;
266   GWEN_IDLIST64 *ptrList2;
267   int i;
268 
269   ptrList1=GWEN_IdList64_newWithSteps(256);
270 
271   for (i=0; i<2048; i++) {
272     int64_t rv;
273 
274     rv=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
275     if (rv<0) {
276       DBG_ERROR(GWEN_LOGDOMAIN,
277                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
278                 i, GWEN_Error_SimpleToString((int)rv), (int)rv);
279       return rv;
280     }
281     if (rv!=(int64_t)i) {
282       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv, i);
283       return GWEN_ERROR_INTERNAL;
284     }
285   }
286 
287 
288   for (i=0; i<8; i++) {
289     GWEN_IDTABLE64 *table;
290     int refCounter;
291 
292     table=GWEN_IdList64_GetTableAt(ptrList1, i);
293     refCounter=table->refCount;
294     if (refCounter!=1) {
295       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table (list1) %d: is %d, should be 1 ", i, refCounter);
296       return GWEN_ERROR_INTERNAL;
297     }
298   }
299 
300   ptrList2=GWEN_IdList64_LazyCopy(ptrList1);
301 
302   GWEN_IdList64_free(ptrList1);
303   GWEN_IdList64_free(ptrList2);
304 
305   return 0;
306 }
307 
308 
309 
test5(GWEN_UNUSED GWEN_TEST_MODULE * mod)310 int test5(GWEN_UNUSED GWEN_TEST_MODULE *mod)
311 {
312   GWEN_IDLIST64 *ptrList1;
313   GWEN_IDLIST64 *ptrList2;
314   int i;
315 
316   ptrList1=GWEN_IdList64_newWithSteps(256);
317 
318   for (i=0; i<2048; i++) {
319     int64_t rv;
320 
321     rv=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
322     if (rv<0) {
323       DBG_ERROR(GWEN_LOGDOMAIN,
324                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
325                 i, GWEN_Error_SimpleToString((int)rv), (int)rv);
326       return rv;
327     }
328     if (rv!=(int64_t)i) {
329       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv, i);
330       return GWEN_ERROR_INTERNAL;
331     }
332   }
333 
334   ptrList2=GWEN_IdList64_LazyCopy(ptrList1);
335 
336   for (i=0; i<2048; i++) {
337     int64_t entry;
338 
339     entry=GWEN_IdList64_GetIdAt(ptrList1, i);
340     if (entry<0) {
341       DBG_ERROR(GWEN_LOGDOMAIN,
342                 "Error on GWEN_IdList64_GetIdAt(%d, list1): %s (%d)",
343                 i, GWEN_Error_SimpleToString(entry), (int)entry);
344       return entry;
345     }
346     if (entry != (int64_t)(i+1)) {
347       DBG_ERROR(GWEN_LOGDOMAIN, "Non-matching entry at index %d: is %ld, should be %d ", i, (unsigned long) entry, i);
348       return GWEN_ERROR_INTERNAL;
349     }
350   }
351 
352   for (i=0; i<2048; i++) {
353     int64_t entry;
354 
355     entry=GWEN_IdList64_GetIdAt(ptrList2, i);
356     if (entry<0) {
357       DBG_ERROR(GWEN_LOGDOMAIN,
358                 "Error on GWEN_IdList64_GetIdAt(%d, list2): %s (%d)",
359                 i, GWEN_Error_SimpleToString(entry), (int)entry);
360       return entry;
361     }
362     if (entry != (int64_t)(i+1)) {
363       DBG_ERROR(GWEN_LOGDOMAIN, "Non-matching entry at index %d: is %ld, should be %d ", i, (unsigned long) entry, i);
364       return GWEN_ERROR_INTERNAL;
365     }
366   }
367 
368 
369   for (i=0; i<8; i++) {
370     GWEN_IDTABLE64 *table;
371     int refCounter;
372 
373     table=GWEN_IdList64_GetTableAt(ptrList1, i);
374     refCounter=table->refCount;
375     if (refCounter!=1) {
376       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table (list1) %d: is %d, should be 1 ", i, refCounter);
377       return GWEN_ERROR_INTERNAL;
378     }
379   }
380 
381   for (i=0; i<8; i++) {
382     GWEN_IDTABLE64 *table;
383     int refCounter;
384 
385     table=GWEN_IdList64_GetTableAt(ptrList2, i);
386     refCounter=table->refCount;
387     if (refCounter!=1) {
388       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table (list2) %d: is %d, should be 1 ", i, refCounter);
389       return GWEN_ERROR_INTERNAL;
390     }
391   }
392 
393   GWEN_IdList64_free(ptrList2);
394   GWEN_IdList64_free(ptrList1);
395 
396   return 0;
397 }
398 
399 
400 
test6(GWEN_UNUSED GWEN_TEST_MODULE * mod)401 int test6(GWEN_UNUSED GWEN_TEST_MODULE *mod)
402 {
403   GWEN_IDLIST64 *ptrList1;
404   GWEN_IDLIST64 *ptrList2;
405   int i;
406 
407   ptrList1=GWEN_IdList64_newWithSteps(256);
408 
409   for (i=0; i<2048; i++) {
410     int64_t rv;
411 
412     rv=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
413     if (rv<0) {
414       DBG_ERROR(GWEN_LOGDOMAIN,
415                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
416                 i, GWEN_Error_SimpleToString((int)rv), (int)rv);
417       return rv;
418     }
419     if (rv!=(int64_t)i) {
420       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv, i);
421       return GWEN_ERROR_INTERNAL;
422     }
423   }
424 
425   ptrList2=GWEN_IdList64_LazyCopy(ptrList1);
426 
427   for (i=0; i<8; i++) {
428     GWEN_IDTABLE64 *table;
429     int refCounter;
430 
431     table=GWEN_IdList64_GetTableAt(ptrList1, i);
432     refCounter=table->refCount;
433     if (refCounter!=1) {
434       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 1", i, refCounter);
435       return GWEN_ERROR_INTERNAL;
436     }
437   }
438 
439   GWEN_IdList64_free(ptrList1);
440 
441   for (i=0; i<8; i++) {
442     GWEN_IDTABLE64 *table;
443     int refCounter;
444 
445     table=GWEN_IdList64_GetTableAt(ptrList2, i);
446     refCounter=table->refCount;
447     if (refCounter!=1) {
448       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table (list2) %d: is %d, should be 1 ", i, refCounter);
449       return GWEN_ERROR_INTERNAL;
450     }
451   }
452 
453   GWEN_IdList64_free(ptrList2);
454 
455   return 0;
456 }
457 
458 
459 
test7(GWEN_UNUSED GWEN_TEST_MODULE * mod)460 int test7(GWEN_UNUSED GWEN_TEST_MODULE *mod)
461 {
462   GWEN_IDLIST64 *ptrList1;
463   GWEN_IDLIST64 *ptrList2;
464   int i;
465   int64_t iEntry;
466 
467   ptrList1=GWEN_IdList64_newWithSteps(256);
468 
469   for (i=0; i<2048; i++) {
470     int64_t rv;
471 
472     rv=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
473     if (rv<0) {
474       DBG_ERROR(GWEN_LOGDOMAIN,
475                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
476                 i, GWEN_Error_SimpleToString((int)rv), (int)rv);
477       return rv;
478     }
479     if (rv!=(int64_t)i) {
480       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv, i);
481       return GWEN_ERROR_INTERNAL;
482     }
483   }
484 
485   for (i=0; i<8; i++) {
486     GWEN_IDTABLE64 *table;
487     int refCounter;
488 
489     table=GWEN_IdList64_GetTableAt(ptrList1, i);
490     refCounter=table->refCount;
491     if (refCounter!=1) {
492       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 1", i, refCounter);
493       return GWEN_ERROR_INTERNAL;
494     }
495   }
496 
497   ptrList2=GWEN_IdList64_LazyCopy(ptrList1);
498 
499   for (i=0; i<8; i++) {
500     GWEN_IDTABLE64 *table;
501     int refCounter;
502 
503     table=GWEN_IdList64_GetTableAt(ptrList2, i);
504     refCounter=table->refCount;
505     if (refCounter!=1) {
506       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 1 ", i, refCounter);
507       return GWEN_ERROR_INTERNAL;
508     }
509   }
510 
511   for (i=0; i<8; i++) {
512     GWEN_IDTABLE64 *table1;
513     GWEN_IDTABLE64 *table2;
514 
515     table1=GWEN_IdList64_GetTableAt(ptrList1, i);
516     table2=GWEN_IdList64_GetTableAt(ptrList2, i);
517     if (table1!=table2) {
518       DBG_ERROR(GWEN_LOGDOMAIN, "Table at position %d should be equal but isn't", i);
519       return GWEN_ERROR_INTERNAL;
520     }
521   }
522 
523 
524   iEntry=GWEN_IdList64_AddId(ptrList2, (uint64_t) 2048);
525   if (iEntry<0) {
526     DBG_ERROR(GWEN_LOGDOMAIN,
527               "Error on GWEN_IdList64_AddId(%d): %s (%d)",
528               i, GWEN_Error_SimpleToString((int)iEntry), (int)iEntry);
529     return (int)iEntry;
530   }
531   if (iEntry!=(int64_t)2048) {
532     DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be 2048", i, (unsigned long) iEntry);
533     return GWEN_ERROR_INTERNAL;
534   }
535 
536 
537   if (GWEN_IdList64_GetIdAt(ptrList1, 2048)>0) {
538     DBG_ERROR(GWEN_LOGDOMAIN, "Entry 2048 exists in list1 when it should not");
539     return GWEN_ERROR_INTERNAL;
540   }
541 
542   iEntry=GWEN_IdList64_GetIdAt(ptrList2, 2049);
543   if (iEntry>0) {
544     DBG_ERROR(GWEN_LOGDOMAIN, "Entry 2049 exists in list2 when it should not (%ld)", (long int) iEntry);
545     return GWEN_ERROR_INTERNAL;
546   }
547 
548 
549   for (i=0; i<8; i++) {
550     GWEN_IDTABLE64 *table;
551     int refCounter;
552 
553     table=GWEN_IdList64_GetTableAt(ptrList1, i);
554     refCounter=table->refCount;
555     if (refCounter!=2) {
556       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 2 ", i, refCounter);
557       return GWEN_ERROR_INTERNAL;
558     }
559   }
560 
561   for (i=0; i<8; i++) {
562     GWEN_IDTABLE64 *table;
563     int refCounter;
564 
565     table=GWEN_IdList64_GetTableAt(ptrList2, i);
566     refCounter=table->refCount;
567     if (refCounter!=2) {
568       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 2 ", i, refCounter);
569       return GWEN_ERROR_INTERNAL;
570     }
571   }
572 
573   for (i=8; i<9; i++) {
574     GWEN_IDTABLE64 *table;
575     int refCounter;
576 
577     table=GWEN_IdList64_GetTableAt(ptrList2, i);
578     refCounter=table->refCount;
579     if (refCounter!=1) {
580       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 1 ", i, refCounter);
581       return GWEN_ERROR_INTERNAL;
582     }
583   }
584 
585   return 0;
586 }
587 
588 
589 
test8(GWEN_UNUSED GWEN_TEST_MODULE * mod)590 int test8(GWEN_UNUSED GWEN_TEST_MODULE *mod)
591 {
592   GWEN_IDLIST64 *ptrList1;
593   GWEN_IDLIST64 *ptrList2;
594   int i;
595 
596   ptrList1=GWEN_IdList64_newWithSteps(256);
597 
598   for (i=0; i<2048; i++) {
599     int64_t rv;
600 
601     rv=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
602     if (rv<0) {
603       DBG_ERROR(GWEN_LOGDOMAIN,
604                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
605                 i, GWEN_Error_SimpleToString((int)rv), (int)rv);
606       return rv;
607     }
608     if (rv!=(int64_t)i) {
609       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv, i);
610       return GWEN_ERROR_INTERNAL;
611     }
612   }
613 
614   ptrList2=GWEN_IdList64_LazyCopy(ptrList1);
615 
616   for (i=2048; i<4096; i++) {
617     int64_t rv;
618 
619     rv=GWEN_IdList64_AddId(ptrList2, (uint64_t) i+1);
620     if (rv<0) {
621       DBG_ERROR(GWEN_LOGDOMAIN,
622                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
623                 i, GWEN_Error_SimpleToString((int)rv), (int)rv);
624       return rv;
625     }
626     if (rv!=(int64_t)i) {
627       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv, i);
628       return GWEN_ERROR_INTERNAL;
629     }
630   }
631 
632 
633   for (i=0; i<2048; i++) {
634     int64_t entry;
635 
636     entry=GWEN_IdList64_GetIdAt(ptrList1, i);
637     if (entry<0) {
638       DBG_ERROR(GWEN_LOGDOMAIN,
639                 "Error on GWEN_IdList64_GetIdAt(%d, list1): %s (%d)",
640                 i, GWEN_Error_SimpleToString(entry), (int)entry);
641       return entry;
642     }
643     if (entry != (int64_t)(i+1)) {
644       DBG_ERROR(GWEN_LOGDOMAIN, "Non-matching entry at index %d: is %ld, should be %d ", i, (unsigned long) entry, i);
645       return GWEN_ERROR_INTERNAL;
646     }
647   }
648 
649   if (GWEN_IdList64_GetIdAt(ptrList1, 2048)>0) {
650     DBG_ERROR(GWEN_LOGDOMAIN, "Entry 2048 exists in list1 when it should not");
651     return GWEN_ERROR_INTERNAL;
652   }
653 
654   for (i=0; i<4096; i++) {
655     int64_t entry;
656 
657     entry=GWEN_IdList64_GetIdAt(ptrList2, i);
658     if (entry<0) {
659       DBG_ERROR(GWEN_LOGDOMAIN,
660                 "Error on GWEN_IdList64_GetIdAt(%d, list2): %s (%d)",
661                 i, GWEN_Error_SimpleToString(entry), (int)entry);
662       return entry;
663     }
664     if (entry != (int64_t)(i+1)) {
665       DBG_ERROR(GWEN_LOGDOMAIN, "Non-matching entry at index %d: is %ld, should be %d ", i, (unsigned long) entry, i);
666       return GWEN_ERROR_INTERNAL;
667     }
668   }
669 
670   if (GWEN_IdList64_GetIdAt(ptrList1, 4096)>=0) {
671     DBG_ERROR(GWEN_LOGDOMAIN, "Entry 4096 exists in list2 when it should not");
672     return GWEN_ERROR_INTERNAL;
673   }
674 
675 
676   for (i=0; i<8; i++) {
677     GWEN_IDTABLE64 *table;
678     int refCounter;
679 
680     table=GWEN_IdList64_GetTableAt(ptrList1, i);
681     refCounter=table->refCount;
682     if (refCounter!=2) {
683       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 2 ", i, refCounter);
684       return GWEN_ERROR_INTERNAL;
685     }
686   }
687 
688   for (i=0; i<8; i++) {
689     GWEN_IDTABLE64 *table;
690     int refCounter;
691 
692     table=GWEN_IdList64_GetTableAt(ptrList2, i);
693     refCounter=table->refCount;
694     if (refCounter!=2) {
695       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 2 ", i, refCounter);
696       return GWEN_ERROR_INTERNAL;
697     }
698   }
699 
700   for (i=8; i<16; i++) {
701     GWEN_IDTABLE64 *table;
702     int refCounter;
703 
704     table=GWEN_IdList64_GetTableAt(ptrList2, i);
705     refCounter=table->refCount;
706     if (refCounter!=1) {
707       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 1 ", i, refCounter);
708       return GWEN_ERROR_INTERNAL;
709     }
710   }
711 
712 
713   GWEN_IdList64_free(ptrList1);
714 
715   for (i=0; i<(2048/256); i++) {
716     GWEN_IDTABLE64 *table;
717     int refCounter;
718 
719     table=GWEN_IdList64_GetTableAt(ptrList2, i);
720     refCounter=table->refCount;
721     if (refCounter!=1) {
722       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table (list2) %d: is %d, should be 1 ", i, refCounter);
723       return GWEN_ERROR_INTERNAL;
724     }
725   }
726 
727   return 0;
728 }
729 
730 
731 
test9(GWEN_UNUSED GWEN_TEST_MODULE * mod)732 int test9(GWEN_UNUSED GWEN_TEST_MODULE *mod)
733 {
734   GWEN_IDLIST64 *ptrList1;
735   GWEN_IDLIST64 *ptrList2;
736   int i;
737   int rv;
738 
739   ptrList1=GWEN_IdList64_newWithSteps(256);
740 
741   for (i=0; i<2047; i++) {
742     int64_t rv64;
743 
744     rv64=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
745     if (rv64<0) {
746       DBG_ERROR(GWEN_LOGDOMAIN,
747                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
748                 i, GWEN_Error_SimpleToString((int)rv64), (int)rv64);
749       return rv64;
750     }
751     if (rv64!=(int64_t)i) {
752       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv64, i);
753       return GWEN_ERROR_INTERNAL;
754     }
755   }
756 
757   for (i=0; i<8; i++) {
758     GWEN_IDTABLE64 *table;
759     int refCounter;
760 
761     table=GWEN_IdList64_GetTableAt(ptrList1, i);
762     refCounter=table->refCount;
763     if (refCounter!=1) {
764       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 1", i, refCounter);
765       return GWEN_ERROR_INTERNAL;
766     }
767   }
768 
769   ptrList2=GWEN_IdList64_LazyCopy(ptrList1);
770 
771   rv=GWEN_IdList64_AddId(ptrList2, (uint64_t) 2047);
772   if (rv<0) {
773     DBG_ERROR(GWEN_LOGDOMAIN,
774               "Error on GWEN_IdList64_AddId(%d): %s (%d)",
775               i, GWEN_Error_SimpleToString((int)rv), (int)rv);
776     return rv;
777   }
778   if (rv!=(int64_t)2047) {
779     DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be 2047", i, (unsigned long) rv);
780     return GWEN_ERROR_INTERNAL;
781   }
782 
783   if (GWEN_IdList64_GetTableAt(ptrList2, 8)) {
784     DBG_ERROR(GWEN_LOGDOMAIN, "Table 8 exists when it should not");
785     return GWEN_ERROR_INTERNAL;
786   }
787 
788 
789   if (GWEN_IdList64_GetIdAt(ptrList2, 2049)>0) {
790     DBG_ERROR(GWEN_LOGDOMAIN, "Entry 2049 exists in list2 when it should not");
791     return GWEN_ERROR_INTERNAL;
792   }
793 
794 
795   for (i=0; i<8; i++) {
796     GWEN_IDTABLE64 *table;
797     int refCounter;
798 
799     table=GWEN_IdList64_GetTableAt(ptrList1, i);
800     refCounter=table->refCount;
801     if (refCounter!=2) {
802       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 2 ", i, refCounter);
803       return GWEN_ERROR_INTERNAL;
804     }
805   }
806 
807   for (i=0; i<8; i++) {
808     GWEN_IDTABLE64 *table;
809     int refCounter;
810 
811     table=GWEN_IdList64_GetTableAt(ptrList2, i);
812     refCounter=table->refCount;
813     if (refCounter!=2) {
814       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 2 ", i, refCounter);
815       return GWEN_ERROR_INTERNAL;
816     }
817   }
818 
819   for (i=8; i<8; i++) {
820     GWEN_IDTABLE64 *table;
821     int refCounter;
822 
823     table=GWEN_IdList64_GetTableAt(ptrList2, i);
824     refCounter=table->refCount;
825     if (refCounter!=1) {
826       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table %d: is %d, should be 1 ", i, refCounter);
827       return GWEN_ERROR_INTERNAL;
828     }
829   }
830 
831   return 0;
832 }
833 
834 
835 
test10(GWEN_UNUSED GWEN_TEST_MODULE * mod)836 int test10(GWEN_UNUSED GWEN_TEST_MODULE *mod)
837 {
838   GWEN_IDLIST64 *ptrList1;
839   GWEN_IDLIST64 *ptrList2;
840   int i;
841 
842   ptrList1=GWEN_IdList64_newWithSteps(256);
843 
844   for (i=0; i<2048; i++) {
845     int64_t rv;
846 
847     rv=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
848     if (rv<0) {
849       DBG_ERROR(GWEN_LOGDOMAIN,
850                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
851                 i, GWEN_Error_SimpleToString((int)rv), (int)rv);
852       return rv;
853     }
854     if (rv!=(int64_t)i) {
855       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv, i);
856       return GWEN_ERROR_INTERNAL;
857     }
858   }
859 
860   ptrList2=GWEN_IdList64_dup(ptrList1);
861 
862   for (i=0; i<2048; i++) {
863     int64_t entry;
864 
865     entry=GWEN_IdList64_GetIdAt(ptrList1, i);
866     if (entry<0) {
867       DBG_ERROR(GWEN_LOGDOMAIN,
868                 "Error on GWEN_IdList64_GetIdAt(%d, list1): %s (%d)",
869                 i, GWEN_Error_SimpleToString(entry), (int)entry);
870       return entry;
871     }
872     if (entry != (int64_t)(i+1)) {
873       DBG_ERROR(GWEN_LOGDOMAIN, "Non-matching entry at index %d: is %ld, should be %d ", i, (unsigned long) entry, i);
874       return GWEN_ERROR_INTERNAL;
875     }
876   }
877 
878   for (i=0; i<2048; i++) {
879     int64_t entry;
880 
881     entry=GWEN_IdList64_GetIdAt(ptrList2, i);
882     if (entry<0) {
883       DBG_ERROR(GWEN_LOGDOMAIN,
884                 "Error on GWEN_IdList64_GetIdAt(%d, list2): %s (%d)",
885                 i, GWEN_Error_SimpleToString(entry), (int)entry);
886       return entry;
887     }
888     if (entry != (int64_t)(i+1)) {
889       DBG_ERROR(GWEN_LOGDOMAIN, "Non-matching entry at index %d: is %ld, should be %d ", i, (unsigned long) entry, i);
890       return GWEN_ERROR_INTERNAL;
891     }
892   }
893 
894 
895   for (i=0; i<8; i++) {
896     GWEN_IDTABLE64 *table;
897     int refCounter;
898 
899     table=GWEN_IdList64_GetTableAt(ptrList1, i);
900     refCounter=table->refCount;
901     if (refCounter!=1) {
902       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table (list1) %d: is %d, should be 1 ", i, refCounter);
903       return GWEN_ERROR_INTERNAL;
904     }
905   }
906 
907   for (i=0; i<8; i++) {
908     GWEN_IDTABLE64 *table;
909     int refCounter;
910 
911     table=GWEN_IdList64_GetTableAt(ptrList2, i);
912     refCounter=table->refCount;
913     if (refCounter!=1) {
914       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected reference counter in table (list2) %d: is %d, should be 1 ", i, refCounter);
915       return GWEN_ERROR_INTERNAL;
916     }
917   }
918 
919   GWEN_IdList64_free(ptrList2);
920   GWEN_IdList64_free(ptrList1);
921 
922   return 0;
923 }
924 
925 
926 
test11(GWEN_UNUSED GWEN_TEST_MODULE * mod)927 int test11(GWEN_UNUSED GWEN_TEST_MODULE *mod)
928 {
929   GWEN_IDLIST64 *ptrList1;
930   GWEN_IDLIST64 *ptrList2;
931   int i;
932   int rv;
933 
934   ptrList1=GWEN_IdList64_newWithSteps(256);
935 
936   for (i=0; i<2048; i++) {
937     int64_t rv64;
938 
939     rv64=GWEN_IdList64_AddId(ptrList1, (uint64_t) i+1);
940     if (rv64<0) {
941       DBG_ERROR(GWEN_LOGDOMAIN,
942                 "Error on GWEN_IdList64_AddId(%d): %s (%d)",
943                 i, GWEN_Error_SimpleToString((int)rv64), (int)rv64);
944       return rv64;
945     }
946     if (rv64!=(int64_t)i) {
947       DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected index returned for entry %d: is %ld, should be %d ", i, (unsigned long) rv64, i);
948       return GWEN_ERROR_INTERNAL;
949     }
950   }
951 
952   ptrList2=GWEN_IdList64_dup(ptrList1);
953 
954 
955   rv=_compareList1AgainstList2(ptrList1, ptrList2);
956   if (rv<0) {
957     DBG_ERROR(GWEN_LOGDOMAIN, "here (%d)", (int) rv);
958     GWEN_IdList64_free(ptrList2);
959     GWEN_IdList64_free(ptrList1);
960     return (int) rv;
961   }
962 
963   GWEN_IdList64_free(ptrList2);
964   GWEN_IdList64_free(ptrList1);
965 
966   return 0;
967 }
968 
969 
970 
971 
972 
_compareList1AgainstList2(GWEN_IDLIST64 * idList1,GWEN_IDLIST64 * idList2)973 int _compareList1AgainstList2(GWEN_IDLIST64 *idList1, GWEN_IDLIST64 *idList2)
974 {
975   GWEN_IDLIST64_ITERATOR *iter1;
976   GWEN_IDLIST64_ITERATOR *iter2;
977   uint64_t id1;
978   uint64_t id2;
979   uint64_t index=0;
980 
981   iter1=GWEN_IdList64_Iterator_new(idList1);
982   iter2=GWEN_IdList64_Iterator_new(idList2);
983   id1=GWEN_IdList64_Iterator_GetFirstId(iter1);
984   id2=GWEN_IdList64_Iterator_GetFirstId(iter2);
985   while (id1>0 && id2>0) {
986     if (id1 != id2) {
987       fprintf(stderr, "Tables differ at position %lu (%lu != %lu)\n",
988               (unsigned long) index,
989               (unsigned long) id1,
990               (unsigned long) id2);
991       GWEN_IdList64_Iterator_free(iter2);
992       GWEN_IdList64_Iterator_free(iter1);
993       return GWEN_ERROR_GENERIC;
994     }
995     index++;
996     id1=GWEN_IdList64_Iterator_GetNextId(iter1);
997     id2=GWEN_IdList64_Iterator_GetNextId(iter2);
998   }
999   if (GWEN_IdList64_Iterator_GetNextId(iter1)>0 || GWEN_IdList64_Iterator_GetNextId(iter2)>0) {
1000     fprintf(stderr, "NUmber of elements in lists differs\n");
1001     GWEN_IdList64_Iterator_free(iter2);
1002     GWEN_IdList64_Iterator_free(iter1);
1003     return GWEN_ERROR_GENERIC;
1004   }
1005   GWEN_IdList64_Iterator_free(iter2);
1006   GWEN_IdList64_Iterator_free(iter1);
1007 
1008   return 0;
1009 }
1010 
1011 
1012 
1013 #else
1014 
GWEN_IdList64_AddTests(GWEN_TEST_MODULE * mod)1015 int GWEN_IdList64_AddTests(GWEN_TEST_MODULE *mod)
1016 {
1017   DBG_ERROR(GWEN_LOGDOMAIN, "Gwenhywfar was compiled without test code enabled.");
1018   return GWEN_ERROR_GENERIC;
1019 }
1020 
1021 #endif
1022 
1023