Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Unified Diff: core/fxcrt/cfx_string_pool_template_unittest.cpp

Issue 2341683005: Add string pools to save storage. (Closed)
Patch Set: fix compile Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: core/fxcrt/cfx_string_pool_template_unittest.cpp
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 0000000000000000000000000000000000000000..95a9007b92c982b78b3b82891056b4646a42fdd7
--- /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.
dsinclair 2016/09/15 20:23:26 Doesn't this check that the underlying storage is
Tom Sepez 2016/09/15 20:51:24 Yes, I think that's inline with the comment, for a
+ EXPECT_EQ(nullptr, null1.m_pData.Get());
+ EXPECT_EQ(nullptr, null2.m_pData.Get());
+ EXPECT_NE(goats1.m_pData, goats2.m_pData);
Tom Sepez 2016/09/15 20:52:37 Note that we don't get here because operator== on
+
+ 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);
+
dsinclair 2016/09/15 20:23:26 Should EXPECT_EQ(interned_goats1, interned_goats2)
Tom Sepez 2016/09/15 20:51:24 Yes, but that should be covered by the basic strin
+ // 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);
dsinclair 2016/09/15 20:23:26 Is it intentional that the null1 and goats1 are po
Tom Sepez 2016/09/15 20:51:24 Yes, otherwise they're not equal.
+ 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);
+}
« no previous file with comments | « BUILD.gn ('k') | core/fxcrt/fx_basic_bstring.cpp » ('j') | core/fxcrt/include/fx_string.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698