kokkos-utils 0.0.7
Loading...
Searching...
No Matches
Helpers.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_UTILS_CALLBACKS_HELPERS_HPP
2#define KOKKOS_UTILS_CALLBACKS_HELPERS_HPP
3
4#include "gmock/gmock.h"
5
7
9{
10
11#define TEST_F_WITH_CB_MGR(__test_fixture_name__, __test_name__) \
12 class __test_fixture_name__ : public ::testing::Test, \
13 public scoped::callbacks::Manager {}; \
14 TEST_F(__test_fixture_name__, __test_name__)
15
16#define DEFINE_EVENT_MATCHER_IN(__namespace__, __eventtype__) \
17 template <typename... Matchers> \
18 auto A##__eventtype__(Matchers&&... matchers) \
19 { \
20 using EventType = __namespace__::__eventtype__; \
21 if constexpr (sizeof...(Matchers) == 0) \
22 return ::testing::VariantWith<EventType>(::testing::_); \
23 else \
24 return ::testing::VariantWith<EventType>(::testing::AllOf(std::forward<Matchers>(matchers)...)); \
25 }
26
27#define DEFINE_EVENT_MATCHER(__eventtype__) DEFINE_EVENT_MATCHER_IN(Kokkos::utils::callbacks, __eventtype__)
28
50
51#define DEFINE_EVENT_WITH_NAME_MATCHER_IN(__namespace__, __eventtype__) \
52 template <typename Matcher> \
53 auto A##__eventtype__##WithName(Matcher&& matcher) \
54 { \
55 using EventType = __namespace__::__eventtype__; \
56 return ::testing::VariantWith<EventType>(::testing::Field(&EventType::name, std::forward<Matcher>(matcher))); \
57 }
58
59#define DEFINE_EVENT_WITH_NAME_MATCHER(__eventtype__) DEFINE_EVENT_WITH_NAME_MATCHER_IN(Kokkos::utils::callbacks, __eventtype__)
60
69
70namespace impl
71{
72
73template <typename T, typename... Matchers>
75{
76public:
77 template <typename... Matchers_> requires std::conjunction_v<std::is_same<std::remove_cvref_t<Matchers_>, Matchers>...>
78 explicit ContainsInOrderMatcher(Matchers_&&... matchers_)
79 : matchers{std::forward<Matchers_>(matchers_)...} {}
80
81 template <typename IterableType>
82 bool MatchAndExplain([[maybe_unused]]const IterableType& arg, [[maybe_unused]]::testing::MatchResultListener* const listener) const
83 {
84 if constexpr(sizeof...(Matchers) == 0) {
85 return true;
86 } else {
87 return MatchAndExplainImpl<0>(arg.cbegin(), arg.cend(), listener);
88 }
89 }
90
91 void DescribeTo(std::ostream* out) const
92 {
93 *out << "contains in order elements that match ";
95 }
96
97 void DescribeNegationTo(std::ostream* out) const
98 {
99 *out << "does not contain in order elements that match ";
101 }
102
103private:
104 template <size_t Idx, typename IteratorType>
105 bool MatchAndExplainImpl(const IteratorType& it_first, const IteratorType& it_last, ::testing::MatchResultListener* const listener) const
106 {
107 if (it_first == it_last) {;
108 if (listener->IsInterested()) {
109 *listener->stream() << "does not contain in order an element that "
110 << ::testing::DescribeMatcher<T>(std::get<Idx>(matchers), false);
111 }
112 return false;
113 }
114
115 if (::testing::Matches(std::get<Idx>(matchers))(*it_first)) {
116 if constexpr (Idx == sizeof...(Matchers) - 1) {
117 return true;
118 } else {
119 return MatchAndExplainImpl<Idx + 1>(std::next(it_first), it_last, listener);
120 }
121 } else {
122 return MatchAndExplainImpl<Idx>(std::next(it_first), it_last, listener);
123 }
124 }
125
126 void DescribeInnerMatchersImpl(std::ostream* out) const
127 {
128 *out << "(";
129 if constexpr (sizeof...(Matchers) >= 2)
130 {
131 [&] <size_t... Idxs>(std::index_sequence<Idxs...>) {
132 ((*out << ::testing::DescribeMatcher<T>(std::get<Idxs>(matchers), false) << ", "), ...);
133 }(std::make_index_sequence<sizeof...(Matchers) - 1>{});
134 }
135 if constexpr (sizeof...(Matchers) >= 1)
136 *out << ::testing::DescribeMatcher<T>(std::get<sizeof...(Matchers) - 1>(matchers), false);
137 *out << ")";
138 }
139
140private:
141 std::tuple<Matchers...> matchers;
142};
143
144template <typename ElementType, typename ElementMatcher>
146{
147public:
148 explicit ElementAtMatcher(const size_t index, ElementMatcher matcher)
149 : m_index(index), m_matcher(std::move(matcher)) {}
150
151 template <typename IterableType>
152 bool MatchAndExplain(const IterableType& arg, ::testing::MatchResultListener* const listener) const
153 {
154 if (m_index >= arg.size()) {
155 if (listener->IsInterested()) {
156 *listener->stream() << "index " << m_index << " is out of bounds (size " << arg.size() << ")";
157 return false;
158 }
159 }
160 return ::testing::ExplainMatchResult(m_matcher, arg[m_index], listener);
161 }
162
163 void DescribeTo(std::ostream* out) const
164 {
165 *out << "element at index " << m_index << " "
166 << ::testing::DescribeMatcher<ElementType>(m_matcher, false);
167 }
168
169 void DescribeNegationTo(std::ostream* out) const
170 {
171 *out << "element at index " << m_index << " "
172 << ::testing::DescribeMatcher<ElementType>(m_matcher, true);
173 }
174
175private:
176 size_t m_index;
177 ElementMatcher m_matcher;
178};
179
180} // namespace impl
181
182template <typename T, typename... Matchers>
183auto ContainsInOrder(Matchers&&... matchers) {
184 return ::testing::MakePolymorphicMatcher(impl::ContainsInOrderMatcher<T, std::remove_cvref_t<Matchers>...>(std::forward<Matchers>(matchers)...));
185}
186
194template <typename ElementType, typename ElementMatcher>
195auto ElementAt(const size_t index, ElementMatcher&& matcher) {
196 return ::testing::MakePolymorphicMatcher(
197 impl::ElementAtMatcher<ElementType, std::remove_cvref_t<ElementMatcher>>{index, std::forward<ElementMatcher>(matcher)});
198}
199
200template <typename T>
202
208template <>
210{
211 decltype(auto) operator()(const AllocDescriptor& descr) const {
212 return ::testing::AllOf(
213 ::testing::Field(
215 ::testing::Field(&Kokkos::Tools::SpaceHandle::name, ::testing::StrEq(descr.kpsh.name))
216 ),
217 ::testing::Field(
219 ::testing::StrEq(descr.name)
220 ),
221 ::testing::Field(
223 ::testing::Eq(descr.size)
224 )
225 );
226 }
227};
228
234template <>
236{
237 decltype(auto) operator()(const BeginDeepCopyEvent& event) const {
238 return ::testing::AllOf(
239 ::testing::Field(&BeginDeepCopyEvent::dst, PartialMatcher<AllocDescriptor>{}(event.dst)),
240 ::testing::Field(&BeginDeepCopyEvent::src, PartialMatcher<AllocDescriptor>{}(event.src))
241 );
242 }
243};
244
245} // namespace Kokkos::utils::callbacks
246
247#endif // KOKKOS_UTILS_CALLBACKS_HELPERS_HPP
bool MatchAndExplain(const IterableType &arg, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:82
void DescribeNegationTo(std::ostream *out) const
Definition Helpers.hpp:97
void DescribeInnerMatchersImpl(std::ostream *out) const
Definition Helpers.hpp:126
bool MatchAndExplainImpl(const IteratorType &it_first, const IteratorType &it_last, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:105
void DescribeTo(std::ostream *out) const
Definition Helpers.hpp:163
void DescribeNegationTo(std::ostream *out) const
Definition Helpers.hpp:169
ElementAtMatcher(const size_t index, ElementMatcher matcher)
Definition Helpers.hpp:148
bool MatchAndExplain(const IterableType &arg, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:152
#define DEFINE_EVENT_MATCHER(__eventtype__)
Definition Helpers.hpp:27
#define DEFINE_EVENT_WITH_NAME_MATCHER(__eventtype__)
Definition Helpers.hpp:59
auto ElementAt(const size_t index, ElementMatcher &&matcher)
Check that an element at a given index matches.
Definition Helpers.hpp:195
auto ContainsInOrder(Matchers &&... matchers)
Definition Helpers.hpp:183
Helper struct to hold descriptors of a data allocation.
Definition Events.hpp:135
Kokkos::Tools::SpaceHandle kpsh
Definition Events.hpp:136
Allocate-data event associated with Kokkos::Tools::Experimental::EventSet::allocate_data.
Definition Events.hpp:146
Begin-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::begin_deep_copy.
Definition Events.hpp:162
Begin-fence event associated with Kokkos::Tools::Experimental::EventSet::begin_fence.
Definition Events.hpp:117
Begin-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_red...
Definition Events.hpp:63
Begin-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_scan.
Definition Events.hpp:81
Begin-single event associated with Kokkos::Tools::Experimental::EventSet::begin_single.
Definition Events.hpp:99
Create-profile-section event associated with Kokkos::Tools::Experimental::EventSet::create_profile_se...
Definition Events.hpp:177
Deallocate-data event associated with Kokkos::Tools::Experimental::EventSet::deallocate_data.
Definition Events.hpp:154
Destroy-profile-section event associated with Kokkos::Tools::Experimental::EventSet::destroy_profile_...
Definition Events.hpp:202
End-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::end_deep_copy.
Definition Events.hpp:171
End-fence event associated with Kokkos::Tools::Experimental::EventSet::end_fence.
Definition Events.hpp:127
End-parallel-for event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_for.
Definition Events.hpp:55
End-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_reduce.
Definition Events.hpp:73
End-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_scan.
Definition Events.hpp:91
End-single event associated with Kokkos::Tools::Experimental::EventSet::end_single.
Definition Events.hpp:109
Pop-region event associated with Kokkos::Tools::Experimental::EventSet::pop_region.
Definition Events.hpp:218
Profile event associated with Kokkos::Tools::Experimental::EventSet::profile_event.
Definition Events.hpp:224
Push-region event associated with Kokkos::Tools::Experimental::EventSet::push_region.
Definition Events.hpp:210
Start-profile-section event associated with Kokkos::Tools::Experimental::EventSet::start_profile_sect...
Definition Events.hpp:186
Stop-profile-section event associated with Kokkos::Tools::Experimental::EventSet::stop_profile_sectio...
Definition Events.hpp:194