Actual source code: ex6.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1: static char help[] = "Tests options database";

  3:  #include <petscsys.h>

  5: #define PetscAssert(expr) do {            \
  6: if (PetscUnlikely(!(expr)))               \
  7:   SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB, \
  8:            "Assertion: `%s' failed.",     \
  9:            PetscStringize(expr));         \
 10: } while(0)

 12: int main(int argc,char **argv)
 13: {
 14:   const char     *val;
 15:   PetscBool      has;

 18:   PetscOptionsSetValue(NULL,"-skip_petscrc", NULL);if (ierr) return ierr;
 19:   PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;

 21:   PetscOptionsHasName(NULL,NULL,"-abc",&has);
 22:   PetscAssert(has == PETSC_FALSE);
 23:   PetscOptionsHasName(NULL,"","-abc",&has);
 24:   PetscAssert(has == PETSC_FALSE);
 25:   PetscOptionsHasName(NULL,"a","-bc",&has);
 26:   PetscAssert(has == PETSC_FALSE);
 27:   PetscOptionsHasName(NULL,"ab","-c",&has);
 28:   PetscAssert(has == PETSC_FALSE);
 29:   PetscOptionsHasName(NULL,"abc","-",&has);
 30:   PetscAssert(has == PETSC_FALSE);

 32:   PetscOptionsSetValue(NULL,"-abc",NULL);
 33:   PetscOptionsHasName(NULL,NULL,"-abc",&has);
 34:   PetscAssert(has == PETSC_TRUE);
 35:   PetscOptionsHasName(NULL,"","-abc",&has);
 36:   PetscAssert(has == PETSC_TRUE);
 37:   PetscOptionsHasName(NULL,"a","-bc",&has);
 38:   PetscAssert(has == PETSC_TRUE);
 39:   PetscOptionsHasName(NULL,"ab","-c",&has);
 40:   PetscAssert(has == PETSC_TRUE);
 41:   PetscOptionsHasName(NULL,"abc","-",&has);
 42:   PetscAssert(has == PETSC_TRUE);
 43:   PetscOptionsClearValue(NULL,"-abc");
 44:   PetscOptionsClearValue(NULL,"-ABC");

 46:   PetscOptionsPrefixPush(NULL,"a");
 47:   PetscOptionsSetValue(NULL,"-x",NULL);
 48:   PetscOptionsHasName(NULL,NULL,"-ax",&has);
 49:   PetscAssert(has == PETSC_TRUE);
 50:   PetscOptionsPrefixPush(NULL,"b");
 51:   PetscOptionsSetValue(NULL,"-xy",NULL);
 52:   PetscOptionsHasName(NULL,NULL,"-abxy",&has);
 53:   PetscAssert(has == PETSC_TRUE);
 54:   PetscOptionsPrefixPop(NULL);
 55:   PetscOptionsPrefixPush(NULL,"c");
 56:   PetscOptionsSetValue(NULL,"-xz",NULL);
 57:   PetscOptionsHasName(NULL,NULL,"-acxz",&has);
 58:   PetscAssert(has == PETSC_TRUE);
 59:   PetscOptionsPrefixPop(NULL);
 60:   PetscOptionsPrefixPop(NULL);
 61:   PetscOptionsClearValue(NULL,"-ax");
 62:   PetscOptionsClearValue(NULL,"-abxy");
 63:   PetscOptionsClearValue(NULL,"-acxz");

 65:   PetscOptionsSetValue(NULL,"-FOO",NULL);
 66:   PetscOptionsSetValue(NULL,"-FOO","BAR");
 67:   PetscOptionsSetValue(NULL,"-FOO",NULL);
 68:   PetscOptionsClearValue(NULL,"-FOO");
 69:   PetscOptionsSetValue(NULL,"-FOO","BAR");
 70:   PetscOptionsSetValue(NULL,"-FOO",NULL);
 71:   PetscOptionsSetValue(NULL,"-FOO","BAR");
 72:   PetscOptionsClearValue(NULL,"-FOO");

 74:   {
 75:     char name[] = "-*_42", c;
 76:     for (c = 'a'; c <= 'z'; c++) {
 77:       name[1] = c;
 78:       PetscOptionsHasName(NULL,NULL,name,&has);
 79:       PetscAssert(has == PETSC_FALSE);
 80:     }
 81:     for (c = 'a'; c <= 'z'; c++) {
 82:       name[1] = c;
 83:       PetscOptionsHasName(NULL,NULL,name,&has);
 84:       PetscAssert(has == PETSC_FALSE);
 85:       PetscOptionsSetValue(NULL,name,NULL);
 86:       PetscOptionsHasName(NULL,NULL,name,&has);
 87:       PetscAssert(has == PETSC_TRUE);
 88:     }
 89:     for (c = 'A'; c <= 'Z'; c++) {
 90:       name[1] = c;
 91:       PetscOptionsHasName(NULL,NULL,name,&has);
 92:       PetscAssert(has == PETSC_TRUE);
 93:       PetscOptionsClearValue(NULL,name);
 94:       PetscOptionsHasName(NULL,NULL,name,&has);
 95:       PetscAssert(has == PETSC_FALSE);
 96:     }
 97:     for (c = 'Z'; c >= 'A'; c--) {
 98:       name[1] = c;
 99:       PetscOptionsHasName(NULL,NULL,name,&has);
100:       PetscAssert(has == PETSC_FALSE);
101:       PetscOptionsSetValue(NULL,name,NULL);
102:       PetscOptionsHasName(NULL,NULL,name,&has);
103:       PetscAssert(has == PETSC_TRUE);
104:     }
105:     for (c = 'a'; c <= 'z'; c++) {
106:       name[1] = c;
107:       PetscOptionsHasName(NULL,NULL,name,&has);
108:       PetscAssert(has == PETSC_TRUE);
109:       PetscOptionsClearValue(NULL,name);
110:       PetscOptionsHasName(NULL,NULL,name,&has);
111:       PetscAssert(has == PETSC_FALSE);
112:     }
113:     for (c = 'a'; c <= 'z'; c++) {
114:       name[1] = c;
115:       PetscOptionsHasName(NULL,NULL,name,&has);
116:       PetscAssert(has == PETSC_FALSE);
117:     }
118:   }

120:   PetscOptionsSetValue(NULL,"-abc_xyz","123");
121:   PetscOptionsFindPair(NULL,NULL,"-abc_xyz",&val,&has);
122:   PetscAssert(has == PETSC_TRUE && !strcmp(val,"123"));
123:   PetscOptionsFindPair(NULL,NULL,"-abc_42_xyz",&val,&has);
124:   PetscAssert(has == PETSC_TRUE && !strcmp(val,"123"));
125:   PetscOptionsFindPair(NULL,NULL,"-abc_42_1_xyz",&val,&has);
126:   PetscAssert(has == PETSC_TRUE && !strcmp(val,"123"));
127:   PetscOptionsFindPair(NULL,NULL,"-abc_42_1_23_xyz",&val,&has);
128:   PetscAssert(has == PETSC_TRUE && !strcmp(val,"123"));
129:   PetscOptionsFindPair(NULL,NULL,"-abc_42_1_23_456_xyz",&val,&has);
130:   PetscAssert(has == PETSC_TRUE && !strcmp(val,"123"));
131:   PetscOptionsFindPair(NULL,NULL,"-abc_42_1_23_456_789_xyz",&val,&has);
132:   PetscAssert(has == PETSC_TRUE && !strcmp(val,"123"));
133:   PetscOptionsFindPair(NULL,NULL,"-abc_xyz_42",&val,&has);
134:   PetscAssert(has == PETSC_FALSE);
135:   PetscOptionsFindPair(NULL,NULL,"-abc42xyz",&val,&has);
136:   PetscAssert(has == PETSC_FALSE);
137:   PetscOptionsFindPair(NULL,NULL,"-abc42_xyz",&val,&has);
138:   PetscAssert(has == PETSC_FALSE);
139:   PetscOptionsFindPair(NULL,NULL,"-abc_42xyz",&val,&has);
140:   PetscAssert(has == PETSC_FALSE);
141:   PetscOptionsFindPair(NULL,NULL,"-abc0_42_xyz",&val,&has);
142:   PetscAssert(has == PETSC_FALSE);
143:   PetscOptionsFindPair(NULL,NULL,"-abc_42_0xyz",&val,&has);
144:   PetscAssert(has == PETSC_FALSE);
145:   PetscOptionsClearValue(NULL,"-abc_xyz");

147:   PetscFinalize();
148:   return ierr;
149: }


152: /*TEST

154:    test:

156: TEST*/