29static void utest_scalar_int_roundtrip(MPI_Comm comm);
30static void utest_scalar_dble_roundtrip(MPI_Comm comm);
31static void utest_scalar_str_roundtrip(MPI_Comm comm);
32static void utest_struct_roundtrip(MPI_Comm comm);
33static void utest_list_roundtrip(MPI_Comm comm);
34static void utest_empty_list_roundtrip(MPI_Comm comm);
36static int utest_list_default_factory(
37 void ** element_ptr,
struct yac_param ** param_ptr) {
40 if (
value == NULL)
return 1;
46 return (*param_ptr == NULL);
49static void utest_free_int_list(
void ** list) {
51 if (list == NULL)
return;
52 for (
size_t i = 0; list[
i] != NULL; ++
i) free(list[i]);
61 utest_scalar_int_roundtrip(MPI_COMM_SELF);
62 utest_scalar_dble_roundtrip(MPI_COMM_SELF);
63 utest_scalar_str_roundtrip(MPI_COMM_SELF);
64 utest_struct_roundtrip(MPI_COMM_SELF);
65 utest_list_roundtrip(MPI_COMM_SELF);
66 utest_empty_list_roundtrip(MPI_COMM_SELF);
73static void utest_scalar_int_roundtrip(MPI_Comm comm) {
77 char const * param_name =
"ival";
78 int const default_value = 42;
79 int const value_min = -1000;
80 int const value_max = 1000;
81 int const has_default = 1;
82 int const is_defined = 1;
84 int value = default_value;
89 param_name, &
value, default_value, value_min, value_max,
90 has_default, is_defined);
94 if (pack_size == 0)
PUT_ERR(
"int pack size should be > 0");
97 void *
buffer = malloc(pack_size);
104 int unpacked_value = default_value;
107 param_name, &unpacked_value, default_value, value_min, value_max,
108 has_default, is_defined);
113 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
116 if (unpacked_value !=
value)
117 PUT_ERR(
"int unpack should update destination value to source value");
127 char const * param_name =
"ival";
128 int const default_value = 42;
129 int const value_min = -1000;
130 int const value_max = 1000;
131 int const has_default = 1;
132 int const is_defined = 1;
134 int value = default_value + 1;
139 param_name, &
value, default_value, value_min, value_max,
140 has_default, is_defined);
144 if (pack_size == 0)
PUT_ERR(
"int pack size should be > 0");
147 void *
buffer = malloc(pack_size);
154 int unpacked_value = default_value;
157 param_name, &unpacked_value, default_value, value_min, value_max,
158 has_default, is_defined);
163 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
166 if (unpacked_value !=
value)
167 PUT_ERR(
"int unpack should update destination value to source value");
176static void utest_scalar_dble_roundtrip(MPI_Comm comm) {
180 char const * param_name =
"dval";
181 double const default_value = 0.0;
182 double const value_min = -1000.0;
183 double const value_max = 1000.0;
184 int const allow_nan = 0;
185 int const allow_inf = 0;
186 int const has_default = 1;
187 int const is_defined = 1;
189 double value = default_value;
194 param_name, &
value, default_value, value_min, value_max,
195 allow_nan, allow_inf, has_default, is_defined);
199 if (pack_size == 0)
PUT_ERR(
"double pack size should be > 0");
202 void *
buffer = malloc(pack_size);
209 double unpacked_value = default_value;
212 param_name, &unpacked_value, default_value, value_min, value_max,
213 allow_nan, allow_inf, has_default, is_defined);
218 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
221 if (unpacked_value !=
value)
222 PUT_ERR(
"double unpack should update destination value to source value");
231 char const * param_name =
"dval";
232 double const default_value = 0.0;
233 double const value_min = -1000.0;
234 double const value_max = 1000.0;
235 int const allow_nan = 0;
236 int const allow_inf = 0;
237 int const has_default = 1;
238 int const is_defined = 1;
245 param_name, &
value, default_value, value_min, value_max,
246 allow_nan, allow_inf, has_default, is_defined);
250 if (pack_size == 0)
PUT_ERR(
"double pack size should be > 0");
253 void *
buffer = malloc(pack_size);
260 double unpacked_value = default_value;
263 param_name, &unpacked_value, default_value, value_min, value_max,
264 allow_nan, allow_inf, has_default, is_defined);
269 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
272 if (unpacked_value !=
value)
273 PUT_ERR(
"double unpack should update destination value to source value");
282static void utest_scalar_str_roundtrip(MPI_Comm comm) {
286 char const * param_name =
"sval";
287 char const * default_value =
"default";
288 int const allow_null = 0;
289 int const allow_empty = 0;
290 size_t const max_str_len = 128;
291 int const has_default = 1;
292 int const is_defined = 1;
294 char *
value = strdup(default_value);
298 param_name, &
value, default_value, allow_null, allow_empty,
299 max_str_len, has_default, is_defined);
303 if (pack_size == 0)
PUT_ERR(
"string pack size should be > 0");
306 void *
buffer = malloc(pack_size);
313 char * unpacked_value = strdup(default_value);
315 param_name, &unpacked_value, default_value, allow_null, allow_empty,
316 max_str_len, has_default, is_defined);
321 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
324 if (strcmp(unpacked_value,
value) != 0)
325 PUT_ERR(
"string unpack should update destination value to source value");
331 free(unpacked_value);
336 char const * param_name =
"sval";
337 char const * default_value =
"default";
338 int const allow_null = 0;
339 int const allow_empty = 0;
340 size_t const max_str_len = 128;
341 int const has_default = 1;
342 int const is_defined = 1;
345 char *
value = strdup(
346 "this-is-a-long-string-value-that-forces-buffer-growth-in-pack-size");
350 param_name, &
value, default_value, allow_null, allow_empty,
351 max_str_len, has_default, is_defined);
355 if (pack_size == 0)
PUT_ERR(
"string pack size should be > 0");
358 void *
buffer = malloc(pack_size);
365 char * unpacked_value = strdup(default_value);
367 param_name, &unpacked_value, default_value, allow_null, allow_empty,
368 max_str_len, has_default, is_defined);
373 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
376 if (strcmp(unpacked_value,
value) != 0)
377 PUT_ERR(
"string unpack should update destination value to source value");
383 free(unpacked_value);
388static void utest_struct_roundtrip(MPI_Comm comm) {
392 char const * param_name =
"struct";
398 }
data = {42, 1, 3.14, strdup(
"custom")};
406 enum {NUM_SUB_PARAMS =
sizeof(sub_params) /
sizeof(sub_params[0])};
414 if (pack_size == 0)
PUT_ERR(
"struct pack size should be > 0");
417 void *
buffer = malloc(pack_size);
424 struct test_type unpacked_data = {0, 0, 0.0, strdup(
"default")};
432 "d", &unpacked_data.d, 0.0, -1000.0, 1000.0, 0, 0, 1, 1),
439 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
442 if (unpacked_data.i !=
data.i)
443 PUT_ERR(
"non-default struct child should remain default after roundtrip");
444 if (unpacked_data.b !=
data.b)
445 PUT_ERR(
"non-default bool struct child should be unpacked");
446 if (unpacked_data.d !=
data.d)
447 PUT_ERR(
"non-default double struct child should remain default after roundtrip");
448 if (strcmp(unpacked_data.s,
data.s) != 0)
449 PUT_ERR(
"non-default string struct child should be unpacked");
455 free(unpacked_data.s);
462 char const * param_name =
"struct";
468 }
data = {0, 0, 0.0, strdup(
"default")};
476 enum {NUM_SUB_PARAMS =
sizeof(sub_params) /
sizeof(sub_params[0])};
484 if (pack_size == 0)
PUT_ERR(
"struct pack size should be > 0");
487 void *
buffer = malloc(pack_size);
494 struct test_type unpacked_data = {0, 0, 0.0, strdup(
"default")};
502 "d", &unpacked_data.d, 0.0, -1000.0, 1000.0, 0, 0, 1, 1),
509 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
512 if (unpacked_data.i !=
data.i)
513 PUT_ERR(
"default struct child should remain default after roundtrip");
514 if (unpacked_data.b !=
data.b)
515 PUT_ERR(
"default bool struct child should remain default after roundtrip");
516 if (unpacked_data.d !=
data.d)
517 PUT_ERR(
"default double struct child should remain default after roundtrip");
518 if (strcmp(unpacked_data.s,
data.s) != 0)
519 PUT_ERR(
"default string struct child should remain default after roundtrip");
525 free(unpacked_data.s);
530 char const * param_name =
"struct";
536 }
data = {42, 0, 3.14, strdup(
"default")};
544 enum {NUM_SUB_PARAMS =
sizeof(sub_params) /
sizeof(sub_params[0])};
552 if (pack_size == 0)
PUT_ERR(
"struct pack size should be > 0");
555 void *
buffer = malloc(pack_size);
562 struct test_type unpacked_data = {0, 0, 0.0, strdup(
"default")};
570 "d", &unpacked_data.d, 0.0, -1000.0, 1000.0, 0, 0, 1, 1),
577 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
580 if (unpacked_data.i !=
data.i)
581 PUT_ERR(
"default struct child should remain default after roundtrip");
582 if (unpacked_data.b !=
data.b)
583 PUT_ERR(
"default bool struct child should remain default after roundtrip");
584 if (unpacked_data.d !=
data.d)
585 PUT_ERR(
"default double struct child should remain default after roundtrip");
586 if (strcmp(unpacked_data.s,
data.s) != 0)
587 PUT_ERR(
"default string struct child should remain default after roundtrip");
593 free(unpacked_data.s);
598static void utest_list_roundtrip(MPI_Comm comm) {
601 char const * param_name =
"list";
612 PUT_ERR(
"list append for first source element failed");
614 PUT_ERR(
"list append for second source element failed");
622 if (pack_size == 0)
PUT_ERR(
"list pack size should be > 0");
625 void *
buffer = malloc(pack_size);
632 void ** unpacked_list = NULL;
635 param_name, &unpacked_list, utest_list_default_factory, NULL);
640 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
646 PUT_ERR(
"list unpack should create two destination elements");
647 for (
size_t i = 0;
i < count; ++
i) {
648 if (*(
int*)list[i] != *(
int*)unpacked_list[i])
649 PUT_ERR(
"list unpack should preserve elements");
655 utest_free_int_list(list);
656 utest_free_int_list(unpacked_list);
661static void utest_empty_list_roundtrip(MPI_Comm comm) {
664 char const * param_name =
"list";
673 if (pack_size == 0)
PUT_ERR(
"empty-list pack size should be > 0");
676 void *
buffer = malloc(pack_size);
683 void ** unpacked_list = NULL;
686 param_name, &unpacked_list, utest_list_default_factory, NULL);
691 unpacked_param,
buffer, (
int)pack_size, &pos, comm, __func__);
696 PUT_ERR(
"empty list roundtrip should keep destination list empty");
701 utest_free_int_list(list);
702 utest_free_int_list(unpacked_list);
void yac_param_delete(struct yac_param *param)
int yac_param_append(struct yac_param *param, struct yac_param **sub_param)
Append a new default subparameter to a list parameter and return a handle to it.
int yac_param_set_scalar_int(struct yac_param *param, int value)
Set the value of a scalar parameter as an int.
int yac_param_get_sub_count(const struct yac_param *param, size_t *count)
Get the number of subparameters.
Public interface for YAC parameters.
struct yac_param * yac_param_bool_new(const char *name, int *value_ptr, int default_value, int has_default, int is_defined)
Create a bool parameter (backed by int)
struct yac_param * yac_param_dble_new(const char *name, double *value_ptr, double default_value, double value_min, double value_max, int allow_nan, int allow_inf, int has_default, int is_defined)
Create a double parameter.
struct yac_param * yac_param_int_new(const char *name, int *value_ptr, int default_value, int value_min, int value_max, int has_default, int is_defined)
Create an int parameter.
struct yac_param * yac_param_list_new(const char *name, void ***list, yac_param_factory_ptr default_factory, yac_param_element_get_param_ptr element_get_param)
Create a new list parameter.
struct yac_param * yac_param_str_new(const char *name, char **value_ptr, const char *default_value, int allow_NULL, int allow_empty, size_t max_str_len, int has_default, int is_defined)
Create a string parameter.
struct yac_param * yac_param_struct_new(const char *name, struct yac_param **subparams, size_t subparam_count)
Create a new struct parameter with a given name and subparameters.
void yac_param_pack(struct yac_param *param, void *buffer, int buffer_size, int *position, MPI_Comm comm, char const *routine)
Public function to pack a parameter record into a buffer.
size_t yac_param_get_pack_size(struct yac_param *param, MPI_Comm comm, char const *routine)
Public function to get the size in bytes required to pack a parameter record.
void yac_param_unpack(struct yac_param *param, void const *buffer, int buffer_size, int *position, MPI_Comm comm, char const *routine)
Unpack a parameter from an MPI buffer and apply its values to the given parameter.