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
44
45#define DEFINE_EVENT_WITH_NAME_MATCHER(__eventtype__) \
46 template <typename Matcher> \
47 auto A##__eventtype__##WithName(Matcher&& matcher) \
48 { \
49 using EventType = Kokkos::utils::callbacks::__eventtype__; \
50 return ::testing::VariantWith<EventType>(::testing::Field(&EventType::name, std::forward<Matcher>(matcher))); \
51 }
52
60
61namespace impl
62{
63
64template <typename T, typename... Matchers>
66{
67public:
68 template <typename... Matchers_> requires std::conjunction_v<std::is_same<std::remove_cvref_t<Matchers_>, Matchers>...>
69 explicit ContainsInOrderMatcher(Matchers_&&... matchers_)
70 : matchers{std::forward<Matchers_>(matchers_)...} {}
71
72 template <typename IterableType>
73 bool MatchAndExplain([[maybe_unused]]const IterableType& arg, [[maybe_unused]]::testing::MatchResultListener* const listener) const
74 {
75 if constexpr(sizeof...(Matchers) == 0) {
76 return true;
77 } else {
78 return MatchAndExplainImpl<0>(arg.cbegin(), arg.cend(), listener);
79 }
80 }
81
82 void DescribeTo(std::ostream* out) const
83 {
84 *out << "contains in order elements that match ";
86 }
87
88 void DescribeNegationTo(std::ostream* out) const
89 {
90 *out << "does not contain in order elements that match ";
92 }
93
94private:
95 template <size_t Idx, typename IteratorType>
96 bool MatchAndExplainImpl(const IteratorType it_first, const IteratorType it_last, ::testing::MatchResultListener* const listener) const
97 {
98 if (it_first == it_last) {;
99 *listener << "does not contain in order an element that "
100 << ::testing::DescribeMatcher<T>(std::get<Idx>(matchers), false);
101 return false;
102 }
103
104 if (::testing::Matches(std::get<Idx>(matchers))(*it_first)) {
105 if constexpr (Idx == sizeof...(Matchers) - 1) {
106 return true;
107 } else {
108 return MatchAndExplainImpl<Idx + 1>(std::next(it_first), it_last, listener);
109 }
110 } else {
111 return MatchAndExplainImpl<Idx>(std::next(it_first), it_last, listener);
112 }
113 }
114
115 void DescribeInnerMatchersImpl(std::ostream* out) const
116 {
117 *out << "(";
118 if constexpr (sizeof...(Matchers) >= 2)
119 {
120 [&] <size_t... Idxs>(std::index_sequence<Idxs...>) {
121 ((*out << ::testing::DescribeMatcher<T>(std::get<Idxs>(matchers), false) << ", "), ...);
122 }(std::make_index_sequence<sizeof...(Matchers) - 1>{});
123 }
124 if constexpr (sizeof...(Matchers) >= 1)
125 *out << ::testing::DescribeMatcher<T>(std::get<sizeof...(Matchers) - 1>(matchers), false);
126 *out << ")";
127 }
128
129private:
130 std::tuple<Matchers...> matchers;
131};
132
133} // namespace impl
134
135template <typename T, typename... Matchers>
136auto ContainsInOrder(Matchers&&... matchers) {
137 return ::testing::MakePolymorphicMatcher(impl::ContainsInOrderMatcher<T, std::remove_cvref_t<Matchers>...>(std::forward<Matchers>(matchers)...));
138}
139
140} // namespace Kokkos::utils::callbacks
141
142#endif // KOKKOS_UTILS_CALLBACKS_HELPERS_HPP
bool MatchAndExplainImpl(const IteratorType it_first, const IteratorType it_last, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:96
bool MatchAndExplain(const IterableType &arg, ::testing::MatchResultListener *const listener) const
Definition Helpers.hpp:73
void DescribeNegationTo(std::ostream *out) const
Definition Helpers.hpp:88
void DescribeInnerMatchersImpl(std::ostream *out) const
Definition Helpers.hpp:115
#define DEFINE_EVENT_MATCHER(__eventtype__)
Definition Helpers.hpp:16
#define DEFINE_EVENT_WITH_NAME_MATCHER(__eventtype__)
Definition Helpers.hpp:45
auto ContainsInOrder(Matchers &&... matchers)
Definition Helpers.hpp:136
Allocate-data event associated with Kokkos::Tools::Experimental::EventSet::allocate_data.
Definition Events.hpp:113
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-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