Skip to content

Commit

Permalink
Merge pull request #353 from scottaiton/fclaw_conext_changes
Browse files Browse the repository at this point in the history
Modify behavior of fclaw_context_get* functions
  • Loading branch information
scottaiton authored Jul 19, 2024
2 parents d8d6822 + 62b4b84 commit 80b20ac
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
22 changes: 18 additions & 4 deletions src/fclaw_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ typedef struct value
double d;
} value;

int initializing;

void *pointer;

} value_t;
Expand Down Expand Up @@ -81,10 +83,11 @@ context_destroy(void *data)
}

static void
reset_pointers(const char *key, void *data, void *user)
reset_values(const char *key, void *data, void *user)
{
value_t *value = (value_t *)data;
value->pointer = NULL;
value->initializing = 1;
}

fclaw_context_t* fclaw_context_get(fclaw_global_t *glob, const char *name)
Expand All @@ -105,7 +108,7 @@ fclaw_context_t* fclaw_context_get(fclaw_global_t *glob, const char *name)
fclaw_abortf("fclaw_context_get: Context needs to be saved before it can be retrieved again\n");
}
context->saved = 0;
fclaw_pointer_map_iterate(context->values, reset_pointers, NULL);
fclaw_pointer_map_iterate(context->values, reset_values, NULL);
}

return context;
Expand All @@ -123,12 +126,17 @@ void fclaw_context_get_int(fclaw_context_t *context,
{
fclaw_abortf("fclaw_context_get_int: Value %s is not an int\n", name);
}
*value = v->value.i;
if(v->initializing)
{
*value = v->value.i;
v->initializing = 0;
}
}
else if (context->initializing)
{
v = FCLAW_ALLOC(value_t, 1);
v->type = FCLAW_CONTEXT_INT;
v->initializing = 0;
fclaw_pointer_map_insert(context->values, name, v, value_destroy);
}
else
Expand All @@ -150,12 +158,17 @@ void fclaw_context_get_double(fclaw_context_t *context,
{
fclaw_abortf("fclaw_context_get_double: Value %s is not a double\n", name);
}
*value = v->value.d;
if(v->initializing)
{
*value = v->value.d;
v->initializing = 0;
}
}
else if(context->initializing)
{
v = FCLAW_ALLOC(value_t, 1);
v->type = FCLAW_CONTEXT_DOUBLE;
v->initializing = 0;
fclaw_pointer_map_insert(context->values, name, v, value_destroy);
}
else
Expand Down Expand Up @@ -290,6 +303,7 @@ size_t context_unpack(fclaw_global_t *glob, char *buffer, void *data)
{
SC_ABORT_NOT_REACHED();
}
value->initializing = 1;
value->pointer = NULL;
fclaw_pointer_map_insert(context->values, key, value, value_destroy);
FCLAW_FREE(key);
Expand Down
20 changes: 12 additions & 8 deletions src/fclaw_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,31 @@ fclaw_context_t* fclaw_context_get(fclaw_global_t *glob, const char *name);

/**
* @brief Get an integer value from the context object.
* If the value does not exist, the current value does not change.
*
* If the context isn't new and the value does not exist, an error message is printed and the program aborts.
* This function retrieves an integer value from the context object. If the value does not exist in the context,
* the current value remains unchanged. The only case where this function modifies the value is if the context object
* already existed in the global context and this is the first call after fclaw_context_get().
* If the context is not new and the value does not exist, an error message is printed and the program aborts.
*
* @param context the context object
* @param name the name of the value
* @param value a pointer the value, if the value does not exist, the value is not changed
* @param value a pointer to the value
*/
void fclaw_context_get_int(fclaw_context_t *context,
const char *name,
int *value);

/**
* @brief Get a double value from the context object.
* If the value does not exist, the current value does not change.
*
* If the context isn't new and the value does not exist, an error message is printed and the program aborts.
*
*
* This function retrieves an integer value from the context object. If the value does not exist in the context,
* the current value remains unchanged. The only case where this function modifies the value is if the context object
* already existed in the global context and this is the first call after fclaw_context_get().
* If the context is not new and the value does not exist, an error message is printed and the program aborts.
*
* @param context the context object
* @param name the name of the value
* @param value a pointer the value, if the value does not exist, the value is not changed
* @param value a pointer to the value
*/
void fclaw_context_get_double(fclaw_context_t *context,
const char *name,
Expand Down
62 changes: 62 additions & 0 deletions src/fclaw_context.h.TEST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ TEST_CASE("fclaw_context_get_int new context")
}
}

TEST_CASE("fclaw_context_get_int called twice on new context")
{
for(int default_value : {-100, 0, 42})
for(int changed_value : {-100, 0, 42})
{
fclaw_global_t* glob = fclaw_global_new_comm(sc_MPI_COMM_SELF, 1, 0);
fclaw_context_t *context = fclaw_context_get(glob, "test");

int value = default_value;
fclaw_context_get_int(context, "test", &value);
CHECK_EQ(value, default_value);

value = changed_value;
fclaw_context_get_int(context, "test", &value);
CHECK_EQ(value, changed_value);

fclaw_global_destroy(glob);
}
}

TEST_CASE("fclaw_context_get_int new context two values")
{
for(int default_value1 : {-100, 0, 42})
Expand Down Expand Up @@ -126,12 +146,18 @@ TEST_CASE("fclaw_context_get_int existing context")
CHECK_EQ(value1, default_value);

fclaw_context_save(context);

context = fclaw_context_get(glob, "test");

int value2 = 0;
fclaw_context_get_int(context, "test", &value2);
CHECK_EQ(value2, default_value);

value2 = changed_value;
fclaw_context_save(context);

context = fclaw_context_get(glob, "test");

int value3 = 0;
fclaw_context_get_int(context, "test", &value3);
CHECK_EQ(value3, changed_value);
Expand Down Expand Up @@ -232,6 +258,26 @@ TEST_CASE("fclaw_context_get_double new context")
}
}

TEST_CASE("fclaw_context_get_double called twice on new context")
{
for(double default_value : {-100, 0, 42})
for(double changed_value : {-100, 0, 42})
{
fclaw_global_t* glob = fclaw_global_new_comm(sc_MPI_COMM_SELF, 1, 0);
fclaw_context_t *context = fclaw_context_get(glob, "test");

double value = default_value;
fclaw_context_get_double(context, "test", &value);
CHECK_EQ(value, default_value);

value = changed_value;
fclaw_context_get_double(context, "test", &value);
CHECK_EQ(value, changed_value);

fclaw_global_destroy(glob);
}
}

TEST_CASE("fclaw_context_get_double new context two values")
{
for(double default_value1 : {-100, 0, 42})
Expand Down Expand Up @@ -263,12 +309,19 @@ TEST_CASE("fclaw_context_get_double existing context")
CHECK_EQ(value, default_value);

fclaw_context_save(context);

context = fclaw_context_get(glob, "test");

double value2 = 0;
fclaw_context_get_double(context, "test", &value2);
CHECK_EQ(value2, default_value);

value2 = changed_value;

fclaw_context_save(context);

context = fclaw_context_get(glob, "test");

double value3 = 0;
fclaw_context_get_double(context, "test", &value3);
CHECK_EQ(value3, changed_value);
Expand Down Expand Up @@ -297,6 +350,9 @@ TEST_CASE("fclaw_context_get_double existing context two values")
CHECK_EQ(value2, default_value2);

fclaw_context_save(context);

context = fclaw_context_get(glob, "test");

value = 0; value2 = 0;

fclaw_context_get_double(context, "test1", &value);
Expand All @@ -309,6 +365,9 @@ TEST_CASE("fclaw_context_get_double existing context two values")
value2 = changed_value2;

fclaw_context_save(context);

context = fclaw_context_get(glob, "test");

value = 0; value2 = 0;

fclaw_context_get_double(context, "test1", &value);
Expand Down Expand Up @@ -375,6 +434,9 @@ TEST_CASE("fclaw_context_get_double and fclaw_context_get_int called for same va
CHECK_EQ(value_double, default_double);

fclaw_context_save(context);

context = fclaw_context_get(glob, "test");

value = 0; value_double = 0;

fclaw_context_get_int(context, "test", &value);
Expand Down

0 comments on commit 80b20ac

Please sign in to comment.