test: add testbench for string module
This commit is contained in:
@ -224,10 +224,10 @@ namespace YYCCTestbench {
|
||||
Assert(test_split[1] == YYCC_U8("1"), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[2] == YYCC_U8("2"), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[3] == YYCC_U8(""), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
test_split = YYCC::StringHelper::Split(YYCC_U8("test"), YYCC_U8("-")); // no matched decilmer
|
||||
test_split = YYCC::StringHelper::Split(YYCC_U8("test"), YYCC_U8("-")); // no matched delimiter
|
||||
Assert(test_split.size() == 1u, YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[0] == YYCC_U8("test"), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
test_split = YYCC::StringHelper::Split(YYCC_U8("test"), YYCC::yycc_u8string_view()); // empty decilmer
|
||||
test_split = YYCC::StringHelper::Split(YYCC_U8("test"), YYCC::yycc_u8string_view()); // empty delimiter
|
||||
Assert(test_split.size() == 1u, YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[0] == YYCC_U8("test"), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
test_split = YYCC::StringHelper::Split(YYCC::yycc_u8string_view(), YYCC_U8("")); // empty source string
|
||||
@ -413,12 +413,12 @@ namespace YYCCTestbench {
|
||||
YYCC::yycc_u8string test_slashed_path(YYCC::StdPatch::ToUTF8Path(test_path));
|
||||
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
std::wstring wdecilmer(1u, std::filesystem::path::preferred_separator);
|
||||
YYCC::yycc_u8string decilmer(YYCC::EncodingHelper::WcharToUTF8(wdecilmer));
|
||||
std::wstring wdelimiter(1u, std::filesystem::path::preferred_separator);
|
||||
YYCC::yycc_u8string delimiter(YYCC::EncodingHelper::WcharToUTF8(wdelimiter));
|
||||
#else
|
||||
YYCC::yycc_u8string decilmer(1u, std::filesystem::path::preferred_separator);
|
||||
YYCC::yycc_u8string delimiter(1u, std::filesystem::path::preferred_separator);
|
||||
#endif
|
||||
YYCC::yycc_u8string test_joined_path(YYCC::StringHelper::Join(c_UTF8TestStrTable.begin(), c_UTF8TestStrTable.end(), decilmer));
|
||||
YYCC::yycc_u8string test_joined_path(YYCC::StringHelper::Join(c_UTF8TestStrTable.begin(), c_UTF8TestStrTable.end(), delimiter));
|
||||
|
||||
Assert(test_slashed_path == test_joined_path, YYCC_U8("YYCC::StdPatch::ToStdPath, YYCC::StdPatch::ToUTF8Path"));
|
||||
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include <yycc/string/op.hpp>
|
||||
#include <yycc/string/reinterpret.hpp>
|
||||
|
||||
#include <yycc/prelude/core.hpp>
|
||||
|
||||
#define OP ::yycc::string::op
|
||||
|
||||
namespace yycctest::string::op {
|
||||
@ -13,7 +15,36 @@ namespace yycctest::string::op {
|
||||
}
|
||||
|
||||
TEST(StringOp, Replace) {
|
||||
|
||||
// Normal case
|
||||
{
|
||||
auto rv = OP::replace(YYCC_U8("aabbcc"), YYCC_U8("bb"), YYCC_U8("dd"));
|
||||
EXPECT_EQ(rv, YYCC_U8("aaddcc"));
|
||||
}
|
||||
// No matched expected string
|
||||
{
|
||||
auto rv = OP::replace(YYCC_U8("aabbcc"), YYCC_U8("zz"), YYCC_U8("yy"));
|
||||
EXPECT_EQ(rv, YYCC_U8("aabbcc"));
|
||||
}
|
||||
// Empty expected string
|
||||
{
|
||||
auto rv = OP::replace(YYCC_U8("aabbcc"), u8string_view(), YYCC_U8("zz"));
|
||||
EXPECT_EQ(rv, YYCC_U8("aabbcc"));
|
||||
}
|
||||
// Empty replace string
|
||||
{
|
||||
auto rv = OP::replace(YYCC_U8("aaaabbaa"), YYCC_U8("aa"), YYCC_U8(""));
|
||||
EXPECT_EQ(rv, YYCC_U8("bb"));
|
||||
}
|
||||
// Nested replacing
|
||||
{
|
||||
auto rv = OP::replace(YYCC_U8("aaxcc"), YYCC_U8("x"), YYCC_U8("yx"));
|
||||
EXPECT_EQ(rv, YYCC_U8("aayxcc"));
|
||||
}
|
||||
// Empty source string
|
||||
{
|
||||
auto rv = OP::replace(u8string_view(), YYCC_U8(""), YYCC_U8("xy"));
|
||||
EXPECT_EQ(rv, YYCC_U8(""));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(StringOp, Lower) {
|
||||
@ -27,11 +58,39 @@ namespace yycctest::string::op {
|
||||
}
|
||||
|
||||
TEST(StringOp, Join) {
|
||||
|
||||
std::vector<u8string> datas{YYCC_U8(""), YYCC_U8("1"), YYCC_U8("2"), YYCC_U8("")};
|
||||
auto rv = OP::join(datas.begin(), datas.end(), YYCC_U8(", "));
|
||||
EXPECT_EQ(rv, YYCC_U8(", 1, 2, "));
|
||||
}
|
||||
|
||||
TEST(StringOp, Split) {
|
||||
|
||||
// Normal
|
||||
{
|
||||
auto rv = OP::split(YYCC_U8(", 1, 2, "), YYCC_U8(", "));
|
||||
ASSERT_EQ(rv.size(), 4u);
|
||||
EXPECT_EQ(rv[0], YYCC_U8(""));
|
||||
EXPECT_EQ(rv[1], YYCC_U8("1"));
|
||||
EXPECT_EQ(rv[2], YYCC_U8("2"));
|
||||
EXPECT_EQ(rv[3], YYCC_U8(""));
|
||||
}
|
||||
// No matched delimiter
|
||||
{
|
||||
auto rv = OP::split(YYCC_U8("test"), YYCC_U8("-"));
|
||||
ASSERT_EQ(rv.size(), 1u);
|
||||
EXPECT_EQ(rv[0], YYCC_U8("test"));
|
||||
}
|
||||
// Empty delimiter
|
||||
{
|
||||
auto rv = OP::split(YYCC_U8("test"), u8string_view());
|
||||
ASSERT_EQ(rv.size(), 1u);
|
||||
EXPECT_EQ(rv[0], YYCC_U8("test"));
|
||||
}
|
||||
// Empty source string
|
||||
{
|
||||
auto rv = OP::split(u8string_view(), YYCC_U8(""));
|
||||
ASSERT_EQ(rv.size(), 1u);
|
||||
EXPECT_TRUE(rv[0].empty());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace yycctest::string::op
|
||||
|
@ -1,6 +1,60 @@
|
||||
#include <cstring>
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/string/reinterpret.hpp>
|
||||
|
||||
#include <yycc/prelude/core.hpp>
|
||||
|
||||
#define REINTERPRET ::yycc::string::reinterpret
|
||||
#define CONST_VOID_PTR(p) reinterpret_cast<const void*>(p)
|
||||
#define VOID_PTR(p) reinterpret_cast<void*>(p)
|
||||
|
||||
namespace yycctest::string::reinterpret {
|
||||
|
||||
}
|
||||
static u8string PROBE(YYCC_U8("Test"));
|
||||
|
||||
TEST(StringReinterpret, ConstPointer) {
|
||||
const auto* src = PROBE.data();
|
||||
const auto* dst = REINTERPRET::as_ordinary(src);
|
||||
const auto* new_src = REINTERPRET::as_utf8(dst);
|
||||
|
||||
// Pointer should point to the same address after casting.
|
||||
EXPECT_EQ(CONST_VOID_PTR(src), CONST_VOID_PTR(dst));
|
||||
EXPECT_EQ(CONST_VOID_PTR(src), CONST_VOID_PTR(new_src));
|
||||
}
|
||||
|
||||
TEST(StringReinterpret, Pointer) {
|
||||
auto* src = PROBE.data();
|
||||
auto* dst = REINTERPRET::as_ordinary(src);
|
||||
auto* new_src = REINTERPRET::as_utf8(dst);
|
||||
|
||||
// Pointer should point to the same address after casting.
|
||||
EXPECT_EQ(VOID_PTR(src), VOID_PTR(dst));
|
||||
EXPECT_EQ(VOID_PTR(src), VOID_PTR(new_src));
|
||||
}
|
||||
|
||||
TEST(StringReinterpret, String) {
|
||||
auto src = u8string(PROBE);
|
||||
auto dst = REINTERPRET::as_ordinary(src);
|
||||
auto new_src = REINTERPRET::as_utf8(dst);
|
||||
|
||||
// Check memory length and data.
|
||||
ASSERT_EQ(src.length(), dst.length());
|
||||
EXPECT_TRUE(std::memcmp(src.data(), dst.data(), src.length()) == 0);
|
||||
ASSERT_EQ(src.length(), new_src.length());
|
||||
EXPECT_TRUE(std::memcmp(src.data(), new_src.data(), src.length()) == 0);
|
||||
}
|
||||
|
||||
TEST(StringReinterpret, StringView) {
|
||||
auto src = u8string_view(PROBE);
|
||||
auto dst = REINTERPRET::as_ordinary_view(src);
|
||||
auto new_src = REINTERPRET::as_utf8_view(dst);
|
||||
|
||||
// Check memory length and data.
|
||||
ASSERT_EQ(src.length(), dst.length());
|
||||
EXPECT_TRUE(std::memcmp(src.data(), dst.data(), src.length()) == 0);
|
||||
ASSERT_EQ(src.length(), new_src.length());
|
||||
EXPECT_TRUE(std::memcmp(src.data(), new_src.data(), src.length()) == 0);
|
||||
}
|
||||
|
||||
} // namespace yycctest::string::reinterpret
|
||||
|
Reference in New Issue
Block a user