From cd91722a958bc3acc16768f67a48a15a9db82e24 Mon Sep 17 00:00:00 2001 From: Yi Wang Date: Fri, 9 Jun 2017 13:28:54 -0700 Subject: [PATCH] Add more unit tests --- paddle/strings/stringpiece.cc | 4 ++-- paddle/strings/stringpiece.h | 8 +------- paddle/strings/stringpiece_test.cc | 25 ++++++++++++++++++++++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/paddle/strings/stringpiece.cc b/paddle/strings/stringpiece.cc index ef46490fb1..cc6d6a23f6 100644 --- a/paddle/strings/stringpiece.cc +++ b/paddle/strings/stringpiece.cc @@ -30,8 +30,8 @@ StringPiece::StringPiece(const char* d, size_t n) : data_(d), size_(n) { "StringPiece requires len to be 0 for NULL data"); } -StringPiece::StringPiece(const char* s) : data_(s), size_(strlen(s)) { - if (s == NULL) size_ = 0; +StringPiece::StringPiece(const char* s) : data_(s) { + size_ = (s == NULL) ? 0 : strlen(s); } StringPiece::StringPiece(const std::string& s) diff --git a/paddle/strings/stringpiece.h b/paddle/strings/stringpiece.h index febb9036db..f10b5eecf4 100644 --- a/paddle/strings/stringpiece.h +++ b/paddle/strings/stringpiece.h @@ -40,14 +40,8 @@ public: StringPiece(const char* s); StringPiece(const std::string& s); - // For a string, cap() returns the size of storage and len() - // returns the currently used storage. For a StringPiece these - // are the same value. - size_t cap() const { return size_; } - size_t len() const { return size_; } - size_t size() const { return size_; } - const char* data() const { return data_; } + size_t len() const { return size_; } char operator[](size_t n) const { assert(n < len()); diff --git a/paddle/strings/stringpiece_test.cc b/paddle/strings/stringpiece_test.cc index 79fc219a9c..84ebc0ee03 100644 --- a/paddle/strings/stringpiece_test.cc +++ b/paddle/strings/stringpiece_test.cc @@ -22,7 +22,6 @@ TEST(StringPiece, Construct) { paddle::StringPiece s; EXPECT_EQ(NULL, s.data()); EXPECT_EQ(0U, s.len()); - EXPECT_EQ(0U, s.cap()); } { EXPECT_THROW([] { paddle::StringPiece s(NULL, 10000U); }(), @@ -43,12 +42,10 @@ TEST(StringPiece, Construct) { TEST(StringPiece, CopyAndAssign) { paddle::StringPiece empty; EXPECT_EQ(0U, empty.len()); - EXPECT_EQ(0U, empty.cap()); paddle::StringPiece a("hello"); paddle::StringPiece b = a; EXPECT_EQ(b.len(), strlen("hello")); - EXPECT_EQ(b.cap(), strlen("hello")); EXPECT_EQ(a, b); std::string storage("hello"); @@ -56,3 +53,25 @@ TEST(StringPiece, CopyAndAssign) { EXPECT_EQ(a, c); EXPECT_NE(a.data(), c.data()); } + +TEST(StringPiece, Comparison) { + { + paddle::StringPiece a("hello"); + paddle::StringPiece b("world"); + EXPECT_TRUE(a != b); + EXPECT_FALSE(a == b); + EXPECT_TRUE(a < b); + EXPECT_TRUE(a <= b); + EXPECT_FALSE(a > b); + EXPECT_FALSE(a >= b); + } + { + paddle::StringPiece a, b; + EXPECT_TRUE(a == b); + EXPECT_FALSE(a != b); + EXPECT_FALSE(a < b); + EXPECT_FALSE(a > b); + EXPECT_TRUE(a <= b); + EXPECT_TRUE(a >= b); + } +} -- GitLab