diff options
author | tsepez <tsepez@chromium.org> | 2016-04-26 12:13:16 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-04-26 12:13:16 -0700 |
commit | 518fd4c5ababbfbf28e010a9c27098e8f6669e4b (patch) | |
tree | 79e20cf56d24f2aaf9463056ccb52803426be082 /core/fxcrt/fx_basic_wstring_unittest.cpp | |
parent | 5cc24654fb345189140acb4711ff981e1c720951 (diff) | |
download | pdfium-518fd4c5ababbfbf28e010a9c27098e8f6669e4b.tar.xz |
CFX_ByteString::Reserve(), ReleaseBuffer() fixes.
Also identical fixes for CFX_WideString.
Reserve() on an empty string would not actually reserve a
buffer. Currently unused, but there are places where this
would really help.
ReleaseBuffer() would rarely return memory to the system, since
it would short-circuit thinking it could operate in place. Tune
the algorithm slightly so that we hold on when the savings is
small.
Bounds check release buffer args rather than just asserting.
Add tests for all of these.
Review URL: https://codereview.chromium.org/1916303004
Diffstat (limited to 'core/fxcrt/fx_basic_wstring_unittest.cpp')
-rw-r--r-- | core/fxcrt/fx_basic_wstring_unittest.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/core/fxcrt/fx_basic_wstring_unittest.cpp b/core/fxcrt/fx_basic_wstring_unittest.cpp index 708556af1d..02281afd36 100644 --- a/core/fxcrt/fx_basic_wstring_unittest.cpp +++ b/core/fxcrt/fx_basic_wstring_unittest.cpp @@ -591,6 +591,83 @@ TEST(fxcrt, WideStringTrimLeftCopies) { } } +TEST(fxcrt, WideStringReserve) { + { + CFX_WideString str; + str.Reserve(6); + const FX_WCHAR* old_buffer = str.c_str(); + str += L"ABCDEF"; + EXPECT_EQ(old_buffer, str.c_str()); + str += L"Blah Blah Blah Blah Blah Blah"; + EXPECT_NE(old_buffer, str.c_str()); + } + { + CFX_WideString str(L"A"); + str.Reserve(6); + const FX_WCHAR* old_buffer = str.c_str(); + str += L"BCDEF"; + EXPECT_EQ(old_buffer, str.c_str()); + str += L"Blah Blah Blah Blah Blah Blah"; + EXPECT_NE(old_buffer, str.c_str()); + } +} + +TEST(fxcrt, WideStringGetBuffer) { + { + CFX_WideString str; + FX_WCHAR* buffer = str.GetBuffer(12); + wcscpy(buffer, L"clams"); + str.ReleaseBuffer(); + EXPECT_EQ(L"clams", str); + } + { + CFX_WideString str(L"cl"); + FX_WCHAR* buffer = str.GetBuffer(12); + wcscpy(buffer + 2, L"ams"); + str.ReleaseBuffer(); + EXPECT_EQ(L"clams", str); + } +} + +TEST(fxcrt, WideStringReleaseBuffer) { + { + CFX_WideString str; + str.Reserve(12); + str += L"clams"; + const FX_WCHAR* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_EQ(old_buffer, str.c_str()); + EXPECT_EQ(L"clam", str); + } + { + CFX_WideString str(L"c"); + str.Reserve(12); + str += L"lams"; + const FX_WCHAR* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_EQ(old_buffer, str.c_str()); + EXPECT_EQ(L"clam", str); + } + { + CFX_WideString str; + str.Reserve(200); + str += L"clams"; + const FX_WCHAR* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_NE(old_buffer, str.c_str()); + EXPECT_EQ(L"clam", str); + } + { + CFX_WideString str(L"c"); + str.Reserve(200); + str += L"lams"; + const FX_WCHAR* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_NE(old_buffer, str.c_str()); + EXPECT_EQ(L"clam", str); + } +} + TEST(fxcrt, WideStringUTF16LE_Encode) { struct UTF16LEEncodeCase { CFX_WideString ws; |