Blame googlemock/src/gmock-matchers.cc

Packit bd1cd8
// Copyright 2007, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
// Google Mock - a framework for writing C++ mock classes.
Packit bd1cd8
//
Packit bd1cd8
// This file implements Matcher<const string&>, Matcher<string>, and
Packit bd1cd8
// utilities for defining matchers.
Packit bd1cd8
Packit bd1cd8
#include "gmock/gmock-matchers.h"
Packit bd1cd8
#include "gmock/gmock-generated-matchers.h"
Packit bd1cd8
Packit bd1cd8
#include <string.h>
Packit bd1cd8
#include <sstream>
Packit bd1cd8
#include <string>
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a const string& whose value is
Packit bd1cd8
// equal to s.
Packit bd1cd8
Matcher<const internal::string&>::Matcher(const internal::string& s) {
Packit bd1cd8
  *this = Eq(s);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a const string& whose value is
Packit bd1cd8
// equal to s.
Packit bd1cd8
Matcher<const internal::string&>::Matcher(const char* s) {
Packit bd1cd8
  *this = Eq(internal::string(s));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a string whose value is equal to s.
Packit bd1cd8
Matcher<internal::string>::Matcher(const internal::string& s) { *this = Eq(s); }
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a string whose value is equal to s.
Packit bd1cd8
Matcher<internal::string>::Matcher(const char* s) {
Packit bd1cd8
  *this = Eq(internal::string(s));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_STRING_PIECE_
Packit bd1cd8
// Constructs a matcher that matches a const StringPiece& whose value is
Packit bd1cd8
// equal to s.
Packit bd1cd8
Matcher<const StringPiece&>::Matcher(const internal::string& s) {
Packit bd1cd8
  *this = Eq(s);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a const StringPiece& whose value is
Packit bd1cd8
// equal to s.
Packit bd1cd8
Matcher<const StringPiece&>::Matcher(const char* s) {
Packit bd1cd8
  *this = Eq(internal::string(s));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a const StringPiece& whose value is
Packit bd1cd8
// equal to s.
Packit bd1cd8
Matcher<const StringPiece&>::Matcher(StringPiece s) {
Packit bd1cd8
  *this = Eq(s.ToString());
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a StringPiece whose value is equal to s.
Packit bd1cd8
Matcher<StringPiece>::Matcher(const internal::string& s) {
Packit bd1cd8
  *this = Eq(s);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a StringPiece whose value is equal to s.
Packit bd1cd8
Matcher<StringPiece>::Matcher(const char* s) {
Packit bd1cd8
  *this = Eq(internal::string(s));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Constructs a matcher that matches a StringPiece whose value is equal to s.
Packit bd1cd8
Matcher<StringPiece>::Matcher(StringPiece s) {
Packit bd1cd8
  *this = Eq(s.ToString());
Packit bd1cd8
}
Packit bd1cd8
#endif  // GTEST_HAS_STRING_PIECE_
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Joins a vector of strings as if they are fields of a tuple; returns
Packit bd1cd8
// the joined string.
Packit bd1cd8
GTEST_API_ string JoinAsTuple(const Strings& fields) {
Packit bd1cd8
  switch (fields.size()) {
Packit bd1cd8
    case 0:
Packit bd1cd8
      return "";
Packit bd1cd8
    case 1:
Packit bd1cd8
      return fields[0];
Packit bd1cd8
    default:
Packit bd1cd8
      string result = "(" + fields[0];
Packit bd1cd8
      for (size_t i = 1; i < fields.size(); i++) {
Packit bd1cd8
        result += ", ";
Packit bd1cd8
        result += fields[i];
Packit bd1cd8
      }
Packit bd1cd8
      result += ")";
Packit bd1cd8
      return result;
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Returns the description for a matcher defined using the MATCHER*()
Packit bd1cd8
// macro where the user-supplied description string is "", if
Packit bd1cd8
// 'negation' is false; otherwise returns the description of the
Packit bd1cd8
// negation of the matcher.  'param_values' contains a list of strings
Packit bd1cd8
// that are the print-out of the matcher's parameters.
Packit bd1cd8
GTEST_API_ string FormatMatcherDescription(bool negation,
Packit bd1cd8
                                           const char* matcher_name,
Packit bd1cd8
                                           const Strings& param_values) {
Packit bd1cd8
  string result = ConvertIdentifierNameToWords(matcher_name);
Packit bd1cd8
  if (param_values.size() >= 1)
Packit bd1cd8
    result += " " + JoinAsTuple(param_values);
Packit bd1cd8
  return negation ? "not (" + result + ")" : result;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// FindMaxBipartiteMatching and its helper class.
Packit bd1cd8
//
Packit bd1cd8
// Uses the well-known Ford-Fulkerson max flow method to find a maximum
Packit bd1cd8
// bipartite matching. Flow is considered to be from left to right.
Packit bd1cd8
// There is an implicit source node that is connected to all of the left
Packit bd1cd8
// nodes, and an implicit sink node that is connected to all of the
Packit bd1cd8
// right nodes. All edges have unit capacity.
Packit bd1cd8
//
Packit bd1cd8
// Neither the flow graph nor the residual flow graph are represented
Packit bd1cd8
// explicitly. Instead, they are implied by the information in 'graph' and
Packit bd1cd8
// a vector<int> called 'left_' whose elements are initialized to the
Packit bd1cd8
// value kUnused. This represents the initial state of the algorithm,
Packit bd1cd8
// where the flow graph is empty, and the residual flow graph has the
Packit bd1cd8
// following edges:
Packit bd1cd8
//   - An edge from source to each left_ node
Packit bd1cd8
//   - An edge from each right_ node to sink
Packit bd1cd8
//   - An edge from each left_ node to each right_ node, if the
Packit bd1cd8
//     corresponding edge exists in 'graph'.
Packit bd1cd8
//
Packit bd1cd8
// When the TryAugment() method adds a flow, it sets left_[l] = r for some
Packit bd1cd8
// nodes l and r. This induces the following changes:
Packit bd1cd8
//   - The edges (source, l), (l, r), and (r, sink) are added to the
Packit bd1cd8
//     flow graph.
Packit bd1cd8
//   - The same three edges are removed from the residual flow graph.
Packit bd1cd8
//   - The reverse edges (l, source), (r, l), and (sink, r) are added
Packit bd1cd8
//     to the residual flow graph, which is a directional graph
Packit bd1cd8
//     representing unused flow capacity.
Packit bd1cd8
//
Packit bd1cd8
// When the method augments a flow (moving left_[l] from some r1 to some
Packit bd1cd8
// other r2), this can be thought of as "undoing" the above steps with
Packit bd1cd8
// respect to r1 and "redoing" them with respect to r2.
Packit bd1cd8
//
Packit bd1cd8
// It bears repeating that the flow graph and residual flow graph are
Packit bd1cd8
// never represented explicitly, but can be derived by looking at the
Packit bd1cd8
// information in 'graph' and in left_.
Packit bd1cd8
//
Packit bd1cd8
// As an optimization, there is a second vector<int> called right_ which
Packit bd1cd8
// does not provide any new information. Instead, it enables more
Packit bd1cd8
// efficient queries about edges entering or leaving the right-side nodes
Packit bd1cd8
// of the flow or residual flow graphs. The following invariants are
Packit bd1cd8
// maintained:
Packit bd1cd8
//
Packit bd1cd8
// left[l] == kUnused or right[left[l]] == l
Packit bd1cd8
// right[r] == kUnused or left[right[r]] == r
Packit bd1cd8
//
Packit bd1cd8
// . [ source ]                                        .
Packit bd1cd8
// .   |||                                             .
Packit bd1cd8
// .   |||                                             .
Packit bd1cd8
// .   ||\--> left[0]=1  ---\    right[0]=-1 ----\     .
Packit bd1cd8
// .   ||                   |                    |     .
Packit bd1cd8
// .   |\---> left[1]=-1    \--> right[1]=0  ---\|     .
Packit bd1cd8
// .   |                                        ||     .
Packit bd1cd8
// .   \----> left[2]=2  ------> right[2]=2  --\||     .
Packit bd1cd8
// .                                           |||     .
Packit bd1cd8
// .         elements           matchers       vvv     .
Packit bd1cd8
// .                                         [ sink ]  .
Packit bd1cd8
//
Packit bd1cd8
// See Also:
Packit bd1cd8
//   [1] Cormen, et al (2001). "Section 26.2: The Ford-Fulkerson method".
Packit bd1cd8
//       "Introduction to Algorithms (Second ed.)", pp. 651-664.
Packit bd1cd8
//   [2] "Ford-Fulkerson algorithm", Wikipedia,
Packit bd1cd8
//       'http://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm'
Packit bd1cd8
class MaxBipartiteMatchState {
Packit bd1cd8
 public:
Packit bd1cd8
  explicit MaxBipartiteMatchState(const MatchMatrix& graph)
Packit bd1cd8
      : graph_(&graph),
Packit bd1cd8
        left_(graph_->LhsSize(), kUnused),
Packit bd1cd8
        right_(graph_->RhsSize(), kUnused) {
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns the edges of a maximal match, each in the form {left, right}.
Packit bd1cd8
  ElementMatcherPairs Compute() {
Packit bd1cd8
    // 'seen' is used for path finding { 0: unseen, 1: seen }.
Packit bd1cd8
    ::std::vector<char> seen;
Packit bd1cd8
    // Searches the residual flow graph for a path from each left node to
Packit bd1cd8
    // the sink in the residual flow graph, and if one is found, add flow
Packit bd1cd8
    // to the graph. It's okay to search through the left nodes once. The
Packit bd1cd8
    // edge from the implicit source node to each previously-visited left
Packit bd1cd8
    // node will have flow if that left node has any path to the sink
Packit bd1cd8
    // whatsoever. Subsequent augmentations can only add flow to the
Packit bd1cd8
    // network, and cannot take away that previous flow unit from the source.
Packit bd1cd8
    // Since the source-to-left edge can only carry one flow unit (or,
Packit bd1cd8
    // each element can be matched to only one matcher), there is no need
Packit bd1cd8
    // to visit the left nodes more than once looking for augmented paths.
Packit bd1cd8
    // The flow is known to be possible or impossible by looking at the
Packit bd1cd8
    // node once.
Packit bd1cd8
    for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) {
Packit bd1cd8
      // Reset the path-marking vector and try to find a path from
Packit bd1cd8
      // source to sink starting at the left_[ilhs] node.
Packit bd1cd8
      GTEST_CHECK_(left_[ilhs] == kUnused)
Packit bd1cd8
          << "ilhs: " << ilhs << ", left_[ilhs]: " << left_[ilhs];
Packit bd1cd8
      // 'seen' initialized to 'graph_->RhsSize()' copies of 0.
Packit bd1cd8
      seen.assign(graph_->RhsSize(), 0);
Packit bd1cd8
      TryAugment(ilhs, &seen);
Packit bd1cd8
    }
Packit bd1cd8
    ElementMatcherPairs result;
Packit bd1cd8
    for (size_t ilhs = 0; ilhs < left_.size(); ++ilhs) {
Packit bd1cd8
      size_t irhs = left_[ilhs];
Packit bd1cd8
      if (irhs == kUnused) continue;
Packit bd1cd8
      result.push_back(ElementMatcherPair(ilhs, irhs));
Packit bd1cd8
    }
Packit bd1cd8
    return result;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  static const size_t kUnused = static_cast<size_t>(-1);
Packit bd1cd8
Packit bd1cd8
  // Perform a depth-first search from left node ilhs to the sink.  If a
Packit bd1cd8
  // path is found, flow is added to the network by linking the left and
Packit bd1cd8
  // right vector elements corresponding each segment of the path.
Packit bd1cd8
  // Returns true if a path to sink was found, which means that a unit of
Packit bd1cd8
  // flow was added to the network. The 'seen' vector elements correspond
Packit bd1cd8
  // to right nodes and are marked to eliminate cycles from the search.
Packit bd1cd8
  //
Packit bd1cd8
  // Left nodes will only be explored at most once because they
Packit bd1cd8
  // are accessible from at most one right node in the residual flow
Packit bd1cd8
  // graph.
Packit bd1cd8
  //
Packit bd1cd8
  // Note that left_[ilhs] is the only element of left_ that TryAugment will
Packit bd1cd8
  // potentially transition from kUnused to another value. Any other
Packit bd1cd8
  // left_ element holding kUnused before TryAugment will be holding it
Packit bd1cd8
  // when TryAugment returns.
Packit bd1cd8
  //
Packit bd1cd8
  bool TryAugment(size_t ilhs, ::std::vector<char>* seen) {
Packit bd1cd8
    for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) {
Packit bd1cd8
      if ((*seen)[irhs])
Packit bd1cd8
        continue;
Packit bd1cd8
      if (!graph_->HasEdge(ilhs, irhs))
Packit bd1cd8
        continue;
Packit bd1cd8
      // There's an available edge from ilhs to irhs.
Packit bd1cd8
      (*seen)[irhs] = 1;
Packit bd1cd8
      // Next a search is performed to determine whether
Packit bd1cd8
      // this edge is a dead end or leads to the sink.
Packit bd1cd8
      //
Packit bd1cd8
      // right_[irhs] == kUnused means that there is residual flow from
Packit bd1cd8
      // right node irhs to the sink, so we can use that to finish this
Packit bd1cd8
      // flow path and return success.
Packit bd1cd8
      //
Packit bd1cd8
      // Otherwise there is residual flow to some ilhs. We push flow
Packit bd1cd8
      // along that path and call ourselves recursively to see if this
Packit bd1cd8
      // ultimately leads to sink.
Packit bd1cd8
      if (right_[irhs] == kUnused || TryAugment(right_[irhs], seen)) {
Packit bd1cd8
        // Add flow from left_[ilhs] to right_[irhs].
Packit bd1cd8
        left_[ilhs] = irhs;
Packit bd1cd8
        right_[irhs] = ilhs;
Packit bd1cd8
        return true;
Packit bd1cd8
      }
Packit bd1cd8
    }
Packit bd1cd8
    return false;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  const MatchMatrix* graph_;  // not owned
Packit bd1cd8
  // Each element of the left_ vector represents a left hand side node
Packit bd1cd8
  // (i.e. an element) and each element of right_ is a right hand side
Packit bd1cd8
  // node (i.e. a matcher). The values in the left_ vector indicate
Packit bd1cd8
  // outflow from that node to a node on the the right_ side. The values
Packit bd1cd8
  // in the right_ indicate inflow, and specify which left_ node is
Packit bd1cd8
  // feeding that right_ node, if any. For example, left_[3] == 1 means
Packit bd1cd8
  // there's a flow from element #3 to matcher #1. Such a flow would also
Packit bd1cd8
  // be redundantly represented in the right_ vector as right_[1] == 3.
Packit bd1cd8
  // Elements of left_ and right_ are either kUnused or mutually
Packit bd1cd8
  // referent. Mutually referent means that left_[right_[i]] = i and
Packit bd1cd8
  // right_[left_[i]] = i.
Packit bd1cd8
  ::std::vector<size_t> left_;
Packit bd1cd8
  ::std::vector<size_t> right_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_ASSIGN_(MaxBipartiteMatchState);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
const size_t MaxBipartiteMatchState::kUnused;
Packit bd1cd8
Packit bd1cd8
GTEST_API_ ElementMatcherPairs
Packit bd1cd8
FindMaxBipartiteMatching(const MatchMatrix& g) {
Packit bd1cd8
  return MaxBipartiteMatchState(g).Compute();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
static void LogElementMatcherPairVec(const ElementMatcherPairs& pairs,
Packit bd1cd8
                                     ::std::ostream* stream) {
Packit bd1cd8
  typedef ElementMatcherPairs::const_iterator Iter;
Packit bd1cd8
  ::std::ostream& os = *stream;
Packit bd1cd8
  os << "{";
Packit bd1cd8
  const char *sep = "";
Packit bd1cd8
  for (Iter it = pairs.begin(); it != pairs.end(); ++it) {
Packit bd1cd8
    os << sep << "\n  ("
Packit bd1cd8
       << "element #" << it->first << ", "
Packit bd1cd8
       << "matcher #" << it->second << ")";
Packit bd1cd8
    sep = ",";
Packit bd1cd8
  }
Packit bd1cd8
  os << "\n}";
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Tries to find a pairing, and explains the result.
Packit bd1cd8
GTEST_API_ bool FindPairing(const MatchMatrix& matrix,
Packit bd1cd8
                            MatchResultListener* listener) {
Packit bd1cd8
  ElementMatcherPairs matches = FindMaxBipartiteMatching(matrix);
Packit bd1cd8
Packit bd1cd8
  size_t max_flow = matches.size();
Packit bd1cd8
  bool result = (max_flow == matrix.RhsSize());
Packit bd1cd8
Packit bd1cd8
  if (!result) {
Packit bd1cd8
    if (listener->IsInterested()) {
Packit bd1cd8
      *listener << "where no permutation of the elements can "
Packit bd1cd8
                   "satisfy all matchers, and the closest match is "
Packit bd1cd8
                << max_flow << " of " << matrix.RhsSize()
Packit bd1cd8
                << " matchers with the pairings:\n";
Packit bd1cd8
      LogElementMatcherPairVec(matches, listener->stream());
Packit bd1cd8
    }
Packit bd1cd8
    return false;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  if (matches.size() > 1) {
Packit bd1cd8
    if (listener->IsInterested()) {
Packit bd1cd8
      const char *sep = "where:\n";
Packit bd1cd8
      for (size_t mi = 0; mi < matches.size(); ++mi) {
Packit bd1cd8
        *listener << sep << " - element #" << matches[mi].first
Packit bd1cd8
                  << " is matched by matcher #" << matches[mi].second;
Packit bd1cd8
        sep = ",\n";
Packit bd1cd8
      }
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
  return true;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
bool MatchMatrix::NextGraph() {
Packit bd1cd8
  for (size_t ilhs = 0; ilhs < LhsSize(); ++ilhs) {
Packit bd1cd8
    for (size_t irhs = 0; irhs < RhsSize(); ++irhs) {
Packit bd1cd8
      char& b = matched_[SpaceIndex(ilhs, irhs)];
Packit bd1cd8
      if (!b) {
Packit bd1cd8
        b = 1;
Packit bd1cd8
        return true;
Packit bd1cd8
      }
Packit bd1cd8
      b = 0;
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
  return false;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
void MatchMatrix::Randomize() {
Packit bd1cd8
  for (size_t ilhs = 0; ilhs < LhsSize(); ++ilhs) {
Packit bd1cd8
    for (size_t irhs = 0; irhs < RhsSize(); ++irhs) {
Packit bd1cd8
      char& b = matched_[SpaceIndex(ilhs, irhs)];
Packit bd1cd8
      b = static_cast<char>(rand() & 1);  // NOLINT
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
string MatchMatrix::DebugString() const {
Packit bd1cd8
  ::std::stringstream ss;
Packit bd1cd8
  const char *sep = "";
Packit bd1cd8
  for (size_t i = 0; i < LhsSize(); ++i) {
Packit bd1cd8
    ss << sep;
Packit bd1cd8
    for (size_t j = 0; j < RhsSize(); ++j) {
Packit bd1cd8
      ss << HasEdge(i, j);
Packit bd1cd8
    }
Packit bd1cd8
    sep = ";";
Packit bd1cd8
  }
Packit bd1cd8
  return ss.str();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
void UnorderedElementsAreMatcherImplBase::DescribeToImpl(
Packit bd1cd8
    ::std::ostream* os) const {
Packit bd1cd8
  if (matcher_describers_.empty()) {
Packit bd1cd8
    *os << "is empty";
Packit bd1cd8
    return;
Packit bd1cd8
  }
Packit bd1cd8
  if (matcher_describers_.size() == 1) {
Packit bd1cd8
    *os << "has " << Elements(1) << " and that element ";
Packit bd1cd8
    matcher_describers_[0]->DescribeTo(os);
Packit bd1cd8
    return;
Packit bd1cd8
  }
Packit bd1cd8
  *os << "has " << Elements(matcher_describers_.size())
Packit bd1cd8
      << " and there exists some permutation of elements such that:\n";
Packit bd1cd8
  const char* sep = "";
Packit bd1cd8
  for (size_t i = 0; i != matcher_describers_.size(); ++i) {
Packit bd1cd8
    *os << sep << " - element #" << i << " ";
Packit bd1cd8
    matcher_describers_[i]->DescribeTo(os);
Packit bd1cd8
    sep = ", and\n";
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
void UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(
Packit bd1cd8
    ::std::ostream* os) const {
Packit bd1cd8
  if (matcher_describers_.empty()) {
Packit bd1cd8
    *os << "isn't empty";
Packit bd1cd8
    return;
Packit bd1cd8
  }
Packit bd1cd8
  if (matcher_describers_.size() == 1) {
Packit bd1cd8
    *os << "doesn't have " << Elements(1)
Packit bd1cd8
        << ", or has " << Elements(1) << " that ";
Packit bd1cd8
    matcher_describers_[0]->DescribeNegationTo(os);
Packit bd1cd8
    return;
Packit bd1cd8
  }
Packit bd1cd8
  *os << "doesn't have " << Elements(matcher_describers_.size())
Packit bd1cd8
      << ", or there exists no permutation of elements such that:\n";
Packit bd1cd8
  const char* sep = "";
Packit bd1cd8
  for (size_t i = 0; i != matcher_describers_.size(); ++i) {
Packit bd1cd8
    *os << sep << " - element #" << i << " ";
Packit bd1cd8
    matcher_describers_[i]->DescribeTo(os);
Packit bd1cd8
    sep = ", and\n";
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Checks that all matchers match at least one element, and that all
Packit bd1cd8
// elements match at least one matcher. This enables faster matching
Packit bd1cd8
// and better error reporting.
Packit bd1cd8
// Returns false, writing an explanation to 'listener', if and only
Packit bd1cd8
// if the success criteria are not met.
Packit bd1cd8
bool UnorderedElementsAreMatcherImplBase::
Packit bd1cd8
VerifyAllElementsAndMatchersAreMatched(
Packit bd1cd8
    const ::std::vector<string>& element_printouts,
Packit bd1cd8
    const MatchMatrix& matrix,
Packit bd1cd8
    MatchResultListener* listener) const {
Packit bd1cd8
  bool result = true;
Packit bd1cd8
  ::std::vector<char> element_matched(matrix.LhsSize(), 0);
Packit bd1cd8
  ::std::vector<char> matcher_matched(matrix.RhsSize(), 0);
Packit bd1cd8
Packit bd1cd8
  for (size_t ilhs = 0; ilhs < matrix.LhsSize(); ilhs++) {
Packit bd1cd8
    for (size_t irhs = 0; irhs < matrix.RhsSize(); irhs++) {
Packit bd1cd8
      char matched = matrix.HasEdge(ilhs, irhs);
Packit bd1cd8
      element_matched[ilhs] |= matched;
Packit bd1cd8
      matcher_matched[irhs] |= matched;
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  {
Packit bd1cd8
    const char* sep =
Packit bd1cd8
        "where the following matchers don't match any elements:\n";
Packit bd1cd8
    for (size_t mi = 0; mi < matcher_matched.size(); ++mi) {
Packit bd1cd8
      if (matcher_matched[mi])
Packit bd1cd8
        continue;
Packit bd1cd8
      result = false;
Packit bd1cd8
      if (listener->IsInterested()) {
Packit bd1cd8
        *listener << sep << "matcher #" << mi << ": ";
Packit bd1cd8
        matcher_describers_[mi]->DescribeTo(listener->stream());
Packit bd1cd8
        sep = ",\n";
Packit bd1cd8
      }
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  {
Packit bd1cd8
    const char* sep =
Packit bd1cd8
        "where the following elements don't match any matchers:\n";
Packit bd1cd8
    const char* outer_sep = "";
Packit bd1cd8
    if (!result) {
Packit bd1cd8
      outer_sep = "\nand ";
Packit bd1cd8
    }
Packit bd1cd8
    for (size_t ei = 0; ei < element_matched.size(); ++ei) {
Packit bd1cd8
      if (element_matched[ei])
Packit bd1cd8
        continue;
Packit bd1cd8
      result = false;
Packit bd1cd8
      if (listener->IsInterested()) {
Packit bd1cd8
        *listener << outer_sep << sep << "element #" << ei << ": "
Packit bd1cd8
                  << element_printouts[ei];
Packit bd1cd8
        sep = ",\n";
Packit bd1cd8
        outer_sep = "";
Packit bd1cd8
      }
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
  return result;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
}  // namespace testing