Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/guarded_options.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ public:
/// Returns a list of variables with read-only permission but which
/// have not been accessed using the `get()` method.
std::map<std::string, Regions> unreadItems() const {
#if CHECKLEVEL >= 1
#if CHECKLEVEL >= 999
return *unread_variables;
#else
throw BoutException(
"Reading of items in GuardedOptions is not tracked when CHECKLEVEL < 1");
"Reading of items in GuardedOptions is currently disabled");
#endif
}

/// Returns a list of variables with read-write permission but which
/// have not been accessed using the `getWritable()` method.
std::map<std::string, Regions> unwrittenItems() const {
#if CHECKLEVEL >= 1
#if CHECKLEVEL >= 999
return *unwritten_variables;
#else
throw BoutException(
"Reading of items in GuardedOptions is not tracked when CHECKLEVEL < 1");
"Reading of items in GuardedOptions is currently disabled");
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/component.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ std::unique_ptr<Component> Component::create(const std::string &type,
void Component::transform(Options& state) {
GuardedOptions guarded(&state, &state_variable_access);
transform_impl(guarded);
#if CHECKLEVEL >= 1
#if CHECKLEVEL >= 999
for (auto& [varname, region] : guarded.unreadItems()) {
output_warn.write("Did not read from state variable {} in region(s) {}\n", varname,
Permissions::regionNames(region));
Expand Down
6 changes: 5 additions & 1 deletion src/guarded_options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ GuardedOptions::GuardedOptions(Options* options, Permissions* permissions)
throw BoutException(
"Can not construct GuardedOptions with null permissions pointer.");
}
#if CHECKLEVEL >= 1
#if CHECKLEVEL >= 999
*unread_variables = permissions->getVariablesWithPermission(PermissionTypes::Read);
// Only add variables with permission ReadIfSet to
// unread_variables if they are already present in the options
Expand Down Expand Up @@ -81,7 +81,9 @@ const Options& GuardedOptions::get([[maybe_unused]] Regions region) const {
throw BoutException(
"Only have permission to read {} if it is already set, which it is not.", name);
}
#if CHECKLEVEL >= 999
updateAccessRecords(*unread_variables, varname, region);
#endif
return *options;
}
throw BoutException("Do not have read permission for {}.", name);
Expand All @@ -95,7 +97,9 @@ Options& GuardedOptions::getWritable([[maybe_unused]] Regions region) {
const std::string name = options->str();
auto [access, varname] = permissions->canAccess(name, PermissionTypes::Write, region);
if (access) {
#if CHECKLEVEL >= 999
updateAccessRecords(*unwritten_variables, varname, region);
#endif
return *options;
}
throw BoutException("Do not have write permission for {}.", options->str());
Expand Down
34 changes: 33 additions & 1 deletion tests/unit/test_guarded_options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ TEST_F(GuardedOptionsTests, TestGetWritableException) {
guarded_opts["species"]["d"]["pressure_suffix"].getWritable(Regions::Interior),
BoutException);
}
#endif

TEST_F(GuardedOptionsTests, TestUnreadItems) {
const std::map<std::string, Regions> expected1 = {
Expand All @@ -154,22 +155,38 @@ TEST_F(GuardedOptionsTests, TestUnreadItems) {
const std::map<std::string, Regions> expected3 = {{"species:d", Regions::All}};
const std::map<std::string, Regions> expected4;

#if CHECKLEVEL >= 999
EXPECT_EQ(guarded_opts.unreadItems(), expected1);
#else
EXPECT_THROW(guarded_opts.unreadItems(), BoutException);
#endif

guarded_opts["species:he:density"].get(Regions::Interior);
guarded_opts["species:d:pressure"].get(Regions::Interior);
guarded_opts["species:d:collision_frequencies:d_d_coll"].getWritable(
Regions::Boundaries);
#if CHECKLEVEL >= 999
EXPECT_EQ(guarded_opts.unreadItems(), expected2);
#else
EXPECT_THROW(guarded_opts.unreadItems(), BoutException);
#endif

guarded_opts["species"]["he"]["charge"].get();
guarded_opts["species"]["he"]["density"].get();
guarded_opts["species:he:velocity"].get(Regions::Boundaries);
EXPECT_FALSE(guarded_opts["unused"]["option"].get().isSet());
#if CHECKLEVEL >= 999
EXPECT_EQ(guarded_opts.unreadItems(), expected3);
#else
EXPECT_THROW(guarded_opts.unreadItems(), BoutException);
#endif

guarded_opts["species:d:velocity"].get();
#if CHECKLEVEL >= 999
EXPECT_EQ(guarded_opts.unreadItems(), expected4);
#else
EXPECT_THROW(guarded_opts.unreadItems(), BoutException);
#endif
}

TEST_F(GuardedOptionsTests, TestUnwrittenItems) {
Expand All @@ -183,22 +200,37 @@ TEST_F(GuardedOptionsTests, TestUnwrittenItems) {
{"species:d:pressure", Regions::Interior}};
const std::map<std::string, Regions> expected3;

#if CHECKLEVEL >= 999
EXPECT_EQ(guarded_opts.unwrittenItems(), expected1);
#else
EXPECT_THROW(guarded_opts.unwrittenItems(), BoutException);
#endif

guarded_opts["species:he:pressure"].get(Regions::Interior);
guarded_opts["species:he:collision_frequency"].get();
#if CHECKLEVEL >= 999
EXPECT_EQ(guarded_opts.unwrittenItems(), expected1);
#else
EXPECT_THROW(guarded_opts.unwrittenItems(), BoutException);
#endif

guarded_opts["species"]["he"]["pressure"].getWritable(Regions::Interior);
guarded_opts["species:d:collision_frequencies:d_d_coll"].getWritable(
Regions::Boundaries);
#if CHECKLEVEL >= 999
EXPECT_EQ(guarded_opts.unwrittenItems(), expected2);
#else
EXPECT_THROW(guarded_opts.unwrittenItems(), BoutException);
#endif

guarded_opts["species:he:collision_frequency"].getWritable();
guarded_opts["species:d:pressure"].getWritable(Regions::Interior);
#if CHECKLEVEL >= 999
EXPECT_EQ(guarded_opts.unwrittenItems(), expected3);
}
#else
EXPECT_THROW(guarded_opts.unwrittenItems(), BoutException);
#endif
}

TEST_F(GuardedOptionsTests, TestNullOptions) {
EXPECT_THROW(GuardedOptions(nullptr, &permissions), BoutException);
Expand Down