00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include <stdarg.h>
00071 #include <stdio.h>
00072 #include <stdlib.h>
00073 #include <string.h>
00074
00075 #include "qset.h"
00076 #include "mem.h"
00077
00078 typedef int i2T;
00079 #define MAXerrorCount 100
00080
00081 #define FOREACHint_( ints ) FOREACHsetelement_( i2T, ints, i2)
00082 #define FOREACHint4_( ints ) FOREACHsetelement_( i2T, ints, i4)
00083 #define FOREACHint_i_( ints ) FOREACHsetelement_i_( i2T, ints, i2)
00084 #define FOREACHintreverse_( ints ) FOREACHsetelementreverse_( i2T, ints, i2)
00085 #define FOREACHintreverse12_( ints ) FOREACHsetelementreverse12_( i2T, ints, i2)
00086
00087 enum {
00088 MAXint= 0x7fffffff,
00089 };
00090
00091 char prompt[]= "testqset N [M] -- Test qset.c and mem.c\n\
00092 Test qsets of 0..N integers with a check every M iterations (default ~log10)\n\
00093 Additional checking and logging if M is 1\n\
00094 For example:\n\
00095 testqset 10000\n\
00096 ";
00097
00098 int error_count= 0;
00099
00100
00101
00102 #define qh_MEMalign ((int)(sizeof(void *)))
00103 #define qh_MEMbufsize 0x10000
00104 #define qh_MEMinitbuf 0x20000
00105
00106
00107
00108
00109
00110
00111 void qh_exit(int exitcode) {
00112 exit(exitcode);
00113 }
00114
00115 void qh_free(void *mem) {
00116 free(mem);
00117 }
00118
00119 void *qh_malloc(size_t size) {
00120 return malloc(size);
00121 }
00122
00123 void qh_errexit(int exitcode, void *f, void *r)
00124 {
00125 f= r;
00126 qh_exit(exitcode);
00127 }
00128
00129 void qh_fprintf(FILE *fp, int msgcode, const char *fmt, ... )
00130 {
00131 static int needs_cr= 0;
00132
00133 size_t fmtlen= strlen(fmt);
00134 va_list args;
00135
00136 if (!fp) {
00137 fprintf(stderr, "qh_fprintf: fp not defined for '%s'", fmt);
00138 qh_errexit(6232, NULL, NULL);
00139 }
00140 if(fmtlen>0){
00141 if(fmt[fmtlen-1]=='\n'){
00142 if(needs_cr && fmtlen>1){
00143 fprintf(fp, "\n");
00144 }
00145 needs_cr= 0;
00146 }else{
00147 needs_cr= 1;
00148 }
00149 }
00150 if(msgcode>=6000 && msgcode<7000){
00151 fprintf(fp, "Error TQ%d ", msgcode);
00152 }
00153 va_start(args, fmt);
00154 vfprintf(fp, fmt, args);
00155 va_end(args);
00156 }
00157
00158
00159 int main(int argc, char **argv);
00160 void readOptions(int argc, char **argv, const char *promptstr, int *numInts, int *checkEvery);
00161 void setupMemory(int tracelevel, int numInts, int **intarray);
00162
00163 void testSetappendSettruncate(int numInts, int *intarray, int checkEvery);
00164 void testSetdelSetadd(int numInts, int *intarray, int checkEvery);
00165 void testSetappendSet(int numInts, int *intarray, int checkEvery);
00166 void testSetcompactCopy(int numInts, int *intarray, int checkEvery);
00167 void testSetequalInEtc(int numInts, int *intarray, int checkEvery);
00168 void testSettemp(int numInts, int *intarray, int checkEvery);
00169 void testSetlastEtc(int numInts, int *intarray, int checkEvery);
00170 void testSetdelsortedEtc(int numInts, int *intarray, int checkEvery);
00171
00172 int log_i(setT *set, const char *s, int i, int numInts, int checkEvery);
00173 void checkSetContents(const char *name, setT *set, int count, int rangeA, int rangeB, int rangeC);
00174
00175 int main(int argc, char **argv) {
00176 int *intarray= NULL;
00177 int numInts;
00178 int checkEvery= MAXint;
00179 int curlong, totlong;
00180 int tracelevel= 4;
00181
00182 readOptions(argc, argv, prompt, &numInts, &checkEvery);
00183 setupMemory(tracelevel, numInts, &intarray);
00184
00185 testSetappendSettruncate(numInts, intarray, checkEvery);
00186 testSetdelSetadd(numInts, intarray, checkEvery);
00187 testSetappendSet(numInts, intarray, checkEvery);
00188 testSetcompactCopy(numInts, intarray, checkEvery);
00189 testSetequalInEtc(numInts, intarray, checkEvery);
00190 testSettemp(numInts, intarray, checkEvery);
00191 testSetlastEtc(numInts, intarray, checkEvery);
00192 testSetdelsortedEtc(numInts, intarray, checkEvery);
00193 printf("\n\nNot testing qh_setduplicate and qh_setfree2.\n These routines use heap-allocated set contents. See qhull tests.\n");
00194
00195 qh_memstatistics(stdout);
00196 qh_memfreeshort(&curlong, &totlong);
00197 if (curlong || totlong){
00198 qh_fprintf(stderr, 8043, "qh_memfreeshort: did not free %d bytes of long memory(%d pieces)\n", totlong, curlong);
00199 error_count++;
00200 }
00201 if(error_count){
00202 qh_fprintf(stderr, 8012, "testqset: %d errors\n\n", error_count);
00203 exit(1);
00204 }else{
00205 printf("testqset: OK\n\n");
00206 }
00207 return 0;
00208 }
00209
00210 void readOptions(int argc, char **argv, const char *promptstr, int *numInts, int *checkEvery)
00211 {
00212 long numIntsArg;
00213 long checkEveryArg;
00214 char *endp;
00215
00216 if (argc != 2 && argc != 3) {
00217 printf(promptstr);
00218 exit(0);
00219 }
00220 numIntsArg= strtol(argv[1], &endp, 10);
00221 if(numIntsArg<1){
00222 qh_fprintf(stderr, 6301, "First argument should be 1 or greater. Got '%s'\n", argv[1]);
00223 exit(1);
00224 }
00225 if(numIntsArg>MAXint){
00226 qh_fprintf(stderr, 6302, "qset does not currently support 64-bit ints. Maximum count is %d\n", MAXint);
00227 exit(1);
00228 }
00229 *numInts= (int)numIntsArg;
00230
00231 if(argc>2){
00232 checkEveryArg= strtol(argv[2], &endp, 10);
00233 if(checkEveryArg<1){
00234 qh_fprintf(stderr, 6321, "checkEvery argument should be 1 or greater. Got '%s'\n", argv[2]);
00235 exit(1);
00236 }
00237 if(checkEveryArg>MAXint){
00238 qh_fprintf(stderr, 6322, "qset does not currently support 64-bit ints. Maximum checkEvery is %d\n", MAXint);
00239 exit(1);
00240 }
00241 *checkEvery= (int)checkEveryArg;
00242 }
00243 }
00244
00245 void setupMemory(int tracelevel, int numInts, int **intarray)
00246 {
00247 int i;
00248 if(numInts<0 || numInts*(int)sizeof(int)<0){
00249 qh_fprintf(stderr, 6303, "qset does not currently support 64-bit ints. Integer overflow\n");
00250 exit(1);
00251 }
00252 *intarray= qh_malloc(numInts * sizeof(int));
00253 if(!*intarray){
00254 qh_fprintf(stderr, 6304, "Failed to allocate %d bytes of memory\n", numInts * sizeof(int));
00255 exit(1);
00256 }
00257 for(i= 0; i<numInts; i++){
00258 (*intarray)[i] =i;
00259 }
00260
00261 qh_meminit(stderr);
00262 qh_meminitbuffers(tracelevel, qh_MEMalign, 4 , qh_MEMbufsize,qh_MEMinitbuf);
00263 qh_memsize(10);
00264 qh_memsize(20);
00265 qh_memsize(30);
00266 qh_memsize(40);
00267 qh_memsetup();
00268
00269 qh_fprintf(stderr, 8001, "SETelemsize is %d bytes for pointer-to-int\n", SETelemsize);
00270 }
00271
00272 void testSetappendSettruncate(int numInts, int *intarray, int checkEvery)
00273 {
00274 setT *ints= qh_setnew(4);
00275 int i, isCheck;
00276
00277 qh_fprintf(stderr, 8002, "\n\nTesting qh_setappend 0..%d. Test", numInts-1);
00278 for(i= 0; i<numInts; i++){
00279 isCheck= log_i(ints, "i", i, numInts, checkEvery);
00280 qh_setappend(&ints, intarray+i);
00281 if(isCheck){
00282 checkSetContents("qh_setappend", ints, i+1, 0, -1, -1);
00283 }
00284 }
00285
00286 qh_fprintf(stderr, 8014, "\n\nTesting qh_settruncate %d and 0. Test", numInts/2);
00287 if(numInts>=2){
00288 isCheck= log_i(ints, "n", numInts/2, numInts, checkEvery);
00289 qh_settruncate(ints, numInts/2);
00290 checkSetContents("qh_settruncate by half", ints, numInts/2, 0, -1, -1);
00291 }
00292 isCheck= log_i(ints, "n", 0, numInts, checkEvery);
00293 qh_settruncate(ints, 0);
00294 checkSetContents("qh_settruncate", ints, 0, -1, -1, -1);
00295
00296 qh_fprintf(stderr, 8003, "\n\nTesting qh_setappend2ndlast 0,0..%d. Test 0", numInts-1);
00297 qh_setfree(&ints);
00298 ints= qh_setnew(4);
00299 qh_setappend(&ints, intarray+0);
00300 for(i= 0; i<numInts; i++){
00301 isCheck= log_i(ints, "i", i, numInts, checkEvery);
00302 qh_setappend2ndlast(&ints, intarray+i);
00303 if(isCheck){
00304 checkSetContents("qh_setappend2ndlast", ints, i+2, 0, 0, -1);
00305 }
00306 }
00307 qh_fprintf(stderr, 8015, "\n\nTesting SETtruncate_ %d and 0. Test", numInts/2);
00308 if(numInts>=2){
00309 isCheck= log_i(ints, "n", numInts/2, numInts, checkEvery);
00310 SETtruncate_(ints, numInts/2);
00311 checkSetContents("SETtruncate_ by half", ints, numInts/2, 0, -1, -1);
00312 }
00313 isCheck= log_i(ints, "n", 0, numInts, checkEvery);
00314 SETtruncate_(ints, 0);
00315 checkSetContents("SETtruncate_", ints, 0, -1, -1, -1);
00316
00317 qh_setfree(&ints);
00318 }
00319
00320 void testSetdelSetadd(int numInts, int *intarray, int checkEvery)
00321 {
00322 setT *ints=qh_setnew(1);
00323 int i,j, isCheck;
00324
00325 qh_fprintf(stderr, 8003, "\n\nTesting qh_setdelnthsorted and qh_setaddnth 1..%d. Test", numInts-1);
00326 for(j=1; j<numInts; j++){
00327 if(log_i(ints, "j", j, numInts, MAXint)){
00328 for(i= qh_setsize(ints); i<j; i++){
00329 qh_setappend(&ints, intarray+i);
00330 }
00331 checkSetContents("qh_setappend", ints, j, 0, -1, -1);
00332 for(i= 0; i<j && i<100; i++){
00333 isCheck= log_i(ints, "", i, numInts, checkEvery);
00334 qh_setdelnthsorted(ints, i);
00335 qh_setaddnth(&ints, i, intarray+i);
00336 if(checkEvery==1){
00337 checkSetContents("qh_setdelnthsorted qh_setaddnth", ints, j, 0, -1, -1);
00338 }
00339 }
00340 checkSetContents("qh_setdelnthsorted qh_setaddnth 2", ints, j, 0, -1, -1);
00341 }
00342 }
00343 qh_setfree(&ints);
00344 }
00345
00346 void testSetappendSet(int numInts, int *intarray, int checkEvery)
00347 {
00348 setT *ints=qh_setnew(1);
00349 setT *ints2;
00350 int i,j,k;
00351
00352 qh_fprintf(stderr, 8016, "\n\nTesting qh_setappend_set 0..%d. Test", numInts-1);
00353 for(j=0; j<numInts; j++){
00354 if(log_i(ints, "j", j, numInts, numInts)){
00355 for(i= qh_setsize(ints); i<j; i++){
00356 qh_setappend(&ints, intarray+i);
00357 }
00358 if(checkEvery==1){
00359 checkSetContents("qh_setappend", ints, j, 0, -1, -1);
00360 }
00361 ints2= qh_setnew(j==0 ? 0 : j-1);
00362 for(i= 0; i<=j && i<=20; i++){
00363 if(log_i(ints, "", i, numInts, numInts)){
00364 for(k= qh_setsize(ints2); k<i; k++){
00365 qh_setappend(&ints2, intarray+k);
00366 }
00367 if(checkEvery==1){
00368 checkSetContents("qh_setappend 2", ints2, i, 0, -1, -1);
00369 }
00370 qh_setappend_set(&ints, ints2);
00371 checkSetContents("qh_setappend_set", ints, i+j, 0, (j==0 ? -1 : 0), -1);
00372 qh_settruncate(ints, j);
00373 if(checkEvery==1){
00374 checkSetContents("qh_settruncate", ints, j, 0, -1, -1);
00375 }
00376 }
00377 }
00378 qh_setfree(&ints2);
00379 }
00380 }
00381 qh_setfree(&ints);
00382 }
00383
00384 void testSetcompactCopy(int numInts, int *intarray, int checkEvery)
00385 {
00386 setT *ints= qh_setnew(20);
00387 setT *ints2= NULL;
00388 int i,j,k;
00389
00390 qh_fprintf(stderr, 8017, "\n\nTesting qh_setcompact and qh_setcopy 0..%d. Test", numInts-1);
00391 for(j=0; j<numInts; j++){
00392 if(log_i(ints, "j", j, numInts, checkEvery)){
00393 for(i= qh_setsize(ints); i<j; i++){
00394 for(k= 0; k<i%7; k++){
00395 qh_setappend(&ints, NULL);
00396 }
00397 qh_setappend(&ints, intarray+i);
00398 }
00399 qh_setfree(&ints2);
00400 ints2= qh_setcopy(ints, 0);
00401 qh_setcompact(ints);
00402 qh_setcompact(ints2);
00403 checkSetContents("qh_setcompact", ints, j, 0, 0, -1);
00404 checkSetContents("qh_setcompact", ints2, j, 0, 0, -1);
00405 qh_setcompact(ints);
00406 checkSetContents("qh_setcompact", ints, j, 0, 0, -1);
00407 }
00408 }
00409 qh_setfree(&ints);
00410 qh_setfree(&ints2);
00411 }
00412
00413 void testSetdelsortedEtc(int numInts, int *intarray, int checkEvery)
00414 {
00415 setT *ints= qh_setnew(1);
00416 setT *ints2= NULL;
00417 int i,j;
00418
00419 qh_fprintf(stderr, 8018, "\n\nTesting qh_setdel*, qh_setaddsorted, and 0..%d. Test", numInts-1);
00420 for(j=0; j<numInts; j++){
00421 if(log_i(ints, "j", j, numInts, checkEvery)){
00422 for(i= qh_setsize(ints); i<j; i++){
00423 qh_setaddsorted(&ints, intarray+i);
00424 }
00425 checkSetContents("qh_setaddsorted", ints, j, 0, 0, -1);
00426 if(j>3){
00427 qh_setdelsorted(ints, intarray+i/2);
00428 checkSetContents("qh_setdelsorted", ints, j-1, 0, i/2+1, -1);
00429 qh_setaddsorted(&ints, intarray+i/2);
00430 checkSetContents("qh_setaddsorted i/2", ints, j, 0, 0, -1);
00431 }
00432 qh_setdellast(ints);
00433 checkSetContents("qh_setdellast", ints, (j ? j-1 : 0), 0, -1, -1);
00434 if(j>0){
00435 qh_setaddsorted(&ints, intarray+j-1);
00436 checkSetContents("qh_setaddsorted j-1", ints, j, 0, -1, -1);
00437 }
00438 if(j>4){
00439 qh_setdelnthsorted(ints, i/2);
00440 if (checkEvery==1)
00441 checkSetContents("qh_setdelnthsorted", ints, j-1, 0, i/2+1, -1);
00442
00443 qh_setdelsorted(ints, intarray+i/2+1);
00444 checkSetContents("qh_setdelsorted 2", ints, j-2, 0, i/2+2, -1);
00445 qh_setaddsorted(&ints, intarray+i/2+1);
00446 if (checkEvery==1)
00447 checkSetContents("qh_setaddsorted i/2+1", ints, j-1, 0, i/2+1, -1);
00448 qh_setaddsorted(&ints, intarray+i/2);
00449 checkSetContents("qh_setaddsorted i/2 again", ints, j, 0, -1, -1);
00450 }
00451 qh_setfree(&ints2);
00452 ints2= qh_setcopy(ints, 0);
00453 qh_setcompact(ints);
00454 qh_setcompact(ints2);
00455 checkSetContents("qh_setcompact", ints, j, 0, 0, -1);
00456 checkSetContents("qh_setcompact 2", ints2, j, 0, 0, -1);
00457 qh_setcompact(ints);
00458 checkSetContents("qh_setcompact 3", ints, j, 0, 0, -1);
00459 qh_setfree(&ints2);
00460 }
00461 }
00462 qh_setfreelong(&ints);
00463 if(ints){
00464 qh_setfree(&ints);
00465 }
00466 }
00467
00468 void testSetequalInEtc(int numInts, int *intarray, int checkEvery)
00469 {
00470 setT *ints= NULL;
00471 setT *ints2= NULL;
00472 setT *ints3= NULL;
00473 int i,j,n;
00474
00475 qh_fprintf(stderr, 8019, "\n\nTesting qh_setequal*, qh_setin*, qh_setdel, qh_setdelnth, and qh_setlarger 0..%d. Test", numInts-1);
00476 for(j=0; j<numInts; j++){
00477 if(log_i(ints, "j", j, numInts, checkEvery)){
00478 n= qh_setsize(ints);
00479 qh_setlarger(&ints);
00480 checkSetContents("qh_setlarger", ints, n, 0, -1, -1);
00481 for(i= qh_setsize(ints); i<j; i++){
00482 qh_setappend(&ints, intarray+i);
00483 }
00484 checkSetContents("qh_setappend", ints, j, 0, -1, -1);
00485 if(!qh_setequal(ints, ints)){
00486 qh_fprintf(stderr, 6300, "testSetequalInEtc: set not equal to itself at length %d\n", j);
00487 error_count++;
00488 }
00489 if(j==0 && !qh_setequal(ints, ints2)){
00490 qh_fprintf(stderr, 6323, "testSetequalInEtc: empty set not equal to null set\n");
00491 error_count++;
00492 }
00493 if(j>0){
00494 if(qh_setequal(ints, ints2)){
00495 qh_fprintf(stderr, 6324, "testSetequalInEtc: non-empty set equal to empty set\n", j);
00496 error_count++;
00497 }
00498 qh_setfree(&ints3);
00499 ints3= qh_setcopy(ints, 0);
00500 checkSetContents("qh_setreplace", ints3, j, 0, -1, -1);
00501 qh_setreplace(ints3, intarray+j/2, intarray+j/2+1);
00502 if(j==1){
00503 checkSetContents("qh_setreplace 2", ints3, j, j/2+1, -1, -1);
00504 }else if(j==2){
00505 checkSetContents("qh_setreplace 3", ints3, j, 0, j/2+1, -1);
00506 }else{
00507 checkSetContents("qh_setreplace 3", ints3, j, 0, j/2+1, j/2+1);
00508 }
00509 if(qh_setequal(ints, ints3)){
00510 qh_fprintf(stderr, 6325, "testSetequalInEtc: modified set equal to original set at %d/2\n", j);
00511 error_count++;
00512 }
00513 if(!qh_setequal_except(ints, intarray+j/2, ints3, intarray+j/2+1)){
00514 qh_fprintf(stderr, 6326, "qh_setequal_except: modified set not equal to original set except modified\n", j);
00515 error_count++;
00516 }
00517 if(qh_setequal_except(ints, intarray+j/2, ints3, intarray)){
00518 qh_fprintf(stderr, 6327, "qh_setequal_except: modified set equal to original set with wrong excepts\n", j);
00519 error_count++;
00520 }
00521 if(!qh_setequal_skip(ints, j/2, ints3, j/2)){
00522 qh_fprintf(stderr, 6328, "qh_setequal_skip: modified set not equal to original set except modified\n", j);
00523 error_count++;
00524 }
00525 if(j>2 && qh_setequal_skip(ints, j/2, ints3, 0)){
00526 qh_fprintf(stderr, 6329, "qh_setequal_skip: modified set equal to original set with wrong excepts\n", j);
00527 error_count++;
00528 }
00529 if(intarray+j/2+1!=qh_setdel(ints3, intarray+j/2+1)){
00530 qh_fprintf(stderr, 6330, "qh_setdel: failed to find added element\n", j);
00531 error_count++;
00532 }
00533 checkSetContents("qh_setdel", ints3, j-1, 0, j-1, (j==1 ? -1 : j/2+1));
00534 if(j>3){
00535 qh_setdelnth(ints3, j/2);
00536 checkSetContents("qh_setdelnth", ints3, j-2, 0, j-2, (j==2 ? -1 : j/2+1));
00537 }
00538 if(qh_setin(ints3, intarray+j/2)){
00539 qh_fprintf(stderr, 6331, "qh_setin: found deleted element\n");
00540 error_count++;
00541 }
00542 if(j>4 && !qh_setin(ints3, intarray+1)){
00543 qh_fprintf(stderr, 6332, "qh_setin: did not find second element\n");
00544 error_count++;
00545 }
00546 if(j>4 && !qh_setin(ints3, intarray+j-2)){
00547 qh_fprintf(stderr, 6333, "qh_setin: did not find last element\n");
00548 error_count++;
00549 }
00550 if(-1!=qh_setindex(ints2, intarray)){
00551 qh_fprintf(stderr, 6334, "qh_setindex: found element in empty set\n");
00552 error_count++;
00553 }
00554 if(-1!=qh_setindex(ints3, intarray+j/2)){
00555 qh_fprintf(stderr, 6335, "qh_setindex: found deleted element in set\n");
00556 error_count++;
00557 }
00558 if(0!=qh_setindex(ints, intarray)){
00559 qh_fprintf(stderr, 6336, "qh_setindex: did not find first in set\n");
00560 error_count++;
00561 }
00562 if(j-1!=qh_setindex(ints, intarray+j-1)){
00563 qh_fprintf(stderr, 6337, "qh_setindex: did not find last in set\n");
00564 error_count++;
00565 }
00566 }
00567 qh_setfree(&ints2);
00568 }
00569 }
00570 qh_setfree(&ints3);
00571 qh_setfreelong(&ints);
00572 if(ints){
00573 qh_setfree(&ints);
00574 }
00575 }
00576
00577
00578 void testSetlastEtc(int numInts, int *intarray, int checkEvery)
00579 {
00580 setT *ints= NULL;
00581 setT *ints2= NULL;
00582 int i,j,prepend;
00583
00584 qh_fprintf(stderr, 8020, "\n\nTesting qh_setlast, qh_setnew_delnthsorted, qh_setunique, and qh_setzero 0..%d. Test", numInts-1);
00585 for(j=0; j<numInts; j++){
00586 if(log_i(ints, "j", j, numInts, checkEvery)){
00587 for(i= qh_setsize(ints); i<j; i++){
00588 if(!qh_setunique(&ints, intarray+i)){
00589 qh_fprintf(stderr, 6340, "qh_setunique: not able to append next element %d\n", i);
00590 error_count++;
00591 }
00592 if(checkEvery==1){
00593 checkSetContents("qh_setunique", ints, i+1, 0, -1, -1);
00594 }
00595 if(qh_setunique(&ints, intarray+i)){
00596 qh_fprintf(stderr, 6341, "qh_setunique: appended next element twice %d\n", i);
00597 error_count++;
00598 }
00599 if(qh_setunique(&ints, intarray+i/2)){
00600 qh_fprintf(stderr, 6346, "qh_setunique: appended middle element twice %d/2\n", i);
00601 error_count++;
00602 }
00603 }
00604 checkSetContents("qh_setunique 2", ints, j, 0, -1, -1);
00605 if(j==0 && NULL!=qh_setlast(ints)){
00606 qh_fprintf(stderr, 6339, "qh_setlast: returned last element of empty set\n");
00607 error_count++;
00608 }
00609 if(j>0){
00610 if(intarray+j-1!=qh_setlast(ints)){
00611 qh_fprintf(stderr, 6338, "qh_setlast: wrong last element\n");
00612 error_count++;
00613 }
00614 prepend= (j<100 ? j/4 : 0);
00615 ints2= qh_setnew_delnthsorted(ints, qh_setsize(ints), j/2, prepend);
00616 if(qh_setsize(ints2)!=j+prepend-1){
00617 qh_fprintf(stderr, 6345, "qh_setnew_delnthsorted: Expecting %d elements, got %d\n", j+prepend-1, qh_setsize(ints2));
00618 error_count++;
00619 }
00620
00621 for(i= 0; i<prepend; i++){
00622 void **p= &SETelem_(ints2, i);
00623 *p= intarray+0;
00624 }
00625 for(i= 0; i<prepend; i++){
00626 qh_setdelnthsorted(ints2, 0);
00627 }
00628 checkSetContents("qh_setnew_delnthsorted", ints2, j-1, 0, j/2+1, -1);
00629 if(j>2){
00630 qh_setzero(ints2, j/2, j-1);
00631 if(qh_setsize(ints2)!=j-1){
00632 qh_fprintf(stderr, 6342, "qh_setzero: Expecting %d elements, got %d\n", j, qh_setsize(ints2));
00633 error_count++;
00634 }
00635 qh_setcompact(ints2);
00636 checkSetContents("qh_setzero", ints2, j/2, 0, -1, -1);
00637 }
00638 }
00639 qh_setfree(&ints2);
00640 }
00641 }
00642 qh_setfreelong(&ints);
00643 if(ints){
00644 qh_setfree(&ints);
00645 }
00646 }
00647
00648 void testSettemp(int numInts, int *intarray, int checkEvery)
00649 {
00650 setT *ints= NULL;
00651 setT *ints2= NULL;
00652 setT *ints3= NULL;
00653 int i,j;
00654
00655 qh_fprintf(stderr, 8021, "\n\nTesting qh_settemp* 0..%d. Test", numInts-1);
00656 for(j=0; j<numInts; j++){
00657 if(log_i(ints, "j", j, numInts, checkEvery)){
00658 if(j<20){
00659 for(i=0; i<j; i++){
00660 ints2= qh_settemp(j);
00661 }
00662 qh_settempfree_all();
00663 }
00664 for(i= qh_setsize(ints); i<j; i++){
00665 qh_setappend(&ints, intarray+i);
00666 }
00667 ints2= qh_settemp(j);
00668 if(j>0){
00669 qh_settemppush(ints);
00670 ints3= qh_settemppop();
00671 if(ints!=ints3){
00672 qh_fprintf(stderr, 6343, "qh_settemppop: didn't pop the push\n");
00673 error_count++;
00674 }
00675 }
00676 qh_settempfree(&ints2);
00677 }
00678 }
00679 qh_setfreelong(&ints);
00680 if(ints){
00681 qh_setfree(&ints);
00682 }
00683 }
00684
00685
00686
00687
00688
00689
00690 int log_i(setT *set, const char *s, int i, int numInts, int checkEvery)
00691 {
00692 int j= i;
00693 int scale= 1;
00694 int e= 0;
00695 int *i2, **i2p;
00696
00697 if(*s || checkEvery==1){
00698 if(i<10){
00699 qh_fprintf(stderr, 8004, " %s%d", s, i);
00700 }else{
00701 if(i==11 && checkEvery==1){
00702 qh_fprintf(stderr, 8005, "\nResults after 10: ");
00703 FOREACHint_(set){
00704 qh_fprintf(stderr, 8006, " %d", *i2);
00705 }
00706 qh_fprintf(stderr, 8007, " Continue");
00707 }
00708 while((j= j/10)>=1){
00709 scale *= 10;
00710 e++;
00711 }
00712 if(i==numInts-1){
00713 qh_fprintf(stderr, 8008, " %s%d", s, i);
00714 }else if(i==scale){
00715 if(i<=1000){
00716 qh_fprintf(stderr, 8010, " %s%d", s, i);
00717 }else{
00718 qh_fprintf(stderr, 8009, " %s1e%d", s, e);
00719 }
00720 }
00721 }
00722 }
00723 if(i<1000 || i%checkEvery==0 || i== scale || i==numInts-1){
00724 return 1;
00725 }
00726 return 0;
00727 }
00728
00729
00730
00731
00732
00733 void checkSetContents(const char *name, setT *set, int count, int rangeA, int rangeB, int rangeC)
00734 {
00735
00736 i2T *i2, **i2p;
00737 int i2_i, i2_n;
00738 int prev= -1;
00739 int i;
00740 int first= -3;
00741 int second= -3;
00742 int rangeCount=1;
00743 int actualSize= 0;
00744
00745 qh_setcheck(set, name, 0);
00746 if(set){
00747 SETreturnsize_(set, actualSize);
00748 if(*qh_setendpointer(set)!=NULL){
00749 qh_fprintf(stderr, 6344, "%s: qh_setendpointer(), 0x%x, is not NULL terminator of set 0x%x", name, qh_setendpointer(set), set);
00750 error_count++;
00751 }
00752 }
00753 if(actualSize!=qh_setsize(set)){
00754 qh_fprintf(stderr, 6305, "%s: SETreturnsize_() returned %d while qh_setsize() returns %d\n", name, actualSize, qh_setsize(set));
00755 error_count++;
00756 }else if(actualSize!=count){
00757 qh_fprintf(stderr, 6306, "%s: Expecting %d elements for set. Got %d elements\n", name, count, actualSize);
00758 error_count++;
00759 }
00760 if(SETempty_(set)){
00761 if(count!=0){
00762 qh_fprintf(stderr, 6307, "%s: Got empty set instead of count %d, rangeA %d, rangeB %d, rangeC %d\n", name, count, rangeA, rangeB, rangeC);
00763 error_count++;
00764 }
00765 }else{
00766
00767 i2T **p= SETaddr_(set, i2T);
00768 if(*p!=SETfirstt_(set, i2T)){
00769 qh_fprintf(stderr, 6309, "%s: SETaddr_(set, i2t) [%p] is not the same as SETfirst_(set) [%p]\n", name, SETaddr_(set, i2T), SETfirst_(set));
00770 error_count++;
00771 }
00772 first= *(int *)SETfirst_(set);
00773 if(SETfirst_(set)!=SETfirstt_(set, i2T)){
00774 qh_fprintf(stderr, 6308, "%s: SETfirst_(set) [%p] is not the same as SETfirstt_(set, i2T [%p]\n", name, SETfirst_(set), SETfirstt_(set, i2T));
00775 error_count++;
00776 }
00777 if(qh_setsize(set)>1){
00778 second= *(int *)SETsecond_(set);
00779 if(SETsecond_(set)!=SETsecondt_(set, i2T)){
00780 qh_fprintf(stderr, 6310, "%s: SETsecond_(set) [%p] is not the same as SETsecondt_(set, i2T) [%p]\n", name, SETsecond_(set), SETsecondt_(set, i2T));
00781 error_count++;
00782 }
00783 }
00784 }
00785
00786 i= 0;
00787 FOREACHint_(set){
00788 if(i2!=SETfirst_(set) && *i2!=prev+1){
00789 break;
00790 }
00791 prev= *i2;
00792 if(SETindex_(set, i2)!=i){
00793 qh_fprintf(stderr, 6311, "%s: Expecting SETIndex_(set, pointer-to-%d) to be %d. Got %d\n", name, *i2, i, SETindex_(set, i2));
00794 error_count++;;
00795 }
00796 if(i2!=SETref_(i2)){
00797 qh_fprintf(stderr, 6312, "%s: SETref_(i2) [%p] does not point to i2 (the %d'th element)\n", name, SETref_(i2), i);
00798 error_count++;;
00799 }
00800 i++;
00801 }
00802 FOREACHint_i_(set){
00803
00804 i2T **p= SETelemaddr_(set, i2_i, i2T);
00805 if(i2!=*p){
00806 qh_fprintf(stderr, 6320, "%s: SETelemaddr_(set, %d, i2T) [%p] does not point to i2\n", name, i2_i, SETelemaddr_(set, i2_i, int));
00807 error_count++;;
00808 }
00809 if(i2_i==0){
00810 if(first!=*i2){
00811 qh_fprintf(stderr, 6314, "%s: First element is %d instead of SETfirst %d\n", name, *i2, first);
00812 error_count++;;
00813 }
00814 if(rangeA!=*i2){
00815 qh_fprintf(stderr, 6315, "%s: starts with %d instead of rangeA %d\n", name, *i2, rangeA);
00816 error_count++;;
00817 }
00818 prev= rangeA;
00819 }else{
00820 if(i2_i==1 && second!=*i2){
00821 qh_fprintf(stderr, 6316, "%s: Second element is %d instead of SETsecond %d\n", name, *i2, second);
00822 error_count++;;
00823 }
00824 if(prev+1==*i2){
00825 prev++;
00826 }else{
00827 if(*i2==rangeB){
00828 prev= rangeB;
00829 rangeB= -1;
00830 rangeCount++;
00831 }else if(rangeB==-1 && *i2==rangeC){
00832 prev= rangeC;
00833 rangeC= -1;
00834 rangeCount++;
00835 }else{
00836 prev++;
00837 qh_fprintf(stderr, 6317, "%s: Expecting %d'th element to be %d. Got %d\n", name, i2_i, prev, *i2);
00838 error_count++;
00839 }
00840 }
00841 }
00842 if(i2!=SETelem_(set, i2_i)){
00843 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);
00844 error_count++;;
00845 }
00846 if(SETelemt_(set, i2_i, i2T)!=SETelem_(set, i2_i)){
00847 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);
00848 error_count++;;
00849 }
00850 }
00851 if(error_count>=MAXerrorCount){
00852 qh_fprintf(stderr, 8011, "testqset: Stop testing after %d errors\n", error_count);
00853 exit(1);
00854 }
00855 }
00856