summaryrefslogtreecommitdiff
path: root/core/fxcrt/cfx_string_pool_template_unittest.cpp
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-09-15 14:01:31 -0700
committerCommit bot <commit-bot@chromium.org>2016-09-15 14:01:31 -0700
commitd9871435eb7cea00a173baf780934f9d3525329a (patch)
treeec96dffc6e25865d6015f754b1a6406621c31bf4 /core/fxcrt/cfx_string_pool_template_unittest.cpp
parent0a17fafd723e8684d1deb4b5ceea58967a0154da (diff)
downloadpdfium-d9871435eb7cea00a173baf780934f9d3525329a.tar.xz
Add string pools to save storage.
Adds string hashes so CFX strings will interoperate with STL unordered containers. These will be employed per-document in a subsequent cl. BUG=pdfium:597 Review-Url: https://codereview.chromium.org/2341683005
Diffstat (limited to 'core/fxcrt/cfx_string_pool_template_unittest.cpp')
-rw-r--r--core/fxcrt/cfx_string_pool_template_unittest.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/core/fxcrt/cfx_string_pool_template_unittest.cpp b/core/fxcrt/cfx_string_pool_template_unittest.cpp
new file mode 100644
index 0000000000..95a9007b92
--- /dev/null
+++ b/core/fxcrt/cfx_string_pool_template_unittest.cpp
@@ -0,0 +1,94 @@
+// Copyright 2016 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/include/cfx_string_pool_template.h"
+#include "core/fxcrt/include/fx_string.h"
+#include "testing/fx_string_testhelpers.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(fxcrt, ByteStringPool) {
+ CFX_ByteStringPool pool;
+
+ CFX_ByteString null1;
+ CFX_ByteString null2;
+ CFX_ByteString goats1("goats");
+ CFX_ByteString goats2("goats");
+
+ // Underlying storage, if non-null, is not shared.
+ EXPECT_EQ(nullptr, null1.m_pData.Get());
+ EXPECT_EQ(nullptr, null2.m_pData.Get());
+ EXPECT_NE(goats1.m_pData, goats2.m_pData);
+
+ CFX_ByteString interned_null1 = pool.Intern(null1);
+ CFX_ByteString interned_null2 = pool.Intern(null2);
+ CFX_ByteString interned_goats1 = pool.Intern(goats1);
+ CFX_ByteString interned_goats2 = pool.Intern(goats2);
+
+ // Strings are logically equal after being interned.
+ EXPECT_EQ(null1, interned_null1);
+ EXPECT_EQ(null2, interned_null2);
+ EXPECT_EQ(goats1, interned_goats1);
+ EXPECT_EQ(goats2, interned_goats2);
+
+ // Interned underlying storage, if non-null, belongs to first seen.
+ EXPECT_EQ(nullptr, interned_null1.m_pData.Get());
+ EXPECT_EQ(nullptr, interned_null2.m_pData.Get());
+ EXPECT_EQ(goats1.m_pData, interned_goats1.m_pData);
+ EXPECT_EQ(goats1.m_pData, interned_goats2.m_pData);
+
+ pool.Clear();
+ CFX_ByteString reinterned_null2 = pool.Intern(null2);
+ CFX_ByteString reinterned_null1 = pool.Intern(null2);
+ CFX_ByteString reinterned_goats2 = pool.Intern(goats2);
+ CFX_ByteString reinterned_goats1 = pool.Intern(goats2);
+
+ // After clearing pool, storage was re-interned using second strings.
+ EXPECT_EQ(nullptr, interned_null1.m_pData.Get());
+ EXPECT_EQ(nullptr, interned_null2.m_pData.Get());
+ EXPECT_EQ(goats2.m_pData, reinterned_goats1.m_pData);
+ EXPECT_EQ(goats2.m_pData, reinterned_goats2.m_pData);
+}
+
+TEST(fxcrt, WideStringPool) {
+ CFX_WideStringPool pool;
+
+ CFX_WideString null1;
+ CFX_WideString null2;
+ CFX_WideString goats1(L"goats");
+ CFX_WideString goats2(L"goats");
+
+ // Underlying storage, if non-null, is not shared.
+ EXPECT_EQ(nullptr, null1.m_pData.Get());
+ EXPECT_EQ(nullptr, null2.m_pData.Get());
+ EXPECT_NE(goats1.m_pData, goats2.m_pData);
+
+ CFX_WideString interned_null1 = pool.Intern(null1);
+ CFX_WideString interned_null2 = pool.Intern(null2);
+ CFX_WideString interned_goats1 = pool.Intern(goats1);
+ CFX_WideString interned_goats2 = pool.Intern(goats2);
+
+ // Strings are logically equal after being interned.
+ EXPECT_EQ(null1, interned_null1);
+ EXPECT_EQ(null2, interned_null2);
+ EXPECT_EQ(goats1, interned_goats1);
+ EXPECT_EQ(goats2, interned_goats2);
+
+ // Interned underlying storage, if non-null, belongs to first seen.
+ EXPECT_EQ(nullptr, interned_null1.m_pData.Get());
+ EXPECT_EQ(nullptr, interned_null2.m_pData.Get());
+ EXPECT_EQ(goats1.m_pData, interned_goats1.m_pData);
+ EXPECT_EQ(goats1.m_pData, interned_goats2.m_pData);
+
+ pool.Clear();
+ CFX_WideString reinterned_null2 = pool.Intern(null2);
+ CFX_WideString reinterned_null1 = pool.Intern(null2);
+ CFX_WideString reinterned_goats2 = pool.Intern(goats2);
+ CFX_WideString reinterned_goats1 = pool.Intern(goats2);
+
+ // After clearing pool, storage was re-interned using second strings.
+ EXPECT_EQ(nullptr, interned_null1.m_pData.Get());
+ EXPECT_EQ(nullptr, interned_null2.m_pData.Get());
+ EXPECT_EQ(goats2.m_pData, reinterned_goats1.m_pData);
+ EXPECT_EQ(goats2.m_pData, reinterned_goats2.m_pData);
+}