diff --git a/test/common/propertysystem/CMakeLists.txt b/test/common/propertysystem/CMakeLists.txt index a023dbb47e4d078f4bc715e662f02089631795ab..7311eb17956feb76ae3071c5cb950492f427a823 100644 --- a/test/common/propertysystem/CMakeLists.txt +++ b/test/common/propertysystem/CMakeLists.txt @@ -1,3 +1,6 @@ # build the test for the property system dumux_add_test(SOURCES test_propertysystem.cc LABELS unit) + +dumux_add_test(SOURCES test_propertysystem_aliases.cc + LABELS unit) diff --git a/test/common/propertysystem/test_propertysystem_aliases.cc b/test/common/propertysystem/test_propertysystem_aliases.cc new file mode 100644 index 0000000000000000000000000000000000000000..c02c1f852301e01613ac7887a038ba1220275b2f --- /dev/null +++ b/test/common/propertysystem/test_propertysystem_aliases.cc @@ -0,0 +1,133 @@ +// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +// vi: set et ts=4 sw=4 sts=4: +/***************************************************************************** + * See the file COPYING for full copying permissions. * + * * + * This program 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. * + * * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. * + *****************************************************************************/ +/*! + * \file + * \ingroup Common + * \ingroup Tests + * \brief Testing the Dumux property system + */ + +#include <iostream> +#include <type_traits> + +#include <dune/common/classname.hh> +#include <dune/common/exceptions.hh> + +#include <dumux/common/properties/propertysystem.hh> + +namespace Dumux::Properties { + +// create some properties like for test_propertysystem +// in order to make property aliases work, we need to use the macro to define properties +DUMUX_DEFINE_PROPERTY(Scalar) +DUMUX_DEFINE_PROPERTY(CoordinateType) +DUMUX_DEFINE_PROPERTY(UseTpfaFlux) + +namespace TTag { +// create some type tags: +// the tuple is sorted by precedence, the first one overwriting the following +struct Base +{ + using Scalar = float; +}; +struct Grid +{ + template<class TypeTag> + using CoordinateType = GetPropType<TypeTag, Scalar>; +}; +struct CCTpfaDisc +{ + using InheritsFrom = std::tuple<Grid, Base>; + using UseTpfaFlux = std::true_type; +}; +struct BoxDisc +{ + using InheritsFrom = std::tuple<Grid, Base>; +}; +struct OnePModel +{ + using InheritsFrom = std::tuple<Base>; + using Scalar = std::size_t; // unused because the property is also specialized below +}; +struct OnePTest +{ + using InheritsFrom = std::tuple<OnePModel, BoxDisc>; + using Scalar = int; +}; + +} // end namespace TTag + +// overwrite this one by specialization (takes precedence over alias) +template<class TypeTag> +struct Scalar<TypeTag, TTag::OnePModel> { using type = double; }; + +} // end namespace Dumux::Properties + +int main(int argc, char* argv[]) +{ + using namespace Dumux; + using namespace Properties; + + { + using Scalar = GetPropType<TTag::Base, Scalar>; + if (!std::is_same_v<Scalar, float>) + DUNE_THROW(Dune::InvalidStateException, "Property Scalar in TTag::Base should be float but is " << Dune::className<Scalar>()); + } + { + using Scalar = GetPropType<TTag::OnePTest, Scalar>; + if (!std::is_same_v<Scalar, int>) + DUNE_THROW(Dune::InvalidStateException, "Property Scalar in TTag::OnePTest should be int but is " << Dune::className<Scalar>()); + } + { + using Scalar = GetPropType<TTag::OnePModel, Scalar>; + if (!std::is_same_v<Scalar, double>) + DUNE_THROW(Dune::InvalidStateException, "Property Scalar in TTag::OnePModel should be double but is " << Dune::className<Scalar>()); + } + { + static_assert( + !hasDefinedType<TTag::Base, CoordinateType>(), + "Property type should be undefined for TTag::Base" + ); + } + { + using CoordinateType = GetPropTypeOr<TTag::Base, CoordinateType, double>; + static_assert( + std::is_same_v<CoordinateType, double>, + "Property is expected to default to double" + ); + } + { + using CoordinateType = GetPropType<TTag::OnePTest, CoordinateType>; + if (!std::is_same_v<CoordinateType, int>) + DUNE_THROW(Dune::InvalidStateException, "Property CoordinateType in TTag::OnePTest should be int but is " << Dune::className<CoordinateType>()); + } + { + using CoordinateType = GetPropType<TTag::CCTpfaDisc, CoordinateType>; + if (!std::is_same_v<CoordinateType, float>) + DUNE_THROW(Dune::InvalidStateException, "Property CoordinateType in TTag::CCTpfaDisc should be float but is " << Dune::className<CoordinateType>()); + } + { + constexpr bool useTpfaFlux = getPropValue<TTag::CCTpfaDisc, UseTpfaFlux>(); + if (!useTpfaFlux) + DUNE_THROW(Dune::InvalidStateException, "Property UseTpfaFlux in TTag::CCTpfaDisc should be true!"); + } + + std::cout << "All tests passed!" << std::endl; + return 0; +}