blob: 562d80ed8cdbb025c89645ee23d4e2c20bb3381b [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "StringPool.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019
20#include <gtest/gtest.h>
21#include <string>
22
23namespace aapt {
24
25TEST(StringPoolTest, InsertOneString) {
26 StringPool pool;
27
28 StringPool::Ref ref = pool.makeRef(u"wut");
29 EXPECT_EQ(*ref, u"wut");
30}
31
32TEST(StringPoolTest, InsertTwoUniqueStrings) {
33 StringPool pool;
34
35 StringPool::Ref ref = pool.makeRef(u"wut");
36 StringPool::Ref ref2 = pool.makeRef(u"hey");
37
38 EXPECT_EQ(*ref, u"wut");
39 EXPECT_EQ(*ref2, u"hey");
40}
41
42TEST(StringPoolTest, DoNotInsertNewDuplicateString) {
43 StringPool pool;
44
45 StringPool::Ref ref = pool.makeRef(u"wut");
46 StringPool::Ref ref2 = pool.makeRef(u"wut");
47
48 EXPECT_EQ(*ref, u"wut");
49 EXPECT_EQ(*ref2, u"wut");
50 EXPECT_EQ(1u, pool.size());
51}
52
53TEST(StringPoolTest, MaintainInsertionOrderIndex) {
54 StringPool pool;
55
56 StringPool::Ref ref = pool.makeRef(u"z");
57 StringPool::Ref ref2 = pool.makeRef(u"a");
58 StringPool::Ref ref3 = pool.makeRef(u"m");
59
60 EXPECT_EQ(0u, ref.getIndex());
61 EXPECT_EQ(1u, ref2.getIndex());
62 EXPECT_EQ(2u, ref3.getIndex());
63}
64
65TEST(StringPoolTest, PruneStringsWithNoReferences) {
66 StringPool pool;
67
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068 StringPool::Ref refA = pool.makeRef(u"foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069 {
70 StringPool::Ref ref = pool.makeRef(u"wut");
71 EXPECT_EQ(*ref, u"wut");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 EXPECT_EQ(2u, pool.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074 StringPool::Ref refB = pool.makeRef(u"bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076 EXPECT_EQ(3u, pool.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077 pool.prune();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078 EXPECT_EQ(2u, pool.size());
79 StringPool::const_iterator iter = begin(pool);
80 EXPECT_EQ((*iter)->value, u"foo");
81 EXPECT_LT((*iter)->index, 2u);
82 ++iter;
83 EXPECT_EQ((*iter)->value, u"bar");
84 EXPECT_LT((*iter)->index, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085}
86
87TEST(StringPoolTest, SortAndMaintainIndexesInReferences) {
88 StringPool pool;
89
90 StringPool::Ref ref = pool.makeRef(u"z");
91 StringPool::StyleRef ref2 = pool.makeRef(StyleString{ {u"a"} });
92 StringPool::Ref ref3 = pool.makeRef(u"m");
93
94 EXPECT_EQ(*ref, u"z");
95 EXPECT_EQ(0u, ref.getIndex());
96
97 EXPECT_EQ(*(ref2->str), u"a");
98 EXPECT_EQ(1u, ref2.getIndex());
99
100 EXPECT_EQ(*ref3, u"m");
101 EXPECT_EQ(2u, ref3.getIndex());
102
103 pool.sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
104 return a.value < b.value;
105 });
106
107
108 EXPECT_EQ(*ref, u"z");
109 EXPECT_EQ(2u, ref.getIndex());
110
111 EXPECT_EQ(*(ref2->str), u"a");
112 EXPECT_EQ(0u, ref2.getIndex());
113
114 EXPECT_EQ(*ref3, u"m");
115 EXPECT_EQ(1u, ref3.getIndex());
116}
117
118TEST(StringPoolTest, SortAndStillDedupe) {
119 StringPool pool;
120
121 StringPool::Ref ref = pool.makeRef(u"z");
122 StringPool::Ref ref2 = pool.makeRef(u"a");
123 StringPool::Ref ref3 = pool.makeRef(u"m");
124
125 pool.sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
126 return a.value < b.value;
127 });
128
129 StringPool::Ref ref4 = pool.makeRef(u"z");
130 StringPool::Ref ref5 = pool.makeRef(u"a");
131 StringPool::Ref ref6 = pool.makeRef(u"m");
132
133 EXPECT_EQ(ref4.getIndex(), ref.getIndex());
134 EXPECT_EQ(ref5.getIndex(), ref2.getIndex());
135 EXPECT_EQ(ref6.getIndex(), ref3.getIndex());
136}
137
138TEST(StringPoolTest, AddStyles) {
139 StringPool pool;
140
141 StyleString str {
142 { u"android" },
143 {
144 Span{ { u"b" }, 2, 6 }
145 }
146 };
147
148 StringPool::StyleRef ref = pool.makeRef(str);
149
150 EXPECT_EQ(0u, ref.getIndex());
151 EXPECT_EQ(std::u16string(u"android"), *(ref->str));
152 ASSERT_EQ(1u, ref->spans.size());
153
154 const StringPool::Span& span = ref->spans.front();
155 EXPECT_EQ(*(span.name), u"b");
156 EXPECT_EQ(2u, span.firstChar);
157 EXPECT_EQ(6u, span.lastChar);
158}
159
160TEST(StringPoolTest, DoNotDedupeStyleWithSameStringAsNonStyle) {
161 StringPool pool;
162
163 StringPool::Ref ref = pool.makeRef(u"android");
164
165 StyleString str { { u"android" } };
166 StringPool::StyleRef styleRef = pool.makeRef(str);
167
168 EXPECT_NE(ref.getIndex(), styleRef.getIndex());
169}
170
Adam Lesinski769de982015-04-10 19:43:55 -0700171TEST(StringPoolTest, FlattenEmptyStringPoolUtf8) {
Adam Lesinski803c7c82016-04-06 16:09:43 -0700172 using namespace android; // For NO_ERROR on Windows.
173
Adam Lesinski769de982015-04-10 19:43:55 -0700174 StringPool pool;
175 BigBuffer buffer(1024);
176 StringPool::flattenUtf8(&buffer, pool);
177
178 std::unique_ptr<uint8_t[]> data = util::copy(buffer);
Adam Lesinski803c7c82016-04-06 16:09:43 -0700179 ResStringPool test;
180 ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);
Adam Lesinski769de982015-04-10 19:43:55 -0700181}
182
Adam Lesinski52364f72016-01-11 13:10:24 -0800183TEST(StringPoolTest, FlattenOddCharactersUtf16) {
Adam Lesinski803c7c82016-04-06 16:09:43 -0700184 using namespace android; // For NO_ERROR on Windows.
185
Adam Lesinski52364f72016-01-11 13:10:24 -0800186 StringPool pool;
187 pool.makeRef(u"\u093f");
188 BigBuffer buffer(1024);
189 StringPool::flattenUtf16(&buffer, pool);
190
191 std::unique_ptr<uint8_t[]> data = util::copy(buffer);
Adam Lesinski803c7c82016-04-06 16:09:43 -0700192 ResStringPool test;
193 ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);
Adam Lesinski52364f72016-01-11 13:10:24 -0800194 size_t len = 0;
195 const char16_t* str = test.stringAt(0, &len);
196 EXPECT_EQ(1u, len);
197 EXPECT_EQ(u'\u093f', *str);
198 EXPECT_EQ(0u, str[1]);
199}
200
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800201constexpr const char16_t* sLongString = u"バッテリーを長持ちさせるため、バッテリーセーバーは端末のパフォーマンスを抑え、バイブレーション、位置情報サービス、大半のバックグラウンドデータを制限します。メール、SMSや、同期を使 用するその他のアプリは、起動しても更新されないことがあります。バッテリーセーバーは端末の充電中は自動的にOFFになります。";
202
203TEST(StringPoolTest, FlattenUtf8) {
Adam Lesinski803c7c82016-04-06 16:09:43 -0700204 using namespace android; // For NO_ERROR on Windows.
205
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206 StringPool pool;
207
208 StringPool::Ref ref1 = pool.makeRef(u"hello");
209 StringPool::Ref ref2 = pool.makeRef(u"goodbye");
210 StringPool::Ref ref3 = pool.makeRef(sLongString);
Adam Lesinskie1fda9a2016-07-01 14:51:51 -0700211 StringPool::Ref ref4 = pool.makeRef(u"");
212 StringPool::StyleRef ref5 = pool.makeRef(StyleString{
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213 { u"style" },
214 { Span{ { u"b" }, 0, 1 }, Span{ { u"i" }, 2, 3 } }
215 });
216
217 EXPECT_EQ(0u, ref1.getIndex());
218 EXPECT_EQ(1u, ref2.getIndex());
219 EXPECT_EQ(2u, ref3.getIndex());
220 EXPECT_EQ(3u, ref4.getIndex());
Adam Lesinskie1fda9a2016-07-01 14:51:51 -0700221 EXPECT_EQ(4u, ref5.getIndex());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222
223 BigBuffer buffer(1024);
224 StringPool::flattenUtf8(&buffer, pool);
225
Adam Lesinski769de982015-04-10 19:43:55 -0700226 std::unique_ptr<uint8_t[]> data = util::copy(buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227 {
Adam Lesinski803c7c82016-04-06 16:09:43 -0700228 ResStringPool test;
229 ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230
231 EXPECT_EQ(util::getString(test, 0), u"hello");
232 EXPECT_EQ(util::getString(test, 1), u"goodbye");
233 EXPECT_EQ(util::getString(test, 2), sLongString);
Adam Lesinskie1fda9a2016-07-01 14:51:51 -0700234 size_t len;
235 EXPECT_NE(nullptr, test.stringAt(3, &len));
236 EXPECT_EQ(util::getString(test, 4), u"style");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinskie1fda9a2016-07-01 14:51:51 -0700238 const ResStringPool_span* span = test.styleAt(4);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800239 ASSERT_NE(nullptr, span);
240 EXPECT_EQ(util::getString(test, span->name.index), u"b");
241 EXPECT_EQ(0u, span->firstChar);
242 EXPECT_EQ(1u, span->lastChar);
243 span++;
244
Adam Lesinskica2fc352015-04-03 12:08:26 -0700245 ASSERT_NE(ResStringPool_span::END, span->name.index);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800246 EXPECT_EQ(util::getString(test, span->name.index), u"i");
247 EXPECT_EQ(2u, span->firstChar);
248 EXPECT_EQ(3u, span->lastChar);
249 span++;
250
Adam Lesinskica2fc352015-04-03 12:08:26 -0700251 EXPECT_EQ(ResStringPool_span::END, span->name.index);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800252 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253}
254
255} // namespace aapt