Actual source code: ex41.c

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

  3: #include <petsc/private/hashseti.h>
  4: #include <petscsys.h>


  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;

 15:   PetscInitialize(&argc, &argv, NULL, help);

 17:   PetscHSetICreate(&ht);
 18:   PetscTestCheck(ht != NULL);
 19:   PetscHSetIGetSize(ht, &n);
 20:   PetscTestCheck(n == 0);

 22:   PetscHSetIResize(ht, 0);
 23:   PetscHSetIGetSize(ht, &n);
 24:   PetscTestCheck(n == 0);

 26:   PetscHSetIHas(ht, 42, &has);
 27:   PetscTestCheck(has == PETSC_FALSE);

 29:   PetscHSetIAdd(ht, 42);
 30:   PetscHSetIGetSize(ht, &n);
 31:   PetscTestCheck(n == 1);
 32:   PetscHSetIHas(ht, 42, &has);
 33:   PetscTestCheck(has == PETSC_TRUE);

 35:   PetscHSetIDel(ht, 42);
 36:   PetscHSetIGetSize(ht, &n);
 37:   PetscTestCheck(n == 0);
 38:   PetscHSetIHas(ht, 42, &has);
 39:   PetscTestCheck(has == PETSC_FALSE);
 40:   PetscHSetIDel(ht, 42);
 41:   PetscHSetIDel(ht, 24);

 43:   PetscHSetIQueryAdd(ht, 123, &flag);
 44:   PetscTestCheck(flag == PETSC_TRUE);
 45:   PetscHSetIQueryAdd(ht, 123, &flag);
 46:   PetscTestCheck(flag == PETSC_FALSE);
 47:   PetscHSetIQueryDel(ht, 123, &flag);
 48:   PetscTestCheck(flag == PETSC_TRUE);
 49:   PetscHSetIQueryDel(ht, 123, &flag);
 50:   PetscTestCheck(flag == PETSC_FALSE);

 52:   PetscHSetIResize(ht, 13);
 53:   PetscHSetIGetSize(ht, &n);
 54:   PetscTestCheck(n == 0);

 56:   PetscHSetIClear(ht);
 57:   PetscHSetIGetSize(ht, &n);
 58:   PetscTestCheck(n == 0);

 60:   PetscHSetIAdd(ht, 42);
 61:   PetscHSetIAdd(ht, 13);
 62:   PetscHSetIGetSize(ht, &n);
 63:   PetscTestCheck(n == 2);

 65:   off = 0;
 66:   PetscHSetIGetElems(ht, &off, array);
 67:   PetscSortInt(off, array);
 68:   PetscTestCheck(off == 2);
 69:   PetscTestCheck(array[0] == 13);
 70:   PetscTestCheck(array[1] == 42);
 71:   PetscHSetIGetElems(ht, &off, array);
 72:   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);

 79:   off = 0;
 80:   PetscHSetIDuplicate(ht, &hd);
 81:   PetscHSetIGetElems(hd, &off, array);
 82:   PetscSortInt(off, array);
 83:   PetscTestCheck(off == 2);
 84:   PetscTestCheck(array[0] == 13);
 85:   PetscTestCheck(array[1] == 42);
 86:   PetscHSetIDestroy(&hd);

 88:   PetscHSetIAdd(ht, 0);
 89:   PetscHSetIGetSize(ht, &n);
 90:   PetscTestCheck(n != 0);
 91:   PetscHSetIReset(ht);
 92:   PetscHSetIGetSize(ht, &n);
 93:   PetscTestCheck(n == 0);
 94:   PetscHSetIReset(ht);
 95:   PetscHSetIGetSize(ht, &n);
 96:   PetscTestCheck(n == 0);
 97:   PetscHSetIAdd(ht, 0);
 98:   PetscHSetIGetSize(ht, &n);
 99:   PetscTestCheck(n != 0);

101:   PetscHSetIDestroy(&ht);
102:   PetscTestCheck(ht == NULL);

104:   PetscHSetICreate(&ht);
105:   PetscHSetIReset(ht);
106:   PetscHSetIGetSize(ht, &n);
107:   PetscTestCheck(n == 0);
108:   PetscHSetIDestroy(&ht);

110:   PetscHSetICreate(&ht);
111:   PetscHSetICreate(&hd);
112:   n = 10;
113:   PetscHSetIResize(ht, n);
114:   PetscHSetIResize(hd, n);
115:   PetscHSetIGetCapacity(ht, &na);
116:   PetscHSetIGetCapacity(hd, &nb);
117:   PetscTestCheck(na >= n);
118:   PetscTestCheck(nb >= n);
119:   for (i = 0; i < n; i++) {
120:     PetscHSetIAdd(ht, i + 1);
121:     PetscHSetIAdd(hd, i + 1 + n);
122:   }
123:   PetscHSetIGetCapacity(ht, &nb);
124:   PetscTestCheck(nb >= na);
125:   /* Merge ht and hd, and the result is in ht */
126:   PetscHSetIUpdate(ht, hd);
127:   PetscHSetIDestroy(&hd);
128:   PetscHSetIGetSize(ht, &size);
129:   PetscTestCheck(size == (2 * n));
130:   PetscMalloc1(n * 2, &marray);
131:   off = 0;
132:   PetscHSetIGetElems(ht, &off, marray);
133:   PetscHSetIDestroy(&ht);
134:   PetscTestCheck(off == (2 * n));
135:   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:   PetscFree(marray);

142:   PetscFinalize();
143:   return 0;
144: }

146: /*TEST

148:    test:

150: TEST*/