From d4264bbfb4bbc0c016cfdb5704857981e6252344 Mon Sep 17 00:00:00 2001 From: Reid Kawaja <74506315+reidkwja@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:17:31 +0000 Subject: [PATCH 1/4] integrating-base-class --- test/gtest/test_base.hpp | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 test/gtest/test_base.hpp diff --git a/test/gtest/test_base.hpp b/test/gtest/test_base.hpp new file mode 100644 index 0000000000..3cd3d61ee1 --- /dev/null +++ b/test/gtest/test_base.hpp @@ -0,0 +1,89 @@ +/******************************************************************************* + * + * MIT License + * + * Copyright (c) 2024 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +#include +#include +#include +#include +#include + +class TestBase : public ::testing::Test +{ +protected: + std::map original_env_vars; + + void SetUp() override + { + prng::reset_seed(); + + save_env_vars(); + } + + void TearDown() override { restore_env_vars(); } + +private: + void save_env_vars() + { + const char* env_vars[] = {"CMAKE_CURRENT_BINARY_DIR", + "MIOPEN_TEST_MLIR", + "MIOPEN_TEST_COMPOSABLEKERNEL", + "CODECOV_TEST", + "MIOPEN_TEST_DBSYNC", + "MIOPEN_TEST_CONV", + "MIOPEN_TEST_DEEPBENCH", + "MIOPEN_DEBUG_TUNING_ITERATIONS_MAX", + "MIOPEN_TEST_WITH_MIOPENDRIVER", + nullptr}; + + for(const char** var = env_vars; *var != nullptr; ++var) + { + const char* value = std::getenv(*var); + if(value) + { + original_env_vars[*var] = value; + } + else + { + original_env_vars[*var] = ""; + } + } + } + + void restore_env_vars() + { + for(const auto& [key, value] : original_env_vars) + { + if(value.empty()) + { + unsetenv(key.c_str()); + } + else + { + setenv(key.c_str(), value.c_str(), 1); + } + } + } +}; From e52e3c53f306a0aa94ced2ec2203ec218722b76f Mon Sep 17 00:00:00 2001 From: Reid Kawaja <74506315+reidkwja@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:02:19 +0000 Subject: [PATCH 2/4] string-vector env vars --- test/gtest/test_base.hpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/gtest/test_base.hpp b/test/gtest/test_base.hpp index 3cd3d61ee1..8f380997ad 100644 --- a/test/gtest/test_base.hpp +++ b/test/gtest/test_base.hpp @@ -28,6 +28,7 @@ #include #include #include +#include #include class TestBase : public ::testing::Test @@ -47,27 +48,26 @@ class TestBase : public ::testing::Test private: void save_env_vars() { - const char* env_vars[] = {"CMAKE_CURRENT_BINARY_DIR", - "MIOPEN_TEST_MLIR", - "MIOPEN_TEST_COMPOSABLEKERNEL", - "CODECOV_TEST", - "MIOPEN_TEST_DBSYNC", - "MIOPEN_TEST_CONV", - "MIOPEN_TEST_DEEPBENCH", - "MIOPEN_DEBUG_TUNING_ITERATIONS_MAX", - "MIOPEN_TEST_WITH_MIOPENDRIVER", - nullptr}; + std::vector env_vars = {"CMAKE_CURRENT_BINARY_DIR", + "MIOPEN_TEST_MLIR", + "MIOPEN_TEST_COMPOSABLEKERNEL", + "CODECOV_TEST", + "MIOPEN_TEST_DBSYNC", + "MIOPEN_TEST_CONV", + "MIOPEN_TEST_DEEPBENCH", + "MIOPEN_DEBUG_TUNING_ITERATIONS_MAX", + "MIOPEN_TEST_WITH_MIOPENDRIVER"}; - for(const char** var = env_vars; *var != nullptr; ++var) + for(const auto& var : env_vars) { - const char* value = std::getenv(*var); + const char* value = std::getenv(var.c_str()); if(value) { - original_env_vars[*var] = value; + original_env_vars[var] = value; } else { - original_env_vars[*var] = ""; + original_env_vars[var] = ""; } } } From 2d2b936b0d25c44a7e29b5776d14c1a5d3d35293 Mon Sep 17 00:00:00 2001 From: Reid Kawaja <74506315+reidkwja@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:00:56 +0000 Subject: [PATCH 3/4] refactoring w/ miopen/env --- test/gtest/test_base.hpp | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/test/gtest/test_base.hpp b/test/gtest/test_base.hpp index 8f380997ad..e68d708884 100644 --- a/test/gtest/test_base.hpp +++ b/test/gtest/test_base.hpp @@ -24,8 +24,8 @@ * *******************************************************************************/ +#include #include -#include #include #include #include @@ -39,7 +39,6 @@ class TestBase : public ::testing::Test void SetUp() override { prng::reset_seed(); - save_env_vars(); } @@ -60,15 +59,8 @@ class TestBase : public ::testing::Test for(const auto& var : env_vars) { - const char* value = std::getenv(var.c_str()); - if(value) - { - original_env_vars[var] = value; - } - else - { - original_env_vars[var] = ""; - } + auto value = miopen::env::getEnvironmentVariable(var); + original_env_vars[var] = value.value_or(""); } } @@ -78,11 +70,11 @@ class TestBase : public ::testing::Test { if(value.empty()) { - unsetenv(key.c_str()); + miopen::env::clearEnvironmentVariable(key); } else { - setenv(key.c_str(), value.c_str(), 1); + miopen::env::setEnvironmentVariable(key, value); } } } From 70d2ec5b22bcbf0ed2f911ac3efe13478d064fbb Mon Sep 17 00:00:00 2001 From: Reid Kawaja <74506315+reidkwja@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:00:44 +0000 Subject: [PATCH 4/4] template_class --- test/gtest/test_base.hpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/test/gtest/test_base.hpp b/test/gtest/test_base.hpp index e68d708884..883f345933 100644 --- a/test/gtest/test_base.hpp +++ b/test/gtest/test_base.hpp @@ -31,6 +31,7 @@ #include #include +template class TestBase : public ::testing::Test { protected: @@ -47,15 +48,7 @@ class TestBase : public ::testing::Test private: void save_env_vars() { - std::vector env_vars = {"CMAKE_CURRENT_BINARY_DIR", - "MIOPEN_TEST_MLIR", - "MIOPEN_TEST_COMPOSABLEKERNEL", - "CODECOV_TEST", - "MIOPEN_TEST_DBSYNC", - "MIOPEN_TEST_CONV", - "MIOPEN_TEST_DEEPBENCH", - "MIOPEN_DEBUG_TUNING_ITERATIONS_MAX", - "MIOPEN_TEST_WITH_MIOPENDRIVER"}; + std::vector env_vars = DerivedTest::get_env_vars(); for(const auto& var : env_vars) {