Actual source code: ex40.c

  1: static char help[] = "Test PETSc integer hash map.\n\n";

  3: #include <petsc/private/hashmapi.h>
  4: #include <petsc/private/hashmapiv.h>

  6: /* Unused, keep it for testing purposes */
  7: PETSC_HASH_MAP(HMapIP, PetscInt, void *, PetscHashInt, PetscHashEqual, NULL)

  9: /* Unused, keep it for testing purposes */
 10: typedef struct {
 11:   double x;
 12:   double y;
 13:   double z;
 14: } Point;
 15: static Point origin = {0.0, 0.0, 0.0};
 16: PETSC_HASH_MAP(HMapIS, PetscInt, Point, PetscHashInt, PetscHashEqual, origin)

 18: #define PetscTestCheck(expr) PetscCheck(expr, PETSC_COMM_SELF, PETSC_ERR_LIB, "Assertion: `%s' failed.", PetscStringize(expr))

 20: int main(int argc, char **argv)
 21: {
 22:   PetscHMapI   ht = NULL, hd;
 23:   PetscHMapIV  htv;
 24:   PetscInt     n, v, koff, keys[4], voff, vals[4], na, nb, i, size, *karray, off;
 25:   PetscScalar *varray, *vwork;
 26:   PetscBool    has, flag;

 28:   PetscFunctionBeginUser;
 29:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

 31:   PetscCall(PetscHMapICreate(&ht));
 32:   PetscTestCheck(ht != NULL);
 33:   PetscCall(PetscHMapIGetSize(ht, &n));
 34:   PetscTestCheck(n == 0);

 36:   PetscCall(PetscHMapIResize(ht, 0));
 37:   PetscCall(PetscHMapIGetSize(ht, &n));
 38:   PetscTestCheck(n == 0);

 40:   PetscCall(PetscHMapIHas(ht, 123, &has));
 41:   PetscTestCheck(has == PETSC_FALSE);
 42:   PetscCall(PetscHMapIGet(ht, 123, &v));
 43:   PetscTestCheck(v == -1);

 45:   PetscCall(PetscHMapISet(ht, 123, 42));
 46:   PetscCall(PetscHMapIGetSize(ht, &n));
 47:   PetscTestCheck(n == 1);
 48:   PetscCall(PetscHMapIHas(ht, 123, &has));
 49:   PetscTestCheck(has == PETSC_TRUE);
 50:   PetscCall(PetscHMapIGet(ht, 123, &v));
 51:   PetscTestCheck(v == 42);

 53:   PetscCall(PetscHMapIDel(ht, 123));
 54:   PetscCall(PetscHMapIGetSize(ht, &n));
 55:   PetscTestCheck(n == 0);
 56:   PetscCall(PetscHMapIHas(ht, 123, &has));
 57:   PetscTestCheck(has == PETSC_FALSE);
 58:   PetscCall(PetscHMapIGet(ht, 123, &v));
 59:   PetscTestCheck(v == -1);

 61:   PetscCall(PetscHMapIQuerySet(ht, 123, 1, &flag));
 62:   PetscTestCheck(flag == PETSC_TRUE);
 63:   PetscCall(PetscHMapIQuerySet(ht, 123, 1, &flag));
 64:   PetscTestCheck(flag == PETSC_FALSE);
 65:   PetscCall(PetscHMapIQueryDel(ht, 123, &flag));
 66:   PetscTestCheck(flag == PETSC_TRUE);
 67:   PetscCall(PetscHMapIQueryDel(ht, 123, &flag));
 68:   PetscTestCheck(flag == PETSC_FALSE);

 70:   PetscCall(PetscHMapIResize(ht, 13));
 71:   PetscCall(PetscHMapIGetSize(ht, &n));
 72:   PetscTestCheck(n == 0);

 74:   PetscCall(PetscHMapIClear(ht));
 75:   PetscCall(PetscHMapIGetSize(ht, &n));
 76:   PetscTestCheck(n == 0);

 78:   PetscCall(PetscHMapISet(ht, 321, 24));
 79:   PetscCall(PetscHMapISet(ht, 123, 42));
 80:   PetscCall(PetscHMapIGetSize(ht, &n));
 81:   PetscTestCheck(n == 2);

 83:   koff    = 0;
 84:   keys[0] = keys[1] = 0;
 85:   PetscCall(PetscHMapIGetKeys(ht, &koff, keys));
 86:   PetscCall(PetscSortInt(koff, keys));
 87:   PetscTestCheck(koff == 2);
 88:   PetscTestCheck(keys[0] == 123);
 89:   PetscTestCheck(keys[1] == 321);

 91:   voff    = 0;
 92:   vals[0] = vals[1] = 0;
 93:   PetscCall(PetscHMapIGetVals(ht, &voff, vals));
 94:   PetscCall(PetscSortInt(voff, vals));
 95:   PetscTestCheck(voff == 2);
 96:   PetscTestCheck(vals[0] == 24);
 97:   PetscTestCheck(vals[1] == 42);

 99:   koff    = 0;
100:   keys[0] = keys[1] = 0;
101:   voff              = 0;
102:   vals[0] = vals[1] = 0;
103:   PetscCall(PetscHMapIDuplicate(ht, &hd));
104:   PetscCall(PetscHMapIGetKeys(ht, &koff, keys));
105:   PetscCall(PetscHMapIGetVals(ht, &voff, vals));
106:   PetscCall(PetscSortInt(koff, keys));
107:   PetscCall(PetscSortInt(voff, vals));
108:   PetscTestCheck(koff == 2);
109:   PetscTestCheck(voff == 2);
110:   PetscTestCheck(keys[0] == 123);
111:   PetscTestCheck(keys[1] == 321);
112:   PetscTestCheck(vals[0] == 24);
113:   PetscTestCheck(vals[1] == 42);
114:   PetscCall(PetscHMapIDestroy(&hd));

116:   PetscCall(PetscHMapISet(ht, 0, 0));
117:   PetscCall(PetscHMapIGetSize(ht, &n));
118:   PetscTestCheck(n != 0);
119:   PetscCall(PetscHMapIReset(ht));
120:   PetscCall(PetscHMapIGetSize(ht, &n));
121:   PetscTestCheck(n == 0);
122:   PetscCall(PetscHMapIReset(ht));
123:   PetscCall(PetscHMapIGetSize(ht, &n));
124:   PetscTestCheck(n == 0);
125:   PetscCall(PetscHMapISet(ht, 0, 0));
126:   PetscCall(PetscHMapIGetSize(ht, &n));
127:   PetscTestCheck(n != 0);

129:   PetscCall(PetscHMapIDestroy(&ht));
130:   PetscTestCheck(ht == NULL);

132:   PetscCall(PetscHMapICreate(&ht));
133:   PetscCall(PetscHMapIReset(ht));
134:   PetscCall(PetscHMapIGetSize(ht, &n));
135:   PetscTestCheck(n == 0);
136:   PetscCall(PetscHMapIDestroy(&ht));

138:   PetscCall(PetscHMapIVCreate(&htv));
139:   n = 10;
140:   PetscCall(PetscHMapIVResize(htv, n));
141:   PetscCall(PetscHMapIVGetCapacity(htv, &na));
142:   PetscTestCheck(na >= n);
143:   for (i = 0; i < n; i++) PetscCall(PetscHMapIVSet(htv, i + 100, 10.));

145:   PetscCall(PetscHMapIVGetCapacity(htv, &nb));
146:   PetscTestCheck(nb >= na);
147:   for (i = 0; i < (2 * n); i++) PetscCall(PetscHMapIVAddValue(htv, i + 100, 5.));

149:   PetscCall(PetscHMapIVGetSize(htv, &size));
150:   PetscTestCheck(size == (2 * n));
151:   PetscCall(PetscMalloc3(size, &karray, size, &varray, size, &vwork));
152:   off = 0;
153:   PetscCall(PetscHMapIVGetPairs(htv, &off, karray, varray));
154:   PetscTestCheck(off == (2 * n));
155:   PetscCall(PetscSortIntWithDataArray(off, karray, varray, sizeof(PetscScalar), vwork));
156:   for (i = 0; i < n; i++) {
157:     PetscTestCheck(karray[i] == (i + 100));
158:     PetscTestCheck(karray[n + i] == (n + i + 100));
159:     PetscTestCheck(varray[i] == 15.);
160:     PetscTestCheck(varray[n + i] == 5.);
161:   }
162:   PetscCall(PetscFree3(karray, varray, vwork));
163:   PetscCall(PetscHMapIVDestroy(&htv));

165:   PetscCall(PetscFinalize());
166:   return 0;
167: }

169: /*TEST

171:    test:
172:      output_file: output/empty.out

174: TEST*/