Add test for cfx_memorystream reallocations.
Change-Id: Ibc422ea720869ff133723324a77c755cdc5f87b2
Reviewed-on: https://pdfium-review.googlesource.com/c/48850
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/BUILD.gn b/core/fxcrt/BUILD.gn
index de32771..ce80606 100644
--- a/core/fxcrt/BUILD.gn
+++ b/core/fxcrt/BUILD.gn
@@ -138,6 +138,7 @@
"autorestorer_unittest.cpp",
"bytestring_unittest.cpp",
"cfx_bitstream_unittest.cpp",
+ "cfx_memorystream_unittest.cpp",
"cfx_widetextbuf_unittest.cpp",
"fx_bidi_unittest.cpp",
"fx_coordinates_unittest.cpp",
diff --git a/core/fxcrt/cfx_memorystream_unittest.cpp b/core/fxcrt/cfx_memorystream_unittest.cpp
new file mode 100644
index 0000000..99fa4e6
--- /dev/null
+++ b/core/fxcrt/cfx_memorystream_unittest.cpp
@@ -0,0 +1,32 @@
+// Copyright 2019 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/fxcrt/cfx_memorystream.h"
+
+#include "core/fxcrt/retain_ptr.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
+
+namespace {
+
+const char kSomeText[] = "Lets make holes in streams";
+const size_t kSomeTextLen = sizeof(kSomeText) - 1;
+
+} // namespace
+
+TEST(CFX_MemoryStreamTest, SparseBlockWrites) {
+ auto stream = pdfium::MakeRetain<CFX_MemoryStream>();
+ for (FX_FILESIZE offset = 0; offset <= 200000; offset += 100000)
+ stream->WriteBlockAtOffset(kSomeText, offset, kSomeTextLen);
+
+ EXPECT_EQ(200000 + kSomeTextLen, static_cast<size_t>(stream->GetSize()));
+}
+
+TEST(CFX_MemoryStreamTest, OverlappingBlockWrites) {
+ auto stream = pdfium::MakeRetain<CFX_MemoryStream>();
+ for (FX_FILESIZE offset = 0; offset <= 100; ++offset)
+ stream->WriteBlockAtOffset(kSomeText, offset, kSomeTextLen);
+
+ EXPECT_EQ(100 + kSomeTextLen, static_cast<size_t>(stream->GetSize()));
+}