Home | History | Annotate | Download | only in service
      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:
     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.
     35   bool MatchAndExplain(const Layout& l,
     36                        ::testing::MatchResultListener* listener) const {
     37     return MatchAndExplainImpl(&l, listener);
     38   }
     39   bool MatchAndExplain(const Layout* l,
     40                        ::testing::MatchResultListener* listener) const {
     41     return MatchAndExplainImpl(l, listener);
     42   }
     43 
     44   bool MatchAndExplain(const Shape& s,
     45                        ::testing::MatchResultListener* listener) const {
     46     return MatchAndExplainImpl(&s, listener);
     47   }
     48   bool MatchAndExplain(const Shape* s,
     49                        ::testing::MatchResultListener* listener) const {
     50     return MatchAndExplainImpl(s, listener);
     51   }
     52 
     53   bool MatchAndExplain(const HloInstruction& instr,
     54                        ::testing::MatchResultListener* listener) const {
     55     return MatchAndExplainImpl(&instr, listener);
     56   }
     57   bool MatchAndExplain(const HloInstruction* instr,
     58                        ::testing::MatchResultListener* listener) const {
     59     return MatchAndExplainImpl(instr, listener);
     60   }
     61 
     62   void DescribeTo(std::ostream* os) const { pattern_.DescribeTo(os); }
     63 
     64   void DescribeNegationTo(std::ostream* os) const {
     65     *os << "is NOT: ";
     66     DescribeTo(os);
     67   }
     68 
     69  private:
     70   template <typename T>
     71   bool MatchAndExplainImpl(const T* t,
     72                            ::testing::MatchResultListener* listener) const {
     73     MatchOption options{/*.capture=*/true, /*.explain_os=*/listener->stream()};
     74     return Match(t, pattern_, options);
     75   }
     76 
     77   Pattern pattern_;
     78 };
     79 }  // namespace pattern_matcher_gmock_detail
     80 
     81 template <typename Pattern>
     82 ::testing::PolymorphicMatcher<
     83     pattern_matcher_gmock_detail::GmockMatcher<Pattern>>
     84 GmockMatch(Pattern&& p) {
     85   return ::testing::MakePolymorphicMatcher(
     86       pattern_matcher_gmock_detail::GmockMatcher<Pattern>(
     87           std::forward<Pattern>(p)));
     88 }
     89 
     90 }  // namespace xla
     91 
     92 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_PATTERN_MATCHER_GMOCK_H_
     93