Actual source code: ex7.c
1: static char help[] = "Tests ISLocate().\n\n";
3: #include <petscis.h>
5: static PetscErrorCode TestGeneral(void)
6: {
7: MPI_Comm comm = PETSC_COMM_SELF;
8: const PetscInt idx[] = {8, 6, 7, -5, 3, 0, 9};
9: PetscInt n = 7, key = 3, nonkey = 1, keylocation = 4, sortedlocation = 2, location;
10: IS is;
12: PetscFunctionBegin;
13: PetscCall(ISCreateGeneral(comm, n, idx, PETSC_COPY_VALUES, &is));
14: PetscCall(ISLocate(is, key, &location));
15: PetscCheck(location == keylocation, comm, PETSC_ERR_PLIB, "Key %" PetscInt_FMT " not at %" PetscInt_FMT ": %" PetscInt_FMT, key, keylocation, location);
16: PetscCall(ISLocate(is, nonkey, &location));
17: PetscCheck(location < 0, comm, PETSC_ERR_PLIB, "Nonkey %" PetscInt_FMT " found at %" PetscInt_FMT, nonkey, location);
18: PetscCall(ISSort(is));
19: PetscCall(ISLocate(is, key, &location));
20: PetscCheck(location == sortedlocation, comm, PETSC_ERR_PLIB, "Key %" PetscInt_FMT " not at %" PetscInt_FMT ": %" PetscInt_FMT, key, sortedlocation, location);
21: PetscCall(ISLocate(is, nonkey, &location));
22: PetscCheck(location < 0, comm, PETSC_ERR_PLIB, "Nonkey %" PetscInt_FMT " found at %" PetscInt_FMT, nonkey, location);
23: PetscCall(ISDestroy(&is));
24: PetscFunctionReturn(PETSC_SUCCESS);
25: }
27: static PetscErrorCode TestBlock(void)
28: {
29: MPI_Comm comm = PETSC_COMM_SELF;
30: const PetscInt idx[] = {
31: 8, 6, 7, -5, 3, 0, 9,
32: };
33: PetscInt bs = 5, n = 7, key = 16, nonkey = 7, keylocation = 21, sortedlocation = 11, location;
34: IS is;
36: PetscFunctionBegin;
37: PetscCall(ISCreateBlock(comm, bs, n, idx, PETSC_COPY_VALUES, &is));
38: PetscCall(ISLocate(is, key, &location));
39: PetscCheck(location == keylocation, comm, PETSC_ERR_PLIB, "Key %" PetscInt_FMT " not at %" PetscInt_FMT ": %" PetscInt_FMT, key, keylocation, location);
40: PetscCall(ISLocate(is, nonkey, &location));
41: PetscCheck(location < 0, comm, PETSC_ERR_PLIB, "Nonkey %" PetscInt_FMT " found at %" PetscInt_FMT, nonkey, location);
42: PetscCall(ISSort(is));
43: PetscCall(ISLocate(is, key, &location));
44: PetscCheck(location == sortedlocation, comm, PETSC_ERR_PLIB, "Key %" PetscInt_FMT " not at %" PetscInt_FMT ": %" PetscInt_FMT, key, sortedlocation, location);
45: PetscCall(ISLocate(is, nonkey, &location));
46: PetscCheck(location < 0, comm, PETSC_ERR_PLIB, "Nonkey %" PetscInt_FMT " found at %" PetscInt_FMT, nonkey, location);
47: PetscCall(ISDestroy(&is));
48: PetscFunctionReturn(PETSC_SUCCESS);
49: }
51: static PetscErrorCode TestStride(void)
52: {
53: MPI_Comm comm = PETSC_COMM_SELF;
54: PetscInt stride = 7, first = -3, n = 18, key = 39, keylocation = 6;
55: PetscInt nonkey[] = {-2, 123}, i, location;
56: IS is;
58: PetscFunctionBegin;
59: PetscCall(ISCreateStride(comm, n, first, stride, &is));
60: PetscCall(ISLocate(is, key, &location));
61: PetscCheck(location == keylocation, comm, PETSC_ERR_PLIB, "Key %" PetscInt_FMT " not at %" PetscInt_FMT ": %" PetscInt_FMT, key, keylocation, location);
62: for (i = 0; i < 2; i++) {
63: PetscCall(ISLocate(is, nonkey[i], &location));
64: PetscCheck(location < 0, comm, PETSC_ERR_PLIB, "Nonkey %" PetscInt_FMT " found at %" PetscInt_FMT, nonkey[i], location);
65: }
66: PetscCall(ISDestroy(&is));
67: PetscFunctionReturn(PETSC_SUCCESS);
68: }
70: int main(int argc, char **argv)
71: {
72: PetscFunctionBeginUser;
73: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
74: PetscCall(TestGeneral());
75: PetscCall(TestBlock());
76: PetscCall(TestStride());
77: PetscCall(PetscFinalize());
78: return 0;
79: }
81: /*TEST
83: test:
84: output_file: output/ex1_1.out
86: TEST*/