| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2021 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | #include "support/env.h" | ||
| 21 | |||
| 22 | #include <stdlib.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <assert.h> | ||
| 25 | |||
| 26 | 1 | int main(int argc, char **argv) { | |
| 27 | |||
| 28 | /* Empty environment */ | ||
| 29 | 1 | char **env = malloc(1*sizeof(char*)); | |
| 30 | 1 | env[0] = NULL; | |
| 31 | |||
| 32 | // Set initial value | ||
| 33 | 1 | dlb_setenv("VAR1", "value1", &env, ENV_OVERWRITE_NEVER); | |
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( strcmp(env[0], "VAR1=value1") == 0 ); |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( env[1] == NULL ); |
| 36 | // Do not Overwrite | ||
| 37 | 1 | dlb_setenv("VAR1", "value2", &env, ENV_OVERWRITE_NEVER); | |
| 38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( strcmp(env[0], "VAR1=value1") == 0 ); |
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( env[1] == NULL ); |
| 40 | // Overwrite | ||
| 41 | 1 | dlb_setenv("VAR1", "value2", &env, ENV_OVERWRITE_ALWAYS); | |
| 42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( strcmp(env[0], "VAR1=value2") == 0 ); |
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( env[1] == NULL ); |
| 44 | // Modify only if exists (it does) | ||
| 45 | 1 | dlb_setenv("VAR1", "value3", &env, ENV_UPDATE_IF_EXISTS); | |
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( strcmp(env[0], "VAR1=value3") == 0 ); |
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( env[1] == NULL ); |
| 48 | // Modify only if exists (it doesn't) | ||
| 49 | 1 | dlb_setenv("VAR2", "xxx", &env, ENV_UPDATE_IF_EXISTS); | |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( strcmp(env[0], "VAR1=value3") == 0 ); |
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( env[1] == NULL ); |
| 52 | // Append | ||
| 53 | 1 | dlb_setenv("VAR1", "value4", &env, ENV_APPEND); | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( strcmp(env[0], "VAR1=value3 value4") == 0 ); |
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( env[1] == NULL ); |
| 56 | |||
| 57 | 1 | free(env[0]); | |
| 58 | 1 | free(env); | |
| 59 | |||
| 60 | /* Current environment */ | ||
| 61 | |||
| 62 | 1 | const char *var_name = "DLB_TESTING_VAR"; | |
| 63 | |||
| 64 | // Set initial value | ||
| 65 | 1 | dlb_setenv(var_name, "value1", NULL, ENV_OVERWRITE_NEVER); | |
| 66 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( strcmp(getenv(var_name), "value1") == 0 ); |
| 67 | // Do not Overwrite | ||
| 68 | 1 | dlb_setenv(var_name, "value2", NULL, ENV_OVERWRITE_NEVER); | |
| 69 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( strcmp(getenv(var_name), "value1") == 0 ); |
| 70 | // Overwrite | ||
| 71 | 1 | dlb_setenv(var_name, "value2", NULL, ENV_OVERWRITE_ALWAYS); | |
| 72 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( strcmp(getenv(var_name), "value2") == 0 ); |
| 73 | // Modify only if exists (it does) | ||
| 74 | 1 | dlb_setenv(var_name, "value3", NULL, ENV_UPDATE_IF_EXISTS); | |
| 75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( strcmp(getenv(var_name), "value3") == 0 ); |
| 76 | // Append | ||
| 77 | 1 | dlb_setenv(var_name, "value4", NULL, ENV_APPEND); | |
| 78 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( strcmp(getenv(var_name), "value3 value4") == 0 ); |
| 79 | // Modify only if exists (it doesn't) | ||
| 80 | 1 | unsetenv(var_name); | |
| 81 | 1 | dlb_setenv(var_name, "xxx", NULL, ENV_UPDATE_IF_EXISTS); | |
| 82 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( getenv(var_name) == NULL ); |
| 83 | |||
| 84 | 1 | return 0; | |
| 85 | } | ||
| 86 |