Actual source code: ex61.c
1: static const char help[] = "Tests env: directive in test harness language.\n\n";
3: #include <petscsys.h>
5: int main(int argc, char *argv[])
6: {
7: PetscBool env_set;
8: char env_vars[PETSC_MAX_PATH_LEN];
9: int num_env;
10: char **env_vars_arr;
12: PetscFunctionBeginUser;
13: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
14: PetscCall(PetscArrayzero(env_vars, PETSC_MAX_PATH_LEN));
16: PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Test Options", NULL);
17: PetscCall(PetscOptionsString("-env_vars_def", "Environment variables set", NULL, env_vars, env_vars, sizeof(env_vars), &env_set));
18: PetscOptionsEnd();
20: PetscCall(PetscStrToArray(env_vars, ' ', &num_env, &env_vars_arr));
21: for (int i = 0; i < num_env; ++i) {
22: const char *current_var = env_vars_arr[i];
23: PetscBool set, equal;
24: size_t name_len;
25: char env[PETSC_MAX_PATH_LEN];
26: char *name, *value = NULL;
28: // given FOO=bar we want to extract
29: // name = FOO
30: // value = bar
31: PetscCall(PetscStrchr(current_var, '=', &value));
32: PetscCheck(value, PETSC_COMM_SELF, PETSC_ERR_PLIB, "= not found in %s", current_var);
33: PetscCheck(value >= current_var, PETSC_COMM_SELF, PETSC_ERR_PLIB, "= not found in %s", current_var);
34: // value points to '=' so increment it first
35: ++value;
37: name_len = (size_t)(value - current_var);
38: PetscCall(PetscMalloc1(name_len, &name));
39: PetscCall(PetscStrncpy(name, env_vars_arr[i], name_len));
41: PetscCall(PetscArrayzero(env, PETSC_MAX_PATH_LEN));
42: PetscCall(PetscOptionsGetenv(PETSC_COMM_WORLD, name, env, sizeof(env), &set));
43: PetscCheck(set, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Test harness failed to set %s", name);
44: PetscCall(PetscStrcmp(value, env, &equal));
45: PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Test harness failed to set %s to the right value. Expected '%s', have '%s'", name, value, env);
46: PetscCall(PetscFree(name));
47: }
48: PetscCall(PetscStrToArrayDestroy(num_env, env_vars_arr));
50: PetscCall(PetscFinalize());
51: return 0;
52: }
54: /*TEST
56: testset:
57: args: -env_vars_def 'FOO=1 BAR=0 BAZ= BOP=1'
58: suffix: env_set
59: test:
60: env: FOO=1 BAR=0 BAZ= BOP=${FOO}
61: suffix: all_one_line
62: test:
63: env: FOO=1
64: env: BAR=0
65: env: BAZ=
66: env: BOP=${FOO}
67: suffix: all_separate_lines
69: test:
70: args: -env_vars_def 'FOO=hello'
71: env: FOO='hello'
72: suffix: env_set_quoted
74: test:
75: suffix: env_not_set
77: TEST*/