Extract string view. (#7416)
* Add equality operators. * Return a view in substr. * Add proper iterator types.
This commit is contained in:
@@ -457,10 +457,10 @@ TEST(Json, Invalid) {
|
||||
bool has_thrown = false;
|
||||
try {
|
||||
Json load{Json::Load(StringView(str.c_str(), str.size()))};
|
||||
} catch (dmlc::Error const &e) {
|
||||
} catch (dmlc::Error const& e) {
|
||||
std::string msg = e.what();
|
||||
ASSERT_TRUE(msg.find("EOF") != std::string::npos
|
||||
|| msg.find("255") != std::string::npos); // EOF is printed as 255 on s390x
|
||||
// EOF is printed as 255 on s390x
|
||||
ASSERT_TRUE(msg.find("EOF") != std::string::npos || msg.find("255") != std::string::npos);
|
||||
has_thrown = true;
|
||||
};
|
||||
ASSERT_TRUE(has_thrown);
|
||||
@@ -580,14 +580,4 @@ TEST(Json, DISABLED_RoundTripExhaustive) {
|
||||
test(static_cast<uint32_t>(i));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(StringView, Basic) {
|
||||
StringView str{"This is a string."};
|
||||
std::stringstream ss;
|
||||
ss << str;
|
||||
|
||||
std::string res = ss.str();
|
||||
ASSERT_EQ(str.size(), res.size());
|
||||
ASSERT_TRUE(std::equal(res.cbegin(), res.cend(), str.cbegin()));
|
||||
}
|
||||
} // namespace xgboost
|
||||
|
||||
28
tests/cpp/common/test_string_view.cc
Normal file
28
tests/cpp/common/test_string_view.cc
Normal file
@@ -0,0 +1,28 @@
|
||||
/*!
|
||||
* Copyright (c) by XGBoost Contributors 2021
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/string_view.h>
|
||||
#include <string_view>
|
||||
namespace xgboost {
|
||||
TEST(StringView, Basic) {
|
||||
StringView str{"This is a string."};
|
||||
std::stringstream ss;
|
||||
ss << str;
|
||||
|
||||
std::string res = ss.str();
|
||||
ASSERT_EQ(str.size(), res.size());
|
||||
ASSERT_TRUE(std::equal(res.cbegin(), res.cend(), str.cbegin()));
|
||||
|
||||
auto substr = str.substr(5, 2);
|
||||
ASSERT_EQ(substr.size(), 2);
|
||||
|
||||
ASSERT_EQ(StringView{"is"}.size(), 2);
|
||||
ASSERT_TRUE(substr == "is");
|
||||
ASSERT_FALSE(substr != "is");
|
||||
ASSERT_FALSE(substr == "foobar");
|
||||
ASSERT_FALSE(substr == "i");
|
||||
|
||||
ASSERT_TRUE(std::equal(substr.crbegin(), substr.crend(), StringView{"si"}.cbegin()));
|
||||
}
|
||||
} // namespace xgboost
|
||||
Reference in New Issue
Block a user