From edb99264ea3e1b7f70a4ae8f1963fa59af6cf460 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Fri, 9 Aug 2024 11:40:02 +0200 Subject: [PATCH] add test for new parameter system --- CMakeLists_files.cmake | 2 + tests/parametersystem.ini | 3 + tests/test_parametersystem.cpp | 199 +++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 tests/parametersystem.ini create mode 100644 tests/test_parametersystem.cpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index e1a7a817c..d84949490 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -330,6 +330,7 @@ list (APPEND TEST_SOURCE_FILES tests/test_multmatrixtransposed.cpp tests/test_norne_pvt.cpp tests/test_outputdir.cpp + tests/test_parametersystem.cpp tests/test_parallel_wbp_sourcevalues.cpp tests/test_parallelwellinfo.cpp tests/test_partitionCells.cpp @@ -461,6 +462,7 @@ list (APPEND TEST_DATA_FILES tests/include/test1_20x30x10.grdecl tests/include/well_vfp.ecl tests/test10.partition + tests/parametersystem.ini ) diff --git a/tests/parametersystem.ini b/tests/parametersystem.ini new file mode 100644 index 000000000..e08a4a205 --- /dev/null +++ b/tests/parametersystem.ini @@ -0,0 +1,3 @@ +SimpleParamBool=true +SimpleParamFloat=3.0 +SimpleParamString=bar diff --git a/tests/test_parametersystem.cpp b/tests/test_parametersystem.cpp new file mode 100644 index 000000000..b4d883742 --- /dev/null +++ b/tests/test_parametersystem.cpp @@ -0,0 +1,199 @@ +/* + Copyright 2024 Equinor. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ + +#include + +#include + +#define BOOST_TEST_MODULE ParameterSystemTest +#include + +namespace Opm::Parameters { + +struct SimpleParamBool { static constexpr bool value = false; }; +struct SimpleParamDouble { static constexpr double value = 1.0; }; +struct SimpleParamFloat { static constexpr float value = 2.0; }; +struct SimpleParamInt { static constexpr int value = 1; }; +struct SimpleParamString { static constexpr auto value = "foo"; }; +struct SimpleParamBoolN2 +{ + static constexpr auto name = "SimpleB2"; + static constexpr bool value = true; +}; + +} + +namespace { + +class Setting +{ +public: + Setting(const std::string& k, const std::string& v) + : key(k), value(v) + {} + + friend std::ostream& operator<<(std::ostream& os, const Setting& setting) + { + os << setting.key << "=" << setting.value << '\n'; + return os; + } + + bool operator==(const Setting& setting) const + { + return setting.key == key + && setting.value == value; + } + + bool operator !=(const Setting& setting) const + { + return !(*this == setting); + } + +private: + std::string key; + std::string value; +}; + +struct Fixture +{ + Fixture() + { + Opm::Parameters::reset(); + Opm::Parameters::Register("Simple bool parameter"); + Opm::Parameters::Register("Simpler bool parameter"); + Opm::Parameters::Register("Simple double parameter"); + Opm::Parameters::Register("Simple float parameter"); + Opm::Parameters::Register("Simple int parameter"); + Opm::Parameters::Register("Simple string parameter"); + Opm::Parameters::SetDefault(10); + Opm::Parameters::Hide(); + Opm::Parameters::endRegistration(); + } +}; + +std::string trimString(const std::string& input) +{ + std::string result(input); + result.erase(std::remove(result.begin(), result.end(), ' '), result.end()); + result.erase(std::remove(result.begin(), result.end(), '\n'), result.end()); + return result; +} + +} + +BOOST_FIXTURE_TEST_CASE(GetLists, Fixture) +{ + const char* argv[] = { + "test_parametersystem", + "--simple-param-bool=true", + "--simple-param-float=3.0", + "--simple-param-string=bar", + "--unused-param=foo", + }; + + Opm::Parameters::parseCommandLineOptions(5, argv, "", + Opm::Parameters::noPositionalParameters_); + + BOOST_CHECK_EQUAL(Opm::Parameters::IsSet(), true); + BOOST_CHECK_EQUAL(Opm::Parameters::IsSet(), true); + BOOST_CHECK_EQUAL(Opm::Parameters::IsSet(), true); + BOOST_CHECK_EQUAL(Opm::Parameters::IsSet(), false); + BOOST_CHECK_EQUAL(Opm::Parameters::IsSet(), false); + BOOST_CHECK_EQUAL(Opm::Parameters::IsSet(), false); + + using SettingMap = std::vector; + + const SettingMap set_ref = { + {"SimpleParamBool", "true"}, + {"SimpleParamFloat", "3.0"}, + {"SimpleParamString", "bar"}, + }; + + const SettingMap unused_ref = { + {"UnusedParam", "foo"}, + }; + + SettingMap set, unused; + Opm::Parameters::getLists(set, unused); + + BOOST_CHECK_EQUAL_COLLECTIONS(set.begin(), set.end(), + set_ref.begin(), set_ref.end()); + BOOST_CHECK_EQUAL_COLLECTIONS(unused.begin(), unused.end(), + unused_ref.begin(), unused_ref.end()); +} + +BOOST_FIXTURE_TEST_CASE(ParseParameterFile, Fixture) +{ + Opm::Parameters::parseParameterFile("parametersystem.ini", true); + + BOOST_CHECK_EQUAL(Opm::Parameters::Get(), true); + BOOST_CHECK_EQUAL(Opm::Parameters::Get(), 3.f); + BOOST_CHECK_EQUAL(Opm::Parameters::Get(), "bar"); + BOOST_CHECK_EQUAL(Opm::Parameters::Get(), true); + BOOST_CHECK_EQUAL(Opm::Parameters::Get(), 1.0); + BOOST_CHECK_EQUAL(Opm::Parameters::Get(), 10); +} + +BOOST_FIXTURE_TEST_CASE(PrintUsage, Fixture) +{ + std::stringstream usage; + Opm::Parameters::printUsage("", "", usage); + BOOST_CHECK_EQUAL(trimString(usage.str()), +trimString(R"( +Recognized options: + --simple-b2=BOOLEAN Simpler bool parameter. Default: true + --simple-param-bool=BOOLEAN Simple bool parameter. Default: false + --simple-param-double=SCALAR Simple double parameter. Default: 1 + --simple-param-float=SCALAR Simple float parameter. Default: 2 + --simple-param-string=STRING Simple string parameter. Default: "foo" +)")); +} + +BOOST_FIXTURE_TEST_CASE(PrintUsageAll, Fixture) +{ + std::stringstream usage; + Opm::Parameters::printUsage("===foobar===", "", usage, true); + BOOST_CHECK_EQUAL(trimString(usage.str()), +trimString(R"(===foobar=== +Recognized options: + -h,--help Print this help message and exit + --help-all Print all parameters, including obsolete, hidden and deprecated ones. + --simple-b2=BOOLEAN Simpler bool parameter. Default: true + --simple-param-bool=BOOLEAN Simple bool parameter. Default: false + --simple-param-double=SCALAR Simple double parameter. Default: 1 + --simple-param-float=SCALAR Simple float parameter. Default: 2 + --simple-param-int=INTEGER Simple int parameter. Default: 10 + --simple-param-string=STRING Simple string parameter. Default: "foo" +)")); +} + +BOOST_FIXTURE_TEST_CASE(PrintValues, Fixture) +{ + std::stringstream values; + Opm::Parameters::printValues(values); + BOOST_CHECK_EQUAL(trimString(values.str()), +trimString(R"(# [parameters which were specified at compile-time] +SimpleB2="1" +SimpleParamBool="0" +SimpleParamDouble="1" +SimpleParamFloat="2" +SimpleParamInt="10" +SimpleParamString="foo" +)")); +}