kokkos-utils 0.0.5
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
48
49#define DEFINE_EVENT_WITH_NAME_MATCHER_IN(__namespace__, __eventtype__) \
50 template <typename Matcher> \
51 auto A##__eventtype__##WithName(Matcher&& matcher) \
52 { \
53 using EventType = __namespace__::__eventtype__; \
54 return ::testing::VariantWith<EventType>(::testing::Field(&EventType::name, std::forward<Matcher>(matcher))); \
55 }
56
57#define DEFINE_EVENT_WITH_NAME_MATCHER(__eventtype__) DEFINE_EVENT_WITH_NAME_MATCHER_IN(Kokkos::utils::callbacks, __eventtype__)
58
66
67namespace impl
68{
69
70template <typename T, typename... Matchers>
72{
73public:
74 template <typename... Matchers_> requires std::conjunction_v<std::is_same<std::remove_cvref_t<Matchers_>, Matchers>...>
75 explicit ContainsInOrderMatcher(Matchers_&&... matchers_)
76 : matchers{std::forward<Matchers_>(matchers_)...} {}
77
78 template <typename IterableType>
79 bool MatchAndExplain([[maybe_unused]]const IterableType& arg, [[maybe_unused]]::testing::MatchResultListener* const listener) const
80 {
81 if constexpr(sizeof...(Matchers) == 0) {
82 return true;
83 } else {
84 return MatchAndExplainImpl<0>(arg.cbegin(), arg.cend(), listener);
85 }
86 }
87
88 void DescribeTo(std::ostream* out) const
89 {
90 *out << "contains in order elements that match ";
92 }
93
94 void DescribeNegationTo(std::ostream* out) const
95 {
96 *out << "does not contain in order elements that match ";
98 }
99
100private:
101 template <size_t Idx, typename IteratorType>
102 bool MatchAndExplainImpl(const IteratorType& it_first, const IteratorType& it_last, ::testing::MatchResultListener* const listener) const
103 {
104 if (it_first == it_last) {;
105 if (listener->IsInterested()) {
106 *listener->stream() << "does not contain in order an element that "
107 << ::testing::DescribeMatcher<T>(std::get<Idx>(matchers), false);
108 }
109 return false;
110 }
111
112 if (::testing::Matches(std::get<Idx>(matchers))(*it_first)) {
113 if constexpr (Idx == sizeof...(Matchers) - 1) {
114 return true;
115 } else {
116 return MatchAndExplainImpl<Idx + 1>(std::next(it_first), it_last, listener);
117 }
118 } else {
119 return MatchAndExplainImpl<Idx>(std::next(it_first), it_last, listener);
120 }
121 }
122
123 void DescribeInnerMatchersImpl(std::ostream* out) const
124 {
125 *out << "(";
126 if constexpr (sizeof...(Matchers) >= 2)
127 {
128 [&] <size_t... Idxs>(std::index_sequence<Idxs...>) {
129 ((*out << ::testing::DescribeMatcher<T>(std::get<Idxs>(matchers), false) << ", "), ...);
130 }(std::make_index_sequence<sizeof...(Matchers) - 1>{});
131 }
132 if constexpr (sizeof...(Matchers) >= 1)
133 *out << ::testing::DescribeMatcher<T>(std::get<sizeof...(Matchers) - 1>(matchers), false);
134 *out << ")";
135 }
136
137private:
138 std::tuple<Matchers...> matchers;
139};
140
141template <typename ElementType, typename ElementMatcher>
143{
144public:
145 explicit ElementAtMatcher(const size_t index, ElementMatcher matcher)
146 : m_index(index), m_matcher(std::move(matcher)) {}
147
148 template <typename IterableType>
149 bool MatchAndExplain(const IterableType& arg, ::testing::MatchResultListener* const listener) const
150 {
151 if (m_index >= arg.size()) {
152 if (listener->IsInterested()) {
153 *listener->stream() << "index " << m_index << " is out of bounds (size " << arg.size() << ")";
154 return false;
155 }
156 }
157 return ::testing::ExplainMatchResult(m_matcher, arg[m_index], listener);
158 }
159
160 void DescribeTo(std::ostream* out) const
161 {
162 *out << "element at index " << m_index << " "
163 << ::testing::DescribeMatcher<ElementType>(m_matcher, false);
164 }
165
166 void DescribeNegationTo(std::ostream* out) const
167 {
168 *out << "element at index " << m_index << " "
169 << ::testing::DescribeMatcher<ElementType>(m_matcher, true);
170 }
171
172private:
173 size_t m_index;
174 ElementMatcher m_matcher;
175};
176
177} // namespace impl
178
179template <typename T, typename... Matchers>
180auto ContainsInOrder(Matchers&&... matchers) {
181 return ::testing::MakePolymorphicMatcher(impl::ContainsInOrderMatcher<T, std::remove_cvref_t<Matchers>...>(std::forward<Matchers>(matchers)...));
182}
183
191template <typename ElementType, typename ElementMatcher>
192auto ElementAt(const size_t index, ElementMatcher&& matcher) {
193 return ::testing::MakePolymorphicMatcher(
194 impl::ElementAtMatcher<ElementType, std::remove_cvref_t<ElementMatcher>>{index, std::forward<ElementMatcher>(matcher)});
195}
196
197template <typename T>
199
205template <>
207{
208 decltype(auto) operator()(const AllocDescriptor& descr) const {
209 return ::testing::AllOf(
210 ::testing::Field(
212 ::testing::Field(&Kokkos_Profiling_SpaceHandle::name, ::testing::StrEq(descr.kpsh.name))
213 ),
214 ::testing::Field(
216 ::testing::StrEq(descr.name)
217 ),
218 ::testing::Field(
220 ::testing::Eq(descr.size)
221 )
222 );
223 }
224};
225
231template <>
233{
234 decltype(auto) operator()(const BeginDeepCopyEvent& event) const {
235 return ::testing::AllOf(
236 ::testing::Field(&BeginDeepCopyEvent::dst, PartialMatcher<AllocDescriptor>{}(event.dst)),
237 ::testing::Field(&BeginDeepCopyEvent::src, PartialMatcher<AllocDescriptor>{}(event.src))
238 );
239 }
240};
241
242} // namespace Kokkos::utils::callbacks
243
244#endif // KOKKOS_UTILS_CALLBACKS_HELPERS_HPP
bool MatchAndExplain(const IterableType &arg, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:79
void DescribeNegationTo(std::ostream *out) const
Definition Helpers.hpp:94
void DescribeInnerMatchersImpl(std::ostream *out) const
Definition Helpers.hpp:123
bool MatchAndExplainImpl(const IteratorType &it_first, const IteratorType &it_last, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:102
void DescribeTo(std::ostream *out) const
Definition Helpers.hpp:160
void DescribeNegationTo(std::ostream *out) const
Definition Helpers.hpp:166
ElementAtMatcher(const size_t index, ElementMatcher matcher)
Definition Helpers.hpp:145
bool MatchAndExplain(const IterableType &arg, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:149
#define DEFINE_EVENT_MATCHER(__eventtype__)
Definition Helpers.hpp:27
#define DEFINE_EVENT_WITH_NAME_MATCHER(__eventtype__)
Definition Helpers.hpp:57
auto ElementAt(const size_t index, ElementMatcher &&matcher)
Check that an element at a given index matches.
Definition Helpers.hpp:192
auto ContainsInOrder(Matchers &&... matchers)
Definition Helpers.hpp:180
Helper struct to hold descriptors of a data allocation.
Definition Events.hpp:109
Kokkos_Profiling_SpaceHandle kpsh
Definition Events.hpp:110
Allocate-data event associated with Kokkos::Tools::Experimental::EventSet::allocate_data.
Definition Events.hpp:120
Begin-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::begin_deep_copy.
Definition Events.hpp:136
Begin-fence event associated with Kokkos::Tools::Experimental::EventSet::begin_fence.
Definition Events.hpp:91
Begin-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_red...
Definition Events.hpp:55
Begin-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_scan.
Definition Events.hpp:73
Create-profile-section event associated with Kokkos::Tools::Experimental::EventSet::create_profile_se...
Definition Events.hpp:151
Deallocate-data event associated with Kokkos::Tools::Experimental::EventSet::deallocate_data.
Definition Events.hpp:128
Destroy-profile-section event associated with Kokkos::Tools::Experimental::EventSet::destroy_profile_...
Definition Events.hpp:176
End-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::end_deep_copy.
Definition Events.hpp:145
End-fence event associated with Kokkos::Tools::Experimental::EventSet::end_fence.
Definition Events.hpp:101
End-parallel-for event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_for.
Definition Events.hpp:47
End-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_reduce.
Definition Events.hpp:65
End-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_scan.
Definition Events.hpp:83
Pop-region event associated with Kokkos::Tools::Experimental::EventSet::pop_region.
Definition Events.hpp:192
Profile event associated with Kokkos::Tools::Experimental::EventSet::profile_event.
Definition Events.hpp:198
Push-region event associated with Kokkos::Tools::Experimental::EventSet::push_region.
Definition Events.hpp:184
Start-profile-section event associated with Kokkos::Tools::Experimental::EventSet::start_profile_sect...
Definition Events.hpp:160
Stop-profile-section event associated with Kokkos::Tools::Experimental::EventSet::stop_profile_sectio...
Definition Events.hpp:168