testqset.c
Go to the documentation of this file.
1 /*<html><pre> -<a href="../libqhull/index.htm#TOC"
2  >-------------------------------</a><a name="TOP">-</a>
3 
4  testset.c -- test qset.c and its use of mem.c
5 
6  The test sets are pointers to int. Normally a set is a pointer to a type (e.g., facetT, ridgeT, etc.).
7  For consistency in notation, an "int" is typedef'd to i2T
8 
9 Functions and macros from qset.h. Counts occurrences in this test. Does not correspond to thoroughness.
10  qh_setaddsorted -- 4 tests
11  qh_setaddnth -- 1 test
12  qh_setappend -- 7 tests
13  qh_setappend_set -- 1 test
14  qh_setappend2ndlast -- 1 test
15  qh_setcheck -- lots of tests
16  qh_setcompact -- 7 tests
17  qh_setcopy -- 3 tests
18  qh_setdel -- 1 tests
19  qh_setdellast -- 1 tests
20  qh_setdelnth -- 2 tests
21  qh_setdelnthsorted -- 2 tests
22  qh_setdelsorted -- 1 test
23  qh_setduplicate -- not testable here
24  qh_setequal -- 4 tests
25  qh_setequal_except -- 2 tests
26  qh_setequal_skip -- 2 tests
27  qh_setfree -- 11+ tests
28  qh_setfree2 -- not testable here
29  qh_setfreelong -- 2 tests
30  qh_setin -- 3 tests
31  qh_setindex -- 4 tests
32  qh_setlarger -- 1 test
33  qh_setlast -- 2 tests
34  qh_setnew -- 6 tests
35  qh_setnew_delnthsorted
36  qh_setprint -- tested elsewhere
37  qh_setreplace -- 1 test
38  qh_setsize -- 9+ tests
39  qh_settemp -- 2 tests
40  qh_settempfree -- 1 test
41  qh_settempfree_all -- 1 test
42  qh_settemppop -- 1 test
43  qh_settemppush -- 1 test
44  qh_settruncate -- 3 tests
45  qh_setunique -- 3 tests
46  qh_setzero -- 1 test
47  FOREACHint_ -- 2 test
48  FOREACHint4_
49  FOREACHint_i_ -- 1 test
50  FOREACHintreverse_
51  FOREACHintreverse12_
52  FOREACHsetelement_ -- 1 test
53  FOREACHsetelement_i_ -- 1 test
54  FOREACHsetelementreverse_ -- 1 test
55  FOREACHsetelementreverse12_ -- 1 test
56  SETelem_ -- 3 tests
57  SETelemaddr_ -- 2 tests
58  SETelemt_ -- not tested (generic)
59  SETempty_ -- 1 test
60  SETfirst_ -- 4 tests
61  SETfirstt_ -- 2 tests
62  SETindex_ -- 2 tests
63  SETref_ -- 2 tests
64  SETreturnsize_ -- 2 tests
65  SETsecond_ -- 1 test
66  SETsecondt_ -- 2 tests
67  SETtruncate_ -- 2 tests
68 
69  Copyright (c) 2012-2015 C.B. Barber. All rights reserved.
70  $Id: //main/2015/qhull/src/testqset/testqset.c#4 $$Change: 2062 $
71  $DateTime: 2016/01/17 13:13:18 $$Author: bbarber $
72 */
73 
74 #include "libqhull/user.h" /* QHULL_CRTDBG */
75 #include "libqhull/qset.h"
76 #include "libqhull/mem.h"
77 
78 #include <stdarg.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 
83 typedef int i2T;
84 #define MAXerrorCount 100 /* quit after n errors */
85 
86 #define FOREACHint_( ints ) FOREACHsetelement_( i2T, ints, i2)
87 #define FOREACHint4_( ints ) FOREACHsetelement_( i2T, ints, i4)
88 #define FOREACHint_i_( ints ) FOREACHsetelement_i_( i2T, ints, i2)
89 #define FOREACHintreverse_( ints ) FOREACHsetelementreverse_( i2T, ints, i2)
90 #define FOREACHintreverse12_( ints ) FOREACHsetelementreverse12_( i2T, ints, i2)
91 
92 enum {
93  MAXint= 0x7fffffff,
94 };
95 
96 char prompt[]= "testqset N [M] [T5] -- Test qset.c and mem.c\n\
97  \n\
98  If this test fails then qhull will not work.\n\
99  \n\
100  Test qsets of 0..N integers with a check every M iterations (default ~log10)\n\
101  Additional checking and logging if M is 1\n\
102  \n\
103  T5 turns on memory logging (qset does not log)\n\
104  \n\
105  For example:\n\
106  testqset 10000\n\
107 ";
108 
109 int error_count= 0; /* Global error_count. checkSetContents() keeps its own error count. It exits on too many errors */
110 
111 /* Macros normally defined in geom.h */
112 #define fmax_( a,b ) ( ( a ) < ( b ) ? ( b ) : ( a ) )
113 
114 /* Macros normally defined in user.h */
115 
116 #define realT double
117 #define qh_MEMalign ((int)(fmax_(sizeof(realT), sizeof(void *))))
118 #define qh_MEMbufsize 0x10000 /* allocate 64K memory buffers */
119 #define qh_MEMinitbuf 0x20000 /* initially allocate 128K buffer */
120 
121 /* Macros normally defined in QhullSet.h */
122 
123 
124 /* Functions normally defined in user.h for usermem.c */
125 
126 void qh_exit(int exitcode);
127 void qh_fprintf_stderr(int msgcode, const char *fmt, ... );
128 void qh_free(void *mem);
129 void *qh_malloc(size_t size);
130 
131 /* Normally defined in user.c */
132 
133 void qh_errexit(int exitcode, void *f, void *r)
134 {
135  (void)f; /* unused */
136  (void)r; /* unused */
137  qh_exit(exitcode);
138 }
139 
140 /* Normally defined in userprintf.c */
141 
142 void qh_fprintf(FILE *fp, int msgcode, const char *fmt, ... )
143 {
144  static int needs_cr= 0; /* True if qh_fprintf needs a CR */
145 
146  size_t fmtlen= strlen(fmt);
147  va_list args;
148 
149  if (!fp) {
150  /* Do not use qh_fprintf_stderr. This is a standalone program */
151  fprintf(stderr, "QH6232 qh_fprintf: fp not defined for '%s'", fmt);
152  qh_errexit(6232, NULL, NULL);
153  }
154  if(fmtlen>0){
155  if(fmt[fmtlen-1]=='\n'){
156  if(needs_cr && fmtlen>1){
157  fprintf(fp, "\n");
158  }
159  needs_cr= 0;
160  }else{
161  needs_cr= 1;
162  }
163  }
164  if(msgcode>=6000 && msgcode<7000){
165  fprintf(fp, "Error TQ%d ", msgcode);
166  }
167  va_start(args, fmt);
168  vfprintf(fp, fmt, args);
169  va_end(args);
170 }
171 
172 /* Defined below in order of use */
173 int main(int argc, char **argv);
174 void readOptions(int argc, char **argv, const char *promptstr, int *numInts, int *checkEvery, int *traceLevel);
175 void setupMemory(int tracelevel, int numInts, int **intarray);
176 
177 void testSetappendSettruncate(int numInts, int *intarray, int checkEvery);
178 void testSetdelSetadd(int numInts, int *intarray, int checkEvery);
179 void testSetappendSet(int numInts, int *intarray, int checkEvery);
180 void testSetcompactCopy(int numInts, int *intarray, int checkEvery);
181 void testSetequalInEtc(int numInts, int *intarray, int checkEvery);
182 void testSettemp(int numInts, int *intarray, int checkEvery);
183 void testSetlastEtc(int numInts, int *intarray, int checkEvery);
184 void testSetdelsortedEtc(int numInts, int *intarray, int checkEvery);
185 
186 int log_i(setT *set, const char *s, int i, int numInts, int checkEvery);
187 void checkSetContents(const char *name, setT *set, int count, int rangeA, int rangeB, int rangeC);
188 
189 int main(int argc, char **argv) {
190  int *intarray= NULL;
191  int numInts;
192  int checkEvery= MAXint;
193  int curlong, totlong;
194  int traceLevel= 4; /* 4 normally, no tracing since qset does not log. 5 for memory tracing */
195 
196 #if defined(_MSC_VER) && defined(_DEBUG) && defined(QHULL_CRTDBG) /* user.h */
197  _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) );
198  _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG );
199  _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDERR );
200 #endif
201 
202  readOptions(argc, argv, prompt, &numInts, &checkEvery, &traceLevel);
203  setupMemory(traceLevel, numInts, &intarray);
204 
205  testSetappendSettruncate(numInts, intarray, checkEvery);
206  testSetdelSetadd(numInts, intarray, checkEvery);
207  testSetappendSet(numInts, intarray, checkEvery);
208  testSetcompactCopy(numInts, intarray, checkEvery);
209  testSetequalInEtc(numInts, intarray, checkEvery);
210  testSettemp(numInts, intarray, checkEvery);
211  testSetlastEtc(numInts, intarray, checkEvery);
212  testSetdelsortedEtc(numInts, intarray, checkEvery);
213  printf("\n\nNot testing qh_setduplicate and qh_setfree2.\n These routines use heap-allocated set contents. See qhull tests.\n");
214 
215  qh_memstatistics(stdout);
216  qh_memfreeshort(&curlong, &totlong);
217  if (curlong || totlong){
218  qh_fprintf(stderr, 8043, "qh_memfreeshort: did not free %d bytes of long memory(%d pieces)\n", totlong, curlong);
219  error_count++;
220  }
221  if(error_count){
222  qh_fprintf(stderr, 8012, "testqset: %d errors\n\n", error_count);
223  exit(1);
224  }else{
225  printf("testqset: OK\n\n");
226  }
227  return 0;
228 }/*main*/
229 
230 void readOptions(int argc, char **argv, const char *promptstr, int *numInts, int *checkEvery, int *traceLevel)
231 {
232  long numIntsArg;
233  long checkEveryArg;
234  char *endp;
235  int isTracing= 0;
236 
237  if (argc < 2 || argc > 4) {
238  printf("%s", promptstr);
239  exit(0);
240  }
241  numIntsArg= strtol(argv[1], &endp, 10);
242  if(numIntsArg<1){
243  qh_fprintf(stderr, 6301, "First argument should be 1 or greater. Got '%s'\n", argv[1]);
244  exit(1);
245  }
246  if(numIntsArg>MAXint){
247  qh_fprintf(stderr, 6302, "qset does not currently support 64-bit ints. Maximum count is %d\n", MAXint);
248  exit(1);
249  }
250  *numInts= (int)numIntsArg;
251 
252  if(argc==3 && argv[2][0]=='T' && argv[2][1]=='5' ){
253  isTracing= 1;
254  *traceLevel= 5;
255  }
256  if(argc==4 || (argc==3 && !isTracing)){
257  checkEveryArg= strtol(argv[2], &endp, 10);
258  if(checkEveryArg<1){
259  qh_fprintf(stderr, 6321, "checkEvery argument should be 1 or greater. Got '%s'\n", argv[2]);
260  exit(1);
261  }
262  if(checkEveryArg>MAXint){
263  qh_fprintf(stderr, 6322, "qset does not currently support 64-bit ints. Maximum checkEvery is %d\n", MAXint);
264  exit(1);
265  }
266  if(argc==4){
267  if(argv[3][0]=='T' && argv[3][1]=='5' ){
268  isTracing= 1;
269  *traceLevel= 5;
270  }else{
271  qh_fprintf(stderr, 6242, "Optional third argument must be 'T5'. Got '%s'\n", argv[3]);
272  exit(1);
273  }
274  }
275  *checkEvery= (int)checkEveryArg;
276  }
277 }/*readOptions*/
278 
279 void setupMemory(int tracelevel, int numInts, int **intarray)
280 {
281  int i;
282  if(numInts<0 || numInts*(int)sizeof(int)<0){
283  qh_fprintf(stderr, 6303, "qset does not currently support 64-bit ints. Integer overflow\n");
284  exit(1);
285  }
286  *intarray= qh_malloc(numInts * sizeof(int));
287  if(!*intarray){
288  qh_fprintf(stderr, 6304, "Failed to allocate %d bytes of memory\n", numInts * sizeof(int));
289  exit(1);
290  }
291  for(i= 0; i<numInts; i++){
292  (*intarray)[i] =i;
293  }
294 
295  qh_meminit(stderr);
296  qh_meminitbuffers(tracelevel, qh_MEMalign, 4 /*sizes*/, qh_MEMbufsize,qh_MEMinitbuf);
297  qh_memsize(10);
298  qh_memsize(20);
299  qh_memsize(30);
300  qh_memsize(40);
301  qh_memsetup();
302 
303  qh_fprintf(stderr, 8001, "SETelemsize is %d bytes for pointer-to-int\n", SETelemsize);
304 }/*setupMemmory*/
305 
306 void testSetappendSettruncate(int numInts, int *intarray, int checkEvery)
307 {
308  setT *ints= qh_setnew(4);
309  int i, isCheck;
310 
311  qh_fprintf(stderr, 8002, "\n\nTesting qh_setappend 0..%d. Test", numInts-1);
312  for(i= 0; i<numInts; i++){
313  isCheck= log_i(ints, "i", i, numInts, checkEvery);
314  qh_setappend(&ints, intarray+i);
315  if(isCheck){
316  checkSetContents("qh_setappend", ints, i+1, 0, -1, -1);
317  }
318  }
319 
320  qh_fprintf(stderr, 8014, "\n\nTesting qh_settruncate %d and 0. Test", numInts/2);
321  if(numInts>=2){
322  isCheck= log_i(ints, "n", numInts/2, numInts, checkEvery);
323  qh_settruncate(ints, numInts/2);
324  checkSetContents("qh_settruncate by half", ints, numInts/2, 0, -1, -1);
325  }
326  isCheck= log_i(ints, "n", 0, numInts, checkEvery);
327  qh_settruncate(ints, 0);
328  checkSetContents("qh_settruncate", ints, 0, -1, -1, -1);
329 
330  qh_fprintf(stderr, 8003, "\n\nTesting qh_setappend2ndlast 0,0..%d. Test 0", numInts-1);
331  qh_setfree(&ints);
332  ints= qh_setnew(4);
333  qh_setappend(&ints, intarray+0);
334  for(i= 0; i<numInts; i++){
335  isCheck= log_i(ints, "i", i, numInts, checkEvery);
336  qh_setappend2ndlast(&ints, intarray+i);
337  if(isCheck){
338  checkSetContents("qh_setappend2ndlast", ints, i+2, 0, 0, -1);
339  }
340  }
341  qh_fprintf(stderr, 8015, "\n\nTesting SETtruncate_ %d and 0. Test", numInts/2);
342  if(numInts>=2){
343  isCheck= log_i(ints, "n", numInts/2, numInts, checkEvery);
344  SETtruncate_(ints, numInts/2);
345  checkSetContents("SETtruncate_ by half", ints, numInts/2, 0, -1, -1);
346  }
347  isCheck= log_i(ints, "n", 0, numInts, checkEvery);
348  SETtruncate_(ints, 0);
349  checkSetContents("SETtruncate_", ints, 0, -1, -1, -1);
350 
351  qh_setfree(&ints);
352 }/*testSetappendSettruncate*/
353 
354 void testSetdelSetadd(int numInts, int *intarray, int checkEvery)
355 {
356  setT *ints=qh_setnew(1);
357  int i,j,isCheck;
358 
359  qh_fprintf(stderr, 8003, "\n\nTesting qh_setdelnthsorted and qh_setaddnth 1..%d. Test", numInts-1);
360  for(j=1; j<numInts; j++){ /* size 0 not valid */
361  if(log_i(ints, "j", j, numInts, MAXint)){
362  for(i= qh_setsize(ints); i<j; i++){
363  qh_setappend(&ints, intarray+i);
364  }
365  checkSetContents("qh_setappend", ints, j, 0, -1, -1);
366  for(i= 0; i<j && i<100; i++){ /* otherwise too slow */
367  isCheck= log_i(ints, "", i, numInts, checkEvery);
368  (void)isCheck; /* unused */
370  qh_setaddnth(&ints, i, intarray+i);
371  if(checkEvery==1){
372  checkSetContents("qh_setdelnthsorted qh_setaddnth", ints, j, 0, -1, -1);
373  }
374  }
375  checkSetContents("qh_setdelnthsorted qh_setaddnth 2", ints, j, 0, -1, -1);
376  }
377  }
378  qh_setfree(&ints);
379 }/*testSetdelSetadd*/
380 
381 void testSetappendSet(int numInts, int *intarray, int checkEvery)
382 {
383  setT *ints=qh_setnew(1);
384  setT *ints2;
385  int i,j,k;
386 
387  qh_fprintf(stderr, 8016, "\n\nTesting qh_setappend_set 0..%d. Test", numInts-1);
388  for(j=0; j<numInts; j++){
389  if(log_i(ints, "j", j, numInts, numInts)){
390  for(i= qh_setsize(ints); i<j; i++){
391  qh_setappend(&ints, intarray+i);
392  }
393  if(checkEvery==1){
394  checkSetContents("qh_setappend", ints, j, 0, -1, -1);
395  }
396  ints2= qh_setnew(j==0 ? 0 : j-1); /* One less than needed */
397  for(i= 0; i<=j && i<=20; i++){ /* otherwise too slow */
398  if(log_i(ints, "", i, numInts, numInts)){
399  for(k= qh_setsize(ints2); k<i; k++){
400  qh_setappend(&ints2, intarray+k);
401  }
402  if(checkEvery==1){
403  checkSetContents("qh_setappend 2", ints2, i, 0, -1, -1);
404  }
405  qh_setappend_set(&ints, ints2);
406  checkSetContents("qh_setappend_set", ints, i+j, 0, (j==0 ? -1 : 0), -1);
407  qh_settruncate(ints, j);
408  if(checkEvery==1){
409  checkSetContents("qh_settruncate", ints, j, 0, -1, -1);
410  }
411  }
412  }
413  qh_setfree(&ints2);
414  }
415  }
416  qh_setfree(&ints);
417 }/*testSetappendSet*/
418 
419 void testSetcompactCopy(int numInts, int *intarray, int checkEvery)
420 {
421  setT *ints= qh_setnew(20);
422  setT *ints2= NULL;
423  int i,j,k;
424 
425  qh_fprintf(stderr, 8017, "\n\nTesting qh_setcompact and qh_setcopy 0..%d. Test", numInts-1);
426  for(j=0; j<numInts; j++){
427  if(log_i(ints, "j", j, numInts, checkEvery)){
428  for(i= qh_setsize(ints); i<j; i++){ /* Test i<j to test the empty set */
429  for(k= 0; k<i%7; k++){
430  qh_setappend(&ints, NULL);
431  }
432  qh_setappend(&ints, intarray+i);
433  }
434  qh_setfree(&ints2);
435  ints2= qh_setcopy(ints, 0);
437  qh_setcompact(ints2);
438  checkSetContents("qh_setcompact", ints, j, 0, 0, -1);
439  checkSetContents("qh_setcompact", ints2, j, 0, 0, -1);
441  checkSetContents("qh_setcompact", ints, j, 0, 0, -1);
442  }
443  }
444  qh_setfree(&ints);
445  qh_setfree(&ints2);
446 }/*testSetcompactCopy*/
447 
448 void testSetdelsortedEtc(int numInts, int *intarray, int checkEvery)
449 {
450  setT *ints= qh_setnew(1);
451  setT *ints2= NULL;
452  int i,j;
453 
454  qh_fprintf(stderr, 8018, "\n\nTesting qh_setdel*, qh_setaddsorted, and 0..%d. Test", numInts-1);
455  for(j=0; j<numInts; j++){
456  if(log_i(ints, "j", j, numInts, checkEvery)){
457  for(i= qh_setsize(ints); i<j; i++){ /* Test i<j to test the empty set */
458  qh_setaddsorted(&ints, intarray+i);
459  }
460  checkSetContents("qh_setaddsorted", ints, j, 0, 0, -1);
461  if(j>3){
462  qh_setdelsorted(ints, intarray+i/2);
463  checkSetContents("qh_setdelsorted", ints, j-1, 0, i/2+1, -1);
464  qh_setaddsorted(&ints, intarray+i/2);
465  checkSetContents("qh_setaddsorted i/2", ints, j, 0, 0, -1);
466  }
468  checkSetContents("qh_setdellast", ints, (j ? j-1 : 0), 0, -1, -1);
469  if(j>0){
470  qh_setaddsorted(&ints, intarray+j-1);
471  checkSetContents("qh_setaddsorted j-1", ints, j, 0, -1, -1);
472  }
473  if(j>4){
474  qh_setdelnthsorted(ints, i/2);
475  if (checkEvery==1)
476  checkSetContents("qh_setdelnthsorted", ints, j-1, 0, i/2+1, -1);
477  /* test qh_setdelnth and move-to-front */
478  qh_setdelsorted(ints, intarray+i/2+1);
479  checkSetContents("qh_setdelsorted 2", ints, j-2, 0, i/2+2, -1);
480  qh_setaddsorted(&ints, intarray+i/2+1);
481  if (checkEvery==1)
482  checkSetContents("qh_setaddsorted i/2+1", ints, j-1, 0, i/2+1, -1);
483  qh_setaddsorted(&ints, intarray+i/2);
484  checkSetContents("qh_setaddsorted i/2 again", ints, j, 0, -1, -1);
485  }
486  qh_setfree(&ints2);
487  ints2= qh_setcopy(ints, 0);
489  qh_setcompact(ints2);
490  checkSetContents("qh_setcompact", ints, j, 0, 0, -1);
491  checkSetContents("qh_setcompact 2", ints2, j, 0, 0, -1);
493  checkSetContents("qh_setcompact 3", ints, j, 0, 0, -1);
494  qh_setfree(&ints2);
495  }
496  }
498  if(ints){
499  qh_setfree(&ints); /* Was quick memory */
500  }
501 }/*testSetdelsortedEtc*/
502 
503 void testSetequalInEtc(int numInts, int *intarray, int checkEvery)
504 {
505  setT *ints= NULL;
506  setT *ints2= NULL;
507  setT *ints3= NULL;
508  int i,j,n;
509 
510  qh_fprintf(stderr, 8019, "\n\nTesting qh_setequal*, qh_setin*, qh_setdel, qh_setdelnth, and qh_setlarger 0..%d. Test", numInts-1);
511  for(j=0; j<numInts; j++){
512  if(log_i(ints, "j", j, numInts, checkEvery)){
513  n= qh_setsize(ints);
514  qh_setlarger(&ints);
515  checkSetContents("qh_setlarger", ints, n, 0, -1, -1);
516  for(i= qh_setsize(ints); i<j; i++){ /* Test i<j to test the empty set */
517  qh_setappend(&ints, intarray+i);
518  }
519  checkSetContents("qh_setappend", ints, j, 0, -1, -1);
520  if(!qh_setequal(ints, ints)){
521  qh_fprintf(stderr, 6300, "testSetequalInEtc: set not equal to itself at length %d\n", j);
522  error_count++;
523  }
524  if(j==0 && !qh_setequal(ints, ints2)){
525  qh_fprintf(stderr, 6323, "testSetequalInEtc: empty set not equal to null set\n");
526  error_count++;
527  }
528  if(j>0){
529  if(qh_setequal(ints, ints2)){
530  qh_fprintf(stderr, 6324, "testSetequalInEtc: non-empty set equal to empty set\n", j);
531  error_count++;
532  }
533  qh_setfree(&ints3);
534  ints3= qh_setcopy(ints, 0);
535  checkSetContents("qh_setreplace", ints3, j, 0, -1, -1);
536  qh_setreplace(ints3, intarray+j/2, intarray+j/2+1);
537  if(j==1){
538  checkSetContents("qh_setreplace 2", ints3, j, j/2+1, -1, -1);
539  }else if(j==2){
540  checkSetContents("qh_setreplace 3", ints3, j, 0, j/2+1, -1);
541  }else{
542  checkSetContents("qh_setreplace 3", ints3, j, 0, j/2+1, j/2+1);
543  }
544  if(qh_setequal(ints, ints3)){
545  qh_fprintf(stderr, 6325, "testSetequalInEtc: modified set equal to original set at %d/2\n", j);
546  error_count++;
547  }
548  if(!qh_setequal_except(ints, intarray+j/2, ints3, intarray+j/2+1)){
549  qh_fprintf(stderr, 6326, "qh_setequal_except: modified set not equal to original set except modified\n", j);
550  error_count++;
551  }
552  if(qh_setequal_except(ints, intarray+j/2, ints3, intarray)){
553  qh_fprintf(stderr, 6327, "qh_setequal_except: modified set equal to original set with wrong excepts\n", j);
554  error_count++;
555  }
556  if(!qh_setequal_skip(ints, j/2, ints3, j/2)){
557  qh_fprintf(stderr, 6328, "qh_setequal_skip: modified set not equal to original set except modified\n", j);
558  error_count++;
559  }
560  if(j>2 && qh_setequal_skip(ints, j/2, ints3, 0)){
561  qh_fprintf(stderr, 6329, "qh_setequal_skip: modified set equal to original set with wrong excepts\n", j);
562  error_count++;
563  }
564  if(intarray+j/2+1!=qh_setdel(ints3, intarray+j/2+1)){
565  qh_fprintf(stderr, 6330, "qh_setdel: failed to find added element\n", j);
566  error_count++;
567  }
568  checkSetContents("qh_setdel", ints3, j-1, 0, j-1, (j==1 ? -1 : j/2+1)); /* swaps last element with deleted element */
569  if(j>3){
570  qh_setdelnth(ints3, j/2); /* Delete at the same location as the original replace, for only one out-of-order element */
571  checkSetContents("qh_setdelnth", ints3, j-2, 0, j-2, (j==2 ? -1 : j/2+1));
572  }
573  if(qh_setin(ints3, intarray+j/2)){
574  qh_fprintf(stderr, 6331, "qh_setin: found deleted element\n");
575  error_count++;
576  }
577  if(j>4 && !qh_setin(ints3, intarray+1)){
578  qh_fprintf(stderr, 6332, "qh_setin: did not find second element\n");
579  error_count++;
580  }
581  if(j>4 && !qh_setin(ints3, intarray+j-2)){
582  qh_fprintf(stderr, 6333, "qh_setin: did not find last element\n");
583  error_count++;
584  }
585  if(-1!=qh_setindex(ints2, intarray)){
586  qh_fprintf(stderr, 6334, "qh_setindex: found element in empty set\n");
587  error_count++;
588  }
589  if(-1!=qh_setindex(ints3, intarray+j/2)){
590  qh_fprintf(stderr, 6335, "qh_setindex: found deleted element in set\n");
591  error_count++;
592  }
593  if(0!=qh_setindex(ints, intarray)){
594  qh_fprintf(stderr, 6336, "qh_setindex: did not find first in set\n");
595  error_count++;
596  }
597  if(j-1!=qh_setindex(ints, intarray+j-1)){
598  qh_fprintf(stderr, 6337, "qh_setindex: did not find last in set\n");
599  error_count++;
600  }
601  }
602  qh_setfree(&ints2);
603  }
604  }
605  qh_setfree(&ints3);
607  if(ints){
608  qh_setfree(&ints); /* Was quick memory */
609  }
610 }/*testSetequalInEtc*/
611 
612 
613 void testSetlastEtc(int numInts, int *intarray, int checkEvery)
614 {
615  setT *ints= NULL;
616  setT *ints2= NULL;
617  int i,j,prepend;
618 
619  qh_fprintf(stderr, 8020, "\n\nTesting qh_setlast, qh_setnew_delnthsorted, qh_setunique, and qh_setzero 0..%d. Test", numInts-1);
620  for(j=0; j<numInts; j++){
621  if(log_i(ints, "j", j, numInts, checkEvery)){
622  for(i= qh_setsize(ints); i<j; i++){ /* Test i<j to test the empty set */
623  if(!qh_setunique(&ints, intarray+i)){
624  qh_fprintf(stderr, 6340, "qh_setunique: not able to append next element %d\n", i);
625  error_count++;
626  }
627  if(checkEvery==1){
628  checkSetContents("qh_setunique", ints, i+1, 0, -1, -1);
629  }
630  if(qh_setunique(&ints, intarray+i)){
631  qh_fprintf(stderr, 6341, "qh_setunique: appended next element twice %d\n", i);
632  error_count++;
633  }
634  if(qh_setunique(&ints, intarray+i/2)){
635  qh_fprintf(stderr, 6346, "qh_setunique: appended middle element twice %d/2\n", i);
636  error_count++;
637  }
638  }
639  checkSetContents("qh_setunique 2", ints, j, 0, -1, -1);
640  if(j==0 && NULL!=qh_setlast(ints)){
641  qh_fprintf(stderr, 6339, "qh_setlast: returned last element of empty set\n");
642  error_count++;
643  }
644  if(j>0){
645  if(intarray+j-1!=qh_setlast(ints)){
646  qh_fprintf(stderr, 6338, "qh_setlast: wrong last element\n");
647  error_count++;
648  }
649  prepend= (j<100 ? j/4 : 0);
650  ints2= qh_setnew_delnthsorted(ints, qh_setsize(ints), j/2, prepend);
651  if(qh_setsize(ints2)!=j+prepend-1){
652  qh_fprintf(stderr, 6345, "qh_setnew_delnthsorted: Expecting %d elements, got %d\n", j+prepend-1, qh_setsize(ints2));
653  error_count++;
654  }
655  /* Define prepended elements. Otherwise qh_setdelnthsorted may fail */
656  for(i= 0; i<prepend; i++){
657  void **p= &SETelem_(ints2, i);
658  *p= intarray+0;
659  }
660  for(i= 0; i<prepend; i++){
661  qh_setdelnthsorted(ints2, 0); /* delete undefined prefix */
662  }
663  checkSetContents("qh_setnew_delnthsorted", ints2, j-1, 0, j/2+1, -1);
664  if(j>2){
665  qh_setzero(ints2, j/2, j-1); /* max size may be j-1 */
666  if(qh_setsize(ints2)!=j-1){
667  qh_fprintf(stderr, 6342, "qh_setzero: Expecting %d elements, got %d\n", j, qh_setsize(ints2));
668  error_count++;
669  }
670  qh_setcompact(ints2);
671  checkSetContents("qh_setzero", ints2, j/2, 0, -1, -1);
672  }
673  }
674  qh_setfree(&ints2);
675  }
676  }
678  if(ints){
679  qh_setfree(&ints); /* Was quick memory */
680  }
681 }/*testSetlastEtc*/
682 
683 void testSettemp(int numInts, int *intarray, int checkEvery)
684 {
685  setT *ints= NULL;
686  setT *ints2= NULL;
687  setT *ints3= NULL;
688  int i,j;
689 
690  qh_fprintf(stderr, 8021, "\n\nTesting qh_settemp* 0..%d. Test", numInts-1);
691  for(j=0; j<numInts; j++){
692  if(log_i(ints, "j", j, numInts, checkEvery)){
693  if(j<20){
694  for(i=0; i<j; i++){
695  ints2= qh_settemp(j);
696  }
698  }
699  for(i= qh_setsize(ints); i<j; i++){ /* Test i<j to test the empty set */
700  qh_setappend(&ints, intarray+i);
701  }
702  ints2= qh_settemp(j);
703  if(j>0){
705  ints3= qh_settemppop();
706  if(ints!=ints3){
707  qh_fprintf(stderr, 6343, "qh_settemppop: didn't pop the push\n");
708  error_count++;
709  }
710  }
711  qh_settempfree(&ints2);
712  }
713  }
715  if(ints){
716  qh_setfree(&ints); /* Was quick memory */
717  }
718 }/*testSettemp*/
719 
720 /* Check that a set contains count elements
721  Ranges are consecutive (e.g., 1,2,3,...) starting with first, mid, and last
722  Use -1 for missing ranges
723  Returns -1 if should check results
724 */
725 int log_i(setT *set, const char *s, int i, int numInts, int checkEvery)
726 {
727  int j= i;
728  int scale= 1;
729  int e= 0;
730  int *i2, **i2p;
731 
732  if(*s || checkEvery==1){
733  if(i<10){
734  qh_fprintf(stderr, 8004, " %s%d", s, i);
735  }else{
736  if(i==11 && checkEvery==1){
737  qh_fprintf(stderr, 8005, "\nResults after 10: ");
738  FOREACHint_(set){
739  qh_fprintf(stderr, 8006, " %d", *i2);
740  }
741  qh_fprintf(stderr, 8007, " Continue");
742  }
743  while((j= j/10)>=1){
744  scale *= 10;
745  e++;
746  }
747  if(i==numInts-1){
748  qh_fprintf(stderr, 8008, " %s%d", s, i);
749  }else if(i==scale){
750  if(i<=1000){
751  qh_fprintf(stderr, 8010, " %s%d", s, i);
752  }else{
753  qh_fprintf(stderr, 8009, " %s1e%d", s, e);
754  }
755  }
756  }
757  }
758  if(i<1000 || i%checkEvery==0 || i== scale || i==numInts-1){
759  return 1;
760  }
761  return 0;
762 }/*log_i*/
763 
764 /* Check that a set contains count elements
765  Ranges are consecutive (e.g., 1,2,3,...) starting with first, mid, and last
766  Use -1 for missing ranges
767 */
768 void checkSetContents(const char *name, setT *set, int count, int rangeA, int rangeB, int rangeC)
769 {
770 
771  i2T *i2, **i2p;
772  int i2_i, i2_n;
773  int prev= -1; /* avoid warning */
774  int i;
775  int first= -3;
776  int second= -3;
777  int rangeCount=1;
778  int actualSize= 0;
779 
780  qh_setcheck(set, name, 0);
781  if(set){
782  SETreturnsize_(set, actualSize); /* normally used only when speed is critical */
783  if(*qh_setendpointer(set)!=NULL){
784  qh_fprintf(stderr, 6344, "%s: qh_setendpointer(), 0x%x, is not NULL terminator of set 0x%x", name, qh_setendpointer(set), set);
785  error_count++;
786  }
787  }
788  if(actualSize!=qh_setsize(set)){
789  qh_fprintf(stderr, 6305, "%s: SETreturnsize_() returned %d while qh_setsize() returns %d\n", name, actualSize, qh_setsize(set));
790  error_count++;
791  }else if(actualSize!=count){
792  qh_fprintf(stderr, 6306, "%s: Expecting %d elements for set. Got %d elements\n", name, count, actualSize);
793  error_count++;
794  }
795  if(SETempty_(set)){
796  if(count!=0){
797  qh_fprintf(stderr, 6307, "%s: Got empty set instead of count %d, rangeA %d, rangeB %d, rangeC %d\n", name, count, rangeA, rangeB, rangeC);
798  error_count++;
799  }
800  }else{
801  /* Must be first, otherwise trips msvc 8 */
802  i2T **p= SETaddr_(set, i2T);
803  if(*p!=SETfirstt_(set, i2T)){
804  qh_fprintf(stderr, 6309, "%s: SETaddr_(set, i2t) [%p] is not the same as SETfirst_(set) [%p]\n", name, SETaddr_(set, i2T), SETfirst_(set));
805  error_count++;
806  }
807  first= *(int *)SETfirst_(set);
808  if(SETfirst_(set)!=SETfirstt_(set, i2T)){
809  qh_fprintf(stderr, 6308, "%s: SETfirst_(set) [%p] is not the same as SETfirstt_(set, i2T [%p]\n", name, SETfirst_(set), SETfirstt_(set, i2T));
810  error_count++;
811  }
812  if(qh_setsize(set)>1){
813  second= *(int *)SETsecond_(set);
814  if(SETsecond_(set)!=SETsecondt_(set, i2T)){
815  qh_fprintf(stderr, 6310, "%s: SETsecond_(set) [%p] is not the same as SETsecondt_(set, i2T) [%p]\n", name, SETsecond_(set), SETsecondt_(set, i2T));
816  error_count++;
817  }
818  }
819  }
820  /* Test first run of ints in set*/
821  i= 0;
822  FOREACHint_(set){
823  if(i2!=SETfirst_(set) && *i2!=prev+1){
824  break;
825  }
826  prev= *i2;
827  if(SETindex_(set, i2)!=i){
828  qh_fprintf(stderr, 6311, "%s: Expecting SETIndex_(set, pointer-to-%d) to be %d. Got %d\n", name, *i2, i, SETindex_(set, i2));
829  error_count++;;
830  }
831  if(i2!=SETref_(i2)){
832  qh_fprintf(stderr, 6312, "%s: SETref_(i2) [%p] does not point to i2 (the %d'th element)\n", name, SETref_(i2), i);
833  error_count++;;
834  }
835  i++;
836  }
837  FOREACHint_i_(set){
838  /* Must be first conditional, otherwise it trips up msvc 8 */
839  i2T **p= SETelemaddr_(set, i2_i, i2T);
840  if(i2!=*p){
841  qh_fprintf(stderr, 6320, "%s: SETelemaddr_(set, %d, i2T) [%p] does not point to i2\n", name, i2_i, SETelemaddr_(set, i2_i, int));
842  error_count++;;
843  }
844  if(i2_i==0){
845  if(first!=*i2){
846  qh_fprintf(stderr, 6314, "%s: First element is %d instead of SETfirst %d\n", name, *i2, first);
847  error_count++;;
848  }
849  if(rangeA!=*i2){
850  qh_fprintf(stderr, 6315, "%s: starts with %d instead of rangeA %d\n", name, *i2, rangeA);
851  error_count++;;
852  }
853  prev= rangeA;
854  }else{
855  if(i2_i==1 && second!=*i2){
856  qh_fprintf(stderr, 6316, "%s: Second element is %d instead of SETsecond %d\n", name, *i2, second);
857  error_count++;;
858  }
859  if(prev+1==*i2){
860  prev++;
861  }else{
862  if(*i2==rangeB){
863  prev= rangeB;
864  rangeB= -1;
865  rangeCount++;
866  }else if(rangeB==-1 && *i2==rangeC){
867  prev= rangeC;
868  rangeC= -1;
869  rangeCount++;
870  }else{
871  prev++;
872  qh_fprintf(stderr, 6317, "%s: Expecting %d'th element to be %d. Got %d\n", name, i2_i, prev, *i2);
873  error_count++;
874  }
875  }
876  }
877  if(i2!=SETelem_(set, i2_i)){
878  qh_fprintf(stderr, 6318, "%s: SETelem_(set, %d) [%p] is not i2 [%p] (the %d'th element)\n", name, i2_i, SETelem_(set, i2_i), i2, i2_i);
879  error_count++;;
880  }
881  if(SETelemt_(set, i2_i, i2T)!=SETelem_(set, i2_i)){ /* Normally SETelemt_ is used for generic sets */
882  qh_fprintf(stderr, 6319, "%s: SETelemt_(set, %d, i2T) [%p] is not SETelem_(set, %d) [%p] (the %d'th element)\n", name, i2_i, SETelemt_(set, i2_i, int), i2_i, SETelem_(set, i2_i), i2_i);
883  error_count++;;
884  }
885  }
887  qh_fprintf(stderr, 8011, "testqset: Stop testing after %d errors\n", error_count);
888  exit(1);
889  }
890 }/*checkSetContents*/
891 
SETref_
#define SETref_(elem)
Definition: qset.h:319
i2T
int i2T
Definition: testqset.c:83
error_count
int error_count
Definition: testqset.c:109
testSetequalInEtc
void testSetequalInEtc(int numInts, int *intarray, int checkEvery)
Definition: testqset.c:503
qh_setdelsorted
void * qh_setdelsorted(setT *set, void *oldelem)
Definition: qset.c:504
qh_MEMbufsize
#define qh_MEMbufsize
Definition: testqset.c:118
setupMemory
void setupMemory(int tracelevel, int numInts, int **intarray)
Definition: testqset.c:279
qh_meminitbuffers
void qh_meminitbuffers(int tracelevel, int alignment, int numsizes, int bufsize, int bufinit)
Definition: mem.c:346
qh_setappend_set
void qh_setappend_set(setT **setp, setT *setA)
Definition: qset.c:163
obb.fmt
fmt
Definition: obb.py:145
qh_setfreelong
void qh_setfreelong(setT **setp)
Definition: qset.c:770
FOREACHint_i_
#define FOREACHint_i_(ints)
Definition: testqset.c:88
qh_setdelnthsorted
void * qh_setdelnthsorted(setT *set, int nth)
Definition: qset.c:465
SETelemaddr_
#define SETelemaddr_(set, n, type)
Definition: qset.h:353
qh_free
void qh_free(void *mem)
Definition: usermem.c:77
qh_setdel
void * qh_setdel(setT *set, void *oldelem)
Definition: qset.c:344
qh_setunique
int qh_setunique(setT **set, void *elem)
Definition: qset.c:1299
FOREACHint_
#define FOREACHint_(ints)
Definition: testqset.c:86
qh_settemppush
void qh_settemppush(setT *set)
Definition: qset.c:1247
qh_setdelnth
void * qh_setdelnth(setT *set, int nth)
Definition: qset.c:424
testSetdelSetadd
void testSetdelSetadd(int numInts, int *intarray, int checkEvery)
Definition: testqset.c:354
qh_setlast
void * qh_setlast(setT *set)
Definition: qset.c:895
qh_setfree
void qh_setfree(setT **setp)
Definition: qset.c:716
qh_MEMalign
#define qh_MEMalign
Definition: testqset.c:117
qh_exit
void qh_exit(int exitcode)
Definition: usermem.c:38
qh_setlarger
void qh_setlarger(setT **oldsetp)
Definition: qset.c:855
qh_setcopy
setT * qh_setcopy(setT *set, int extra)
Definition: qset.c:310
qh_malloc
void * qh_malloc(size_t size)
Definition: usermem.c:90
SETsecondt_
#define SETsecondt_(set, type)
Definition: qset.h:388
qh_setappend2ndlast
void qh_setappend2ndlast(setT **setp, void *newelem)
Definition: qset.c:207
log_i
int log_i(setT *set, const char *s, int i, int numInts, int checkEvery)
Definition: testqset.c:725
octree.r
r
Definition: octree.py:9
qh_memsize
void qh_memsize(int size)
Definition: mem.c:404
SETelemt_
#define SETelemt_(set, n, type)
Definition: qset.h:342
qh_setappend
void qh_setappend(setT **setp, void *newelem)
Definition: qset.c:131
doxygen_xml_parser.args
args
Definition: doxygen_xml_parser.py:884
qh_setdellast
void * qh_setdellast(setT *set)
Definition: qset.c:384
SETreturnsize_
#define SETreturnsize_(set, size)
Definition: qset.h:408
qh_settemp
setT * qh_settemp(int setsize)
Definition: qset.c:1146
testSetappendSet
void testSetappendSet(int numInts, int *intarray, int checkEvery)
Definition: testqset.c:381
qh_settruncate
void qh_settruncate(setT *set, int size)
Definition: qset.c:1275
setT
Definition: qset.h:83
qh_setequal_skip
int qh_setequal_skip(setT *setA, int skipA, setT *setB, int skipB)
Definition: qset.c:677
SETelem_
#define SETelem_(set, n)
Definition: qset.h:331
qh_setequal_except
int qh_setequal_except(setT *setA, void *skipelemA, setT *setB, void *skipelemB)
Definition: qset.c:629
qh_setequal
int qh_setequal(setT *setA, setT *setB)
Definition: qset.c:587
qh_setsize
int qh_setsize(setT *set)
Definition: qset.c:1111
SETtruncate_
#define SETtruncate_(set, size)
Definition: qset.h:445
prompt
char prompt[]
Definition: testqset.c:96
qh_setaddsorted
void qh_setaddsorted(setT **setp, void *newelem)
Definition: qset.c:100
qh_fprintf_stderr
void qh_fprintf_stderr(int msgcode, const char *fmt,...)
Definition: usermem.c:57
SETfirst_
#define SETfirst_(set)
Definition: qset.h:362
testSetdelsortedEtc
void testSetdelsortedEtc(int numInts, int *intarray, int checkEvery)
Definition: testqset.c:448
SETsecond_
#define SETsecond_(set)
Definition: qset.h:380
qh_settemppop
setT * qh_settemppop(void)
Definition: qset.c:1221
name
name
qh_setnew
setT * qh_setnew(int setsize)
Definition: qset.c:924
qh_errexit
void qh_errexit(int exitcode, void *f, void *r)
Definition: testqset.c:133
testSetappendSettruncate
void testSetappendSettruncate(int numInts, int *intarray, int checkEvery)
Definition: testqset.c:306
SETaddr_
#define SETaddr_(set, type)
Definition: qset.h:396
SETelemsize
#define SETelemsize
Definition: qset.h:100
testSetlastEtc
void testSetlastEtc(int numInts, int *intarray, int checkEvery)
Definition: testqset.c:613
qh_MEMinitbuf
#define qh_MEMinitbuf
Definition: testqset.c:119
MAXint
@ MAXint
Definition: testqset.c:93
SETempty_
#define SETempty_(set)
Definition: qset.h:419
qh_setreplace
void qh_setreplace(setT *set, void *oldelem, void *newelem)
Definition: qset.c:1080
qh_settempfree_all
void qh_settempfree_all(void)
Definition: qset.c:1201
checkSetContents
void checkSetContents(const char *name, setT *set, int count, int rangeA, int rangeB, int rangeC)
Definition: testqset.c:768
SETfirstt_
#define SETfirstt_(set, type)
Definition: qset.h:371
main
int main(int argc, char **argv)
Definition: testqset.c:189
qh_memfreeshort
void qh_memfreeshort(int *curlong, int *totlong)
Definition: mem.c:288
user.h
qh_settempfree
void qh_settempfree(setT **set)
Definition: qset.c:1174
MAXerrorCount
#define MAXerrorCount
Definition: testqset.c:84
ints
ints
qh_memsetup
void qh_memsetup(void)
Definition: mem.c:373
qset.h
qh_setaddnth
void qh_setaddnth(setT **setp, int nth, void *newelem)
Definition: qset.c:61
mem.h
qh_setindex
int qh_setindex(setT *set, void *atelem)
Definition: qset.c:821
qh_setendpointer
void ** qh_setendpointer(setT *set)
Definition: qset.c:566
qh_setzero
void qh_setzero(setT *set, int idx, int size)
Definition: qset.c:1327
qh_meminit
void qh_meminit(FILE *ferr)
Definition: mem.c:317
testSettemp
void testSettemp(int numInts, int *intarray, int checkEvery)
Definition: testqset.c:683
SETindex_
#define SETindex_(set, elem)
Definition: qset.h:308
readOptions
void readOptions(int argc, char **argv, const char *promptstr, int *numInts, int *checkEvery, int *traceLevel)
Definition: testqset.c:230
qh_fprintf
void qh_fprintf(FILE *fp, int msgcode, const char *fmt,...)
Definition: testqset.c:142
qh_setnew_delnthsorted
setT * qh_setnew_delnthsorted(setT *set, int size, int nth, int prepend)
Definition: qset.c:967
qh_setin
int qh_setin(setT *set, void *setelem)
Definition: qset.c:795
qh_setcompact
void qh_setcompact(setT *set)
Definition: qset.c:276
testSetcompactCopy
void testSetcompactCopy(int numInts, int *intarray, int checkEvery)
Definition: testqset.c:419
qh_memstatistics
void qh_memstatistics(FILE *fp)
Definition: mem.c:431
qh_setcheck
void qh_setcheck(setT *set, const char *tname, unsigned id)
Definition: qset.c:234


hpp-fcl
Author(s):
autogenerated on Fri Aug 2 2024 02:45:15