19static void utest_remove_duplicates_int(
void);
20static void utest_name_type_pairs(
void);
21static void utest_qsort_index(
void);
22static void utest_getenv_bool(MPI_Comm comm);
23static void utest_getenv_int(MPI_Comm comm);
24static void utest_getenv_str(MPI_Comm comm);
31 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
34 PUT_ERR(
"ERROR: test_utils_common requires exactly 4 MPI processes");
39 utest_remove_duplicates_int();
40 utest_name_type_pairs();
42 utest_getenv_bool(MPI_COMM_WORLD);
43 utest_getenv_int(MPI_COMM_WORLD);
44 utest_getenv_str(MPI_COMM_WORLD);
51static int utest_compare_int(
void const * a,
void const * b) {
53 int aa = *(
int const *)a;
54 int bb = *(
int const *)b;
56 return (aa > bb) - (aa < bb);
59static void utest_unsetenv_all(MPI_Comm comm,
char const *
name) {
67static void utest_setenv_all(
68 MPI_Comm comm,
char const *
name,
char const * root_value,
69 char const * other_value) {
72 MPI_Comm_rank(comm, &rank);
74 char const *
value = (
rank == 0)?root_value:other_value;
84static void utest_remove_duplicates_int(
void) {
90 if (n != 0)
PUT_ERR(
"yac_remove_duplicates_int failed for empty input");
94 int values[] = {1, 1, 2, 2, 2, 4, 4, 7};
95 int ref[] = {1, 2, 4, 7};
96 size_t n =
sizeof(values) /
sizeof(values[0]);
100 if (n !=
sizeof(ref) /
sizeof(ref[0]))
101 PUT_ERR(
"yac_remove_duplicates_int produced wrong output length");
102 for (
size_t i = 0;
i <
n; ++
i)
103 if (values[i] != ref[i])
104 PUT_ERR(
"yac_remove_duplicates_int produced wrong output values");
108 int values[] = {-2, -1, 0, 3};
109 int ref[] = {-2, -1, 0, 3};
110 size_t n =
sizeof(values) /
sizeof(values[0]);
114 if (n !=
sizeof(ref) /
sizeof(ref[0]))
115 PUT_ERR(
"yac_remove_duplicates_int changed unique input length");
116 for (
size_t i = 0;
i <
n; ++
i)
117 if (values[i] != ref[i])
118 PUT_ERR(
"yac_remove_duplicates_int changed unique input values");
122static void utest_name_type_pairs(
void) {
124 enum { alpha = -3, beta = 2, gamma = 2, invalid = 42 };
127 utest_name_type_table,
137 utest_name_type_table, utest_name_type_table_size, beta);
138 if ((
name == NULL) || strcmp(
name,
"beta"))
139 PUT_ERR(
"yac_name_type_pair_get_name returned wrong result");
143 utest_name_type_table, utest_name_type_table_size, invalid) != NULL)
144 PUT_ERR(
"yac_name_type_pair_get_name should return NULL for unknown type");
148 utest_name_type_table, utest_name_type_table_size,
"alpha") != alpha)
149 PUT_ERR(
"yac_name_type_pair_get_type returned wrong result for alpha");
153 utest_name_type_table, utest_name_type_table_size,
"BETA") != beta)
154 PUT_ERR(
"yac_name_type_pair_get_type returned wrong result for BETA");
158 utest_name_type_table, utest_name_type_table_size,
"invalid") !=
160 PUT_ERR(
"yac_name_type_pair_get_type should return INT_MAX for unknown names");
164 utest_name_type_table, utest_name_type_table_size, NULL) != INT_MAX)
165 PUT_ERR(
"yac_name_type_pair_get_type should return INT_MAX for NULL input");
168static void utest_qsort_index(
void) {
171 int values[] = {5, 1, 3, 1, 4};
172 int ref[] = {5, 1, 3, 1, 4};
173 size_t idx[] = {0, 1, 2, 3, 4};
174 int const sorted_ref[] = {1, 1, 3, 4, 5};
177 values,
sizeof(values) /
sizeof(values[0]),
sizeof(values[0]),
178 utest_compare_int, idx);
180 for (
size_t i = 0;
i <
sizeof(values) /
sizeof(values[0]); ++
i)
181 if (values[i] != sorted_ref[i])
182 PUT_ERR(
"yac_qsort_index failed to sort values");
184 int seen[] = {0, 0, 0, 0, 0};
185 for (
size_t i = 0;
i <
sizeof(idx) /
sizeof(idx[0]); ++
i) {
186 if (idx[i] >=
sizeof(ref) /
sizeof(ref[0])) {
187 PUT_ERR(
"yac_qsort_index returned out-of-range index");
190 if (seen[idx[i]])
PUT_ERR(
"yac_qsort_index returned duplicate index");
192 if (ref[idx[i]] != values[i])
193 PUT_ERR(
"yac_qsort_index index mapping does not match sorted values");
198 int values[] = {3, 2, 1};
199 int const sorted_ref[] = {1, 2, 3};
202 values,
sizeof(values) /
sizeof(values[0]),
sizeof(values[0]),
203 utest_compare_int, NULL);
205 for (
size_t i = 0;
i <
sizeof(values) /
sizeof(values[0]); ++
i)
206 if (values[i] != sorted_ref[i])
207 PUT_ERR(
"yac_qsort_index failed for idx == NULL");
211static void utest_getenv_bool(MPI_Comm comm) {
214 MPI_Comm_rank(comm, &rank);
216 char const * env_name =
"YAC_UTEST_BOOL";
219 utest_unsetenv_all(comm, env_name);
221 if (
value != 1)
PUT_ERR(
"yac_getenv_bool returned wrong default value");
225 utest_setenv_all(comm, env_name, NULL,
"on");
228 PUT_ERR(
"yac_getenv_bool returned wrong value for unset on root");
232 utest_setenv_all(comm, env_name,
"on", NULL);
235 PUT_ERR(
"yac_getenv_bool returned wrong value for unset on non-root");
241 utest_setenv_all(comm, env_name,
"on",
"off");
243 if (
value != 1)
PUT_ERR(
"yac_getenv_bool returned wrong value for 'on'");
248 utest_setenv_all(comm, env_name,
"off",
"on");
250 if (
value != 0)
PUT_ERR(
"yac_getenv_bool returned wrong value for 'off'");
261 utest_unsetenv_all(comm, env_name);
264static void utest_getenv_int(MPI_Comm comm) {
267 MPI_Comm_rank(comm, &rank);
269 char const * env_name =
"YAC_UTEST_INT";
272 utest_unsetenv_all(comm, env_name);
274 if (
value != 7)
PUT_ERR(
"yac_getenv_int returned wrong default value");
278 utest_setenv_all(comm, env_name, NULL,
"12");
281 PUT_ERR(
"yac_getenv_int returned wrong value for unset on root");
285 utest_setenv_all(comm, env_name,
"12", NULL);
288 PUT_ERR(
"yac_getenv_int returned wrong value for unset on non-root");
293 utest_setenv_all(comm, env_name,
"2",
"2");
296 PUT_ERR(
"yac_getenv_int returned wrong value for below threshold");
302 snprintf(str_rank,
sizeof(str_rank),
"%d", rank);
303 utest_setenv_all(comm, env_name, str_rank, str_rank);
306 PUT_ERR(
"yac_getenv_int returned wrong value for rank-dependent input");
317 utest_unsetenv_all(comm, env_name);
320static void utest_getenv_str(MPI_Comm comm) {
323 MPI_Comm_rank(comm, &rank);
325 char const * env_name =
"YAC_UTEST_STR";
328 utest_unsetenv_all(comm, env_name);
330 if ((str_value == NULL) || strcmp(str_value,
"fallback"))
331 PUT_ERR(
"yac_getenv_str returned wrong default value");
336 utest_setenv_all(comm, env_name,
"",
"");
338 if ((str_value == NULL) || strcmp(str_value,
"fallback"))
339 PUT_ERR(
"yac_getenv_str returned wrong value for empty string");
344 utest_setenv_all(comm, env_name,
"root_value",
"other_value");
346 if ((str_value == NULL) || strcmp(str_value,
"root_value"))
347 PUT_ERR(
"yac_getenv_str returned wrong value for non-empty string");
353 utest_setenv_all(comm, env_name,
"",
"other_value");
355 if ((str_value == NULL) || strcmp(str_value,
"fallback"))
357 "yac_getenv_str returned wrong value for empty string on root");
363 utest_setenv_all(comm, env_name,
"root_value",
"");
365 if ((str_value == NULL) || strcmp(str_value,
"root_value"))
367 "yac_getenv_str returned wrong value for empty string on non-root");
373 utest_setenv_all(comm, env_name, NULL,
"other_value");
375 if ((str_value == NULL) || strcmp(str_value,
"fallback"))
376 PUT_ERR(
"yac_getenv_str returned wrong value for unset on root");
381 utest_setenv_all(comm, env_name,
"root_value", NULL);
383 if ((str_value == NULL) || strcmp(str_value,
"root_value"))
384 PUT_ERR(
"yac_getenv_str returned wrong value for unset on non-root");
389 utest_unsetenv_all(comm, env_name);
int yac_name_type_pair_get_type(struct yac_name_type_pair const *pairs, size_t count, char const *name)
#define DEF_NAME_TYPE_PAIR(NAME, TYPE)
char const * yac_name_type_pair_get_name(struct yac_name_type_pair const *pairs, size_t count, int type)
#define DEF_NAME_TYPE_PAIRS(NAME,...)
void yac_qsort_index(void *a_, size_t count, size_t size, int(*compare)(void const *, void const *), size_t *idx)
int yac_getenv_int(const char *name, int default_value, int threshold, MPI_Comm comm)
static void yac_remove_duplicates_int(int *array, size_t *n)
int yac_getenv_bool(const char *name, int default_value, MPI_Comm comm)
char * yac_getenv_str(const char *name, const char *default_value, MPI_Comm comm)