提交 cd91722a 编写于 作者: Y Yi Wang

Add more unit tests

上级 c06c6789
...@@ -30,8 +30,8 @@ StringPiece::StringPiece(const char* d, size_t n) : data_(d), size_(n) { ...@@ -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 requires len to be 0 for NULL data");
} }
StringPiece::StringPiece(const char* s) : data_(s), size_(strlen(s)) { StringPiece::StringPiece(const char* s) : data_(s) {
if (s == NULL) size_ = 0; size_ = (s == NULL) ? 0 : strlen(s);
} }
StringPiece::StringPiece(const std::string& s) StringPiece::StringPiece(const std::string& s)
......
...@@ -40,14 +40,8 @@ public: ...@@ -40,14 +40,8 @@ public:
StringPiece(const char* s); StringPiece(const char* s);
StringPiece(const std::string& 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_; } const char* data() const { return data_; }
size_t len() const { return size_; }
char operator[](size_t n) const { char operator[](size_t n) const {
assert(n < len()); assert(n < len());
......
...@@ -22,7 +22,6 @@ TEST(StringPiece, Construct) { ...@@ -22,7 +22,6 @@ TEST(StringPiece, Construct) {
paddle::StringPiece s; paddle::StringPiece s;
EXPECT_EQ(NULL, s.data()); EXPECT_EQ(NULL, s.data());
EXPECT_EQ(0U, s.len()); EXPECT_EQ(0U, s.len());
EXPECT_EQ(0U, s.cap());
} }
{ {
EXPECT_THROW([] { paddle::StringPiece s(NULL, 10000U); }(), EXPECT_THROW([] { paddle::StringPiece s(NULL, 10000U); }(),
...@@ -43,12 +42,10 @@ TEST(StringPiece, Construct) { ...@@ -43,12 +42,10 @@ TEST(StringPiece, Construct) {
TEST(StringPiece, CopyAndAssign) { TEST(StringPiece, CopyAndAssign) {
paddle::StringPiece empty; paddle::StringPiece empty;
EXPECT_EQ(0U, empty.len()); EXPECT_EQ(0U, empty.len());
EXPECT_EQ(0U, empty.cap());
paddle::StringPiece a("hello"); paddle::StringPiece a("hello");
paddle::StringPiece b = a; paddle::StringPiece b = a;
EXPECT_EQ(b.len(), strlen("hello")); EXPECT_EQ(b.len(), strlen("hello"));
EXPECT_EQ(b.cap(), strlen("hello"));
EXPECT_EQ(a, b); EXPECT_EQ(a, b);
std::string storage("hello"); std::string storage("hello");
...@@ -56,3 +53,25 @@ TEST(StringPiece, CopyAndAssign) { ...@@ -56,3 +53,25 @@ TEST(StringPiece, CopyAndAssign) {
EXPECT_EQ(a, c); EXPECT_EQ(a, c);
EXPECT_NE(a.data(), c.data()); 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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册