1 static char help[] = "Test PETSc integer hash map.\n\n"; 2 3 #include <petsc/private/hashmapi.h> 4 #include <petsc/private/hashmapiv.h> 5 #include <petscsys.h> 6 7 /* Unused, keep it for testing purposes */ 8 PETSC_HASH_MAP(HMapIP, PetscInt, void*, PetscHashInt, PetscHashEqual, NULL) 9 10 /* Unused, keep it for testing purposes */ 11 typedef struct { double x; double y; double z; } Point; 12 static Point origin = {0.0, 0.0, 0.0}; 13 PETSC_HASH_MAP(HMapIS, PetscInt, Point, PetscHashInt, PetscHashEqual, origin) 14 15 #define PetscTestCheck(expr) PetscCheck(expr,PETSC_COMM_SELF,PETSC_ERR_LIB, "Assertion: `%s' failed.", PetscStringize(expr)) 16 17 int main(int argc,char **argv) 18 { 19 PetscHMapI ht = NULL, hd; 20 PetscHMapIV htv; 21 PetscInt n, v, koff, keys[4], voff, vals[4],na,nb,i,size,*karray,off; 22 PetscScalar *varray,*vwork; 23 PetscBool has, flag; 24 25 PetscFunctionBeginUser; 26 PetscCall(PetscInitialize(&argc,&argv,NULL,help)); 27 28 PetscCall(PetscHMapICreate(&ht)); 29 PetscTestCheck(ht != NULL); 30 PetscCall(PetscHMapIGetSize(ht,&n)); 31 PetscTestCheck(n == 0); 32 33 PetscCall(PetscHMapIResize(ht,0)); 34 PetscCall(PetscHMapIGetSize(ht,&n)); 35 PetscTestCheck(n == 0); 36 37 PetscCall(PetscHMapIHas(ht,123,&has)); 38 PetscTestCheck(has == PETSC_FALSE); 39 PetscCall(PetscHMapIGet(ht,123,&v)); 40 PetscTestCheck(v == -1); 41 42 PetscCall(PetscHMapISet(ht,123,42)); 43 PetscCall(PetscHMapIGetSize(ht,&n)); 44 PetscTestCheck(n == 1); 45 PetscCall(PetscHMapIHas(ht,123,&has)); 46 PetscTestCheck(has == PETSC_TRUE); 47 PetscCall(PetscHMapIGet(ht,123,&v)); 48 PetscTestCheck(v == 42); 49 50 PetscCall(PetscHMapIDel(ht,123)); 51 PetscCall(PetscHMapIGetSize(ht,&n)); 52 PetscTestCheck(n == 0); 53 PetscCall(PetscHMapIHas(ht,123,&has)); 54 PetscTestCheck(has == PETSC_FALSE); 55 PetscCall(PetscHMapIGet(ht,123,&v)); 56 PetscTestCheck(v == -1); 57 58 PetscCall(PetscHMapIQuerySet(ht,123,1,&flag)); 59 PetscTestCheck(flag == PETSC_TRUE); 60 PetscCall(PetscHMapIQuerySet(ht,123,1,&flag)); 61 PetscTestCheck(flag == PETSC_FALSE); 62 PetscCall(PetscHMapIQueryDel(ht,123,&flag)); 63 PetscTestCheck(flag == PETSC_TRUE); 64 PetscCall(PetscHMapIQueryDel(ht,123,&flag)); 65 PetscTestCheck(flag == PETSC_FALSE); 66 67 PetscCall(PetscHMapIResize(ht,13)); 68 PetscCall(PetscHMapIGetSize(ht,&n)); 69 PetscTestCheck(n == 0); 70 71 PetscCall(PetscHMapIClear(ht)); 72 PetscCall(PetscHMapIGetSize(ht,&n)); 73 PetscTestCheck(n == 0); 74 75 PetscCall(PetscHMapISet(ht,321,24)); 76 PetscCall(PetscHMapISet(ht,123,42)); 77 PetscCall(PetscHMapIGetSize(ht,&n)); 78 PetscTestCheck(n == 2); 79 80 koff = 0; keys[0] = keys[1] = 0; 81 PetscCall(PetscHMapIGetKeys(ht,&koff,keys)); 82 PetscCall(PetscSortInt(koff,keys)); 83 PetscTestCheck(koff == 2); 84 PetscTestCheck(keys[0] == 123); 85 PetscTestCheck(keys[1] == 321); 86 87 voff = 0; vals[0] = vals[1] = 0; 88 PetscCall(PetscHMapIGetVals(ht,&voff,vals)); 89 PetscCall(PetscSortInt(voff,vals)); 90 PetscTestCheck(voff == 2); 91 PetscTestCheck(vals[0] == 24); 92 PetscTestCheck(vals[1] == 42); 93 94 koff = 0; keys[0] = keys[1] = 0; 95 voff = 0; vals[0] = vals[1] = 0; 96 PetscCall(PetscHMapIDuplicate(ht,&hd)); 97 PetscCall(PetscHMapIGetKeys(ht,&koff,keys)); 98 PetscCall(PetscHMapIGetVals(ht,&voff,vals)); 99 PetscCall(PetscSortInt(koff,keys)); 100 PetscCall(PetscSortInt(voff,vals)); 101 PetscTestCheck(koff == 2); 102 PetscTestCheck(voff == 2); 103 PetscTestCheck(keys[0] == 123); 104 PetscTestCheck(keys[1] == 321); 105 PetscTestCheck(vals[0] == 24); 106 PetscTestCheck(vals[1] == 42); 107 PetscCall(PetscHMapIDestroy(&hd)); 108 109 PetscCall(PetscHMapISet(ht,0,0)); 110 PetscCall(PetscHMapIGetSize(ht,&n)); 111 PetscTestCheck(n != 0); 112 PetscCall(PetscHMapIReset(ht)); 113 PetscCall(PetscHMapIGetSize(ht,&n)); 114 PetscTestCheck(n == 0); 115 PetscCall(PetscHMapIReset(ht)); 116 PetscCall(PetscHMapIGetSize(ht,&n)); 117 PetscTestCheck(n == 0); 118 PetscCall(PetscHMapISet(ht,0,0)); 119 PetscCall(PetscHMapIGetSize(ht,&n)); 120 PetscTestCheck(n != 0); 121 122 PetscCall(PetscHMapIDestroy(&ht)); 123 PetscTestCheck(ht == NULL); 124 125 PetscCall(PetscHMapICreate(&ht)); 126 PetscCall(PetscHMapIReset(ht)); 127 PetscCall(PetscHMapIGetSize(ht,&n)); 128 PetscTestCheck(n == 0); 129 PetscCall(PetscHMapIDestroy(&ht)); 130 131 PetscCall(PetscHMapIVCreate(&htv)); 132 n = 10; 133 PetscCall(PetscHMapIVResize(htv,n)); 134 PetscCall(PetscHMapIVGetCapacity(htv,&na)); 135 PetscTestCheck(na>=n); 136 for (i=0; i<n; i++) PetscCall(PetscHMapIVSet(htv,i+100,10.)); 137 138 PetscCall(PetscHMapIVGetCapacity(htv,&nb)); 139 PetscTestCheck(nb>=na); 140 for (i=0; i<(2*n); i++) PetscCall(PetscHMapIVAddValue(htv,i+100,5.)); 141 142 PetscCall(PetscHMapIVGetSize(htv,&size)); 143 PetscTestCheck(size==(2*n)); 144 PetscCall(PetscMalloc3(size,&karray,size,&varray,size,&vwork)); 145 off = 0; 146 PetscCall(PetscHMapIVGetPairs(htv,&off,karray,varray)); 147 PetscTestCheck(off==(2*n)); 148 PetscCall(PetscSortIntWithDataArray(off,karray,varray,sizeof(PetscScalar),vwork)); 149 for (i=0; i<n; i++) { 150 PetscTestCheck(karray[i]==(i+100)); 151 PetscTestCheck(karray[n+i]==(n+i+100)); 152 PetscTestCheck(varray[i]==15.); 153 PetscTestCheck(varray[n+i]==5.); 154 } 155 PetscCall(PetscFree3(karray,varray,vwork)); 156 PetscCall(PetscHMapIVDestroy(&htv)); 157 158 PetscCall(PetscFinalize()); 159 return 0; 160 } 161 162 /*TEST 163 164 test: 165 166 TEST*/ 167