diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 43a965da..6fed4cac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ target_sources(oomph_common PRIVATE utils.cpp) target_sources(oomph_common PRIVATE thread_id.cpp) target_sources(oomph_common PRIVATE rank_topology.cpp) +target_sources(oomph_common PRIVATE env_vars.cpp) if (OOMPH_WITH_MPI) add_subdirectory(mpi) diff --git a/src/env_vars.cpp b/src/env_vars.cpp new file mode 100644 index 00000000..01b21ccb --- /dev/null +++ b/src/env_vars.cpp @@ -0,0 +1,71 @@ +/* + * ghex-org + * + * Copyright (c) 2014-2022, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + */ + +extern char** environ; + +#include +#include +#include "./env_vars.hpp" + +namespace oomph +{ + +env_vars::env_vars() +{ + char** s = environ; + for (; *s; s++) + { + std::string_view v(*s); + auto const pos = v.find('='); + if (pos == std::string_view::npos) m_vars[v] = std::string_view(); + else + m_vars[v.substr(0, pos)] = v.substr(pos + 1); + } +} + +void +env_vars::print() const noexcept +{ + for (auto const& p : m_vars) std::cout << p.first << "=" << p.second << "\n"; + std::cout << std::endl; +} + +std::vector> +env_vars::key_contains(char const* k) const +{ + std::vector> res; + + std::string_view v(k); + auto pred = [v](auto const& p) { return p.first.find(v) != std::string_view::npos; }; + auto it = m_vars.begin(); + while (true) + { + it = std::find_if(it, m_vars.end(), pred); + if (it == m_vars.end()) break; + else + { + res.push_back(*it); + ++it; + if (it == m_vars.end()) break; + } + } + return res; +} + +std::optional +env_vars::find(char const* k) const noexcept +{ + auto it = m_vars.find(std::string_view(k)); + if (it == m_vars.end()) return {}; + else + return it->second; +} + +} // namespace oomph diff --git a/src/env_vars.hpp b/src/env_vars.hpp new file mode 100644 index 00000000..55481162 --- /dev/null +++ b/src/env_vars.hpp @@ -0,0 +1,37 @@ +/* + * ghex-org + * + * Copyright (c) 2014-2022, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + */ +#pragma once + +#include +#include +#include +#include + +namespace oomph +{ + +class env_vars +{ + private: + using map_type = std::map; + + map_type m_vars; + + public: + env_vars(); + + void print() const noexcept; + + std::vector> key_contains(char const* k) const; + + std::optional find(char const* k) const noexcept; +}; + +} // namespace oomph diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 274f9f7b..70000393 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,6 +6,9 @@ add_subdirectory(mpi_runner) # compile tests # --------------------------------------------------------------------- +# list of serial tests to be executed +set(serial_tests test_env_vars) + # list of tests to be executed set(parallel_tests test_context test_send_recv test_send_multi test_cancel test_locality) if (OOMPH_ENABLE_BARRIER) @@ -23,7 +26,9 @@ endfunction() # compile an object library for each test # tests will be compiled only once and then linked against all enabled oomph backends -foreach(t ${parallel_tests}) +list(APPEND all_tests ${serial_tests} ${parallel_tests}) +list(REMOVE_DUPLICATES all_tests) +foreach(t ${all_tests}) compile_test(${t}) endforeach() @@ -31,6 +36,20 @@ endforeach() # link and register tests # --------------------------------------------------------------------- +function(reg_serial_test t) + add_executable(${t} $) + oomph_target_compile_options(${t}) + target_link_libraries(${t} PRIVATE gtest_main) + target_link_libraries(${t} PRIVATE oomph_common) + add_test( + NAME ${t} + COMMAND $) +endfunction() + +foreach(t ${serial_tests}) + reg_serial_test(${t}) +endforeach() + # creates an executable by linking to object file and to selected oomph backend function(reg_parallel_test t_ lib n) set(t ${t_}_${lib}) diff --git a/test/test_env_vars.cpp b/test/test_env_vars.cpp new file mode 100644 index 00000000..8b0d0458 --- /dev/null +++ b/test/test_env_vars.cpp @@ -0,0 +1,44 @@ +/* + * ghex-org + * + * Copyright (c) 2014-2022, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + * + */ +#include +#include "../src/env_vars.hpp" + +TEST(env_vars, print) +{ + using namespace oomph; + + env_vars ev; + ev.print(); + + std::cout << "==========" << std::endl; + for (const auto& p : ev.key_contains("HOM")) + { + std::cout << p.first << " = " << p.second << std::endl; + } + std::cout << "==========" << std::endl; + for (const auto& p : ev.key_contains("hom")) + { + std::cout << p.first << " = " << p.second << std::endl; + } + std::cout << "==========" << std::endl; + for (const auto& p : ev.key_contains("H")) + { + std::cout << p.first << " = " << p.second << std::endl; + } + + std::cout << "==========" << std::endl; + if (auto v = ev.find("HOME")) + { + std::cout << *v << std::endl; + } +} + +