File indexing completed on 2025-08-06 08:19:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 #include "gmock/gmock-matchers.h"
0038 #include "gmock/gmock-generated-matchers.h"
0039
0040 #include <string.h>
0041 #include <sstream>
0042 #include <string>
0043
0044 namespace testing {
0045
0046
0047
0048 Matcher<const internal::string&>::Matcher(const internal::string& s) {
0049 *this = Eq(s);
0050 }
0051
0052
0053
0054 Matcher<const internal::string&>::Matcher(const char* s) {
0055 *this = Eq(internal::string(s));
0056 }
0057
0058
0059 Matcher<internal::string>::Matcher(const internal::string& s) { *this = Eq(s); }
0060
0061
0062 Matcher<internal::string>::Matcher(const char* s) {
0063 *this = Eq(internal::string(s));
0064 }
0065
0066 #if GTEST_HAS_STRING_PIECE_
0067
0068
0069 Matcher<const StringPiece&>::Matcher(const internal::string& s) {
0070 *this = Eq(s);
0071 }
0072
0073
0074
0075 Matcher<const StringPiece&>::Matcher(const char* s) {
0076 *this = Eq(internal::string(s));
0077 }
0078
0079
0080
0081 Matcher<const StringPiece&>::Matcher(StringPiece s) {
0082 *this = Eq(s.ToString());
0083 }
0084
0085
0086 Matcher<StringPiece>::Matcher(const internal::string& s) {
0087 *this = Eq(s);
0088 }
0089
0090
0091 Matcher<StringPiece>::Matcher(const char* s) {
0092 *this = Eq(internal::string(s));
0093 }
0094
0095
0096 Matcher<StringPiece>::Matcher(StringPiece s) {
0097 *this = Eq(s.ToString());
0098 }
0099 #endif
0100
0101 namespace internal {
0102
0103
0104
0105 GTEST_API_ string JoinAsTuple(const Strings& fields) {
0106 switch (fields.size()) {
0107 case 0:
0108 return "";
0109 case 1:
0110 return fields[0];
0111 default:
0112 string result = "(" + fields[0];
0113 for (size_t i = 1; i < fields.size(); i++) {
0114 result += ", ";
0115 result += fields[i];
0116 }
0117 result += ")";
0118 return result;
0119 }
0120 }
0121
0122
0123
0124
0125
0126
0127 GTEST_API_ string FormatMatcherDescription(bool negation,
0128 const char* matcher_name,
0129 const Strings& param_values) {
0130 string result = ConvertIdentifierNameToWords(matcher_name);
0131 if (param_values.size() >= 1)
0132 result += " " + JoinAsTuple(param_values);
0133 return negation ? "not (" + result + ")" : result;
0134 }
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198 class MaxBipartiteMatchState {
0199 public:
0200 explicit MaxBipartiteMatchState(const MatchMatrix& graph)
0201 : graph_(&graph),
0202 left_(graph_->LhsSize(), kUnused),
0203 right_(graph_->RhsSize(), kUnused) {
0204 }
0205
0206
0207 ElementMatcherPairs Compute() {
0208
0209 ::std::vector<char> seen;
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222 for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) {
0223
0224
0225 GTEST_CHECK_(left_[ilhs] == kUnused)
0226 << "ilhs: " << ilhs << ", left_[ilhs]: " << left_[ilhs];
0227
0228 seen.assign(graph_->RhsSize(), 0);
0229 TryAugment(ilhs, &seen);
0230 }
0231 ElementMatcherPairs result;
0232 for (size_t ilhs = 0; ilhs < left_.size(); ++ilhs) {
0233 size_t irhs = left_[ilhs];
0234 if (irhs == kUnused) continue;
0235 result.push_back(ElementMatcherPair(ilhs, irhs));
0236 }
0237 return result;
0238 }
0239
0240 private:
0241 static const size_t kUnused = static_cast<size_t>(-1);
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259 bool TryAugment(size_t ilhs, ::std::vector<char>* seen) {
0260 for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) {
0261 if ((*seen)[irhs])
0262 continue;
0263 if (!graph_->HasEdge(ilhs, irhs))
0264 continue;
0265
0266 (*seen)[irhs] = 1;
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277 if (right_[irhs] == kUnused || TryAugment(right_[irhs], seen)) {
0278
0279 left_[ilhs] = irhs;
0280 right_[irhs] = ilhs;
0281 return true;
0282 }
0283 }
0284 return false;
0285 }
0286
0287 const MatchMatrix* graph_;
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299 ::std::vector<size_t> left_;
0300 ::std::vector<size_t> right_;
0301
0302 GTEST_DISALLOW_ASSIGN_(MaxBipartiteMatchState);
0303 };
0304
0305 const size_t MaxBipartiteMatchState::kUnused;
0306
0307 GTEST_API_ ElementMatcherPairs
0308 FindMaxBipartiteMatching(const MatchMatrix& g) {
0309 return MaxBipartiteMatchState(g).Compute();
0310 }
0311
0312 static void LogElementMatcherPairVec(const ElementMatcherPairs& pairs,
0313 ::std::ostream* stream) {
0314 typedef ElementMatcherPairs::const_iterator Iter;
0315 ::std::ostream& os = *stream;
0316 os << "{";
0317 const char *sep = "";
0318 for (Iter it = pairs.begin(); it != pairs.end(); ++it) {
0319 os << sep << "\n ("
0320 << "element #" << it->first << ", "
0321 << "matcher #" << it->second << ")";
0322 sep = ",";
0323 }
0324 os << "\n}";
0325 }
0326
0327
0328 GTEST_API_ bool FindPairing(const MatchMatrix& matrix,
0329 MatchResultListener* listener) {
0330 ElementMatcherPairs matches = FindMaxBipartiteMatching(matrix);
0331
0332 size_t max_flow = matches.size();
0333 bool result = (max_flow == matrix.RhsSize());
0334
0335 if (!result) {
0336 if (listener->IsInterested()) {
0337 *listener << "where no permutation of the elements can "
0338 "satisfy all matchers, and the closest match is "
0339 << max_flow << " of " << matrix.RhsSize()
0340 << " matchers with the pairings:\n";
0341 LogElementMatcherPairVec(matches, listener->stream());
0342 }
0343 return false;
0344 }
0345
0346 if (matches.size() > 1) {
0347 if (listener->IsInterested()) {
0348 const char *sep = "where:\n";
0349 for (size_t mi = 0; mi < matches.size(); ++mi) {
0350 *listener << sep << " - element #" << matches[mi].first
0351 << " is matched by matcher #" << matches[mi].second;
0352 sep = ",\n";
0353 }
0354 }
0355 }
0356 return true;
0357 }
0358
0359 bool MatchMatrix::NextGraph() {
0360 for (size_t ilhs = 0; ilhs < LhsSize(); ++ilhs) {
0361 for (size_t irhs = 0; irhs < RhsSize(); ++irhs) {
0362 char& b = matched_[SpaceIndex(ilhs, irhs)];
0363 if (!b) {
0364 b = 1;
0365 return true;
0366 }
0367 b = 0;
0368 }
0369 }
0370 return false;
0371 }
0372
0373 void MatchMatrix::Randomize() {
0374 for (size_t ilhs = 0; ilhs < LhsSize(); ++ilhs) {
0375 for (size_t irhs = 0; irhs < RhsSize(); ++irhs) {
0376 char& b = matched_[SpaceIndex(ilhs, irhs)];
0377 b = static_cast<char>(rand() & 1);
0378 }
0379 }
0380 }
0381
0382 string MatchMatrix::DebugString() const {
0383 ::std::stringstream ss;
0384 const char *sep = "";
0385 for (size_t i = 0; i < LhsSize(); ++i) {
0386 ss << sep;
0387 for (size_t j = 0; j < RhsSize(); ++j) {
0388 ss << HasEdge(i, j);
0389 }
0390 sep = ";";
0391 }
0392 return ss.str();
0393 }
0394
0395 void UnorderedElementsAreMatcherImplBase::DescribeToImpl(
0396 ::std::ostream* os) const {
0397 if (matcher_describers_.empty()) {
0398 *os << "is empty";
0399 return;
0400 }
0401 if (matcher_describers_.size() == 1) {
0402 *os << "has " << Elements(1) << " and that element ";
0403 matcher_describers_[0]->DescribeTo(os);
0404 return;
0405 }
0406 *os << "has " << Elements(matcher_describers_.size())
0407 << " and there exists some permutation of elements such that:\n";
0408 const char* sep = "";
0409 for (size_t i = 0; i != matcher_describers_.size(); ++i) {
0410 *os << sep << " - element #" << i << " ";
0411 matcher_describers_[i]->DescribeTo(os);
0412 sep = ", and\n";
0413 }
0414 }
0415
0416 void UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(
0417 ::std::ostream* os) const {
0418 if (matcher_describers_.empty()) {
0419 *os << "isn't empty";
0420 return;
0421 }
0422 if (matcher_describers_.size() == 1) {
0423 *os << "doesn't have " << Elements(1)
0424 << ", or has " << Elements(1) << " that ";
0425 matcher_describers_[0]->DescribeNegationTo(os);
0426 return;
0427 }
0428 *os << "doesn't have " << Elements(matcher_describers_.size())
0429 << ", or there exists no permutation of elements such that:\n";
0430 const char* sep = "";
0431 for (size_t i = 0; i != matcher_describers_.size(); ++i) {
0432 *os << sep << " - element #" << i << " ";
0433 matcher_describers_[i]->DescribeTo(os);
0434 sep = ", and\n";
0435 }
0436 }
0437
0438
0439
0440
0441
0442
0443 bool UnorderedElementsAreMatcherImplBase::
0444 VerifyAllElementsAndMatchersAreMatched(
0445 const ::std::vector<string>& element_printouts,
0446 const MatchMatrix& matrix,
0447 MatchResultListener* listener) const {
0448 bool result = true;
0449 ::std::vector<char> element_matched(matrix.LhsSize(), 0);
0450 ::std::vector<char> matcher_matched(matrix.RhsSize(), 0);
0451
0452 for (size_t ilhs = 0; ilhs < matrix.LhsSize(); ilhs++) {
0453 for (size_t irhs = 0; irhs < matrix.RhsSize(); irhs++) {
0454 char matched = matrix.HasEdge(ilhs, irhs);
0455 element_matched[ilhs] |= matched;
0456 matcher_matched[irhs] |= matched;
0457 }
0458 }
0459
0460 {
0461 const char* sep =
0462 "where the following matchers don't match any elements:\n";
0463 for (size_t mi = 0; mi < matcher_matched.size(); ++mi) {
0464 if (matcher_matched[mi])
0465 continue;
0466 result = false;
0467 if (listener->IsInterested()) {
0468 *listener << sep << "matcher #" << mi << ": ";
0469 matcher_describers_[mi]->DescribeTo(listener->stream());
0470 sep = ",\n";
0471 }
0472 }
0473 }
0474
0475 {
0476 const char* sep =
0477 "where the following elements don't match any matchers:\n";
0478 const char* outer_sep = "";
0479 if (!result) {
0480 outer_sep = "\nand ";
0481 }
0482 for (size_t ei = 0; ei < element_matched.size(); ++ei) {
0483 if (element_matched[ei])
0484 continue;
0485 result = false;
0486 if (listener->IsInterested()) {
0487 *listener << outer_sep << sep << "element #" << ei << ": "
0488 << element_printouts[ei];
0489 sep = ",\n";
0490 outer_sep = "";
0491 }
0492 }
0493 }
0494 return result;
0495 }
0496
0497 }
0498 }