TestHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
7 
8 #include <string>
9 
10 #include "DataStructures/DataBox/Tag.hpp"
11 #include "DataStructures/DataBox/TagName.hpp"
12 #include "DataStructures/DataBox/TagTraits.hpp"
13 #include "Utilities/TMPL.hpp"
14 #include "Utilities/TypeTraits.hpp"
15 #include "Utilities/TypeTraits/CreateHasStaticMemberVariable.hpp"
16 #include "Utilities/TypeTraits/CreateHasTypeAlias.hpp"
17 #include "Utilities/TypeTraits/CreateIsCallable.hpp"
18 
19 namespace TestHelpers {
20 
21 namespace db {
22 
23 namespace detail {
24 CREATE_HAS_TYPE_ALIAS(argument_tags)
25 CREATE_HAS_TYPE_ALIAS_V(argument_tags)
28 CREATE_HAS_TYPE_ALIAS(parent_tag)
29 CREATE_HAS_TYPE_ALIAS_V(parent_tag)
30 CREATE_HAS_TYPE_ALIAS(return_type)
31 CREATE_HAS_TYPE_ALIAS_V(return_type)
36 
39 
40 template <typename Tag>
41 void check_tag_name(const std::string& expected_name) {
42  CHECK(::db::tag_name<Tag>() == expected_name);
43  if (is_name_callable_v<Tag> and not ::db::is_compute_tag_v<Tag>) {
44  INFO("Do not define name for Tag '" << ::db::tag_name<Tag>() << "',");
45  INFO("as it will automatically be generated with that name.");
46  CHECK(::db::tag_name<Tag>() != pretty_type::short_name<Tag>());
47  }
48 }
49 } // namespace detail
50 
51 template <typename Tag>
52 void test_base_tag(const std::string& expected_name) {
53  static_assert(::db::is_base_tag_v<Tag>,
54  "A base tag must be derived from db::BaseTag, but "
55  "not from db::SimpleTag nor db::ComputeTag");
56  static_assert(
57  not detail::has_type_v<Tag>,
58  "The only reason to use a base tag is fetch an item without knowing the "
59  "type. Since the type is known, make it a simple tag.");
60  detail::check_tag_name<Tag>(expected_name);
61 }
62 
63 template <typename Tag>
64 void test_compute_tag(const std::string& expected_name) {
65  static_assert(::db::is_compute_tag_v<Tag>,
66  "A compute tag must be derived from db::ComputeTag");
67  static_assert(detail::has_return_type_v<Tag>);
68  static_assert(detail::has_argument_tags_v<Tag>);
69  static_assert(detail::has_base_v<Tag>);
70  static_assert(::db::is_simple_tag_v<typename Tag::base>,
71  "The base type alias of a compute tag must be a simple tag.");
72  static_assert(std::is_same_v<typename Tag::return_type, typename Tag::type>);
73  detail::check_tag_name<Tag>(expected_name);
74 }
75 
76 template <typename Tag>
77 void test_prefix_tag(const std::string& expected_name) {
78  static_assert(std::is_base_of_v<::db::PrefixTag, Tag>,
79  "A prefix tag must be derived from db::PrefixTag");
80  static_assert(::db::is_simple_tag_v<Tag>,
81  "A prefix tag should also be a simple tag");
82  static_assert(detail::has_type_v<Tag>);
83  static_assert(detail::has_tag_v<Tag>);
84  detail::check_tag_name<Tag>(expected_name);
85 }
86 
87 template <typename Tag>
88 void test_reference_tag(const std::string& expected_name) {
89  static_assert(::db::is_reference_tag_v<Tag>,
90  "A reference tag must be derived from db::ReferenceTag");
91  static_assert(detail::has_argument_tags_v<Tag>);
92  static_assert(detail::has_base_v<Tag>);
93  static_assert(detail::has_parent_tag_v<Tag>);
94  static_assert(::db::is_simple_tag_v<typename Tag::base>,
95  "The base type alias of a reference tag must be a simple tag.");
96  static_assert(std::is_same_v<typename Tag::parent_tag,
97  tmpl::front<typename Tag::argument_tags>>);
98  detail::check_tag_name<Tag>(expected_name);
99 }
100 
101 template <typename Tag>
102 void test_simple_tag(const std::string& expected_name) {
103  static_assert(::db::is_simple_tag_v<Tag>,
104  "A simple tag must be derived from db::SimpleTag, but not "
105  "db::ComputeTag");
106  static_assert(detail::has_type_v<Tag>);
107  if constexpr (detail::has_base_v<Tag>) {
108  static_assert(::db::is_base_tag_v<typename Tag::base>,
109  "The base type alias of a simple tag must be a base tag.");
110  }
111  static_assert(not std::is_same_v<Tag, typename Tag::type>,
112  "A type cannot be its own tag.");
113  detail::check_tag_name<Tag>(expected_name);
114 }
115 
116 } // namespace db
117 
118 } // namespace TestHelpers
std::string
TestingFramework.hpp
CREATE_IS_CALLABLE_V
#define CREATE_IS_CALLABLE_V(METHOD_NAME)
Generate a type trait to check if a class has a member function that can be invoked with arguments of...
Definition: CreateIsCallable.hpp:68
CREATE_HAS_TYPE_ALIAS
#define CREATE_HAS_TYPE_ALIAS(ALIAS_NAME)
Generate a type trait to check if a class has a type alias with a particular name,...
Definition: CreateHasTypeAlias.hpp:27
CREATE_HAS_TYPE_ALIAS_V
#define CREATE_HAS_TYPE_ALIAS_V(ALIAS_NAME)
Generate a type trait to check if a class has a type alias with a particular name,...
Definition: CreateHasTypeAlias.hpp:39
TMPL.hpp
db
Namespace for DataBox related things.
Definition: DataBox.hpp:44
CREATE_IS_CALLABLE
#define CREATE_IS_CALLABLE(METHOD_NAME)
Generate a type trait to check if a class has a member function that can be invoked with arguments of...
Definition: CreateIsCallable.hpp:31
string