| 23 | namespace testing { |
| 24 | |
| 25 | bool HloMatcher::MatchAndExplain( |
| 26 | const HloInstruction* instruction, |
| 27 | ::testing::MatchResultListener* listener) const { |
| 28 | // These cases are self-explanatory from the printed value. |
| 29 | if (!instruction) { |
| 30 | return false; |
| 31 | } |
| 32 | *listener << "(" << instruction->ToString() << ")"; |
| 33 | if (instruction->opcode() != opcode_) { |
| 34 | return false; |
| 35 | } |
| 36 | // Special case: no operand matchers means don't verify. |
| 37 | if (operands_.empty()) { |
| 38 | return true; |
| 39 | } |
| 40 | const auto& operands = instruction->operands(); |
| 41 | if (operands.size() != operands_.size()) { |
| 42 | *listener << " has too " |
| 43 | << (operands.size() > operands_.size() ? "many" : "few") |
| 44 | << " operands (got " << operands.size() << ", want " |
| 45 | << operands_.size() << ")"; |
| 46 | return false; |
| 47 | } |
| 48 | for (int index = 0; index < operands.size(); index++) { |
| 49 | ::testing::StringMatchResultListener inner_listener; |
| 50 | if (!operands_[index].MatchAndExplain(operands[index], &inner_listener)) { |
| 51 | if (listener->IsInterested()) { |
| 52 | *listener << "\noperand " << index << ":\n\t" |
| 53 | << operands[index]->ToString() |
| 54 | << "\ndoesn't match expected:\n\t"; |
| 55 | operands_[index].DescribeTo(listener->stream()); |
| 56 | string explanation = inner_listener.str(); |
| 57 | if (!explanation.empty()) { |
| 58 | *listener << ", " << explanation; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void HloMatcher::DescribeTo(::std::ostream* os) const { |
| 68 | *os << opcode_; |