kokkos-utils 0.0.1
 
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(__eventtype__) \
17 template <typename... Matchers> \
18 auto A##__eventtype__(Matchers&&... matchers) \
19 { \
20 using EventType = Kokkos::utils::callbacks::__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
46
47#define DEFINE_EVENT_WITH_NAME_MATCHER(__eventtype__) \
48 template <typename Matcher> \
49 auto A##__eventtype__##WithName(Matcher&& matcher) \
50 { \
51 using EventType = Kokkos::utils::callbacks::__eventtype__; \
52 return ::testing::VariantWith<EventType>(::testing::Field(&EventType::name, std::forward<Matcher>(matcher))); \
53 }
54
62
63namespace impl
64{
65
66template <typename T, typename... Matchers>
68{
69public:
70 template <typename... Matchers_> requires std::conjunction_v<std::is_same<std::remove_cvref_t<Matchers_>, Matchers>...>
71 explicit ContainsInOrderMatcher(Matchers_&&... matchers_)
72 : matchers{std::forward<Matchers_>(matchers_)...} {}
73
74 template <typename IterableType>
75 bool MatchAndExplain([[maybe_unused]]const IterableType& arg, [[maybe_unused]]::testing::MatchResultListener* const listener) const
76 {
77 if constexpr(sizeof...(Matchers) == 0) {
78 return true;
79 } else {
80 return MatchAndExplainImpl<0>(arg.cbegin(), arg.cend(), listener);
81 }
82 }
83
84 void DescribeTo(std::ostream* out) const
85 {
86 *out << "contains in order elements that match ";
88 }
89
90 void DescribeNegationTo(std::ostream* out) const
91 {
92 *out << "does not contain in order elements that match ";
94 }
95
96private:
97 template <size_t Idx, typename IteratorType>
98 bool MatchAndExplainImpl(const IteratorType it_first, const IteratorType it_last, ::testing::MatchResultListener* const listener) const
99 {
100 if (it_first == it_last) {;
101 *listener << "does not contain in order an element that "
102 << ::testing::DescribeMatcher<T>(std::get<Idx>(matchers), false);
103 return false;
104 }
105
106 if (::testing::Matches(std::get<Idx>(matchers))(*it_first)) {
107 if constexpr (Idx == sizeof...(Matchers) - 1) {
108 return true;
109 } else {
110 return MatchAndExplainImpl<Idx + 1>(std::next(it_first), it_last, listener);
111 }
112 } else {
113 return MatchAndExplainImpl<Idx>(std::next(it_first), it_last, listener);
114 }
115 }
116
117 void DescribeInnerMatchersImpl(std::ostream* out) const
118 {
119 *out << "(";
120 if constexpr (sizeof...(Matchers) >= 2)
121 {
122 [&] <size_t... Idxs>(std::index_sequence<Idxs...>) {
123 ((*out << ::testing::DescribeMatcher<T>(std::get<Idxs>(matchers), false) << ", "), ...);
124 }(std::make_index_sequence<sizeof...(Matchers) - 1>{});
125 }
126 if constexpr (sizeof...(Matchers) >= 1)
127 *out << ::testing::DescribeMatcher<T>(std::get<sizeof...(Matchers) - 1>(matchers), false);
128 *out << ")";
129 }
130
131private:
132 std::tuple<Matchers...> matchers;
133};
134
135} // namespace impl
136
137template <typename T, typename... Matchers>
138auto ContainsInOrder(Matchers&&... matchers) {
139 return ::testing::MakePolymorphicMatcher(impl::ContainsInOrderMatcher<T, std::remove_cvref_t<Matchers>...>(std::forward<Matchers>(matchers)...));
140}
141
142template <typename T>
144
150template <>
152{
153 decltype(auto) operator()(AllocDescriptor descr) const {
154 return ::testing::AllOf(
155 ::testing::Field(
157 ::testing::Field(&Kokkos_Profiling_SpaceHandle::name, ::testing::StrEq(descr.kpsh.name))
158 ),
159 ::testing::Field(
161 ::testing::StrEq(std::move(descr.name))
162 ),
163 ::testing::Field(
165 ::testing::Eq(descr.size)
166 )
167 );
168 }
169};
170
176template <>
178{
179 decltype(auto) operator()(BeginDeepCopyEvent event) const {
180 return ::testing::AllOf(
181 ::testing::Field(&BeginDeepCopyEvent::dst, PartialMatcher<AllocDescriptor>{}(std::move(event.dst))),
182 ::testing::Field(&BeginDeepCopyEvent::src, PartialMatcher<AllocDescriptor>{}(std::move(event.src)))
183 );
184 }
185};
186
187} // namespace Kokkos::utils::callbacks
188
189#endif // KOKKOS_UTILS_CALLBACKS_HELPERS_HPP
bool MatchAndExplainImpl(const IteratorType it_first, const IteratorType it_last, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:98
bool MatchAndExplain(const IterableType &arg, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:75
void DescribeNegationTo(std::ostream *out) const
Definition Helpers.hpp:90
void DescribeInnerMatchersImpl(std::ostream *out) const
Definition Helpers.hpp:117
#define DEFINE_EVENT_MATCHER(__eventtype__)
Definition Helpers.hpp:16
#define DEFINE_EVENT_WITH_NAME_MATCHER(__eventtype__)
Definition Helpers.hpp:47
auto ContainsInOrder(Matchers &&... matchers)
Definition Helpers.hpp:138
Helper struct to hold descriptors of a data allocation.
Definition Events.hpp:102
Kokkos_Profiling_SpaceHandle kpsh
Definition Events.hpp:103
Allocate-data event associated with Kokkos::Tools::Experimental::EventSet::allocate_data.
Definition Events.hpp:113
Begin-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::begin_deep_copy.
Definition Events.hpp:129
Begin-fence event associated with Kokkos::Tools::Experimental::EventSet::begin_fence.
Definition Events.hpp:84
Begin-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_red...
Definition Events.hpp:48
Begin-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::begin_parallel_scan.
Definition Events.hpp:66
Create-profile-section event associated with Kokkos::Tools::Experimental::EventSet::create_profile_se...
Definition Events.hpp:144
Deallocate-data event associated with Kokkos::Tools::Experimental::EventSet::deallocate_data.
Definition Events.hpp:121
Destroy-profile-section event associated with Kokkos::Tools::Experimental::EventSet::destroy_profile_...
Definition Events.hpp:169
End-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::end_deep_copy.
Definition Events.hpp:138
End-fence event associated with Kokkos::Tools::Experimental::EventSet::end_fence.
Definition Events.hpp:94
End-parallel-for event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_for.
Definition Events.hpp:40
End-parallel-reduce event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_reduce.
Definition Events.hpp:58
End-parallel-scan event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_scan.
Definition Events.hpp:76
Pop-region event associated with Kokkos::Tools::Experimental::EventSet::pop_region.
Definition Events.hpp:185
Profile event associated with Kokkos::Tools::Experimental::EventSet::profile_event.
Definition Events.hpp:191
Push-region event associated with Kokkos::Tools::Experimental::EventSet::push_region.
Definition Events.hpp:177
Start-profile-section event associated with Kokkos::Tools::Experimental::EventSet::start_profile_sect...
Definition Events.hpp:153
Stop-profile-section event associated with Kokkos::Tools::Experimental::EventSet::stop_profile_sectio...
Definition Events.hpp:161