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

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