• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_PATTERN_MATCHER_GMOCK_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_PATTERN_MATCHER_GMOCK_H_
18 
19 #include <ostream>
20 #include "tensorflow/compiler/xla/service/pattern_matcher.h"
21 #include "tensorflow/compiler/xla/test.h"
22 #include "tensorflow/core/platform/test.h"
23 
24 namespace xla {
25 
26 namespace pattern_matcher_gmock_detail {
27 template <typename Pattern>
28 class GmockMatcher {
29  public:
GmockMatcher(Pattern p)30   explicit GmockMatcher(Pattern p) : pattern_(std::move(p)) {}
31 
32   // In service of better error messages, list out the overloads explicitly
33   // rather than just using a template.  gMock's polymorphism plus
34   // pattern_matcher yields some pretty gnarly stuff.
MatchAndExplain(const Layout & l,::testing::MatchResultListener * listener)35   bool MatchAndExplain(const Layout& l,
36                        ::testing::MatchResultListener* listener) const {
37     return MatchAndExplainImpl(&l, listener);
38   }
MatchAndExplain(const Layout * l,::testing::MatchResultListener * listener)39   bool MatchAndExplain(const Layout* l,
40                        ::testing::MatchResultListener* listener) const {
41     return MatchAndExplainImpl(l, listener);
42   }
MatchAndExplain(Layout * l,::testing::MatchResultListener * listener)43   bool MatchAndExplain(Layout* l,
44                        ::testing::MatchResultListener* listener) const {
45     return MatchAndExplainImpl(l, listener);
46   }
47 
MatchAndExplain(const Shape & s,::testing::MatchResultListener * listener)48   bool MatchAndExplain(const Shape& s,
49                        ::testing::MatchResultListener* listener) const {
50     return MatchAndExplainImpl(&s, listener);
51   }
MatchAndExplain(const Shape * s,::testing::MatchResultListener * listener)52   bool MatchAndExplain(const Shape* s,
53                        ::testing::MatchResultListener* listener) const {
54     return MatchAndExplainImpl(s, listener);
55   }
MatchAndExplain(Shape * s,::testing::MatchResultListener * listener)56   bool MatchAndExplain(Shape* s,
57                        ::testing::MatchResultListener* listener) const {
58     return MatchAndExplainImpl(s, listener);
59   }
60 
MatchAndExplain(const HloInstruction & instr,::testing::MatchResultListener * listener)61   bool MatchAndExplain(const HloInstruction& instr,
62                        ::testing::MatchResultListener* listener) const {
63     return MatchAndExplainImpl(&instr, listener);
64   }
MatchAndExplain(const HloInstruction * instr,::testing::MatchResultListener * listener)65   bool MatchAndExplain(const HloInstruction* instr,
66                        ::testing::MatchResultListener* listener) const {
67     return MatchAndExplainImpl(instr, listener);
68   }
MatchAndExplain(HloInstruction * instr,::testing::MatchResultListener * listener)69   bool MatchAndExplain(HloInstruction* instr,
70                        ::testing::MatchResultListener* listener) const {
71     return MatchAndExplainImpl(instr, listener);
72   }
73 
DescribeTo(std::ostream * os)74   void DescribeTo(std::ostream* os) const { pattern_.DescribeTo(os); }
75 
DescribeNegationTo(std::ostream * os)76   void DescribeNegationTo(std::ostream* os) const {
77     *os << "is NOT: ";
78     DescribeTo(os);
79   }
80 
81  private:
82   template <typename T>
MatchAndExplainImpl(T * t,::testing::MatchResultListener * listener)83   bool MatchAndExplainImpl(T* t,
84                            ::testing::MatchResultListener* listener) const {
85     MatchOption options{/*.capture=*/true, /*.explain_os=*/listener->stream()};
86     return Match(t, pattern_, options);
87   }
88 
89   Pattern pattern_;
90 };
91 }  // namespace pattern_matcher_gmock_detail
92 
93 template <typename Pattern>
94 ::testing::PolymorphicMatcher<
95     pattern_matcher_gmock_detail::GmockMatcher<Pattern>>
GmockMatch(Pattern && p)96 GmockMatch(Pattern&& p) {
97   return ::testing::MakePolymorphicMatcher(
98       pattern_matcher_gmock_detail::GmockMatcher<Pattern>(
99           std::forward<Pattern>(p)));
100 }
101 
102 }  // namespace xla
103 
104 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_PATTERN_MATCHER_GMOCK_H_
105