Coverage Report

Created: 2026-06-30 06:52

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