kokkos-utils 0.0.4
 
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 *listener << "does not contain in order an element that "
106 << ::testing::DescribeMatcher<T>(std::get<Idx>(matchers), false);
107 return false;
108 }
109
110 if (::testing::Matches(std::get<Idx>(matchers))(*it_first)) {
111 if constexpr (Idx == sizeof...(Matchers) - 1) {
112 return true;
113 } else {
114 return MatchAndExplainImpl<Idx + 1>(std::next(it_first), it_last, listener);
115 }
116 } else {
117 return MatchAndExplainImpl<Idx>(std::next(it_first), it_last, listener);
118 }
119 }
120
121 void DescribeInnerMatchersImpl(std::ostream* out) const
122 {
123 *out << "(";
124 if constexpr (sizeof...(Matchers) >= 2)
125 {
126 [&] <size_t... Idxs>(std::index_sequence<Idxs...>) {
127 ((*out << ::testing::DescribeMatcher<T>(std::get<Idxs>(matchers), false) << ", "), ...);
128 }(std::make_index_sequence<sizeof...(Matchers) - 1>{});
129 }
130 if constexpr (sizeof...(Matchers) >= 1)
131 *out << ::testing::DescribeMatcher<T>(std::get<sizeof...(Matchers) - 1>(matchers), false);
132 *out << ")";
133 }
134
135private:
136 std::tuple<Matchers...> matchers;
137};
138
139template <typename ElementType, typename ElementMatcher>
141{
142public:
143 explicit ElementAtMatcher(const size_t index, ElementMatcher matcher)
144 : m_index(index), m_matcher(std::move(matcher)) {}
145
146 template <typename IterableType>
147 bool MatchAndExplain(const IterableType& arg, ::testing::MatchResultListener* const listener) const
148 {
149 if (m_index >= arg.size()) {
150 *listener << "index " << m_index << " is out of bounds (size " << arg.size() << ")";
151 return false;
152 }
153 return ::testing::ExplainMatchResult(m_matcher, arg[m_index], listener);
154 }
155
156 void DescribeTo(std::ostream* out) const
157 {
158 *out << "element at index " << m_index << " "
159 << ::testing::DescribeMatcher<ElementType>(m_matcher, false);
160 }
161
162 void DescribeNegationTo(std::ostream* out) const
163 {
164 *out << "element at index " << m_index << " "
165 << ::testing::DescribeMatcher<ElementType>(m_matcher, true);
166 }
167
168private:
169 size_t m_index;
170 ElementMatcher m_matcher;
171};
172
173} // namespace impl
174
175template <typename T, typename... Matchers>
176auto ContainsInOrder(Matchers&&... matchers) {
177 return ::testing::MakePolymorphicMatcher(impl::ContainsInOrderMatcher<T, std::remove_cvref_t<Matchers>...>(std::forward<Matchers>(matchers)...));
178}
179
187template <typename ElementType, typename ElementMatcher>
188auto ElementAt(const size_t index, ElementMatcher&& matcher) {
189 return ::testing::MakePolymorphicMatcher(
190 impl::ElementAtMatcher<ElementType, std::remove_cvref_t<ElementMatcher>>{index, std::forward<ElementMatcher>(matcher)});
191}
192
193template <typename T>
195
201template <>
203{
204 decltype(auto) operator()(const AllocDescriptor& descr) const {
205 return ::testing::AllOf(
206 ::testing::Field(
208 ::testing::Field(&Kokkos_Profiling_SpaceHandle::name, ::testing::StrEq(descr.kpsh.name))
209 ),
210 ::testing::Field(
212 ::testing::StrEq(descr.name)
213 ),
214 ::testing::Field(
216 ::testing::Eq(descr.size)
217 )
218 );
219 }
220};
221
227template <>
229{
230 decltype(auto) operator()(const BeginDeepCopyEvent& event) const {
231 return ::testing::AllOf(
232 ::testing::Field(&BeginDeepCopyEvent::dst, PartialMatcher<AllocDescriptor>{}(event.dst)),
233 ::testing::Field(&BeginDeepCopyEvent::src, PartialMatcher<AllocDescriptor>{}(event.src))
234 );
235 }
236};
237
238} // namespace Kokkos::utils::callbacks
239
240#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:121
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:156
void DescribeNegationTo(std::ostream *out) const
Definition Helpers.hpp:162
ElementAtMatcher(const size_t index, ElementMatcher matcher)
Definition Helpers.hpp:143
bool MatchAndExplain(const IterableType &arg, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:147
#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:188
auto ContainsInOrder(Matchers &&... matchers)
Definition Helpers.hpp:176
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