Skip to content
Open
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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
71 changes: 71 additions & 0 deletions src/env_vars.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <algorithm>
#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<std::pair<std::string_view, std::string_view>>
env_vars::key_contains(char const* k) const
{
std::vector<std::pair<std::string_view, std::string_view>> 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<std::string_view>
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
37 changes: 37 additions & 0 deletions src/env_vars.hpp
Original file line number Diff line number Diff line change
@@ -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 <map>
#include <string_view>
#include <optional>
#include <vector>

namespace oomph
{

class env_vars
{
private:
using map_type = std::map<std::string_view, std::string_view>;

map_type m_vars;

public:
env_vars();

void print() const noexcept;

std::vector<std::pair<std::string_view, std::string_view>> key_contains(char const* k) const;

std::optional<std::string_view> find(char const* k) const noexcept;
};

} // namespace oomph
21 changes: 20 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,14 +26,30 @@ 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()

# ---------------------------------------------------------------------
# link and register tests
# ---------------------------------------------------------------------

function(reg_serial_test t)
add_executable(${t} $<TARGET_OBJECTS:${t}_obj>)
oomph_target_compile_options(${t})
target_link_libraries(${t} PRIVATE gtest_main)
target_link_libraries(${t} PRIVATE oomph_common)
add_test(
NAME ${t}
COMMAND $<TARGET_FILE:${t}>)
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})
Expand Down
44 changes: 44 additions & 0 deletions test/test_env_vars.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>
#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;
}
}