File: pmix_environ.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (225 lines) | stat: -rw-r--r-- 7,306 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (c) 2022      Amazon.com, Inc. or its affiliates.
 *                         All Rights reserved.
 * Copyright (c) 2022      Nanook Consulting  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 *
 */

#include "src/include/pmix_config.h"

#include <stdlib.h>
#include <stdio.h>

#include "src/include/pmix_globals.h"
#include "src/util/pmix_environ.h"
#include "src/util/pmix_argv.h"

int main(int argc, char *argv[])
{
    char **test_array;
    char **new_array;
    const char *tmp;
    pmix_status_t ret;
    int ret_int;
    PMIX_HIDE_UNUSED_PARAMS(argc, argv);

    /*
     * mess with environ
     */

    /* first test: does overwrite work? */
    ret = PMIx_Setenv("pmix_environ_test_string_3", "three", true, &environ);
    if (ret != PMIX_SUCCESS) {
        printf("PMIx_Setenv returned %d\n", ret);
        return 1;
    }
    ret = PMIx_Setenv("pmix_environ_test_string_3", "four", false, &environ);
    if (ret != PMIX_SUCCESS) {
        printf("PMIx_Setenv returned %d\n", ret);
        return 1;
    }
    if (strcmp(getenv("pmix_environ_test_string_3"), "three") != 0) {
        printf("PMIx_Setenv overwrote.  Found %s expected %s\n",
               getenv("pmix_environ_test_string_3"), "three");
        return 1;
    }
    ret = PMIx_Setenv("pmix_environ_test_string_3", "four", true, &environ);
    if (ret != PMIX_SUCCESS) {
        printf("PMIx_Setenv returned %d\n", ret);
        return 1;
    }
    tmp = getenv("pmix_environ_test_string_3");
    if (NULL == tmp) {
        printf("could not getenv(\"pmix_environ_test_string_3\")\n");
        return 1;
    }
    if (strcmp(tmp, "four") != 0) {
        printf("PMIx_Setenv overwrote.  Found %s expected %s\n",
               tmp, "four");
        return 1;
    }

    /* second test: do we handle environ properly in unsetenv */
    ret_int = putenv("pmix_environ_test_string_1=one");
    if (ret_int != 0) {
        printf("putenv() returned %d\n", ret_int);
        return 1;
    }
    ret_int = setenv("pmix_environ_test_string_2", "two", true);
    if (ret_int != 0) {
        printf("setenv() returned %d\n", ret_int);
        return 1;
    }
    ret = PMIx_Setenv("pmix_environ_test_string_3", "three", true, &environ);
    if (ret != PMIX_SUCCESS) {
        printf("PMIx_Setenv returned %d\n", ret);
        return 1;
    }
    ret = pmix_unsetenv("pmix_environ_test_string_1", &environ);
    if (ret != PMIX_SUCCESS) {
        printf("pmix_unsetenv returned %d\n", ret);
        return 1;
    }
    ret = pmix_unsetenv("pmix_environ_test_string_2", &environ);
    if (ret != PMIX_SUCCESS) {
        printf("pmix_unsetenv returned %d\n", ret);
        return 1;
    }
    ret = pmix_unsetenv("pmix_environ_test_string_3", &environ);
    if (ret != PMIX_SUCCESS) {
        printf("pmix_unsetenv returned %d\n", ret);
        return 1;
    }

    /* third test: can we merge environ into a new env.  Note that the
     * new env is empty, so we can compare environ and newenv later
     * and they should match.  We'll test the actual merging later
     * without environ as the additions, so we have more control over
     * the situation */
    ret_int = putenv("pmix_environ_test_string_1=one");
    if (ret_int != 0) {
        printf("putenv() returned %d\n", ret_int);
        return 1;
    }
    ret_int = setenv("pmix_environ_test_string_2", "two", true);
    if (ret_int != 0) {
        printf("setenv() returned %d\n", ret_int);
        return 1;
    }
    ret = PMIx_Setenv("pmix_environ_test_string_3", "three", true, &environ);
    if (ret != PMIX_SUCCESS) {
        printf("PMIx_Setenv returned %d\n", ret);
        return 1;
    }
    test_array = NULL;
    ret = pmix_environ_merge_inplace(&test_array, environ);
    if (ret != PMIX_SUCCESS) {
        printf("pmix_environ_merge returned %d\n", ret);
        return 1;
    }
    PMIx_Argv_free(test_array);

    /* fourth test: make sure environ_merge_inplace does. */
    test_array = NULL;
    PMIx_Argv_append_nosize(&test_array, "pmix_environ_test_string_1=old_one");
    PMIx_Argv_append_nosize(&test_array, "pmix_environ_test_string_2=old_two");
    PMIx_Argv_append_nosize(&test_array, "pmix_environ_test_string_3=old_three");
    PMIx_Argv_append_nosize(&test_array, "pmix_environ_test_string_4=old_four");

    new_array = NULL;
    PMIx_Argv_append_nosize(&new_array, "pmix_environ_test_string_1=new_one");
    PMIx_Argv_append_nosize(&new_array, "pmix_environ_test_string_5=new_five");

    ret = pmix_environ_merge_inplace(&test_array, new_array);
    if (ret != PMIX_SUCCESS) {
        printf("pmix_environ_merge returned %d\n", ret);
        return 1;
    }

    if (NULL == pmix_getenv("pmix_environ_test_string_1", test_array)) {
        printf("new env missing string 1\n");
        return 1;
    }
    if (NULL == pmix_getenv("pmix_environ_test_string_2", test_array)) {
        printf("new env missing string 2\n");
        return 1;
    }
    if (NULL == pmix_getenv("pmix_environ_test_string_3", test_array)) {
        printf("new env missing string 3\n");
        return 1;
    }
    if (NULL == pmix_getenv("pmix_environ_test_string_4", test_array)) {
        printf("new env missing string 4\n");
        return 1;
    }
    if (NULL == pmix_getenv("pmix_environ_test_string_5", test_array)) {
        printf("new env missing string 5\n");
        return 1;
    }

    tmp = pmix_getenv("pmix_environ_test_string_1", test_array);
    if (NULL == tmp) {
        printf("could not getenv(\"pmix_environ_test_string_1\")\n");
        return 1;
    }
    if (strcmp("old_one", tmp) != 0) {
        printf("wrong value.  expected %s, found %s\n",
               "old_one", tmp);
        return 1;
    }
    tmp = pmix_getenv("pmix_environ_test_string_2", test_array);
    if (NULL == tmp) {
        printf("could not getenv(\"pmix_environ_test_string_2\")\n");
        return 1;
    }
    if (strcmp("old_two", tmp) != 0) {
        printf("wrong value.  expected %s, found %s\n",
               "old_two", tmp);
        return 1;
    }
    tmp = pmix_getenv("pmix_environ_test_string_3", test_array);
    if (NULL == tmp) {
        printf("could not getenv(\"pmix_environ_test_string_3\")\n");
        return 1;
    }
    if (strcmp("old_three", tmp) != 0) {
        printf("wrong value.  expected %s, found %s\n",
               "old_three", tmp);
        return 1;
    }
    tmp = pmix_getenv("pmix_environ_test_string_4", test_array);
    if (NULL == tmp) {
        printf("could not getenv(\"pmix_environ_test_string_4\")\n");
        return 1;
    }
    if (strcmp("old_four", tmp) != 0) {
        printf("wrong value.  expected %s, found %s\n",
               "old_four", tmp);
        return 1;
    }
    tmp = pmix_getenv("pmix_environ_test_string_5", test_array);
    if (NULL == tmp) {
        printf("could not getenv(\"pmix_environ_test_string_5\")\n");
        return 1;
    }
    if (strcmp("new_five", tmp) != 0) {
        printf("wrong value.  expected %s, found %s\n",
               "new_five", tmp);
        return 1;
    }

    if (5 != PMIx_Argv_count(test_array)) {
        printf("test array has wrong length.  found %d, expected %d\n",
               PMIx_Argv_count(test_array), 5);
        return 1;
    }

    PMIx_Argv_free(test_array);

    return 0;
}