Page MenuHomec4science

test_named_parameter_pack.cc
No OneTemporary

File Metadata

Created
Thu, Jan 23, 01:25

test_named_parameter_pack.cc

/**
* @file test_named_parameter_pack.cc
*
* @author Till Junge <till.junge@altermail.ch>
*
* @date 20 Jan 2019
*
* @brief test for the named parameter packs
*
* Copyright © 2019 Till Junge
*
* µSpectre is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3, or (at
* your option) any later version.
*
* µSpectre 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 Lesser General Public License
* along with µSpectre; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Additional permission under GNU GPL version 3 section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with proprietary FFT implementations or numerical libraries, containing parts
* covered by the terms of those libraries' licenses, the licensors of this
* Program grant you additional permission to convey the resulting work.
*/
#include "tests.hh"
#include "common/named_parameter_pack.hh"
namespace muSpectre {
BOOST_AUTO_TEST_SUITE(named_parameter_pack_tests);
BOOST_AUTO_TEST_CASE(ValueTest) {
Eigen::Matrix<Real, 3, 3> mat{};
mat.setIdentity();
ParameterValue boolean{false};
ParameterValue real{2.5};
ParameterValue matrix{mat};
ParameterValue pack{NamedParameterPack{}};
BOOST_CHECK_EQUAL(boolean.get_tag(), ParamTag::Bool);
BOOST_CHECK_EQUAL(real.get_tag(), ParamTag::Real);
BOOST_CHECK_EQUAL(matrix.get_tag(), ParamTag::RealMatrix);
BOOST_CHECK_EQUAL(pack.get_tag(), ParamTag::Pack);
BOOST_CHECK_EQUAL(boolean.get_value<ParamTag::Bool>(), false);
BOOST_CHECK_THROW(boolean.get_value<ParamTag::Real>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(boolean.get_value<ParamTag::RealMatrix>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(boolean.get_value<ParamTag::Pack>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(real.get_value<ParamTag::Bool>(),
mpark::bad_variant_access);
BOOST_CHECK_EQUAL(real.get_value<ParamTag::Real>(), 2.5);
BOOST_CHECK_THROW(real.get_value<ParamTag::RealMatrix>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(real.get_value<ParamTag::Pack>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(matrix.get_value<ParamTag::Bool>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(matrix.get_value<ParamTag::Real>(),
mpark::bad_variant_access);
BOOST_CHECK_EQUAL(
(matrix.get_value<ParamTag::RealMatrix>() - Eigen::Matrix3d::Identity())
.norm(),
0.);
BOOST_CHECK_THROW(matrix.get_value<ParamTag::Pack>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(pack.get_value<ParamTag::Bool>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(pack.get_value<ParamTag::Real>(),
mpark::bad_variant_access);
BOOST_CHECK_THROW(pack.get_value<ParamTag::RealMatrix>(),
mpark::bad_variant_access);
BOOST_CHECK_EQUAL(pack.get_value<ParamTag::Pack>()->size(), 0);
}
BOOST_AUTO_TEST_CASE(base_test) {
Eigen::Matrix<Real, 3, 3> mat{};
mat.setIdentity();
NamedParameterPack test_pack01{{"bool", false}};
NamedParameterPack test_pack02{{"real", 2.5}, {"bool", true}};
NamedParameterPack test_pack03{{"matrix", ParameterValue{mat}}};
NamedParameterPack test_pack04{{"nested", test_pack02}};
NamedParameterPack pack{{"real", 2.5},
{"bool", true},
{"matrix", ParameterValue{mat}},
{"nested", test_pack01}};
BOOST_CHECK_EQUAL(pack.get_value<ParamTag::Real>("real"), 2.5);
BOOST_CHECK_EQUAL(pack.get_value<ParamTag::Bool>("bool"), true);
BOOST_CHECK_EQUAL(
(mat - pack.get_value<ParamTag::RealMatrix>("matrix")).norm(), 0);
BOOST_CHECK_EQUAL(pack.get("nested").get_tag(), ParamTag::Pack);
BOOST_CHECK_EQUAL(
pack.get_value<ParamTag::Pack>("nested")->get_value<ParamTag::Bool>(
"bool"),
false);
}
BOOST_AUTO_TEST_SUITE_END();
} // namespace muSpectre

Event Timeline