1 static char help[] = "Test PETSc integer hash set.\n\n"; 2 3 #include <petsc/private/hashseti.h> 4 #include <petscsys.h> 5 6 #define PetscTestCheck(expr) PetscCheck(expr,PETSC_COMM_SELF,PETSC_ERR_LIB,"Assertion: `%s' failed.",PetscStringize(expr)) 7 8 int main(int argc,char **argv) 9 { 10 PetscHSetI ht = NULL, hd; 11 PetscInt n, off, array[4],na,nb,i,*marray,size; 12 PetscBool has, flag; 13 14 PetscFunctionBeginUser; 15 PetscCall(PetscInitialize(&argc,&argv,NULL,help)); 16 17 PetscCall(PetscHSetICreate(&ht)); 18 PetscTestCheck(ht != NULL); 19 PetscCall(PetscHSetIGetSize(ht,&n)); 20 PetscTestCheck(n == 0); 21 22 PetscCall(PetscHSetIResize(ht,0)); 23 PetscCall(PetscHSetIGetSize(ht,&n)); 24 PetscTestCheck(n == 0); 25 26 PetscCall(PetscHSetIHas(ht,42,&has)); 27 PetscTestCheck(has == PETSC_FALSE); 28 29 PetscCall(PetscHSetIAdd(ht,42)); 30 PetscCall(PetscHSetIGetSize(ht,&n)); 31 PetscTestCheck(n == 1); 32 PetscCall(PetscHSetIHas(ht,42,&has)); 33 PetscTestCheck(has == PETSC_TRUE); 34 35 PetscCall(PetscHSetIDel(ht,42)); 36 PetscCall(PetscHSetIGetSize(ht,&n)); 37 PetscTestCheck(n == 0); 38 PetscCall(PetscHSetIHas(ht,42,&has)); 39 PetscTestCheck(has == PETSC_FALSE); 40 PetscCall(PetscHSetIDel(ht,42)); 41 PetscCall(PetscHSetIDel(ht,24)); 42 43 PetscCall(PetscHSetIQueryAdd(ht,123,&flag)); 44 PetscTestCheck(flag == PETSC_TRUE); 45 PetscCall(PetscHSetIQueryAdd(ht,123,&flag)); 46 PetscTestCheck(flag == PETSC_FALSE); 47 PetscCall(PetscHSetIQueryDel(ht,123,&flag)); 48 PetscTestCheck(flag == PETSC_TRUE); 49 PetscCall(PetscHSetIQueryDel(ht,123,&flag)); 50 PetscTestCheck(flag == PETSC_FALSE); 51 52 PetscCall(PetscHSetIResize(ht,13)); 53 PetscCall(PetscHSetIGetSize(ht,&n)); 54 PetscTestCheck(n == 0); 55 56 PetscCall(PetscHSetIClear(ht)); 57 PetscCall(PetscHSetIGetSize(ht,&n)); 58 PetscTestCheck(n == 0); 59 60 PetscCall(PetscHSetIAdd(ht,42)); 61 PetscCall(PetscHSetIAdd(ht,13)); 62 PetscCall(PetscHSetIGetSize(ht,&n)); 63 PetscTestCheck(n == 2); 64 65 off = 0; 66 PetscCall(PetscHSetIGetElems(ht,&off,array)); 67 PetscCall(PetscSortInt(off,array)); 68 PetscTestCheck(off == 2); 69 PetscTestCheck(array[0] == 13); 70 PetscTestCheck(array[1] == 42); 71 PetscCall(PetscHSetIGetElems(ht,&off,array)); 72 PetscCall(PetscSortInt(2,array+2)); 73 PetscTestCheck(off == 4); 74 PetscTestCheck(array[0] == 13); 75 PetscTestCheck(array[1] == 42); 76 PetscTestCheck(array[0] == 13); 77 PetscTestCheck(array[1] == 42); 78 79 off = 0; 80 PetscCall(PetscHSetIDuplicate(ht,&hd)); 81 PetscCall(PetscHSetIGetElems(hd,&off,array)); 82 PetscCall(PetscSortInt(off,array)); 83 PetscTestCheck(off == 2); 84 PetscTestCheck(array[0] == 13); 85 PetscTestCheck(array[1] == 42); 86 PetscCall(PetscHSetIDestroy(&hd)); 87 88 PetscCall(PetscHSetIAdd(ht,0)); 89 PetscCall(PetscHSetIGetSize(ht,&n)); 90 PetscTestCheck(n != 0); 91 PetscCall(PetscHSetIReset(ht)); 92 PetscCall(PetscHSetIGetSize(ht,&n)); 93 PetscTestCheck(n == 0); 94 PetscCall(PetscHSetIReset(ht)); 95 PetscCall(PetscHSetIGetSize(ht,&n)); 96 PetscTestCheck(n == 0); 97 PetscCall(PetscHSetIAdd(ht,0)); 98 PetscCall(PetscHSetIGetSize(ht,&n)); 99 PetscTestCheck(n != 0); 100 101 PetscCall(PetscHSetIDestroy(&ht)); 102 PetscTestCheck(ht == NULL); 103 104 PetscCall(PetscHSetICreate(&ht)); 105 PetscCall(PetscHSetIReset(ht)); 106 PetscCall(PetscHSetIGetSize(ht,&n)); 107 PetscTestCheck(n == 0); 108 PetscCall(PetscHSetIDestroy(&ht)); 109 110 PetscCall(PetscHSetICreate(&ht)); 111 PetscCall(PetscHSetICreate(&hd)); 112 n = 10; 113 PetscCall(PetscHSetIResize(ht,n)); 114 PetscCall(PetscHSetIResize(hd,n)); 115 PetscCall(PetscHSetIGetCapacity(ht,&na)); 116 PetscCall(PetscHSetIGetCapacity(hd,&nb)); 117 PetscTestCheck(na>=n); 118 PetscTestCheck(nb>=n); 119 for (i=0; i<n; i++) { 120 PetscCall(PetscHSetIAdd(ht,i+1)); 121 PetscCall(PetscHSetIAdd(hd,i+1+n)); 122 } 123 PetscCall(PetscHSetIGetCapacity(ht,&nb)); 124 PetscTestCheck(nb>=na); 125 /* Merge ht and hd, and the result is in ht */ 126 PetscCall(PetscHSetIUpdate(ht,hd)); 127 PetscCall(PetscHSetIDestroy(&hd)); 128 PetscCall(PetscHSetIGetSize(ht,&size)); 129 PetscTestCheck(size==(2*n)); 130 PetscCall(PetscMalloc1(n*2,&marray)); 131 off = 0; 132 PetscCall(PetscHSetIGetElems(ht,&off,marray)); 133 PetscCall(PetscHSetIDestroy(&ht)); 134 PetscTestCheck(off==(2*n)); 135 PetscCall(PetscSortInt(off,marray)); 136 for (i=0; i<n; i++) { 137 PetscTestCheck(marray[i]==(i+1)); 138 PetscTestCheck(marray[n+i]==(i+1+n)); 139 } 140 PetscCall(PetscFree(marray)); 141 142 PetscCall(PetscFinalize()); 143 return 0; 144 } 145 146 /*TEST 147 148 test: 149 150 TEST*/ 151