diff options
-rw-r--r-- | core/include/fxcrt/fx_string.h | 10 | ||||
-rw-r--r-- | core/src/fxcrt/fx_basic_bstring_unittest.cpp | 222 | ||||
-rw-r--r-- | core/src/fxcrt/fx_basic_wstring_unittest.cpp | 223 |
3 files changed, 453 insertions, 2 deletions
diff --git a/core/include/fxcrt/fx_string.h b/core/include/fxcrt/fx_string.h index c98a77cfe1..b4d7249209 100644 --- a/core/include/fxcrt/fx_string.h +++ b/core/include/fxcrt/fx_string.h @@ -182,7 +182,10 @@ struct CFX_StringData { FX_STRSIZE m_nAllocLength; FX_CHAR m_String[1]; }; -class CFX_ByteString + +// A mutable string with shared buffers using copy-on-write semantics that +// avoids the cost of std::string's iterator stability guarantees. +class CFX_ByteString { public: typedef FX_CHAR value_type; @@ -595,7 +598,10 @@ struct CFX_StringDataW { FX_STRSIZE m_nAllocLength; FX_WCHAR m_String[1]; }; -class CFX_WideString + +// A mutable string with shared buffers using copy-on-write semantics that +// avoids the cost of std::string's iterator stability guarantees. +class CFX_WideString { public: typedef FX_WCHAR value_type; diff --git a/core/src/fxcrt/fx_basic_bstring_unittest.cpp b/core/src/fxcrt/fx_basic_bstring_unittest.cpp index 57cfc8047e..7259c97b1f 100644 --- a/core/src/fxcrt/fx_basic_bstring_unittest.cpp +++ b/core/src/fxcrt/fx_basic_bstring_unittest.cpp @@ -45,6 +45,114 @@ TEST(fxcrt, ByteStringOperatorLT) { EXPECT_FALSE(def < abc); } +TEST(fxcrt, ByteStringOperatorEQ) { + CFX_ByteString byte_string("hello"); + ASSERT_TRUE(byte_string == byte_string); + + CFX_ByteString byte_string_same1("hello"); + ASSERT_TRUE(byte_string == byte_string_same1); + ASSERT_TRUE(byte_string_same1 == byte_string); + + CFX_ByteString byte_string_same2(byte_string); + ASSERT_TRUE(byte_string == byte_string_same2); + ASSERT_TRUE(byte_string_same2 == byte_string); + + CFX_ByteString byte_string1("he"); + CFX_ByteString byte_string2("hellp"); + CFX_ByteString byte_string3("hellod"); + ASSERT_FALSE(byte_string == byte_string1); + ASSERT_FALSE(byte_string == byte_string2); + ASSERT_FALSE(byte_string == byte_string3); + ASSERT_FALSE(byte_string1 == byte_string); + ASSERT_FALSE(byte_string2 == byte_string); + ASSERT_FALSE(byte_string3 == byte_string); + + CFX_ByteStringC byte_string_c_same1("hello"); + ASSERT_TRUE(byte_string == byte_string_c_same1); + ASSERT_TRUE(byte_string_c_same1 == byte_string); + + CFX_ByteStringC byte_string_c1("he"); + CFX_ByteStringC byte_string_c2("hellp"); + CFX_ByteStringC byte_string_c3("hellod"); + ASSERT_FALSE(byte_string == byte_string_c1); + ASSERT_FALSE(byte_string == byte_string_c2); + ASSERT_FALSE(byte_string == byte_string_c3); + ASSERT_FALSE(byte_string_c1 == byte_string); + ASSERT_FALSE(byte_string_c2 == byte_string); + ASSERT_FALSE(byte_string_c3 == byte_string); + + const char* c_string_same1 = "hello"; + ASSERT_TRUE(byte_string == c_string_same1); +#if 0 + // TODO(tsepez): See, you don't want implicit c_str() casting. + // This degrades to a pointer comparision, which flunks. + ASSERT_TRUE(c_string_same1 == byte_string); +#endif + + const char* c_string1 = "he"; + const char* c_string2 = "hellp"; + const char* c_string3 = "hellod"; + ASSERT_FALSE(byte_string == c_string1); + ASSERT_FALSE(byte_string == c_string2); + ASSERT_FALSE(byte_string == c_string3); + ASSERT_FALSE(c_string1 == byte_string); + ASSERT_FALSE(c_string2 == byte_string); + ASSERT_FALSE(c_string3 == byte_string); +} + +TEST(fxcrt, ByteStringOperatorNE) { + CFX_ByteString byte_string("hello"); + ASSERT_FALSE(byte_string != byte_string); + + CFX_ByteString byte_string_same1("hello"); + ASSERT_FALSE(byte_string != byte_string_same1); + ASSERT_FALSE(byte_string_same1 != byte_string); + + CFX_ByteString byte_string_same2(byte_string); + ASSERT_FALSE(byte_string != byte_string_same2); + ASSERT_FALSE(byte_string_same2 != byte_string); + + CFX_ByteString byte_string1("he"); + CFX_ByteString byte_string2("hellp"); + CFX_ByteString byte_string3("hellod"); + ASSERT_TRUE(byte_string != byte_string1); + ASSERT_TRUE(byte_string != byte_string2); + ASSERT_TRUE(byte_string != byte_string3); + ASSERT_TRUE(byte_string1 != byte_string); + ASSERT_TRUE(byte_string2 != byte_string); + ASSERT_TRUE(byte_string3 != byte_string); + + CFX_ByteStringC byte_string_c_same1("hello"); + ASSERT_FALSE(byte_string != byte_string_c_same1); + ASSERT_FALSE(byte_string_c_same1 != byte_string); + + CFX_ByteStringC byte_string_c1("he"); + CFX_ByteStringC byte_string_c2("hellp"); + CFX_ByteStringC byte_string_c3("hellod"); + ASSERT_TRUE(byte_string != byte_string_c1); + ASSERT_TRUE(byte_string != byte_string_c2); + ASSERT_TRUE(byte_string != byte_string_c3); + ASSERT_TRUE(byte_string_c1 != byte_string); + ASSERT_TRUE(byte_string_c2 != byte_string); + ASSERT_TRUE(byte_string_c3 != byte_string); + + const char* c_string_same1 = "hello"; + ASSERT_FALSE(byte_string != c_string_same1); +#if 0 + // See above TODO. + ASSERT_FALSE(c_string_same1 != byte_string); +#endif + const char* c_string1 = "he"; + const char* c_string2 = "hellp"; + const char* c_string3 = "hellod"; + ASSERT_TRUE(byte_string != c_string1); + ASSERT_TRUE(byte_string != c_string2); + ASSERT_TRUE(byte_string != c_string3); + ASSERT_TRUE(c_string1 != byte_string); + ASSERT_TRUE(c_string2 != byte_string); + ASSERT_TRUE(c_string3 != byte_string); +} + TEST(fxcrt, ByteStringCNull) { CFX_ByteStringC null_string; EXPECT_EQ(null_string.GetPtr(), nullptr); @@ -268,3 +376,117 @@ TEST(fxcrt, ByteStringCOperatorLT) { EXPECT_TRUE(abc < def); EXPECT_FALSE(def < abc); } + +TEST(fxcrt, ByteStringCOperatorEQ) { + CFX_ByteStringC byte_string_c("hello"); + ASSERT_TRUE(byte_string_c == byte_string_c); + + CFX_ByteStringC byte_string_c_same1("hello"); + ASSERT_TRUE(byte_string_c == byte_string_c_same1); + ASSERT_TRUE(byte_string_c_same1 == byte_string_c); + + CFX_ByteStringC byte_string_c_same2(byte_string_c); + ASSERT_TRUE(byte_string_c == byte_string_c_same2); + ASSERT_TRUE(byte_string_c_same2 == byte_string_c); + + CFX_ByteStringC byte_string_c1("he"); + CFX_ByteStringC byte_string_c2("hellp"); + CFX_ByteStringC byte_string_c3("hellod"); + ASSERT_FALSE(byte_string_c == byte_string_c1); + ASSERT_FALSE(byte_string_c == byte_string_c2); + ASSERT_FALSE(byte_string_c == byte_string_c3); + ASSERT_FALSE(byte_string_c1 == byte_string_c); + ASSERT_FALSE(byte_string_c2 == byte_string_c); + ASSERT_FALSE(byte_string_c3 == byte_string_c); + + CFX_ByteString byte_string_same1("hello"); + ASSERT_TRUE(byte_string_c == byte_string_same1); + ASSERT_TRUE(byte_string_same1 == byte_string_c); + + CFX_ByteString byte_string1("he"); + CFX_ByteString byte_string2("hellp"); + CFX_ByteString byte_string3("hellod"); + ASSERT_FALSE(byte_string_c == byte_string1); + ASSERT_FALSE(byte_string_c == byte_string2); + ASSERT_FALSE(byte_string_c == byte_string3); + ASSERT_FALSE(byte_string1 == byte_string_c); + ASSERT_FALSE(byte_string2 == byte_string_c); + ASSERT_FALSE(byte_string3 == byte_string_c); + + const char* c_string_same1 = "hello"; + ASSERT_TRUE(byte_string_c == c_string_same1); +#if 0 + // TODO(tsepez): missing operator (but no implicit cast to c_str). + ASSERT_TRUE(c_string_same1 == byte_string_c); +#endif + + const char* c_string1 = "he"; + const char* c_string2 = "hellp"; + const char* c_string3 = "hellod"; + ASSERT_FALSE(byte_string_c == c_string1); + ASSERT_FALSE(byte_string_c == c_string2); + ASSERT_FALSE(byte_string_c == c_string3); +#if 0 + // See above TODO. + ASSERT_FALSE(c_string1 == byte_string_c); + ASSERT_FALSE(c_string2 == byte_string_c); + ASSERT_FALSE(c_string3 == byte_string_c); +#endif +} + +TEST(fxcrt, ByteStringCOperatorNE) { + CFX_ByteStringC byte_string_c("hello"); + ASSERT_FALSE(byte_string_c != byte_string_c); + + CFX_ByteStringC byte_string_c_same1("hello"); + ASSERT_FALSE(byte_string_c != byte_string_c_same1); + ASSERT_FALSE(byte_string_c_same1 != byte_string_c); + + CFX_ByteStringC byte_string_c_same2(byte_string_c); + ASSERT_FALSE(byte_string_c != byte_string_c_same2); + ASSERT_FALSE(byte_string_c_same2 != byte_string_c); + + CFX_ByteStringC byte_string_c1("he"); + CFX_ByteStringC byte_string_c2("hellp"); + CFX_ByteStringC byte_string_c3("hellod"); + ASSERT_TRUE(byte_string_c != byte_string_c1); + ASSERT_TRUE(byte_string_c != byte_string_c2); + ASSERT_TRUE(byte_string_c != byte_string_c3); + ASSERT_TRUE(byte_string_c1 != byte_string_c); + ASSERT_TRUE(byte_string_c2 != byte_string_c); + ASSERT_TRUE(byte_string_c3 != byte_string_c); + + CFX_ByteString byte_string_same1("hello"); + ASSERT_FALSE(byte_string_c != byte_string_same1); + ASSERT_FALSE(byte_string_same1 != byte_string_c); + + CFX_ByteString byte_string1("he"); + CFX_ByteString byte_string2("hellp"); + CFX_ByteString byte_string3("hellod"); + ASSERT_TRUE(byte_string_c != byte_string1); + ASSERT_TRUE(byte_string_c != byte_string2); + ASSERT_TRUE(byte_string_c != byte_string3); + ASSERT_TRUE(byte_string1 != byte_string_c); + ASSERT_TRUE(byte_string2 != byte_string_c); + ASSERT_TRUE(byte_string3 != byte_string_c); + + const char* c_string_same1 = "hello"; + ASSERT_FALSE(byte_string_c != c_string_same1); +#if 0 + // TODO(tsepez): missing operator (but no implicit cast to c_str). + ASSERT_FALSE(c_string_same1 != byte_string_c); +#endif + + const char* c_string1 = "he"; + const char* c_string2 = "hellp"; + const char* c_string3 = "hellod"; + ASSERT_TRUE(byte_string_c != c_string1); + ASSERT_TRUE(byte_string_c != c_string2); + ASSERT_TRUE(byte_string_c != c_string3); +#if 0 + // See above TODO. + ASSERT_TRUE(c_string1 != byte_string_c); + ASSERT_TRUE(c_string2 != byte_string_c); + ASSERT_TRUE(c_string3 != byte_string_c); +#endif +} diff --git a/core/src/fxcrt/fx_basic_wstring_unittest.cpp b/core/src/fxcrt/fx_basic_wstring_unittest.cpp index 084410ad19..16d71cb5b8 100644 --- a/core/src/fxcrt/fx_basic_wstring_unittest.cpp +++ b/core/src/fxcrt/fx_basic_wstring_unittest.cpp @@ -45,6 +45,120 @@ TEST(fxcrt, WideStringOperatorLT) { EXPECT_FALSE(def < abc); } +TEST(fxcrt, WideStringOperatorEQ) { + CFX_WideString wide_string(L"hello"); + ASSERT_TRUE(wide_string == wide_string); + + CFX_WideString wide_string_same1(L"hello"); + ASSERT_TRUE(wide_string == wide_string_same1); + ASSERT_TRUE(wide_string_same1 == wide_string); + + CFX_WideString wide_string_same2(wide_string); + ASSERT_TRUE(wide_string == wide_string_same2); + ASSERT_TRUE(wide_string_same2 == wide_string); + + CFX_WideString wide_string1(L"he"); + CFX_WideString wide_string2(L"hellp"); + CFX_WideString wide_string3(L"hellod"); + ASSERT_FALSE(wide_string == wide_string1); + ASSERT_FALSE(wide_string == wide_string2); + ASSERT_FALSE(wide_string == wide_string3); + ASSERT_FALSE(wide_string1 == wide_string); + ASSERT_FALSE(wide_string2 == wide_string); + ASSERT_FALSE(wide_string3 == wide_string); + + CFX_WideStringC wide_string_c_same1(L"hello"); + ASSERT_TRUE(wide_string == wide_string_c_same1); + ASSERT_TRUE(wide_string_c_same1 == wide_string); + + CFX_WideStringC wide_string_c1(L"he"); + CFX_WideStringC wide_string_c2(L"hellp"); + CFX_WideStringC wide_string_c3(L"hellod"); + ASSERT_FALSE(wide_string == wide_string_c1); + ASSERT_FALSE(wide_string == wide_string_c2); + ASSERT_FALSE(wide_string == wide_string_c3); + ASSERT_FALSE(wide_string_c1 == wide_string); + ASSERT_FALSE(wide_string_c2 == wide_string); + ASSERT_FALSE(wide_string_c3 == wide_string); + + const wchar_t* c_string_same1 = L"hello"; + ASSERT_TRUE(wide_string == c_string_same1); +#if 0 + // TODO(tsepez): Missing operator - there's a prototype but no actual + // implementation (at least we already removed implicit c_str() casting). + ASSERT_TRUE(c_string_same1 == wide_string); +#endif + + const wchar_t* c_string1 = L"he"; + const wchar_t* c_string2 = L"hellp"; + const wchar_t* c_string3 = L"hellod"; + ASSERT_FALSE(wide_string == c_string1); + ASSERT_FALSE(wide_string == c_string2); + ASSERT_FALSE(wide_string == c_string3); +#if 0 + // See above TODO. + ASSERT_FALSE(c_string1 == wide_string); + ASSERT_FALSE(c_string2 == wide_string); + ASSERT_FALSE(c_string3 == wide_string); +#endif +} + +TEST(fxcrt, WideStringOperatorNE) { + CFX_WideString wide_string(L"hello"); + ASSERT_FALSE(wide_string != wide_string); + + CFX_WideString wide_string_same1(L"hello"); + ASSERT_FALSE(wide_string != wide_string_same1); + ASSERT_FALSE(wide_string_same1 != wide_string); + + CFX_WideString wide_string_same2(wide_string); + ASSERT_FALSE(wide_string != wide_string_same2); + ASSERT_FALSE(wide_string_same2 != wide_string); + + CFX_WideString wide_string1(L"he"); + CFX_WideString wide_string2(L"hellp"); + CFX_WideString wide_string3(L"hellod"); + ASSERT_TRUE(wide_string != wide_string1); + ASSERT_TRUE(wide_string != wide_string2); + ASSERT_TRUE(wide_string != wide_string3); + ASSERT_TRUE(wide_string1 != wide_string); + ASSERT_TRUE(wide_string2 != wide_string); + ASSERT_TRUE(wide_string3 != wide_string); + + CFX_WideStringC wide_string_c_same1(L"hello"); + ASSERT_FALSE(wide_string != wide_string_c_same1); + ASSERT_FALSE(wide_string_c_same1 != wide_string); + + CFX_WideStringC wide_string_c1(L"he"); + CFX_WideStringC wide_string_c2(L"hellp"); + CFX_WideStringC wide_string_c3(L"hellod"); + ASSERT_TRUE(wide_string != wide_string_c1); + ASSERT_TRUE(wide_string != wide_string_c2); + ASSERT_TRUE(wide_string != wide_string_c3); + ASSERT_TRUE(wide_string_c1 != wide_string); + ASSERT_TRUE(wide_string_c2 != wide_string); + ASSERT_TRUE(wide_string_c3 != wide_string); + + const wchar_t* c_string_same1 = L"hello"; + ASSERT_FALSE(wide_string != c_string_same1); +#if 0 + // See above TODO. + ASSERT_FALSE(c_string_same1 != wide_string); +#endif + const wchar_t* c_string1 = L"he"; + const wchar_t* c_string2 = L"hellp"; + const wchar_t* c_string3 = L"hellod"; + ASSERT_TRUE(wide_string != c_string1); + ASSERT_TRUE(wide_string != c_string2); + ASSERT_TRUE(wide_string != c_string3); +#if 0 + // See above TODO. + ASSERT_TRUE(c_string1 != wide_string); + ASSERT_TRUE(c_string2 != wide_string); + ASSERT_TRUE(c_string3 != wide_string); +#endif +} + #define ByteStringLiteral(str) CFX_ByteString(FX_BSTRC(str)) TEST(fxcrt, WideStringUTF16LE_Encode) { @@ -106,3 +220,112 @@ TEST(fxcrt, WideStringCOperatorLT) { EXPECT_FALSE(def < abc); } +TEST(fxcrt, WideStringCOperatorEQ) { + CFX_WideStringC wide_string_c(L"hello"); + ASSERT_TRUE(wide_string_c == wide_string_c); + + CFX_WideStringC wide_string_c_same1(L"hello"); + ASSERT_TRUE(wide_string_c == wide_string_c_same1); + ASSERT_TRUE(wide_string_c_same1 == wide_string_c); + + CFX_WideStringC wide_string_c_same2(wide_string_c); + ASSERT_TRUE(wide_string_c == wide_string_c_same2); + ASSERT_TRUE(wide_string_c_same2 == wide_string_c); + + CFX_WideStringC wide_string_c1(L"he"); + CFX_WideStringC wide_string_c2(L"hellp"); + CFX_WideStringC wide_string_c3(L"hellod"); + ASSERT_FALSE(wide_string_c == wide_string_c1); + ASSERT_FALSE(wide_string_c == wide_string_c2); + ASSERT_FALSE(wide_string_c == wide_string_c3); + ASSERT_FALSE(wide_string_c1 == wide_string_c); + ASSERT_FALSE(wide_string_c2 == wide_string_c); + ASSERT_FALSE(wide_string_c3 == wide_string_c); + + CFX_WideString wide_string_same1(L"hello"); + ASSERT_TRUE(wide_string_c == wide_string_same1); + ASSERT_TRUE(wide_string_same1 == wide_string_c); + + CFX_WideString wide_string1(L"he"); + CFX_WideString wide_string2(L"hellp"); + CFX_WideString wide_string3(L"hellod"); + ASSERT_FALSE(wide_string_c == wide_string1); + ASSERT_FALSE(wide_string_c == wide_string2); + ASSERT_FALSE(wide_string_c == wide_string3); + ASSERT_FALSE(wide_string1 == wide_string_c); + ASSERT_FALSE(wide_string2 == wide_string_c); + ASSERT_FALSE(wide_string3 == wide_string_c); + +#if 0 + // TODO(tsepez): ambiguos overload prevents compilation + const wchar_t* c_string_same1 = L"hello"; + ASSERT_TRUE(wide_string_c == c_string_same1); + ASSERT_TRUE(c_string_same1 == wide_string_c); + + const wchar_t* c_string1 = L"he"; + const wchar_t* c_string2 = L"hellp"; + const wchar_t* c_string3 = L"hellod"; + ASSERT_FALSE(wide_string_c == c_string1); + ASSERT_FALSE(wide_string_c == c_string2); + ASSERT_FALSE(wide_string_c == c_string3); + + ASSERT_FALSE(c_string1 == wide_string_c); + ASSERT_FALSE(c_string2 == wide_string_c); + ASSERT_FALSE(c_string3 == wide_string_c); +#endif +} + +TEST(fxcrt, WideStringCOperatorNE) { + CFX_WideStringC wide_string_c(L"hello"); + ASSERT_FALSE(wide_string_c != wide_string_c); + + CFX_WideStringC wide_string_c_same1(L"hello"); + ASSERT_FALSE(wide_string_c != wide_string_c_same1); + ASSERT_FALSE(wide_string_c_same1 != wide_string_c); + + CFX_WideStringC wide_string_c_same2(wide_string_c); + ASSERT_FALSE(wide_string_c != wide_string_c_same2); + ASSERT_FALSE(wide_string_c_same2 != wide_string_c); + + CFX_WideStringC wide_string_c1(L"he"); + CFX_WideStringC wide_string_c2(L"hellp"); + CFX_WideStringC wide_string_c3(L"hellod"); + ASSERT_TRUE(wide_string_c != wide_string_c1); + ASSERT_TRUE(wide_string_c != wide_string_c2); + ASSERT_TRUE(wide_string_c != wide_string_c3); + ASSERT_TRUE(wide_string_c1 != wide_string_c); + ASSERT_TRUE(wide_string_c2 != wide_string_c); + ASSERT_TRUE(wide_string_c3 != wide_string_c); + + CFX_WideString wide_string_same1(L"hello"); + ASSERT_FALSE(wide_string_c != wide_string_same1); + ASSERT_FALSE(wide_string_same1 != wide_string_c); + + CFX_WideString wide_string1(L"he"); + CFX_WideString wide_string2(L"hellp"); + CFX_WideString wide_string3(L"hellod"); + ASSERT_TRUE(wide_string_c != wide_string1); + ASSERT_TRUE(wide_string_c != wide_string2); + ASSERT_TRUE(wide_string_c != wide_string3); + ASSERT_TRUE(wide_string1 != wide_string_c); + ASSERT_TRUE(wide_string2 != wide_string_c); + ASSERT_TRUE(wide_string3 != wide_string_c); + +#if 0 + // See above TODO. + const wchar_t* c_string_same1 = L"hello"; + ASSERT_FALSE(wide_string_c != c_string_same1); + ASSERT_FALSE(c_string_same1 != wide_string_c); + + const wchar_t* c_string1 = L"he"; + const wchar_t* c_string2 = L"hellp"; + const wchar_t* c_string3 = L"hellod"; + ASSERT_TRUE(wide_string_c != c_string1); + ASSERT_TRUE(wide_string_c != c_string2); + ASSERT_TRUE(wide_string_c != c_string3); + + ASSERT_TRUE(c_string1 != wide_string_c); + ASSERT_TRUE(c_string2 != wide_string_c); + ASSERT_TRUE(c_string3 != wide_string_c); +#endif +} |