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