diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2017-04-14 03:23:58 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2017-04-14 04:59:25 -0700 |
commit | efce4a0452f1eba55fd35a22d20249f449f0debe (patch) | |
tree | 8466b425dfe32edf90a37fe59c087ef1e3c376ff /tensorflow/compiler/xla/service/hlo_matchers.cc | |
parent | 1c0adf6e58f09201ba5f00b780c1e5700a33c789 (diff) |
Added and using GMock matcher for HloInstruction
Change: 153159175
Diffstat (limited to 'tensorflow/compiler/xla/service/hlo_matchers.cc')
-rw-r--r-- | tensorflow/compiler/xla/service/hlo_matchers.cc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tensorflow/compiler/xla/service/hlo_matchers.cc b/tensorflow/compiler/xla/service/hlo_matchers.cc new file mode 100644 index 0000000000..e022c4836d --- /dev/null +++ b/tensorflow/compiler/xla/service/hlo_matchers.cc @@ -0,0 +1,77 @@ +/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include "tensorflow/compiler/xla/service/hlo_matchers.h" + +#include "tensorflow/compiler/xla/service/hlo_instruction.h" +#include "tensorflow/compiler/xla/test.h" + +namespace xla { +namespace testing { + +bool HloMatcher::MatchAndExplain( + const HloInstruction* instruction, + ::testing::MatchResultListener* listener) const { + // These cases are self-explanatory from the printed value. + if (!instruction || instruction->opcode() != opcode_) { + return false; + } + // Special case: no operand matchers means don't verify. + if (operands_.empty()) { + return true; + } + const auto& operands = instruction->operands(); + if (operands.size() != operands_.size()) { + *listener << "has too " + << (operands.size() > operands_.size() ? "many" : "few") + << " operands (got " << operands.size() << ", want " + << operands_.size() << ")"; + return false; + } + for (int index = 0; index < operands.size(); index++) { + ::testing::StringMatchResultListener inner_listener; + if (!operands_[index].MatchAndExplain(operands[index], &inner_listener)) { + if (listener->IsInterested()) { + *listener << "\noperand " << index << ":\n\t" + << operands[index]->ToString() + << "\ndoesn't match expected:\n\t"; + operands_[index].DescribeTo(listener->stream()); + string explanation = inner_listener.str(); + if (!explanation.empty()) { + *listener << ", " << explanation; + } + } + return false; + } + } + return true; +} + +void HloMatcher::DescribeTo(::std::ostream* os) const { + *os << opcode_; + if (!operands_.empty()) { + *os << "("; + for (int i = 0; i < operands_.size(); i++) { + if (i > 0) { + *os << ", "; + } + operands_[i].DescribeTo(os); + } + *os << ")"; + } +} + +} // namespace testing +} // namespace xla |