xref: /freebsd/contrib/googletest/googlemock/src/gmock-matchers.cc (revision 5ca8c28cd8c725b81781201cfdb5f9969396f934)
1b89a7cc2SEnji Cooper // Copyright 2007, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper 
30b89a7cc2SEnji Cooper // Google Mock - a framework for writing C++ mock classes.
31b89a7cc2SEnji Cooper //
32b89a7cc2SEnji Cooper // This file implements Matcher<const string&>, Matcher<string>, and
33b89a7cc2SEnji Cooper // utilities for defining matchers.
34b89a7cc2SEnji Cooper 
35b89a7cc2SEnji Cooper #include "gmock/gmock-matchers.h"
36b89a7cc2SEnji Cooper 
37b89a7cc2SEnji Cooper #include <string.h>
3828f6c2f2SEnji Cooper 
39b89a7cc2SEnji Cooper #include <iostream>
40b89a7cc2SEnji Cooper #include <sstream>
41b89a7cc2SEnji Cooper #include <string>
4228f6c2f2SEnji Cooper #include <vector>
43b89a7cc2SEnji Cooper 
44b89a7cc2SEnji Cooper namespace testing {
45b89a7cc2SEnji Cooper namespace internal {
46b89a7cc2SEnji Cooper 
47b89a7cc2SEnji Cooper // Returns the description for a matcher defined using the MATCHER*()
48b89a7cc2SEnji Cooper // macro where the user-supplied description string is "", if
49b89a7cc2SEnji Cooper // 'negation' is false; otherwise returns the description of the
50b89a7cc2SEnji Cooper // negation of the matcher.  'param_values' contains a list of strings
51b89a7cc2SEnji Cooper // that are the print-out of the matcher's parameters.
FormatMatcherDescription(bool negation,const char * matcher_name,const std::vector<const char * > & param_names,const Strings & param_values)5228f6c2f2SEnji Cooper GTEST_API_ std::string FormatMatcherDescription(
5328f6c2f2SEnji Cooper     bool negation, const char* matcher_name,
5428f6c2f2SEnji Cooper     const std::vector<const char*>& param_names, const Strings& param_values) {
55b89a7cc2SEnji Cooper   std::string result = ConvertIdentifierNameToWords(matcher_name);
5628f6c2f2SEnji Cooper   if (!param_values.empty()) {
5728f6c2f2SEnji Cooper     result += " " + JoinAsKeyValueTuple(param_names, param_values);
5828f6c2f2SEnji Cooper   }
59b89a7cc2SEnji Cooper   return negation ? "not (" + result + ")" : result;
60b89a7cc2SEnji Cooper }
61b89a7cc2SEnji Cooper 
62b89a7cc2SEnji Cooper // FindMaxBipartiteMatching and its helper class.
63b89a7cc2SEnji Cooper //
64b89a7cc2SEnji Cooper // Uses the well-known Ford-Fulkerson max flow method to find a maximum
65b89a7cc2SEnji Cooper // bipartite matching. Flow is considered to be from left to right.
66b89a7cc2SEnji Cooper // There is an implicit source node that is connected to all of the left
67b89a7cc2SEnji Cooper // nodes, and an implicit sink node that is connected to all of the
68b89a7cc2SEnji Cooper // right nodes. All edges have unit capacity.
69b89a7cc2SEnji Cooper //
70b89a7cc2SEnji Cooper // Neither the flow graph nor the residual flow graph are represented
71b89a7cc2SEnji Cooper // explicitly. Instead, they are implied by the information in 'graph' and
72b89a7cc2SEnji Cooper // a vector<int> called 'left_' whose elements are initialized to the
73b89a7cc2SEnji Cooper // value kUnused. This represents the initial state of the algorithm,
74b89a7cc2SEnji Cooper // where the flow graph is empty, and the residual flow graph has the
75b89a7cc2SEnji Cooper // following edges:
76b89a7cc2SEnji Cooper //   - An edge from source to each left_ node
77b89a7cc2SEnji Cooper //   - An edge from each right_ node to sink
78b89a7cc2SEnji Cooper //   - An edge from each left_ node to each right_ node, if the
79b89a7cc2SEnji Cooper //     corresponding edge exists in 'graph'.
80b89a7cc2SEnji Cooper //
81b89a7cc2SEnji Cooper // When the TryAugment() method adds a flow, it sets left_[l] = r for some
82b89a7cc2SEnji Cooper // nodes l and r. This induces the following changes:
83b89a7cc2SEnji Cooper //   - The edges (source, l), (l, r), and (r, sink) are added to the
84b89a7cc2SEnji Cooper //     flow graph.
85b89a7cc2SEnji Cooper //   - The same three edges are removed from the residual flow graph.
86b89a7cc2SEnji Cooper //   - The reverse edges (l, source), (r, l), and (sink, r) are added
87b89a7cc2SEnji Cooper //     to the residual flow graph, which is a directional graph
88b89a7cc2SEnji Cooper //     representing unused flow capacity.
89b89a7cc2SEnji Cooper //
90b89a7cc2SEnji Cooper // When the method augments a flow (moving left_[l] from some r1 to some
91b89a7cc2SEnji Cooper // other r2), this can be thought of as "undoing" the above steps with
92b89a7cc2SEnji Cooper // respect to r1 and "redoing" them with respect to r2.
93b89a7cc2SEnji Cooper //
94b89a7cc2SEnji Cooper // It bears repeating that the flow graph and residual flow graph are
95b89a7cc2SEnji Cooper // never represented explicitly, but can be derived by looking at the
96b89a7cc2SEnji Cooper // information in 'graph' and in left_.
97b89a7cc2SEnji Cooper //
98b89a7cc2SEnji Cooper // As an optimization, there is a second vector<int> called right_ which
99b89a7cc2SEnji Cooper // does not provide any new information. Instead, it enables more
100b89a7cc2SEnji Cooper // efficient queries about edges entering or leaving the right-side nodes
101b89a7cc2SEnji Cooper // of the flow or residual flow graphs. The following invariants are
102b89a7cc2SEnji Cooper // maintained:
103b89a7cc2SEnji Cooper //
104b89a7cc2SEnji Cooper // left[l] == kUnused or right[left[l]] == l
105b89a7cc2SEnji Cooper // right[r] == kUnused or left[right[r]] == r
106b89a7cc2SEnji Cooper //
107b89a7cc2SEnji Cooper // . [ source ]                                        .
108b89a7cc2SEnji Cooper // .   |||                                             .
109b89a7cc2SEnji Cooper // .   |||                                             .
110b89a7cc2SEnji Cooper // .   ||\--> left[0]=1  ---\    right[0]=-1 ----\     .
111b89a7cc2SEnji Cooper // .   ||                   |                    |     .
112b89a7cc2SEnji Cooper // .   |\---> left[1]=-1    \--> right[1]=0  ---\|     .
113b89a7cc2SEnji Cooper // .   |                                        ||     .
114b89a7cc2SEnji Cooper // .   \----> left[2]=2  ------> right[2]=2  --\||     .
115b89a7cc2SEnji Cooper // .                                           |||     .
116b89a7cc2SEnji Cooper // .         elements           matchers       vvv     .
117b89a7cc2SEnji Cooper // .                                         [ sink ]  .
118b89a7cc2SEnji Cooper //
119b89a7cc2SEnji Cooper // See Also:
120b89a7cc2SEnji Cooper //   [1] Cormen, et al (2001). "Section 26.2: The Ford-Fulkerson method".
121b89a7cc2SEnji Cooper //       "Introduction to Algorithms (Second ed.)", pp. 651-664.
122b89a7cc2SEnji Cooper //   [2] "Ford-Fulkerson algorithm", Wikipedia,
123*5ca8c28cSEnji Cooper //       'https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm'
124b89a7cc2SEnji Cooper class MaxBipartiteMatchState {
125b89a7cc2SEnji Cooper  public:
MaxBipartiteMatchState(const MatchMatrix & graph)126b89a7cc2SEnji Cooper   explicit MaxBipartiteMatchState(const MatchMatrix& graph)
127b89a7cc2SEnji Cooper       : graph_(&graph),
128b89a7cc2SEnji Cooper         left_(graph_->LhsSize(), kUnused),
129b89a7cc2SEnji Cooper         right_(graph_->RhsSize(), kUnused) {}
130b89a7cc2SEnji Cooper 
131b89a7cc2SEnji Cooper   // Returns the edges of a maximal match, each in the form {left, right}.
Compute()132b89a7cc2SEnji Cooper   ElementMatcherPairs Compute() {
133b89a7cc2SEnji Cooper     // 'seen' is used for path finding { 0: unseen, 1: seen }.
134b89a7cc2SEnji Cooper     ::std::vector<char> seen;
135b89a7cc2SEnji Cooper     // Searches the residual flow graph for a path from each left node to
136b89a7cc2SEnji Cooper     // the sink in the residual flow graph, and if one is found, add flow
137b89a7cc2SEnji Cooper     // to the graph. It's okay to search through the left nodes once. The
138b89a7cc2SEnji Cooper     // edge from the implicit source node to each previously-visited left
139b89a7cc2SEnji Cooper     // node will have flow if that left node has any path to the sink
140b89a7cc2SEnji Cooper     // whatsoever. Subsequent augmentations can only add flow to the
141b89a7cc2SEnji Cooper     // network, and cannot take away that previous flow unit from the source.
142b89a7cc2SEnji Cooper     // Since the source-to-left edge can only carry one flow unit (or,
143b89a7cc2SEnji Cooper     // each element can be matched to only one matcher), there is no need
144b89a7cc2SEnji Cooper     // to visit the left nodes more than once looking for augmented paths.
145b89a7cc2SEnji Cooper     // The flow is known to be possible or impossible by looking at the
146b89a7cc2SEnji Cooper     // node once.
147b89a7cc2SEnji Cooper     for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) {
148b89a7cc2SEnji Cooper       // Reset the path-marking vector and try to find a path from
149b89a7cc2SEnji Cooper       // source to sink starting at the left_[ilhs] node.
150b89a7cc2SEnji Cooper       GTEST_CHECK_(left_[ilhs] == kUnused)
151b89a7cc2SEnji Cooper           << "ilhs: " << ilhs << ", left_[ilhs]: " << left_[ilhs];
152b89a7cc2SEnji Cooper       // 'seen' initialized to 'graph_->RhsSize()' copies of 0.
153b89a7cc2SEnji Cooper       seen.assign(graph_->RhsSize(), 0);
154b89a7cc2SEnji Cooper       TryAugment(ilhs, &seen);
155b89a7cc2SEnji Cooper     }
156b89a7cc2SEnji Cooper     ElementMatcherPairs result;
157b89a7cc2SEnji Cooper     for (size_t ilhs = 0; ilhs < left_.size(); ++ilhs) {
158b89a7cc2SEnji Cooper       size_t irhs = left_[ilhs];
159b89a7cc2SEnji Cooper       if (irhs == kUnused) continue;
160b89a7cc2SEnji Cooper       result.push_back(ElementMatcherPair(ilhs, irhs));
161b89a7cc2SEnji Cooper     }
162b89a7cc2SEnji Cooper     return result;
163b89a7cc2SEnji Cooper   }
164b89a7cc2SEnji Cooper 
165b89a7cc2SEnji Cooper  private:
166b89a7cc2SEnji Cooper   static const size_t kUnused = static_cast<size_t>(-1);
167b89a7cc2SEnji Cooper 
168b89a7cc2SEnji Cooper   // Perform a depth-first search from left node ilhs to the sink.  If a
169b89a7cc2SEnji Cooper   // path is found, flow is added to the network by linking the left and
170b89a7cc2SEnji Cooper   // right vector elements corresponding each segment of the path.
171b89a7cc2SEnji Cooper   // Returns true if a path to sink was found, which means that a unit of
172b89a7cc2SEnji Cooper   // flow was added to the network. The 'seen' vector elements correspond
173b89a7cc2SEnji Cooper   // to right nodes and are marked to eliminate cycles from the search.
174b89a7cc2SEnji Cooper   //
175b89a7cc2SEnji Cooper   // Left nodes will only be explored at most once because they
176b89a7cc2SEnji Cooper   // are accessible from at most one right node in the residual flow
177b89a7cc2SEnji Cooper   // graph.
178b89a7cc2SEnji Cooper   //
179b89a7cc2SEnji Cooper   // Note that left_[ilhs] is the only element of left_ that TryAugment will
180b89a7cc2SEnji Cooper   // potentially transition from kUnused to another value. Any other
181b89a7cc2SEnji Cooper   // left_ element holding kUnused before TryAugment will be holding it
182b89a7cc2SEnji Cooper   // when TryAugment returns.
183b89a7cc2SEnji Cooper   //
TryAugment(size_t ilhs,::std::vector<char> * seen)184b89a7cc2SEnji Cooper   bool TryAugment(size_t ilhs, ::std::vector<char>* seen) {
185b89a7cc2SEnji Cooper     for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) {
186b89a7cc2SEnji Cooper       if ((*seen)[irhs]) continue;
187b89a7cc2SEnji Cooper       if (!graph_->HasEdge(ilhs, irhs)) continue;
188b89a7cc2SEnji Cooper       // There's an available edge from ilhs to irhs.
189b89a7cc2SEnji Cooper       (*seen)[irhs] = 1;
190b89a7cc2SEnji Cooper       // Next a search is performed to determine whether
191b89a7cc2SEnji Cooper       // this edge is a dead end or leads to the sink.
192b89a7cc2SEnji Cooper       //
193b89a7cc2SEnji Cooper       // right_[irhs] == kUnused means that there is residual flow from
194b89a7cc2SEnji Cooper       // right node irhs to the sink, so we can use that to finish this
195b89a7cc2SEnji Cooper       // flow path and return success.
196b89a7cc2SEnji Cooper       //
197b89a7cc2SEnji Cooper       // Otherwise there is residual flow to some ilhs. We push flow
198b89a7cc2SEnji Cooper       // along that path and call ourselves recursively to see if this
199b89a7cc2SEnji Cooper       // ultimately leads to sink.
200b89a7cc2SEnji Cooper       if (right_[irhs] == kUnused || TryAugment(right_[irhs], seen)) {
201b89a7cc2SEnji Cooper         // Add flow from left_[ilhs] to right_[irhs].
202b89a7cc2SEnji Cooper         left_[ilhs] = irhs;
203b89a7cc2SEnji Cooper         right_[irhs] = ilhs;
204b89a7cc2SEnji Cooper         return true;
205b89a7cc2SEnji Cooper       }
206b89a7cc2SEnji Cooper     }
207b89a7cc2SEnji Cooper     return false;
208b89a7cc2SEnji Cooper   }
209b89a7cc2SEnji Cooper 
210b89a7cc2SEnji Cooper   const MatchMatrix* graph_;  // not owned
211b89a7cc2SEnji Cooper   // Each element of the left_ vector represents a left hand side node
212b89a7cc2SEnji Cooper   // (i.e. an element) and each element of right_ is a right hand side
213b89a7cc2SEnji Cooper   // node (i.e. a matcher). The values in the left_ vector indicate
214b89a7cc2SEnji Cooper   // outflow from that node to a node on the right_ side. The values
215b89a7cc2SEnji Cooper   // in the right_ indicate inflow, and specify which left_ node is
216b89a7cc2SEnji Cooper   // feeding that right_ node, if any. For example, left_[3] == 1 means
217b89a7cc2SEnji Cooper   // there's a flow from element #3 to matcher #1. Such a flow would also
218b89a7cc2SEnji Cooper   // be redundantly represented in the right_ vector as right_[1] == 3.
219b89a7cc2SEnji Cooper   // Elements of left_ and right_ are either kUnused or mutually
220b89a7cc2SEnji Cooper   // referent. Mutually referent means that left_[right_[i]] = i and
221b89a7cc2SEnji Cooper   // right_[left_[i]] = i.
222b89a7cc2SEnji Cooper   ::std::vector<size_t> left_;
223b89a7cc2SEnji Cooper   ::std::vector<size_t> right_;
224b89a7cc2SEnji Cooper };
225b89a7cc2SEnji Cooper 
226b89a7cc2SEnji Cooper const size_t MaxBipartiteMatchState::kUnused;
227b89a7cc2SEnji Cooper 
FindMaxBipartiteMatching(const MatchMatrix & g)228b89a7cc2SEnji Cooper GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix& g) {
229b89a7cc2SEnji Cooper   return MaxBipartiteMatchState(g).Compute();
230b89a7cc2SEnji Cooper }
231b89a7cc2SEnji Cooper 
LogElementMatcherPairVec(const ElementMatcherPairs & pairs,::std::ostream * stream)232b89a7cc2SEnji Cooper static void LogElementMatcherPairVec(const ElementMatcherPairs& pairs,
233b89a7cc2SEnji Cooper                                      ::std::ostream* stream) {
234b89a7cc2SEnji Cooper   typedef ElementMatcherPairs::const_iterator Iter;
235b89a7cc2SEnji Cooper   ::std::ostream& os = *stream;
236b89a7cc2SEnji Cooper   os << "{";
237b89a7cc2SEnji Cooper   const char* sep = "";
238b89a7cc2SEnji Cooper   for (Iter it = pairs.begin(); it != pairs.end(); ++it) {
239*5ca8c28cSEnji Cooper     os << sep << "\n  (" << "element #" << it->first << ", " << "matcher #"
240*5ca8c28cSEnji Cooper        << it->second << ")";
241b89a7cc2SEnji Cooper     sep = ",";
242b89a7cc2SEnji Cooper   }
243b89a7cc2SEnji Cooper   os << "\n}";
244b89a7cc2SEnji Cooper }
245b89a7cc2SEnji Cooper 
NextGraph()246b89a7cc2SEnji Cooper bool MatchMatrix::NextGraph() {
247b89a7cc2SEnji Cooper   for (size_t ilhs = 0; ilhs < LhsSize(); ++ilhs) {
248b89a7cc2SEnji Cooper     for (size_t irhs = 0; irhs < RhsSize(); ++irhs) {
249b89a7cc2SEnji Cooper       char& b = matched_[SpaceIndex(ilhs, irhs)];
250b89a7cc2SEnji Cooper       if (!b) {
251b89a7cc2SEnji Cooper         b = 1;
252b89a7cc2SEnji Cooper         return true;
253b89a7cc2SEnji Cooper       }
254b89a7cc2SEnji Cooper       b = 0;
255b89a7cc2SEnji Cooper     }
256b89a7cc2SEnji Cooper   }
257b89a7cc2SEnji Cooper   return false;
258b89a7cc2SEnji Cooper }
259b89a7cc2SEnji Cooper 
Randomize()260b89a7cc2SEnji Cooper void MatchMatrix::Randomize() {
261b89a7cc2SEnji Cooper   for (size_t ilhs = 0; ilhs < LhsSize(); ++ilhs) {
262b89a7cc2SEnji Cooper     for (size_t irhs = 0; irhs < RhsSize(); ++irhs) {
263b89a7cc2SEnji Cooper       char& b = matched_[SpaceIndex(ilhs, irhs)];
264b89a7cc2SEnji Cooper       b = static_cast<char>(rand() & 1);  // NOLINT
265b89a7cc2SEnji Cooper     }
266b89a7cc2SEnji Cooper   }
267b89a7cc2SEnji Cooper }
268b89a7cc2SEnji Cooper 
DebugString() const269b89a7cc2SEnji Cooper std::string MatchMatrix::DebugString() const {
270b89a7cc2SEnji Cooper   ::std::stringstream ss;
271b89a7cc2SEnji Cooper   const char* sep = "";
272b89a7cc2SEnji Cooper   for (size_t i = 0; i < LhsSize(); ++i) {
273b89a7cc2SEnji Cooper     ss << sep;
274b89a7cc2SEnji Cooper     for (size_t j = 0; j < RhsSize(); ++j) {
275b89a7cc2SEnji Cooper       ss << HasEdge(i, j);
276b89a7cc2SEnji Cooper     }
277b89a7cc2SEnji Cooper     sep = ";";
278b89a7cc2SEnji Cooper   }
279b89a7cc2SEnji Cooper   return ss.str();
280b89a7cc2SEnji Cooper }
281b89a7cc2SEnji Cooper 
DescribeToImpl(::std::ostream * os) const282b89a7cc2SEnji Cooper void UnorderedElementsAreMatcherImplBase::DescribeToImpl(
283b89a7cc2SEnji Cooper     ::std::ostream* os) const {
284b89a7cc2SEnji Cooper   switch (match_flags()) {
285b89a7cc2SEnji Cooper     case UnorderedMatcherRequire::ExactMatch:
286b89a7cc2SEnji Cooper       if (matcher_describers_.empty()) {
287b89a7cc2SEnji Cooper         *os << "is empty";
288b89a7cc2SEnji Cooper         return;
289b89a7cc2SEnji Cooper       }
290b89a7cc2SEnji Cooper       if (matcher_describers_.size() == 1) {
291b89a7cc2SEnji Cooper         *os << "has " << Elements(1) << " and that element ";
292b89a7cc2SEnji Cooper         matcher_describers_[0]->DescribeTo(os);
293b89a7cc2SEnji Cooper         return;
294b89a7cc2SEnji Cooper       }
295b89a7cc2SEnji Cooper       *os << "has " << Elements(matcher_describers_.size())
296b89a7cc2SEnji Cooper           << " and there exists some permutation of elements such that:\n";
297b89a7cc2SEnji Cooper       break;
298b89a7cc2SEnji Cooper     case UnorderedMatcherRequire::Superset:
299b89a7cc2SEnji Cooper       *os << "a surjection from elements to requirements exists such that:\n";
300b89a7cc2SEnji Cooper       break;
301b89a7cc2SEnji Cooper     case UnorderedMatcherRequire::Subset:
302b89a7cc2SEnji Cooper       *os << "an injection from elements to requirements exists such that:\n";
303b89a7cc2SEnji Cooper       break;
304b89a7cc2SEnji Cooper   }
305b89a7cc2SEnji Cooper 
306b89a7cc2SEnji Cooper   const char* sep = "";
307b89a7cc2SEnji Cooper   for (size_t i = 0; i != matcher_describers_.size(); ++i) {
308b89a7cc2SEnji Cooper     *os << sep;
309b89a7cc2SEnji Cooper     if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
310b89a7cc2SEnji Cooper       *os << " - element #" << i << " ";
311b89a7cc2SEnji Cooper     } else {
312b89a7cc2SEnji Cooper       *os << " - an element ";
313b89a7cc2SEnji Cooper     }
314b89a7cc2SEnji Cooper     matcher_describers_[i]->DescribeTo(os);
315b89a7cc2SEnji Cooper     if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
316b89a7cc2SEnji Cooper       sep = ", and\n";
317b89a7cc2SEnji Cooper     } else {
318b89a7cc2SEnji Cooper       sep = "\n";
319b89a7cc2SEnji Cooper     }
320b89a7cc2SEnji Cooper   }
321b89a7cc2SEnji Cooper }
322b89a7cc2SEnji Cooper 
DescribeNegationToImpl(::std::ostream * os) const323b89a7cc2SEnji Cooper void UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(
324b89a7cc2SEnji Cooper     ::std::ostream* os) const {
325b89a7cc2SEnji Cooper   switch (match_flags()) {
326b89a7cc2SEnji Cooper     case UnorderedMatcherRequire::ExactMatch:
327b89a7cc2SEnji Cooper       if (matcher_describers_.empty()) {
328b89a7cc2SEnji Cooper         *os << "isn't empty";
329b89a7cc2SEnji Cooper         return;
330b89a7cc2SEnji Cooper       }
331b89a7cc2SEnji Cooper       if (matcher_describers_.size() == 1) {
332b89a7cc2SEnji Cooper         *os << "doesn't have " << Elements(1) << ", or has " << Elements(1)
333b89a7cc2SEnji Cooper             << " that ";
334b89a7cc2SEnji Cooper         matcher_describers_[0]->DescribeNegationTo(os);
335b89a7cc2SEnji Cooper         return;
336b89a7cc2SEnji Cooper       }
337b89a7cc2SEnji Cooper       *os << "doesn't have " << Elements(matcher_describers_.size())
338b89a7cc2SEnji Cooper           << ", or there exists no permutation of elements such that:\n";
339b89a7cc2SEnji Cooper       break;
340b89a7cc2SEnji Cooper     case UnorderedMatcherRequire::Superset:
341b89a7cc2SEnji Cooper       *os << "no surjection from elements to requirements exists such that:\n";
342b89a7cc2SEnji Cooper       break;
343b89a7cc2SEnji Cooper     case UnorderedMatcherRequire::Subset:
344b89a7cc2SEnji Cooper       *os << "no injection from elements to requirements exists such that:\n";
345b89a7cc2SEnji Cooper       break;
346b89a7cc2SEnji Cooper   }
347b89a7cc2SEnji Cooper   const char* sep = "";
348b89a7cc2SEnji Cooper   for (size_t i = 0; i != matcher_describers_.size(); ++i) {
349b89a7cc2SEnji Cooper     *os << sep;
350b89a7cc2SEnji Cooper     if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
351b89a7cc2SEnji Cooper       *os << " - element #" << i << " ";
352b89a7cc2SEnji Cooper     } else {
353b89a7cc2SEnji Cooper       *os << " - an element ";
354b89a7cc2SEnji Cooper     }
355b89a7cc2SEnji Cooper     matcher_describers_[i]->DescribeTo(os);
356b89a7cc2SEnji Cooper     if (match_flags() == UnorderedMatcherRequire::ExactMatch) {
357b89a7cc2SEnji Cooper       sep = ", and\n";
358b89a7cc2SEnji Cooper     } else {
359b89a7cc2SEnji Cooper       sep = "\n";
360b89a7cc2SEnji Cooper     }
361b89a7cc2SEnji Cooper   }
362b89a7cc2SEnji Cooper }
363b89a7cc2SEnji Cooper 
364b89a7cc2SEnji Cooper // Checks that all matchers match at least one element, and that all
365b89a7cc2SEnji Cooper // elements match at least one matcher. This enables faster matching
366b89a7cc2SEnji Cooper // and better error reporting.
367b89a7cc2SEnji Cooper // Returns false, writing an explanation to 'listener', if and only
368b89a7cc2SEnji Cooper // if the success criteria are not met.
VerifyMatchMatrix(const::std::vector<std::string> & element_printouts,const MatchMatrix & matrix,MatchResultListener * listener) const369b89a7cc2SEnji Cooper bool UnorderedElementsAreMatcherImplBase::VerifyMatchMatrix(
370b89a7cc2SEnji Cooper     const ::std::vector<std::string>& element_printouts,
371b89a7cc2SEnji Cooper     const MatchMatrix& matrix, MatchResultListener* listener) const {
37228f6c2f2SEnji Cooper   if (matrix.LhsSize() == 0 && matrix.RhsSize() == 0) {
37328f6c2f2SEnji Cooper     return true;
37428f6c2f2SEnji Cooper   }
37528f6c2f2SEnji Cooper 
376*5ca8c28cSEnji Cooper   const bool is_exact_match_with_size_discrepency =
377*5ca8c28cSEnji Cooper       match_flags() == UnorderedMatcherRequire::ExactMatch &&
378*5ca8c28cSEnji Cooper       matrix.LhsSize() != matrix.RhsSize();
379*5ca8c28cSEnji Cooper   if (is_exact_match_with_size_discrepency) {
38028f6c2f2SEnji Cooper     // The element count doesn't match.  If the container is empty,
38128f6c2f2SEnji Cooper     // there's no need to explain anything as Google Mock already
38228f6c2f2SEnji Cooper     // prints the empty container. Otherwise we just need to show
38328f6c2f2SEnji Cooper     // how many elements there actually are.
38428f6c2f2SEnji Cooper     if (matrix.LhsSize() != 0 && listener->IsInterested()) {
385*5ca8c28cSEnji Cooper       *listener << "which has " << Elements(matrix.LhsSize()) << "\n";
38628f6c2f2SEnji Cooper     }
38728f6c2f2SEnji Cooper   }
38828f6c2f2SEnji Cooper 
389*5ca8c28cSEnji Cooper   bool result = !is_exact_match_with_size_discrepency;
390b89a7cc2SEnji Cooper   ::std::vector<char> element_matched(matrix.LhsSize(), 0);
391b89a7cc2SEnji Cooper   ::std::vector<char> matcher_matched(matrix.RhsSize(), 0);
392b89a7cc2SEnji Cooper 
393b89a7cc2SEnji Cooper   for (size_t ilhs = 0; ilhs < matrix.LhsSize(); ilhs++) {
394b89a7cc2SEnji Cooper     for (size_t irhs = 0; irhs < matrix.RhsSize(); irhs++) {
395b89a7cc2SEnji Cooper       char matched = matrix.HasEdge(ilhs, irhs);
396b89a7cc2SEnji Cooper       element_matched[ilhs] |= matched;
397b89a7cc2SEnji Cooper       matcher_matched[irhs] |= matched;
398b89a7cc2SEnji Cooper     }
399b89a7cc2SEnji Cooper   }
400b89a7cc2SEnji Cooper 
401b89a7cc2SEnji Cooper   if (match_flags() & UnorderedMatcherRequire::Superset) {
402b89a7cc2SEnji Cooper     const char* sep =
403b89a7cc2SEnji Cooper         "where the following matchers don't match any elements:\n";
404b89a7cc2SEnji Cooper     for (size_t mi = 0; mi < matcher_matched.size(); ++mi) {
405b89a7cc2SEnji Cooper       if (matcher_matched[mi]) continue;
406b89a7cc2SEnji Cooper       result = false;
407b89a7cc2SEnji Cooper       if (listener->IsInterested()) {
408b89a7cc2SEnji Cooper         *listener << sep << "matcher #" << mi << ": ";
409b89a7cc2SEnji Cooper         matcher_describers_[mi]->DescribeTo(listener->stream());
410b89a7cc2SEnji Cooper         sep = ",\n";
411b89a7cc2SEnji Cooper       }
412b89a7cc2SEnji Cooper     }
413b89a7cc2SEnji Cooper   }
414b89a7cc2SEnji Cooper 
415b89a7cc2SEnji Cooper   if (match_flags() & UnorderedMatcherRequire::Subset) {
416b89a7cc2SEnji Cooper     const char* sep =
417b89a7cc2SEnji Cooper         "where the following elements don't match any matchers:\n";
418b89a7cc2SEnji Cooper     const char* outer_sep = "";
419b89a7cc2SEnji Cooper     if (!result) {
420b89a7cc2SEnji Cooper       outer_sep = "\nand ";
421b89a7cc2SEnji Cooper     }
422b89a7cc2SEnji Cooper     for (size_t ei = 0; ei < element_matched.size(); ++ei) {
423b89a7cc2SEnji Cooper       if (element_matched[ei]) continue;
424b89a7cc2SEnji Cooper       result = false;
425b89a7cc2SEnji Cooper       if (listener->IsInterested()) {
426b89a7cc2SEnji Cooper         *listener << outer_sep << sep << "element #" << ei << ": "
427b89a7cc2SEnji Cooper                   << element_printouts[ei];
428b89a7cc2SEnji Cooper         sep = ",\n";
429b89a7cc2SEnji Cooper         outer_sep = "";
430b89a7cc2SEnji Cooper       }
431b89a7cc2SEnji Cooper     }
432b89a7cc2SEnji Cooper   }
433b89a7cc2SEnji Cooper   return result;
434b89a7cc2SEnji Cooper }
435b89a7cc2SEnji Cooper 
FindPairing(const MatchMatrix & matrix,MatchResultListener * listener) const436b89a7cc2SEnji Cooper bool UnorderedElementsAreMatcherImplBase::FindPairing(
437b89a7cc2SEnji Cooper     const MatchMatrix& matrix, MatchResultListener* listener) const {
438b89a7cc2SEnji Cooper   ElementMatcherPairs matches = FindMaxBipartiteMatching(matrix);
439b89a7cc2SEnji Cooper 
440b89a7cc2SEnji Cooper   size_t max_flow = matches.size();
441b89a7cc2SEnji Cooper   if ((match_flags() & UnorderedMatcherRequire::Superset) &&
442b89a7cc2SEnji Cooper       max_flow < matrix.RhsSize()) {
443b89a7cc2SEnji Cooper     if (listener->IsInterested()) {
444b89a7cc2SEnji Cooper       *listener << "where no permutation of the elements can satisfy all "
445b89a7cc2SEnji Cooper                    "matchers, and the closest match is "
446b89a7cc2SEnji Cooper                 << max_flow << " of " << matrix.RhsSize()
447b89a7cc2SEnji Cooper                 << " matchers with the pairings:\n";
448b89a7cc2SEnji Cooper       LogElementMatcherPairVec(matches, listener->stream());
449b89a7cc2SEnji Cooper     }
450b89a7cc2SEnji Cooper     return false;
451b89a7cc2SEnji Cooper   }
452b89a7cc2SEnji Cooper   if ((match_flags() & UnorderedMatcherRequire::Subset) &&
453b89a7cc2SEnji Cooper       max_flow < matrix.LhsSize()) {
454b89a7cc2SEnji Cooper     if (listener->IsInterested()) {
455b89a7cc2SEnji Cooper       *listener
456b89a7cc2SEnji Cooper           << "where not all elements can be matched, and the closest match is "
457b89a7cc2SEnji Cooper           << max_flow << " of " << matrix.RhsSize()
458b89a7cc2SEnji Cooper           << " matchers with the pairings:\n";
459b89a7cc2SEnji Cooper       LogElementMatcherPairVec(matches, listener->stream());
460b89a7cc2SEnji Cooper     }
461b89a7cc2SEnji Cooper     return false;
462b89a7cc2SEnji Cooper   }
463b89a7cc2SEnji Cooper 
464b89a7cc2SEnji Cooper   if (matches.size() > 1) {
465b89a7cc2SEnji Cooper     if (listener->IsInterested()) {
466b89a7cc2SEnji Cooper       const char* sep = "where:\n";
467b89a7cc2SEnji Cooper       for (size_t mi = 0; mi < matches.size(); ++mi) {
468b89a7cc2SEnji Cooper         *listener << sep << " - element #" << matches[mi].first
469b89a7cc2SEnji Cooper                   << " is matched by matcher #" << matches[mi].second;
470b89a7cc2SEnji Cooper         sep = ",\n";
471b89a7cc2SEnji Cooper       }
472b89a7cc2SEnji Cooper     }
473b89a7cc2SEnji Cooper   }
474b89a7cc2SEnji Cooper   return true;
475b89a7cc2SEnji Cooper }
476b89a7cc2SEnji Cooper 
477b89a7cc2SEnji Cooper }  // namespace internal
478b89a7cc2SEnji Cooper }  // namespace testing
479