diff options
author | Gennadiy Civil <misterg@google.com> | 2019-05-17 19:05:43 (GMT) |
---|---|---|
committer | Gennadiy Civil <misterg@google.com> | 2019-05-17 19:05:44 (GMT) |
commit | 03273a89827ffedfee19cfaae5d85dee60779441 (patch) | |
tree | 05643304ce1e569bbbe6384fd8a51b6587c37979 /googletest/test | |
parent | 9d4cde44a4a3952cf21861f9370b3bed9265dfd7 (diff) | |
parent | 5b4a135f626e60fde618da9eb10ae2a21934de01 (diff) | |
download | googletest-03273a89827ffedfee19cfaae5d85dee60779441.zip googletest-03273a89827ffedfee19cfaae5d85dee60779441.tar.gz googletest-03273a89827ffedfee19cfaae5d85dee60779441.tar.bz2 |
Merge pull request #2254 from chaoran:master
PiperOrigin-RevId: 248759825
Diffstat (limited to 'googletest/test')
-rw-r--r-- | googletest/test/googletest-printers-test.cc | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index d896c24..4bdc9ad 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -1556,6 +1556,65 @@ TEST(PrintOneofTest, Basic) { PrintToString(Type(NonPrintable{}))); } #endif // GTEST_HAS_ABSL +namespace { +class string_ref; + +/** + * This is a synthetic pointer to a fixed size string. + */ +class string_ptr { + public: + string_ptr(const char* data, size_t size) : data_(data), size_(size) {} + + string_ptr& operator++() noexcept { + data_ += size_; + return *this; + } + + string_ref operator*() const noexcept; + + private: + const char* data_; + size_t size_; +}; + +/** + * This is a synthetic reference of a fixed size string. + */ +class string_ref { + public: + string_ref(const char* data, size_t size) : data_(data), size_(size) {} + + string_ptr operator&() const noexcept { return {data_, size_}; } // NOLINT + + bool operator==(const char* s) const noexcept { + if (size_ > 0 && data_[size_ - 1] != 0) { + return std::string(data_, size_) == std::string(s); + } else { + return std::string(data_) == std::string(s); + } + } + + private: + const char* data_; + size_t size_; +}; + +string_ref string_ptr::operator*() const noexcept { return {data_, size_}; } + +TEST(string_ref, compare) { + const char* s = "alex\0davidjohn\0"; + string_ptr ptr(s, 5); + EXPECT_EQ(*ptr, "alex"); + EXPECT_TRUE(*ptr == "alex"); + ++ptr; + EXPECT_EQ(*ptr, "david"); + EXPECT_TRUE(*ptr == "david"); + ++ptr; + EXPECT_EQ(*ptr, "john"); +} + +} // namespace } // namespace gtest_printers_test } // namespace testing |