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