-
Notifications
You must be signed in to change notification settings - Fork 29
Make KeepDropSwitch fail earlier on invalid inputs #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ec447e7
Add reproducer as test
tmadlener e754d28
Add some unittesting for KeepDropSwitch
tmadlener 5dbd09c
Move KeepDropSwitch into k4FWCore namespace
tmadlener bfcfe7c
Check the commands in the KeepDropSwitch constructor
tmadlener 15ac8fd
Adapt test pass condition
tmadlener 592cd81
Remove branch
tmadlener e377fea
Include license notice
tmadlener 415d974
Mark check const as it can be const
tmadlener e3a00cc
Use std::format instead of fmt lib
tmadlener 8d97673
Remove deprecated old name
tmadlener 3390db4
Don't catch and rethrow
tmadlener 8b731ea
Simplify conditional setting of flag
tmadlener d2a3f35
Keep backwards compatibility for gcc11 for now
tmadlener e70a079
Remove obsolete test of KeepDropSwitch via Gaudi
tmadlener File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,17 @@ | |
| */ | ||
| #include "k4FWCore/KeepDropSwitch.h" | ||
|
|
||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <stdexcept> | ||
| #ifdef __cpp_lib_format | ||
| #include <format> | ||
| using std::format; | ||
| #else | ||
| #include <fmt/format.h> | ||
| using fmt::format; | ||
| #endif | ||
|
|
||
| namespace { | ||
| int wildcmp(const char* wild, const char* string) { | ||
| // Written by Jack Handy - <A href="mailto:[email protected]">[email protected]</A> | ||
| const char *cp = nullptr, *mp = nullptr; | ||
|
|
@@ -63,6 +70,19 @@ std::vector<std::string> split(const std::string& s, char delim) { | |
| } | ||
| return elems; | ||
| } | ||
| } // namespace | ||
|
|
||
| namespace k4FWCore { | ||
| KeepDropSwitch::KeepDropSwitch(const InputCommands& cmds) { | ||
| m_commandlines.reserve(cmds.size()); | ||
| for (const auto& cmdLine : cmds) { | ||
| auto [cmd, arg] = extractCommand(cmdLine); | ||
| if (cmd == Cmd::INVALID) { | ||
| throw std::invalid_argument(format("'{}' is not a valid command for the KeepDropSwitch", cmdLine)); | ||
| } | ||
| m_commandlines.emplace_back(cmd, std::move(arg)); | ||
| } | ||
| } | ||
|
|
||
| bool KeepDropSwitch::isOn(const std::string& astring) const { | ||
| auto im = m_cache.find(astring); | ||
|
|
@@ -75,44 +95,29 @@ bool KeepDropSwitch::isOn(const std::string& astring) const { | |
| } | ||
| } | ||
|
|
||
| bool KeepDropSwitch::getFlag(const std::string& astring) const { | ||
| bool KeepDropSwitch::getFlag(const std::string& astring) const noexcept { | ||
| bool flag = true; | ||
| for (const auto& cmdline : m_commandlines) { | ||
| std::vector<std::string> words = split(cmdline, ' '); | ||
| if (words.size() != 2) { | ||
| std::ostringstream msg; | ||
| msg << "malformed command string : " << cmdline; | ||
| throw std::invalid_argument(msg.str()); | ||
| } | ||
| std::string cmd = words[0]; | ||
| std::string pattern = words[1]; | ||
| Cmd theCmd = UNKNOWN; | ||
| if (cmd == "keep") | ||
| theCmd = KEEP; | ||
| else if (cmd == "drop") | ||
| theCmd = DROP; | ||
| else { | ||
| std::ostringstream msg; | ||
| msg << "malformed command in line: " << std::endl; | ||
| msg << cmdline << std::endl; | ||
| msg << "should be keep or drop, lower case" << std::endl; | ||
| throw std::invalid_argument(msg.str()); | ||
| for (const auto& [cmd, pattern] : m_commandlines) { | ||
| if (wildcmp(pattern.c_str(), astring.c_str())) { | ||
| flag = (cmd == Cmd::KEEP); | ||
| } | ||
| bool match = wildcmp(pattern.c_str(), astring.c_str()); | ||
| if (not match) | ||
| continue; | ||
| else if (theCmd == KEEP) | ||
| flag = true; | ||
| else | ||
| flag = false; | ||
| } | ||
| return flag; | ||
| } | ||
|
|
||
| KeepDropSwitch::Cmd KeepDropSwitch::extractCommand(const std::string& cmdline) const { | ||
| KeepDropSwitch::OutputCommand KeepDropSwitch::extractCommand(const std::string& cmdline) const { | ||
| auto words = split(cmdline, ' '); | ||
| for (auto& word : words) | ||
| std::cout << "'" << word << "' "; | ||
| std::cout << std::endl; | ||
| return UNKNOWN; | ||
| if (words.size() != 2) { | ||
| return {Cmd::INVALID, ""}; | ||
| } | ||
| if (words[0] == "keep") { | ||
| return {Cmd::KEEP, words[1]}; | ||
| } | ||
| if (words[0] == "drop") { | ||
| return {Cmd::DROP, words[1]}; | ||
| } | ||
|
|
||
| return {Cmd::INVALID, ""}; | ||
| } | ||
|
|
||
| } // namespace k4FWCore | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #[[ | ||
| Copyright (c) 2014-2024 Key4hep-Project. | ||
|
|
||
| This file is part of Key4hep. | ||
| See https://key4hep.github.io/key4hep-doc/ for further info. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ]] | ||
|
|
||
| add_subdirectory(k4FWCoreTest) | ||
| add_subdirectory(unittests) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #[[ | ||
| Copyright (c) 2014-2024 Key4hep-Project. | ||
|
|
||
| This file is part of Key4hep. | ||
| See https://key4hep.github.io/key4hep-doc/ for further info. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ]] | ||
|
|
||
| set(USE_EXTERNAL_CATCH2 AUTO CACHE STRING "Link against an external Catch2 v3 static library, otherwise build it locally") | ||
| set_property(CACHE USE_EXTERNAL_CATCH2 PROPERTY STRINGS AUTO ON OFF) | ||
|
|
||
| set(CATCH2_MIN_VERSION 3.5.0) | ||
| if(USE_EXTERNAL_CATCH2) | ||
| if (USE_EXTERNAL_CATCH2 STREQUAL AUTO) | ||
| find_package(Catch2 ${CATCH2_MIN_VERSION}) | ||
| else() | ||
| find_package(Catch2 ${CATCH2_MIN_VERSION} REQUIRED) | ||
| endif() | ||
| endif() | ||
|
|
||
| if(NOT Catch2_FOUND) | ||
| message(STATUS "Fetching local copy of Catch2 library for unit-tests...") | ||
| # Build Catch2 with the default flags, to avoid generating warnings when we | ||
| # build it | ||
| set(CXX_FLAGS_CMAKE_USED ${CMAKE_CXX_FLAGS}) | ||
| set(CMAKE_CXX_FLAGS ${CXX_FLAGS_CMAKE_DEFAULTS}) | ||
| Include(FetchContent) | ||
| FetchContent_Declare( | ||
| Catch2 | ||
| GIT_REPOSITORY https://github.com/catchorg/Catch2.git | ||
| GIT_TAG v${CATCH2_MIN_VERSION} | ||
| ) | ||
| FetchContent_MakeAvailable(Catch2) | ||
| set(CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras ${CMAKE_MODULE_PATH}) | ||
|
|
||
| # Disable clang-tidy on external contents | ||
| set_target_properties(Catch2 PROPERTIES CXX_CLANG_TIDY "") | ||
|
|
||
| # Hack around the fact, that the include directories are not declared as | ||
| # SYSTEM for the targets defined this way. Otherwise warnings can still occur | ||
| # in Catch2 code when templates are evaluated (which happens quite a bit) | ||
| get_target_property(CATCH2_IF_INC_DIRS Catch2 INTERFACE_INCLUDE_DIRECTORIES) | ||
| set_target_properties(Catch2 PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${CATCH2_IF_INC_DIRS}") | ||
|
|
||
| # Reset the flags | ||
| set(CMAKE_CXX_FLAGS ${CXX_FLAGS_CMAKE_USED}) | ||
| endif() | ||
|
|
||
| add_executable(unittests_k4FWCore KeepDropSwitchTests.cpp) | ||
| target_link_libraries(unittests_k4FWCore | ||
| PUBLIC | ||
| k4FWCore::k4FWCore | ||
| PRIVATE Catch2::Catch2WithMain | ||
| ) | ||
|
|
||
| include(Catch) | ||
| catch_discover_tests(unittests_k4FWCore | ||
| TEST_PREFIX "UT_" # make it possible to filter easily with -R ^UT | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.