Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "gmock/gmock-matchers.h"
00038 #include "gmock/gmock-generated-matchers.h"
00039
00040 #include <string.h>
00041 #include <sstream>
00042 #include <string>
00043
00044 namespace testing {
00045
00046
00047
00048 Matcher<const internal::string&>::Matcher(const internal::string& s) {
00049 *this = Eq(s);
00050 }
00051
00052
00053
00054 Matcher<const internal::string&>::Matcher(const char* s) {
00055 *this = Eq(internal::string(s));
00056 }
00057
00058
00059 Matcher<internal::string>::Matcher(const internal::string& s) { *this = Eq(s); }
00060
00061
00062 Matcher<internal::string>::Matcher(const char* s) {
00063 *this = Eq(internal::string(s));
00064 }
00065
00066 #if GTEST_HAS_STRING_PIECE_
00067
00068
00069 Matcher<const StringPiece&>::Matcher(const internal::string& s) {
00070 *this = Eq(s);
00071 }
00072
00073
00074
00075 Matcher<const StringPiece&>::Matcher(const char* s) {
00076 *this = Eq(internal::string(s));
00077 }
00078
00079
00080
00081 Matcher<const StringPiece&>::Matcher(StringPiece s) {
00082 *this = Eq(s.ToString());
00083 }
00084
00085
00086 Matcher<StringPiece>::Matcher(const internal::string& s) {
00087 *this = Eq(s);
00088 }
00089
00090
00091 Matcher<StringPiece>::Matcher(const char* s) {
00092 *this = Eq(internal::string(s));
00093 }
00094
00095
00096 Matcher<StringPiece>::Matcher(StringPiece s) {
00097 *this = Eq(s.ToString());
00098 }
00099 #endif // GTEST_HAS_STRING_PIECE_
00100
00101 namespace internal {
00102
00103
00104
00105 GTEST_API_ string JoinAsTuple(const Strings& fields) {
00106 switch (fields.size()) {
00107 case 0:
00108 return "";
00109 case 1:
00110 return fields[0];
00111 default:
00112 string result = "(" + fields[0];
00113 for (size_t i = 1; i < fields.size(); i++) {
00114 result += ", ";
00115 result += fields[i];
00116 }
00117 result += ")";
00118 return result;
00119 }
00120 }
00121
00122
00123
00124
00125
00126
00127 GTEST_API_ string FormatMatcherDescription(bool negation,
00128 const char* matcher_name,
00129 const Strings& param_values) {
00130 string result = ConvertIdentifierNameToWords(matcher_name);
00131 if (param_values.size() >= 1)
00132 result += " " + JoinAsTuple(param_values);
00133 return negation ? "not (" + result + ")" : result;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 class MaxBipartiteMatchState {
00199 public:
00200 explicit MaxBipartiteMatchState(const MatchMatrix& graph)
00201 : graph_(&graph),
00202 left_(graph_->LhsSize(), kUnused),
00203 right_(graph_->RhsSize(), kUnused) {
00204 }
00205
00206
00207 ElementMatcherPairs Compute() {
00208
00209 ::std::vector<char> seen;
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) {
00223
00224
00225 GTEST_CHECK_(left_[ilhs] == kUnused)
00226 << "ilhs: " << ilhs << ", left_[ilhs]: " << left_[ilhs];
00227
00228 seen.assign(graph_->RhsSize(), 0);
00229 TryAugment(ilhs, &seen);
00230 }
00231 ElementMatcherPairs result;
00232 for (size_t ilhs = 0; ilhs < left_.size(); ++ilhs) {
00233 size_t irhs = left_[ilhs];
00234 if (irhs == kUnused) continue;
00235 result.push_back(ElementMatcherPair(ilhs, irhs));
00236 }
00237 return result;
00238 }
00239
00240 private:
00241 static const size_t kUnused = static_cast<size_t>(-1);
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 bool TryAugment(size_t ilhs, ::std::vector<char>* seen) {
00260 for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) {
00261 if ((*seen)[irhs])
00262 continue;
00263 if (!graph_->HasEdge(ilhs, irhs))
00264 continue;
00265
00266 (*seen)[irhs] = 1;
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 if (right_[irhs] == kUnused || TryAugment(right_[irhs], seen)) {
00278
00279 left_[ilhs] = irhs;
00280 right_[irhs] = ilhs;
00281 return true;
00282 }
00283 }
00284 return false;
00285 }
00286
00287 const MatchMatrix* graph_;
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 ::std::vector<size_t> left_;
00300 ::std::vector<size_t> right_;
00301
00302 GTEST_DISALLOW_ASSIGN_(MaxBipartiteMatchState);
00303 };
00304
00305 const size_t MaxBipartiteMatchState::kUnused;
00306
00307 GTEST_API_ ElementMatcherPairs
00308 FindMaxBipartiteMatching(const MatchMatrix& g) {
00309 return MaxBipartiteMatchState(g).Compute();
00310 }
00311
00312 static void LogElementMatcherPairVec(const ElementMatcherPairs& pairs,
00313 ::std::ostream* stream) {
00314 typedef ElementMatcherPairs::const_iterator Iter;
00315 ::std::ostream& os = *stream;
00316 os << "{";
00317 const char *sep = "";
00318 for (Iter it = pairs.begin(); it != pairs.end(); ++it) {
00319 os << sep << "\n ("
00320 << "element #" << it->first << ", "
00321 << "matcher #" << it->second << ")";
00322 sep = ",";
00323 }
00324 os << "\n}";
00325 }
00326
00327
00328 GTEST_API_ bool FindPairing(const MatchMatrix& matrix,
00329 MatchResultListener* listener) {
00330 ElementMatcherPairs matches = FindMaxBipartiteMatching(matrix);
00331
00332 size_t max_flow = matches.size();
00333 bool result = (max_flow == matrix.RhsSize());
00334
00335 if (!result) {
00336 if (listener->IsInterested()) {
00337 *listener << "where no permutation of the elements can "
00338 "satisfy all matchers, and the closest match is "
00339 << max_flow << " of " << matrix.RhsSize()
00340 << " matchers with the pairings:\n";
00341 LogElementMatcherPairVec(matches, listener->stream());
00342 }
00343 return false;
00344 }
00345
00346 if (matches.size() > 1) {
00347 if (listener->IsInterested()) {
00348 const char *sep = "where:\n";
00349 for (size_t mi = 0; mi < matches.size(); ++mi) {
00350 *listener << sep << " - element #" << matches[mi].first
00351 << " is matched by matcher #" << matches[mi].second;
00352 sep = ",\n";
00353 }
00354 }
00355 }
00356 return true;
00357 }
00358
00359 bool MatchMatrix::NextGraph() {
00360 for (size_t ilhs = 0; ilhs < LhsSize(); ++ilhs) {
00361 for (size_t irhs = 0; irhs < RhsSize(); ++irhs) {
00362 char& b = matched_[SpaceIndex(ilhs, irhs)];
00363 if (!b) {
00364 b = 1;
00365 return true;
00366 }
00367 b = 0;
00368 }
00369 }
00370 return false;
00371 }
00372
00373 void MatchMatrix::Randomize() {
00374 for (size_t ilhs = 0; ilhs < LhsSize(); ++ilhs) {
00375 for (size_t irhs = 0; irhs < RhsSize(); ++irhs) {
00376 char& b = matched_[SpaceIndex(ilhs, irhs)];
00377 b = static_cast<char>(rand() & 1);
00378 }
00379 }
00380 }
00381
00382 string MatchMatrix::DebugString() const {
00383 ::std::stringstream ss;
00384 const char *sep = "";
00385 for (size_t i = 0; i < LhsSize(); ++i) {
00386 ss << sep;
00387 for (size_t j = 0; j < RhsSize(); ++j) {
00388 ss << HasEdge(i, j);
00389 }
00390 sep = ";";
00391 }
00392 return ss.str();
00393 }
00394
00395 void UnorderedElementsAreMatcherImplBase::DescribeToImpl(
00396 ::std::ostream* os) const {
00397 if (matcher_describers_.empty()) {
00398 *os << "is empty";
00399 return;
00400 }
00401 if (matcher_describers_.size() == 1) {
00402 *os << "has " << Elements(1) << " and that element ";
00403 matcher_describers_[0]->DescribeTo(os);
00404 return;
00405 }
00406 *os << "has " << Elements(matcher_describers_.size())
00407 << " and there exists some permutation of elements such that:\n";
00408 const char* sep = "";
00409 for (size_t i = 0; i != matcher_describers_.size(); ++i) {
00410 *os << sep << " - element #" << i << " ";
00411 matcher_describers_[i]->DescribeTo(os);
00412 sep = ", and\n";
00413 }
00414 }
00415
00416 void UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(
00417 ::std::ostream* os) const {
00418 if (matcher_describers_.empty()) {
00419 *os << "isn't empty";
00420 return;
00421 }
00422 if (matcher_describers_.size() == 1) {
00423 *os << "doesn't have " << Elements(1)
00424 << ", or has " << Elements(1) << " that ";
00425 matcher_describers_[0]->DescribeNegationTo(os);
00426 return;
00427 }
00428 *os << "doesn't have " << Elements(matcher_describers_.size())
00429 << ", or there exists no permutation of elements such that:\n";
00430 const char* sep = "";
00431 for (size_t i = 0; i != matcher_describers_.size(); ++i) {
00432 *os << sep << " - element #" << i << " ";
00433 matcher_describers_[i]->DescribeTo(os);
00434 sep = ", and\n";
00435 }
00436 }
00437
00438
00439
00440
00441
00442
00443 bool UnorderedElementsAreMatcherImplBase::
00444 VerifyAllElementsAndMatchersAreMatched(
00445 const ::std::vector<string>& element_printouts,
00446 const MatchMatrix& matrix,
00447 MatchResultListener* listener) const {
00448 bool result = true;
00449 ::std::vector<char> element_matched(matrix.LhsSize(), 0);
00450 ::std::vector<char> matcher_matched(matrix.RhsSize(), 0);
00451
00452 for (size_t ilhs = 0; ilhs < matrix.LhsSize(); ilhs++) {
00453 for (size_t irhs = 0; irhs < matrix.RhsSize(); irhs++) {
00454 char matched = matrix.HasEdge(ilhs, irhs);
00455 element_matched[ilhs] |= matched;
00456 matcher_matched[irhs] |= matched;
00457 }
00458 }
00459
00460 {
00461 const char* sep =
00462 "where the following matchers don't match any elements:\n";
00463 for (size_t mi = 0; mi < matcher_matched.size(); ++mi) {
00464 if (matcher_matched[mi])
00465 continue;
00466 result = false;
00467 if (listener->IsInterested()) {
00468 *listener << sep << "matcher #" << mi << ": ";
00469 matcher_describers_[mi]->DescribeTo(listener->stream());
00470 sep = ",\n";
00471 }
00472 }
00473 }
00474
00475 {
00476 const char* sep =
00477 "where the following elements don't match any matchers:\n";
00478 const char* outer_sep = "";
00479 if (!result) {
00480 outer_sep = "\nand ";
00481 }
00482 for (size_t ei = 0; ei < element_matched.size(); ++ei) {
00483 if (element_matched[ei])
00484 continue;
00485 result = false;
00486 if (listener->IsInterested()) {
00487 *listener << outer_sep << sep << "element #" << ei << ": "
00488 << element_printouts[ei];
00489 sep = ",\n";
00490 outer_sep = "";
00491 }
00492 }
00493 }
00494 return result;
00495 }
00496
00497 }
00498 }