/[ascend]/trunk/base/generic/utilities/test/test_ascEnvVar.c
ViewVC logotype

Contents of /trunk/base/generic/utilities/test/test_ascEnvVar.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1061 - (show annotations) (download) (as text)
Sun Jan 7 07:19:52 2007 UTC (13 years, 6 months ago) by johnpye
File MIME type: text/x-csrc
File size: 30267 byte(s)
Fixed compiler warnings from test_ascEnvVar.c
1 /*
2 * Unit test functions for ASCEND: utilities/ascEnvVar.c
3 *
4 * Copyright (C) 2005 Jerry St.Clair
5 *
6 * This file is part of the Ascend Environment.
7 *
8 * The Ascend Environment is free software; you can redistribute it
9 * and/or modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * The Ascend Environment is distributed in hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with the program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
21 * COPYING.
22 */
23
24 #include <stdio.h>
25 #include <utilities/ascConfig.h>
26 #ifdef __WIN32__
27 #include <io.h>
28 #endif
29 #include <utilities/ascEnvVar.h>
30 #include <utilities/ascMalloc.h>
31 #include <general/list.h>
32 #include "CUnit/CUnit.h"
33 #include "test_ascEnvVar.h"
34
35 #ifdef __WIN32__
36 #define SLASH '\\'
37 #define PATHDIV ';'
38 #else /* ! __WIN32__ */
39 #define SLASH '/'
40 #define PATHDIV ':'
41 #endif
42
43 #define STR_LEN 250
44
45 /* compare function for strings used in this test function */
46 static
47 int compare_strings(CONST VOIDPTR p1, CONST VOIDPTR p2)
48 {
49 return strcmp((char *)p1, (char *)p2);
50 }
51
52 static void test_ascEnvVar(void)
53 {
54 char str_path[STR_LEN];
55 char str_path_ss1[STR_LEN];
56 char str_path_ss2[STR_LEN];
57 char str_path_ss3[STR_LEN];
58
59 char str_pathbig[MAX_ENV_VAR_LENGTH*3];
60 char str_pathbig_ss1[MAX_ENV_VAR_LENGTH*3];
61 char str_pathbig_ss2[MAX_ENV_VAR_LENGTH*3];
62
63 int elem_count;
64 const char **paths;
65 struct gl_list_t *name_list;
66 int i;
67 char test_ext_varname[] = "TEST_ASCENVVAR_EXTERNAL_ENV_VAR";
68 char *str_env;
69 int i_initialized_lists = FALSE;
70 unsigned long prior_meminuse;
71
72 prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
73
74 /* make sure list system is initialized */
75 if (FALSE == gl_pool_initialized()) {
76 gl_init();
77 gl_init_pool();
78 i_initialized_lists = TRUE;
79 }
80
81 /* test Asc_InitEnvironment() */
82 CU_TEST(0 == Asc_InitEnvironment(0)); /* init with 0 */
83 CU_TEST(1 == Asc_InitEnvironment(0)); /* should not be able to initialize again */
84 Asc_DestroyEnvironment();
85
86 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
87 CU_TEST(1 == Asc_InitEnvironment(10)); /* should not be able to initialize again */
88 Asc_DestroyEnvironment();
89
90 /* test Asc_SetPathList() */
91
92 CU_TEST(1 == Asc_SetPathList("envar1", str_path)); /* not initialized */
93
94 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
95
96 CU_TEST(1 == Asc_SetPathList(NULL, str_path)); /* NULL var */
97 CU_TEST(1 == Asc_SetPathList("envar1", NULL)); /* NULL path */
98 CU_TEST(1 == Asc_SetPathList("", str_path)); /* empty var */
99 CU_TEST(1 == Asc_SetPathList("envar1", "")); /* empty path */
100 CU_TEST(1 == Asc_SetPathList("envar 1", str_path)); /* embedded space in var */
101
102 memset(str_pathbig, '.', MAX_ENV_VAR_LENGTH);
103 str_pathbig[MAX_ENV_VAR_LENGTH] = '\0';
104
105 CU_TEST(1 == Asc_SetPathList(str_pathbig, str_path)); /* var too big */
106
107 Asc_DestroyEnvironment(); /* start clean in case failure cases left vars */
108 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
109
110 snprintf(str_path, STR_LEN-1, "%s", "ThisStringHasNoSpaces!");
111
112 CU_TEST(0 == Asc_SetPathList("envar1", str_path)); /* single path element */
113 paths = Asc_GetPathList("envar1", &elem_count);
114 CU_TEST_FATAL(NULL != paths);
115 CU_TEST_FATAL(1 == elem_count);
116 CU_TEST(!strcmp(paths[0], str_path));
117 ascfree(paths);
118
119 snprintf(str_path, STR_LEN-1, "%s%c%s", "ThisStringHasNoSpaces+",
120 PATHDIV,
121 "this one does, huh?");
122 snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces+");
123 snprintf(str_path_ss2, STR_LEN-1, "%s", "this one does, huh?");
124
125 CU_TEST(0 == Asc_SetPathList("envar2", str_path)); /* 2 path elements */
126 paths = Asc_GetPathList("envar2", &elem_count);
127 CU_TEST_FATAL(NULL != paths);
128 CU_TEST_FATAL(2 == elem_count);
129 CU_TEST(!strcmp(paths[0], str_path_ss1));
130 CU_TEST(!strcmp(paths[1], str_path_ss2));
131 ascfree(paths);
132
133 snprintf(str_path, STR_LEN-1, "%c%s", PATHDIV, " ThisStringHasNoSpaces! ");
134 snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces!");
135
136 CU_TEST(0 == Asc_SetPathList("envar2e1", str_path)); /* 2 elements, 1 empty */
137 paths = Asc_GetPathList("envar2e1", &elem_count);
138 CU_TEST_FATAL(NULL != paths);
139 CU_TEST_FATAL(1 == elem_count);
140 CU_TEST(!strcmp(paths[0], str_path_ss1));
141 ascfree(paths);
142
143 snprintf(str_path, STR_LEN-1, "%s%c", "< I DO have spaces > ", PATHDIV);
144 snprintf(str_path_ss1, STR_LEN-1, "%s", "< I DO have spaces >");
145
146 CU_TEST(0 == Asc_SetPathList("envar2e2", str_path)); /* 2 elements, other empty */
147 paths = Asc_GetPathList("envar2e2", &elem_count);
148 CU_TEST_FATAL(NULL != paths);
149 CU_TEST_FATAL(1 == elem_count);
150 CU_TEST(!strcmp(paths[0], str_path_ss1));
151 ascfree(paths);
152
153 snprintf(str_path, STR_LEN-1, "%c", PATHDIV);
154
155 CU_TEST(0 == Asc_SetPathList("envar2e3", str_path)); /* 2 elements, both empty */
156 paths = Asc_GetPathList("envar2e3", &elem_count);
157 CU_TEST_FATAL(NULL != paths);
158 CU_TEST(paths[0] == '\0');
159 CU_TEST(0 == elem_count);
160 if (NULL != paths) ascfree(paths);
161
162 snprintf(str_path, STR_LEN-1, " %c ", PATHDIV);
163
164 CU_TEST(0 == Asc_SetPathList("envar2e4", str_path)); /* 2 elements, both empty with ws */
165 paths = Asc_GetPathList("envar2e4", &elem_count);
166 CU_TEST_FATAL(NULL != paths);
167 CU_TEST(paths[0] == '\0');
168 CU_TEST(0 == elem_count);
169 if (NULL != paths) ascfree(paths);
170
171 snprintf(str_path, STR_LEN-1, "%s%c%s%c%s", "< spaces >",
172 PATHDIV,
173 "~NoSpaces~ ",
174 PATHDIV,
175 " another one . ~ $ ");
176 snprintf(str_path_ss1, STR_LEN-1, "%s", "< spaces >");
177 snprintf(str_path_ss2, STR_LEN-1, "%s", "~NoSpaces~");
178 snprintf(str_path_ss3, STR_LEN-1, "%s", "another one . ~ $");
179
180 CU_TEST(0 == Asc_SetPathList("envar3", str_path)); /* 3 path elements */
181 paths = Asc_GetPathList("envar3", &elem_count);
182 CU_TEST_FATAL(NULL != paths);
183 CU_TEST_FATAL(3 == elem_count);
184 CU_TEST(!strcmp(paths[0], str_path_ss1));
185 CU_TEST(!strcmp(paths[1], str_path_ss2));
186 CU_TEST(!strcmp(paths[2], str_path_ss3));
187 ascfree(paths);
188
189 snprintf(str_path, STR_LEN-1, "%s%c%c%s", " < spaces > ",
190 PATHDIV,
191 PATHDIV,
192 " another one . ~ $ ");
193 snprintf(str_path_ss1, STR_LEN-1, "%s", "< spaces >");
194 snprintf(str_path_ss2, STR_LEN-1, "%s", "another one . ~ $");
195
196 CU_TEST(0 == Asc_SetPathList("envar3e1", str_path)); /* 3 elements - middle empty */
197 paths = Asc_GetPathList("envar3e1", &elem_count);
198 CU_TEST_FATAL(NULL != paths);
199 CU_TEST_FATAL(2 == elem_count);
200 CU_TEST(!strcmp(paths[0], str_path_ss1));
201 CU_TEST(!strcmp(paths[1], str_path_ss2));
202 ascfree(paths);
203
204 snprintf(str_path, STR_LEN-1, "%c%s%c", PATHDIV,
205 " \t - =ASCEND rocks || \t",
206 PATHDIV);
207 snprintf(str_path_ss1, STR_LEN-1, "%s", "- =ASCEND rocks ||");
208
209 CU_TEST(0 == Asc_SetPathList("envar3e2", str_path)); /* 3 elements - 1st & 3rd empty */
210 paths = Asc_GetPathList("envar3e2", &elem_count);
211 CU_TEST_FATAL(NULL != paths);
212 CU_TEST_FATAL(1 == elem_count);
213 CU_TEST(!strcmp(paths[0], str_path_ss1));
214 ascfree(paths);
215
216 snprintf(str_path, STR_LEN-1, "%c %c ", PATHDIV, PATHDIV);
217
218 CU_TEST(0 == Asc_SetPathList("envar3e3", str_path)); /* 3 elements - all empty */
219 paths = Asc_GetPathList("envar3e3", &elem_count);
220 CU_TEST_FATAL(NULL != paths);
221 CU_TEST(paths[0] == '\0');
222 CU_TEST(0 == elem_count);
223 if (NULL != paths) ascfree(paths);
224
225 memset(str_pathbig, '.', MAX_ENV_VAR_LENGTH-2);
226 str_pathbig[MAX_ENV_VAR_LENGTH-2] = '\0';
227
228 CU_TEST(0 == Asc_SetPathList("envar4", str_pathbig)); /* large string at max length */
229 paths = Asc_GetPathList("envar4", &elem_count);
230 CU_TEST_FATAL(NULL != paths);
231 CU_TEST_FATAL(1 == elem_count);
232 CU_TEST(!strcmp(paths[0], str_pathbig));
233 ascfree(paths);
234
235 memset(str_pathbig_ss1, '%', MAX_ENV_VAR_LENGTH-1);
236 str_pathbig_ss1[MAX_ENV_VAR_LENGTH-1] = '\0';
237 memset(str_pathbig_ss2, '@', MAX_ENV_VAR_LENGTH-1);
238 str_pathbig_ss2[MAX_ENV_VAR_LENGTH-1] = '\0';
239
240 snprintf(str_pathbig, MAX_ENV_VAR_LENGTH*3, "%s%c%s",
241 str_pathbig_ss1,
242 PATHDIV,
243 str_pathbig_ss2);
244
245 CU_TEST(0 == Asc_SetPathList("envar5", str_pathbig)); /* multiple large strings */
246 paths = Asc_GetPathList("envar5", &elem_count);
247 CU_TEST_FATAL(NULL != paths);
248 CU_TEST_FATAL(2 == elem_count);
249 CU_TEST(!strcmp(paths[0], str_pathbig_ss1));
250 CU_TEST(!strcmp(paths[1], str_pathbig_ss2));
251 ascfree(paths);
252
253 memset(str_pathbig, '`', MAX_ENV_VAR_LENGTH*2);
254 str_pathbig[MAX_ENV_VAR_LENGTH*2] = '\0';
255 memset(str_pathbig_ss1, '`', MAX_ENV_VAR_LENGTH-1);
256 str_pathbig_ss1[MAX_ENV_VAR_LENGTH-1] = '\0';
257
258 CU_TEST(0 == Asc_SetPathList("envar6", str_pathbig)); /* large string over max length */
259 paths = Asc_GetPathList("envar6", &elem_count);
260 CU_TEST_FATAL(NULL != paths);
261 CU_TEST_FATAL(1 == elem_count);
262 CU_TEST(!strcmp(paths[0], str_pathbig_ss1));
263 ascfree(paths);
264
265 memset(str_pathbig, 'a', MAX_ENV_VAR_LENGTH-2);
266 str_pathbig[MAX_ENV_VAR_LENGTH-2] = '\0';
267
268 CU_TEST(0 == Asc_SetPathList(str_pathbig, "\t\teasy\t\t")); /* var name at max length */
269 paths = Asc_GetPathList(str_pathbig, &elem_count);
270 CU_TEST_FATAL(NULL != paths);
271 CU_TEST_FATAL(1 == elem_count);
272 CU_TEST(!strcmp(paths[0], "easy"));
273 ascfree(paths);
274
275 name_list = gl_create(15);
276 CU_TEST_FATAL(NULL != name_list);
277 gl_append_ptr(name_list, "envar1");
278 gl_append_ptr(name_list, "envar2");
279 gl_append_ptr(name_list, "envar2e1");
280 gl_append_ptr(name_list, "envar2e2");
281 gl_append_ptr(name_list, "envar2e3");
282 gl_append_ptr(name_list, "envar2e4");
283 gl_append_ptr(name_list, "envar3");
284 gl_append_ptr(name_list, "envar3e1");
285 gl_append_ptr(name_list, "envar3e2");
286 gl_append_ptr(name_list, "envar3e3");
287 gl_append_ptr(name_list, "envar4");
288 gl_append_ptr(name_list, "envar5");
289 gl_append_ptr(name_list, "envar6");
290 gl_append_ptr(name_list, str_pathbig);
291
292 paths = Asc_EnvNames(&elem_count); /* check the registered names */
293 CU_TEST(NULL != paths);
294 CU_TEST(14 == elem_count);
295 for (i=0 ; i<elem_count ; ++i) {
296 if (0 == gl_search(name_list, paths[i], compare_strings)) {
297 snprintf(str_pathbig, MAX_ENV_VAR_LENGTH*3, "Environment variable name not registered: %s",
298 paths[i]);
299 CU_FAIL(str_pathbig);
300 }
301 }
302 ascfree(paths);
303 gl_destroy(name_list);
304
305 Asc_DestroyEnvironment();
306
307 /* test Asc_PutEnv() */
308
309 snprintf(str_path, STR_LEN-1, "envar1=This is my string");
310
311 CU_TEST(1 == Asc_PutEnv(str_path)); /* not initialized */
312
313 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
314
315 CU_TEST(1 == Asc_PutEnv(NULL)); /* NULL string */
316 CU_TEST(1 == Asc_PutEnv("")); /* empty string */
317 CU_TEST(1 == Asc_PutEnv("=my_path_is_silly")); /* empty var name */
318 CU_TEST(1 == Asc_PutEnv("envar 1=my_path_is_silly")); /* embedded space in var */
319 CU_TEST(1 == Asc_PutEnv("my_path_is_silly")); /* no '=' */
320
321 memset(str_pathbig, '.', MAX_ENV_VAR_LENGTH-1);
322 str_pathbig[MAX_ENV_VAR_LENGTH-1] = '=';
323 sprintf(str_pathbig+MAX_ENV_VAR_LENGTH-1, "path");
324
325 CU_TEST(1 == Asc_PutEnv(str_pathbig)); /* var name too big */
326
327 Asc_DestroyEnvironment(); /* start clean in case failure cases left vars */
328
329 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
330
331 snprintf(str_path, STR_LEN-1, "%s", "envar1=ThisStringHasNoSpaces!");
332 snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces!");
333
334 CU_TEST(0 == Asc_PutEnv(str_path)); /* single path element */
335 paths = Asc_GetPathList("envar1", &elem_count);
336 CU_TEST_FATAL(NULL != paths);
337 CU_TEST_FATAL(1 == elem_count);
338 CU_TEST(!strcmp(paths[0], str_path_ss1));
339 ascfree(paths);
340
341 snprintf(str_path, STR_LEN-1, "envar2 =\t %s%c%s", "ThisStringHasNoSpaces+",
342 PATHDIV,
343 "this one does, huh?");
344 snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces+");
345 snprintf(str_path_ss2, STR_LEN-1, "%s", "this one does, huh?");
346
347 CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 path elements */
348 paths = Asc_GetPathList("envar2", &elem_count);
349 CU_TEST_FATAL(NULL != paths);
350 CU_TEST_FATAL(2 == elem_count);
351 CU_TEST(!strcmp(paths[0], str_path_ss1));
352 CU_TEST(!strcmp(paths[1], str_path_ss2));
353 ascfree(paths);
354
355 snprintf(str_path, STR_LEN-1, " envar2e1 = %c%s", PATHDIV, " ThisStringHasNoSpaces! ");
356 snprintf(str_path_ss1, STR_LEN-1, "%s", "ThisStringHasNoSpaces!");
357
358 CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 elements, 1 empty */
359 paths = Asc_GetPathList("envar2e1", &elem_count);
360 CU_TEST_FATAL(NULL != paths);
361 CU_TEST_FATAL(1 == elem_count);
362 CU_TEST(!strcmp(paths[0], str_path_ss1));
363 ascfree(paths);
364
365 snprintf(str_path, STR_LEN-1, "envar2e2= %s%c ", "< I DO have spaces > ", PATHDIV);
366 snprintf(str_path_ss1, STR_LEN-1, "%s", "< I DO have spaces >");
367
368 CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 elements, other empty */
369 paths = Asc_GetPathList("envar2e2", &elem_count);
370 CU_TEST_FATAL(NULL != paths);
371 CU_TEST_FATAL(1 == elem_count);
372 CU_TEST(!strcmp(paths[0], str_path_ss1));
373 ascfree(paths);
374
375 snprintf(str_path, STR_LEN-1, "envar2e3=%c", PATHDIV);
376
377 CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 elements, both empty */
378 paths = Asc_GetPathList("envar2e3", &elem_count);
379 CU_TEST_FATAL(NULL != paths);
380 CU_TEST(paths[0] == '\0');
381 CU_TEST(0 == elem_count);
382 if (NULL != paths) ascfree(paths);
383
384 snprintf(str_path, STR_LEN-1, "\t envar2e4 = %c ", PATHDIV);
385
386 CU_TEST(0 == Asc_PutEnv(str_path)); /* 2 elements, both empty with ws */
387 paths = Asc_GetPathList("envar2e4", &elem_count);
388 CU_TEST_FATAL(NULL != paths);
389 CU_TEST(paths[0] == '\0');
390 CU_TEST(0 == elem_count);
391 if (NULL != paths) ascfree(paths);
392
393 snprintf(str_path, STR_LEN-1, "envar3=%s%c%s%c%s", "< spaces >",
394 PATHDIV,
395 "~NoSpaces~ ",
396 PATHDIV,
397 " another one . ~ $ ");
398 snprintf(str_path_ss1, STR_LEN-1, "%s", "< spaces >");
399 snprintf(str_path_ss2, STR_LEN-1, "%s", "~NoSpaces~");
400 snprintf(str_path_ss3, STR_LEN-1, "%s", "another one . ~ $");
401
402 CU_TEST(0 == Asc_PutEnv(str_path)); /* 3 path elements */
403 paths = Asc_GetPathList("envar3", &elem_count);
404 CU_TEST_FATAL(NULL != paths);
405 CU_TEST_FATAL(3 == elem_count);
406 CU_TEST(!strcmp(paths[0], str_path_ss1));
407 CU_TEST(!strcmp(paths[1], str_path_ss2));
408 CU_TEST(!strcmp(paths[2], str_path_ss3));
409 ascfree(paths);
410
411 snprintf(str_path, STR_LEN-1, "envar3e1\t=%s%c%c%s", " < spaces > ",
412 PATHDIV,
413 PATHDIV,
414 " another one . ~ $ ");
415 snprintf(str_path_ss1, STR_LEN-1, "%s", "< spaces >");
416 snprintf(str_path_ss2, STR_LEN-1, "%s", "another one . ~ $");
417
418 CU_TEST(0 == Asc_PutEnv(str_path)); /* 3 elements - middle empty */
419 paths = Asc_GetPathList("envar3e1", &elem_count);
420 CU_TEST_FATAL(NULL != paths);
421 CU_TEST_FATAL(2 == elem_count);
422 CU_TEST(!strcmp(paths[0], str_path_ss1));
423 CU_TEST(!strcmp(paths[1], str_path_ss2));
424 ascfree(paths);
425
426 snprintf(str_path, STR_LEN-1, "envar3e2=%c%s%c", PATHDIV,
427 " \t - =ASCEND rocks || \t",
428 PATHDIV);
429 snprintf(str_path_ss1, STR_LEN-1, "%s", "- =ASCEND rocks ||");
430
431 CU_TEST(0 == Asc_PutEnv(str_path)); /* 3 elements - 1st & 3rd empty */
432 paths = Asc_GetPathList("envar3e2", &elem_count);
433 CU_TEST_FATAL(NULL != paths);
434 CU_TEST_FATAL(1 == elem_count);
435 CU_TEST(!strcmp(paths[0], str_path_ss1));
436 ascfree(paths);
437
438 snprintf(str_path, STR_LEN-1, "envar3e3=%c %c ", PATHDIV, PATHDIV);
439
440 CU_TEST(0 == Asc_PutEnv(str_path)); /* 3 elements - all empty */
441 paths = Asc_GetPathList("envar3e3", &elem_count);
442 CU_TEST_FATAL(NULL != paths);
443 CU_TEST(paths[0] == '\0');
444 CU_TEST(0 == elem_count);
445 if (NULL != paths) ascfree(paths);
446
447 memset(str_pathbig_ss1, '.', MAX_ENV_VAR_LENGTH-9);
448 str_pathbig_ss1[MAX_ENV_VAR_LENGTH-9] = '\0';
449 snprintf(str_pathbig, MAX_ENV_VAR_LENGTH-1, "envar4=%s", str_pathbig_ss1);
450
451 CU_TEST(0 == Asc_PutEnv(str_pathbig)); /* large string at max length */
452 paths = Asc_GetPathList("envar4", &elem_count);
453 CU_TEST_FATAL(NULL != paths);
454 CU_TEST_FATAL(1 == elem_count);
455 CU_TEST(!strcmp(paths[0], str_pathbig_ss1));
456 ascfree(paths);
457
458 memset(str_pathbig_ss1, 'a', MAX_ENV_VAR_LENGTH-3);
459 str_pathbig_ss1[MAX_ENV_VAR_LENGTH-3] = '\0';
460 snprintf(str_pathbig, MAX_ENV_VAR_LENGTH, "%s=1", str_pathbig_ss1);
461
462 CU_TEST(0 == Asc_PutEnv(str_pathbig)); /* large string at max length */
463 paths = Asc_GetPathList(str_pathbig_ss1, &elem_count);
464 CU_TEST_FATAL(NULL != paths);
465 CU_TEST_FATAL(1 == elem_count);
466 CU_TEST(!strcmp(paths[0], "1"));
467 ascfree(paths);
468
469 name_list = gl_create(15);
470 CU_TEST_FATAL(NULL != name_list);
471 gl_append_ptr(name_list, "envar1");
472 gl_append_ptr(name_list, "envar2");
473 gl_append_ptr(name_list, "envar2e1");
474 gl_append_ptr(name_list, "envar2e2");
475 gl_append_ptr(name_list, "envar2e3");
476 gl_append_ptr(name_list, "envar2e4");
477 gl_append_ptr(name_list, "envar3");
478 gl_append_ptr(name_list, "envar3e1");
479 gl_append_ptr(name_list, "envar3e2");
480 gl_append_ptr(name_list, "envar3e3");
481 gl_append_ptr(name_list, "envar4");
482 gl_append_ptr(name_list, str_pathbig_ss1);
483
484 paths = Asc_EnvNames(&elem_count); /* check the registered names */
485 CU_TEST(NULL != paths);
486 CU_TEST(12 == elem_count);
487 for (i=0 ; i<elem_count ; ++i) {
488 if (0 == gl_search(name_list, paths[i], compare_strings)) {
489 snprintf(str_pathbig, MAX_ENV_VAR_LENGTH*3, "Environment variable name not registered: %s",
490 paths[i]);
491 CU_FAIL(str_pathbig);
492 }
493 }
494 ascfree(paths);
495 gl_destroy(name_list);
496
497 Asc_DestroyEnvironment(); /* clean up */
498
499 /* test Asc_ImportPathList() */
500
501 CU_TEST(1 == Asc_ImportPathList("envar1")); /* not initialized */
502
503 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
504
505 CU_TEST(1 == Asc_ImportPathList(NULL)); /* NULL varname */
506 CU_TEST(1 == Asc_ImportPathList("")); /* empty varname */
507
508 Asc_DestroyEnvironment(); /* start with a clean list */
509 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
510
511 if (NULL != getenv(test_ext_varname)) { /* make sure our external env var doesn't exist */
512 snprintf(str_path, STR_LEN-1,
513 "External environment variable '%s' already exists. Aborting test of Asc_ImportPathList().",
514 test_ext_varname);
515 str_path[STR_LEN-1] = '\0';
516 CU_FAIL(str_path);
517 }
518 else {
519
520 CU_TEST(1 == Asc_ImportPathList(test_ext_varname)); /* non-existent external env var */
521
522 snprintf(str_path_ss1, STR_LEN-1, "%s", "iHaveNoSpaces");
523 snprintf(str_path, STR_LEN-1, "%s=%s", test_ext_varname, str_path_ss1);
524 CU_TEST_FATAL(0 == putenv(str_path));
525
526 CU_TEST(0 == Asc_ImportPathList(test_ext_varname)); /* import single path env var */
527 paths = Asc_GetPathList(test_ext_varname, &elem_count);
528 CU_TEST_FATAL(NULL != paths);
529 CU_TEST_FATAL(1 == elem_count);
530 CU_TEST(!strcmp(paths[0], str_path_ss1));
531 if (NULL != paths) ascfree(paths);
532
533 snprintf(str_path_ss1, STR_LEN-1, "%s", "~This one has spaces~");
534 snprintf(str_path_ss2, STR_LEN-1, "%s", "<>NotMe<>");
535 snprintf(str_path, STR_LEN-1, "%s=%s%c%s",
536 test_ext_varname,
537 str_path_ss1,
538 PATHDIV,
539 str_path_ss2);
540 CU_TEST_FATAL(0 == putenv(str_path));
541
542 CU_TEST(0 == Asc_ImportPathList(test_ext_varname)); /* import double path env var */
543 paths = Asc_GetPathList(test_ext_varname, &elem_count);
544 CU_TEST_FATAL(NULL != paths);
545 CU_TEST_FATAL(2 == elem_count);
546 CU_TEST(!strcmp(paths[0], str_path_ss1));
547 CU_TEST(!strcmp(paths[1], str_path_ss2));
548 if (NULL != paths) ascfree(paths);
549
550 snprintf(str_path_ss1, STR_LEN-1, "%cusr%clocal%clib", SLASH, SLASH, SLASH);
551 snprintf(str_path_ss2, STR_LEN-1, "%cc%cWindows%cTemp", SLASH, SLASH, SLASH);
552 snprintf(str_path_ss3, STR_LEN-1, "server%c%caccount%csubfolder", SLASH, SLASH, SLASH);
553 snprintf(str_path, STR_LEN-1, "%s=%s%c%s%c%s",
554 test_ext_varname,
555 str_path_ss1,
556 PATHDIV,
557 str_path_ss2,
558 PATHDIV,
559 str_path_ss3);
560 CU_TEST_FATAL(0 == putenv(str_path));
561
562 CU_TEST(0 == Asc_ImportPathList(test_ext_varname)); /* import double path env var */
563 paths = Asc_GetPathList(test_ext_varname, &elem_count);
564 CU_TEST_FATAL(NULL != paths);
565 CU_TEST_FATAL(3 == elem_count);
566 CU_TEST(!strcmp(paths[0], str_path_ss1));
567 CU_TEST(!strcmp(paths[1], str_path_ss2));
568 CU_TEST(!strcmp(paths[2], str_path_ss3));
569 if (NULL != paths) ascfree(paths);
570 }
571
572 snprintf(str_path, STR_LEN-1, "%s=", test_ext_varname); /* clear the temporary external env var */
573 CU_TEST(0 == putenv(str_path));
574 Asc_DestroyEnvironment(); /* clean up */
575
576 /* test Asc_AppendPath() */
577
578 CU_TEST(1 == Asc_AppendPath("envar1", str_path)); /* not initialized */
579
580 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
581
582 CU_TEST(1 == Asc_AppendPath(NULL, str_path)); /* NULL var */
583 CU_TEST(1 == Asc_AppendPath("envar1", NULL)); /* NULL path */
584 CU_TEST(1 == Asc_AppendPath("", str_path)); /* empty var */
585 CU_TEST(1 == Asc_AppendPath("envar1", "")); /* empty path */
586
587 Asc_DestroyEnvironment(); /* start clean in case failure cases left vars */
588 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
589
590 snprintf(str_path_ss1, STR_LEN-1, "path/with\\many;unusual:chars");
591 snprintf(str_path_ss2, STR_LEN-1, " What should I test next? ");
592 snprintf(str_path_ss3, STR_LEN-1, "T");
593
594 CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss1)); /* append a path to a new variable */
595 paths = Asc_GetPathList("envar1", &elem_count);
596 CU_TEST_FATAL(NULL != paths);
597 CU_TEST_FATAL(1 == elem_count);
598 CU_TEST(!strcmp(paths[0], str_path_ss1));
599 ascfree(paths);
600
601 CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss1)); /* append a path 2nd time */
602 paths = Asc_GetPathList("envar1", &elem_count);
603 CU_TEST_FATAL(NULL != paths);
604 CU_TEST_FATAL(2 == elem_count);
605 CU_TEST(!strcmp(paths[0], str_path_ss1));
606 CU_TEST(!strcmp(paths[1], str_path_ss1));
607 ascfree(paths);
608
609 CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss2)); /* append a new path */
610 paths = Asc_GetPathList("envar1", &elem_count);
611 CU_TEST_FATAL(NULL != paths);
612 CU_TEST_FATAL(3 == elem_count);
613 CU_TEST(!strcmp(paths[0], str_path_ss1));
614 CU_TEST(!strcmp(paths[1], str_path_ss1));
615 CU_TEST(!strcmp(paths[2], str_path_ss2));
616 ascfree(paths);
617
618 CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss1)); /* append a path a 3rd time */
619 paths = Asc_GetPathList("envar1", &elem_count);
620 CU_TEST_FATAL(NULL != paths);
621 CU_TEST_FATAL(4 == elem_count);
622 CU_TEST(!strcmp(paths[0], str_path_ss1));
623 CU_TEST(!strcmp(paths[1], str_path_ss1));
624 CU_TEST(!strcmp(paths[2], str_path_ss2));
625 CU_TEST(!strcmp(paths[3], str_path_ss1));
626 ascfree(paths);
627
628 CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss3)); /* append a new path */
629 paths = Asc_GetPathList("envar1", &elem_count);
630 CU_TEST_FATAL(NULL != paths);
631 CU_TEST_FATAL(5 == elem_count);
632 CU_TEST(!strcmp(paths[0], str_path_ss1));
633 CU_TEST(!strcmp(paths[1], str_path_ss1));
634 CU_TEST(!strcmp(paths[2], str_path_ss2));
635 CU_TEST(!strcmp(paths[3], str_path_ss1));
636 CU_TEST(!strcmp(paths[4], str_path_ss3));
637 ascfree(paths);
638
639 Asc_DestroyEnvironment(); /* clean up */
640
641 /* test Asc_GetPathList() - test unusual cases - main use covered in other tests */
642
643 paths = Asc_GetPathList("envar1", &elem_count); /* not initialized */
644 CU_TEST(-1 == elem_count);
645 CU_TEST(NULL == paths);
646
647 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
648
649 paths = Asc_GetPathList(NULL, &elem_count); /* NULL var */
650 CU_TEST(-1 == elem_count);
651 CU_TEST(NULL == paths);
652
653 paths = Asc_GetPathList("envar1", NULL); /* NULL argcPtr */
654 CU_TEST(-1 == elem_count);
655 CU_TEST(NULL == paths);
656
657 paths = Asc_GetPathList("", &elem_count); /* empty var */
658 CU_TEST(0 == elem_count);
659 CU_TEST(NULL == paths);
660
661 paths = Asc_GetPathList("envar1", &elem_count); /* non-existent var */
662 CU_TEST(0 == elem_count);
663 CU_TEST(NULL == paths);
664
665 Asc_DestroyEnvironment(); /* start clean in case failure cases left vars */
666
667 /* test Asc_GetEnv() */
668
669 str_env = Asc_GetEnv("envar1"); /* not initialized */
670 CU_TEST(NULL == str_env);
671
672 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
673
674 str_env = Asc_GetEnv(NULL); /* NULL var */
675 CU_TEST(NULL == str_env);
676
677 str_env = Asc_GetEnv(""); /* empty var */
678 CU_TEST(NULL == str_env);
679
680 str_env = Asc_GetEnv("envar1"); /* non-existent var */
681 CU_TEST(NULL == str_env);
682
683 snprintf(str_path_ss1, STR_LEN-1, "path/with\\many;unusual:chars");
684 snprintf(str_path_ss2, STR_LEN-1, " What should I test next? ");
685 snprintf(str_path_ss3, STR_LEN-1, "T");
686 snprintf(str_path, STR_LEN-1, "%s%c%s%c%s", str_path_ss1, PATHDIV, str_path_ss2, PATHDIV, str_path_ss3);
687
688 CU_TEST(0 == Asc_SetPathList("envar1", str_path_ss1));
689 str_env = Asc_GetEnv("envar1");
690 CU_TEST_FATAL(NULL != str_env);
691 CU_TEST(!strcmp(str_env, str_path_ss1));
692 ascfree(str_env);
693
694 CU_TEST(0 == Asc_SetPathList("envar1", str_path_ss3));
695 str_env = Asc_GetEnv("envar1");
696 CU_TEST_FATAL(NULL != str_env);
697 CU_TEST(!strcmp(str_env, str_path_ss3));
698 ascfree(str_env);
699
700 CU_TEST(0 == Asc_SetPathList("envar1", str_path_ss1));
701 CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss2));
702 CU_TEST(0 == Asc_AppendPath("envar1", str_path_ss3));
703 str_env = Asc_GetEnv("envar1");
704 CU_TEST_FATAL(NULL != str_env);
705 CU_TEST(!strcmp(str_env, str_path));
706 ascfree(str_env);
707
708 Asc_DestroyEnvironment(); /* clean up */
709
710 /* test Asc_EnvNames() */
711
712 paths = Asc_EnvNames(&elem_count); /* not initialized */
713 CU_TEST(NULL == paths);
714 CU_TEST(-1 == elem_count);
715
716 CU_TEST(0 == Asc_InitEnvironment(10)); /* init with typical number */
717
718 paths = Asc_EnvNames(&elem_count); /* empty environment list */
719 CU_TEST_FATAL(NULL != paths);
720 CU_TEST(0 == elem_count);
721 ascfree(paths);
722
723 Asc_SetPathList("envar1", "my val 1");
724 paths = Asc_EnvNames(&elem_count); /* 1 environment var */
725 CU_TEST_FATAL(NULL != paths);
726 CU_TEST_FATAL(1 == elem_count);
727 CU_TEST(!strcmp(paths[0], "envar1"));
728 ascfree(paths);
729
730 Asc_SetPathList("envar2", "my val 2");
731 paths = Asc_EnvNames(&elem_count); /* 2 environment var */
732 CU_TEST_FATAL(NULL != paths);
733 CU_TEST_FATAL(2 == elem_count);
734 CU_TEST(!strcmp(paths[0], "envar1"));
735 CU_TEST(!strcmp(paths[1], "envar2"));
736 ascfree(paths);
737
738 Asc_SetPathList("envar3", "my val 3");
739 paths = Asc_EnvNames(&elem_count); /* 2 environment var */
740 CU_TEST_FATAL(NULL != paths);
741 CU_TEST_FATAL(3 == elem_count);
742 CU_TEST(!strcmp(paths[0], "envar1"));
743 CU_TEST(!strcmp(paths[1], "envar2"));
744 CU_TEST(!strcmp(paths[2], "envar3"));
745 ascfree(paths);
746
747 Asc_DestroyEnvironment(); /* start with a clean list */
748
749 if (TRUE == i_initialized_lists) {
750 gl_destroy_pool();
751 }
752
753 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
754 }
755
756 /*===========================================================================*/
757 /* Registration information */
758
759 static CU_TestInfo ascEnvVar_test_list[] = {
760 {"test_ascEnvVar", test_ascEnvVar},
761 CU_TEST_INFO_NULL
762 };
763
764 static CU_SuiteInfo suites[] = {
765 {"test_utilities_ascEnvVar", NULL, NULL, ascEnvVar_test_list},
766 CU_SUITE_INFO_NULL
767 };
768
769 /*-------------------------------------------------------------------*/
770 CU_ErrorCode test_register_utilities_ascEnvVar(void)
771 {
772 return CU_register_suites(suites);
773 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22