Skip to content

Commit

Permalink
Fix regex (#830)
Browse files Browse the repository at this point in the history
The regex to match variables from patterns in the config file
`outer_loop.yml` was too permissive (for instance `area: "fr"` matched
the positive unsupplied energy from area `ve_fr` which was not wanted),
this PR fixes it.
  • Loading branch information
tbittar authored May 24, 2024
1 parent 979e84b commit a243698
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/OuterLoopInputDataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ OuterLoopPattern::OuterLoopPattern(std::string prefix,
* just cat ;)
*/
std::regex OuterLoopPattern::MakeRegex() const {
auto pattern = "(^" + prefix_ + ").*" + body_;
auto pattern = "(^" + prefix_ + "area<" + body_ + ">" + ")";
return std::regex(pattern);
}
const std::string &OuterLoopPattern::GetPrefix() const { return prefix_; }
Expand Down
7 changes: 5 additions & 2 deletions tests/cpp/outer_loop/outer_loop_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,15 @@ TEST_F(OuterLoopPatternTest, RegexGivenPrefixAndBody) {

auto ret_regex = o.MakeRegex();

ASSERT_EQ(std::regex_search(prefix + body, ret_regex), true);
ASSERT_EQ(std::regex_search(prefix + body, ret_regex), false);
ASSERT_EQ(std::regex_search(prefix + "::" + body + "::suffix", ret_regex),
true);
false);
ASSERT_EQ(std::regex_search(body + prefix, ret_regex), false);
ASSERT_EQ(std::regex_search(prefix + "::", ret_regex), false);
ASSERT_EQ(std::regex_search(body, ret_regex), false);
ASSERT_EQ(std::regex_search(prefix + "area<" + body + ">", ret_regex), true);
ASSERT_EQ(std::regex_search(prefix + "area<" + body + ">::suffix", ret_regex), true);
ASSERT_EQ(std::regex_search(prefix + "area<" + body + "_other_area>::suffix", ret_regex), false);
}

class OuterLoopInputFromYamlTest : public ::testing::Test {};
Expand Down

0 comments on commit a243698

Please sign in to comment.