From e6852fa6a88d6ea112f4c1278e4f64c88cfbdbd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=B6sch?= <48126478+boeschf@users.noreply.github.com> Date: Mon, 27 Jun 2022 15:37:33 +0200 Subject: [PATCH 1/3] read environment variables --- src/CMakeLists.txt | 1 + src/env_vars.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++ src/env_vars.hpp | 37 ++++++++++++++++++++++ test/CMakeLists.txt | 29 +++++++++++++++-- test/test_env_vars.cpp | 44 ++++++++++++++++++++++++++ 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 src/env_vars.cpp create mode 100644 src/env_vars.hpp create mode 100644 test/test_env_vars.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 57716f4c..90afdb03 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..406d2c9b 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) @@ -21,16 +24,38 @@ function(compile_test t_) target_link_libraries(${t} PUBLIC oomph) 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}) +# compile_test(${t}) +#endforeach() + # 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() + # --------------------------------------------------------------------- # 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; + } +} + + From 17c496a8fcdb0689b49099ac3c6290de859b2b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=B6sch?= <48126478+boeschf@users.noreply.github.com> Date: Mon, 27 Jun 2022 15:39:18 +0200 Subject: [PATCH 2/3] removed comments --- test/CMakeLists.txt | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 406d2c9b..a063d4ce 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,13 +24,8 @@ function(compile_test t_) target_link_libraries(${t} PUBLIC oomph) 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}) -# compile_test(${t}) -#endforeach() - # compile an object library for each test +# tests will be compiled only once and then linked against all enabled oomph backends list(APPEND all_tests ${serial_tests} ${parallel_tests}) list(REMOVE_DUPLICATES all_tests) foreach(t ${all_tests}) From aaa0e98013193dd78725c01ca620c0d0a104f92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=B6sch?= <48126478+boeschf@users.noreply.github.com> Date: Mon, 27 Jun 2022 15:39:32 +0200 Subject: [PATCH 3/3] removed comments --- test/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a063d4ce..70000393 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -32,7 +32,6 @@ foreach(t ${all_tests}) compile_test(${t}) endforeach() - # --------------------------------------------------------------------- # link and register tests # ---------------------------------------------------------------------