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 #include "tensorflow/compiler/xla/service/pattern_matcher_gmock.h"
17 #include "tensorflow/compiler/xla/service/pattern_matcher.h"
18 #include "tensorflow/compiler/xla/shape_util.h"
19 #include "tensorflow/compiler/xla/test.h"
20 #include "tensorflow/core/platform/test.h"
21
22 namespace xla {
23 namespace {
24
25 namespace m = ::xla::match;
26 using ::testing::Not;
27
28 template <typename MatchedTy>
Describe(const::testing::Matcher<MatchedTy> & m)29 string Describe(const ::testing::Matcher<MatchedTy>& m) {
30 std::stringstream ss;
31 m.DescribeTo(&ss);
32 return ss.str();
33 }
34
35 template <typename MatchedTy>
Explain(const MatchedTy & val,const::testing::Matcher<typename std::remove_cv<MatchedTy>::type> & m)36 string Explain(
37 const MatchedTy& val,
38 const ::testing::Matcher<typename std::remove_cv<MatchedTy>::type>& m) {
39 ::testing::StringMatchResultListener listener;
40 EXPECT_THAT(val, ::testing::Not(m)); // For the error message.
41 EXPECT_FALSE(m.MatchAndExplain(val, &listener));
42 return listener.str();
43 }
44
45 // This file tests the GmockMatch function. The actual explanation and
46 // description returned by matchers is tested in pattern_matchers_test.
TEST(PatternMatcherGmock,MatchShape)47 TEST(PatternMatcherGmock, MatchShape) {
48 Shape s = ShapeUtil::MakeShape(F32, {10, 100});
49 // You can pass const Shape& or a const Shape*.
50 EXPECT_THAT(s, GmockMatch(m::Shape()));
51 EXPECT_THAT(&s, Not(GmockMatch(m::Shape().WithElementType(F16))));
52 EXPECT_THAT(Describe<Shape>(GmockMatch(m::Shape().IsArray())),
53 "a shape that represents an array");
54 }
55
TEST(PatternMatcherGmock,MatchLayout)56 TEST(PatternMatcherGmock, MatchLayout) {
57 Layout l = LayoutUtil::MakeLayout({0, 1});
58 EXPECT_THAT(l, GmockMatch(m::Layout()));
59 EXPECT_THAT(&l, Not(GmockMatch(m::Layout().WithSparseFormat())));
60 EXPECT_THAT(Describe<Layout>(GmockMatch(m::Layout().WithSparseFormat())),
61 "a layout with format SPARSE");
62 }
63
TEST(PatternMatchGmock,MatchInstruction)64 TEST(PatternMatchGmock, MatchInstruction) {
65 auto instr =
66 HloInstruction::CreateParameter(0, ShapeUtil::MakeShape(F32, {42}), "p");
67 EXPECT_THAT(instr.get(), GmockMatch(m::Parameter()));
68 EXPECT_THAT(*instr, GmockMatch(m::Parameter(0)));
69 EXPECT_THAT(*instr, Not(GmockMatch(m::Parameter(1))));
70 EXPECT_THAT(Describe<HloInstruction*>(GmockMatch(m::Parameter())),
71 "an HloInstruction with opcode parameter");
72 }
73
74 } // anonymous namespace
75 } // namespace xla
76