1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/fxcrt/string_pool_template.h"
6 #include "core/fxcrt/fx_string.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace fxcrt {
10
TEST(StringPool,ByteString)11 TEST(StringPool, ByteString) {
12 ByteStringPool pool;
13
14 ByteString null1;
15 ByteString null2;
16 ByteString goats1("goats");
17 ByteString goats2("goats");
18
19 // Underlying storage, if non-null, is not shared.
20 EXPECT_EQ(nullptr, null1.m_pData.Get());
21 EXPECT_EQ(nullptr, null2.m_pData.Get());
22 EXPECT_NE(goats1.m_pData, goats2.m_pData);
23
24 ByteString interned_null1 = pool.Intern(null1);
25 ByteString interned_null2 = pool.Intern(null2);
26 ByteString interned_goats1 = pool.Intern(goats1);
27 ByteString interned_goats2 = pool.Intern(goats2);
28
29 // Strings are logically equal after being interned.
30 EXPECT_EQ(null1, interned_null1);
31 EXPECT_EQ(null2, interned_null2);
32 EXPECT_EQ(goats1, interned_goats1);
33 EXPECT_EQ(goats2, interned_goats2);
34
35 // Interned underlying storage, if non-null, belongs to first seen.
36 EXPECT_EQ(nullptr, interned_null1.m_pData.Get());
37 EXPECT_EQ(nullptr, interned_null2.m_pData.Get());
38 EXPECT_EQ(goats1.m_pData, interned_goats1.m_pData);
39 EXPECT_EQ(goats1.m_pData, interned_goats2.m_pData);
40
41 pool.Clear();
42 ByteString reinterned_null2 = pool.Intern(null2);
43 ByteString reinterned_null1 = pool.Intern(null2);
44 ByteString reinterned_goats2 = pool.Intern(goats2);
45 ByteString reinterned_goats1 = pool.Intern(goats2);
46
47 // After clearing pool, storage was re-interned using second strings.
48 EXPECT_EQ(nullptr, interned_null1.m_pData.Get());
49 EXPECT_EQ(nullptr, interned_null2.m_pData.Get());
50 EXPECT_EQ(goats2.m_pData, reinterned_goats1.m_pData);
51 EXPECT_EQ(goats2.m_pData, reinterned_goats2.m_pData);
52 }
53
TEST(StringPool,WideString)54 TEST(StringPool, WideString) {
55 WideStringPool pool;
56
57 WideString null1;
58 WideString null2;
59 WideString goats1(L"goats");
60 WideString goats2(L"goats");
61
62 // Underlying storage, if non-null, is not shared.
63 EXPECT_EQ(nullptr, null1.m_pData.Get());
64 EXPECT_EQ(nullptr, null2.m_pData.Get());
65 EXPECT_NE(goats1.m_pData, goats2.m_pData);
66
67 WideString interned_null1 = pool.Intern(null1);
68 WideString interned_null2 = pool.Intern(null2);
69 WideString interned_goats1 = pool.Intern(goats1);
70 WideString interned_goats2 = pool.Intern(goats2);
71
72 // Strings are logically equal after being interned.
73 EXPECT_EQ(null1, interned_null1);
74 EXPECT_EQ(null2, interned_null2);
75 EXPECT_EQ(goats1, interned_goats1);
76 EXPECT_EQ(goats2, interned_goats2);
77
78 // Interned underlying storage, if non-null, belongs to first seen.
79 EXPECT_EQ(nullptr, interned_null1.m_pData.Get());
80 EXPECT_EQ(nullptr, interned_null2.m_pData.Get());
81 EXPECT_EQ(goats1.m_pData, interned_goats1.m_pData);
82 EXPECT_EQ(goats1.m_pData, interned_goats2.m_pData);
83
84 pool.Clear();
85 WideString reinterned_null2 = pool.Intern(null2);
86 WideString reinterned_null1 = pool.Intern(null2);
87 WideString reinterned_goats2 = pool.Intern(goats2);
88 WideString reinterned_goats1 = pool.Intern(goats2);
89
90 // After clearing pool, storage was re-interned using second strings.
91 EXPECT_EQ(nullptr, interned_null1.m_pData.Get());
92 EXPECT_EQ(nullptr, interned_null2.m_pData.Get());
93 EXPECT_EQ(goats2.m_pData, reinterned_goats1.m_pData);
94 EXPECT_EQ(goats2.m_pData, reinterned_goats2.m_pData);
95 }
96
97 } // namespace fxcrt
98