blob: 1367af72e4c1a6a86eb0bf2630c69fb563bb9a52 [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 Lesinskid0f116b2016-07-08 15:00:32 -070018#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070019#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <string>
22
23namespace aapt {
24
25TEST(StringPoolTest, InsertOneString) {
26 StringPool pool;
27
Adam Lesinskid0f116b2016-07-08 15:00:32 -070028 StringPool::Ref ref = pool.makeRef("wut");
29 EXPECT_EQ(*ref, "wut");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030}
31
32TEST(StringPoolTest, InsertTwoUniqueStrings) {
33 StringPool pool;
34
Adam Lesinskid0f116b2016-07-08 15:00:32 -070035 StringPool::Ref ref = pool.makeRef("wut");
36 StringPool::Ref ref2 = pool.makeRef("hey");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinskid0f116b2016-07-08 15:00:32 -070038 EXPECT_EQ(*ref, "wut");
39 EXPECT_EQ(*ref2, "hey");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040}
41
42TEST(StringPoolTest, DoNotInsertNewDuplicateString) {
43 StringPool pool;
44
Adam Lesinskid0f116b2016-07-08 15:00:32 -070045 StringPool::Ref ref = pool.makeRef("wut");
46 StringPool::Ref ref2 = pool.makeRef("wut");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskid0f116b2016-07-08 15:00:32 -070048 EXPECT_EQ(*ref, "wut");
49 EXPECT_EQ(*ref2, "wut");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050 EXPECT_EQ(1u, pool.size());
51}
52
53TEST(StringPoolTest, MaintainInsertionOrderIndex) {
54 StringPool pool;
55
Adam Lesinskid0f116b2016-07-08 15:00:32 -070056 StringPool::Ref ref = pool.makeRef("z");
57 StringPool::Ref ref2 = pool.makeRef("a");
58 StringPool::Ref ref3 = pool.makeRef("m");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
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 Lesinskid0f116b2016-07-08 15:00:32 -070068 StringPool::Ref refA = pool.makeRef("foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069 {
Adam Lesinskid0f116b2016-07-08 15:00:32 -070070 StringPool::Ref ref = pool.makeRef("wut");
71 EXPECT_EQ(*ref, "wut");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072 EXPECT_EQ(2u, pool.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073 }
Adam Lesinskid0f116b2016-07-08 15:00:32 -070074 StringPool::Ref refB = pool.makeRef("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);
Adam Lesinskid0f116b2016-07-08 15:00:32 -070080 EXPECT_EQ((*iter)->value, "foo");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081 EXPECT_LT((*iter)->index, 2u);
82 ++iter;
Adam Lesinskid0f116b2016-07-08 15:00:32 -070083 EXPECT_EQ((*iter)->value, "bar");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 EXPECT_LT((*iter)->index, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085}
86
87TEST(StringPoolTest, SortAndMaintainIndexesInReferences) {
88 StringPool pool;
89
Adam Lesinskid0f116b2016-07-08 15:00:32 -070090 StringPool::Ref ref = pool.makeRef("z");
91 StringPool::StyleRef ref2 = pool.makeRef(StyleString{ {"a"} });
92 StringPool::Ref ref3 = pool.makeRef("m");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskid0f116b2016-07-08 15:00:32 -070094 EXPECT_EQ(*ref, "z");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095 EXPECT_EQ(0u, ref.getIndex());
96
Adam Lesinskid0f116b2016-07-08 15:00:32 -070097 EXPECT_EQ(*(ref2->str), "a");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098 EXPECT_EQ(1u, ref2.getIndex());
99
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700100 EXPECT_EQ(*ref3, "m");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101 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
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700108 EXPECT_EQ(*ref, "z");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109 EXPECT_EQ(2u, ref.getIndex());
110
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700111 EXPECT_EQ(*(ref2->str), "a");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112 EXPECT_EQ(0u, ref2.getIndex());
113
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700114 EXPECT_EQ(*ref3, "m");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115 EXPECT_EQ(1u, ref3.getIndex());
116}
117
118TEST(StringPoolTest, SortAndStillDedupe) {
119 StringPool pool;
120
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700121 StringPool::Ref ref = pool.makeRef("z");
122 StringPool::Ref ref2 = pool.makeRef("a");
123 StringPool::Ref ref3 = pool.makeRef("m");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124
125 pool.sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
126 return a.value < b.value;
127 });
128
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700129 StringPool::Ref ref4 = pool.makeRef("z");
130 StringPool::Ref ref5 = pool.makeRef("a");
131 StringPool::Ref ref6 = pool.makeRef("m");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
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 {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700142 { "android" },
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143 {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700144 Span{ { "b" }, 2, 6 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145 }
146 };
147
148 StringPool::StyleRef ref = pool.makeRef(str);
149
150 EXPECT_EQ(0u, ref.getIndex());
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700151 EXPECT_EQ(std::string("android"), *(ref->str));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152 ASSERT_EQ(1u, ref->spans.size());
153
154 const StringPool::Span& span = ref->spans.front();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700155 EXPECT_EQ(*(span.name), "b");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156 EXPECT_EQ(2u, span.firstChar);
157 EXPECT_EQ(6u, span.lastChar);
158}
159
160TEST(StringPoolTest, DoNotDedupeStyleWithSameStringAsNonStyle) {
161 StringPool pool;
162
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700163 StringPool::Ref ref = pool.makeRef("android");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800164
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700165 StyleString str { { "android" } };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166 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;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700187 pool.makeRef("\u093f");
Adam Lesinski52364f72016-01-11 13:10:24 -0800188 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 Lesinskid0f116b2016-07-08 15:00:32 -0700201constexpr const char* sLongString = "バッテリーを長持ちさせるため、バッテリーセーバーは端末のパフォーマンスを抑え、バイブレーション、位置情報サービス、大半のバックグラウンドデータを制限します。メール、SMSや、同期を使 用するその他のアプリは、起動しても更新されないことがあります。バッテリーセーバーは端末の充電中は自動的にOFFになります。";
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700203TEST(StringPoolTest, Flatten) {
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
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700208 StringPool::Ref ref1 = pool.makeRef("hello");
209 StringPool::Ref ref2 = pool.makeRef("goodbye");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800210 StringPool::Ref ref3 = pool.makeRef(sLongString);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700211 StringPool::Ref ref4 = pool.makeRef("");
Adam Lesinskie1fda9a2016-07-01 14:51:51 -0700212 StringPool::StyleRef ref5 = pool.makeRef(StyleString{
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700213 { "style" },
214 { Span{ { "b" }, 0, 1 }, Span{ { "i" }, 2, 3 } }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215 });
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
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700223 BigBuffer buffers[2] = { BigBuffer(1024), BigBuffer(1024) };
224 StringPool::flattenUtf8(&buffers[0], pool);
225 StringPool::flattenUtf16(&buffers[1], pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700227 // Test both UTF-8 and UTF-16 buffers.
228 for (const BigBuffer& buffer : buffers) {
229 std::unique_ptr<uint8_t[]> data = util::copy(buffer);
230
Adam Lesinski803c7c82016-04-06 16:09:43 -0700231 ResStringPool test;
232 ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700234 EXPECT_EQ(std::string("hello"), util::getString(test, 0));
235 EXPECT_EQ(StringPiece16(u"hello"), util::getString16(test, 0));
236
237 EXPECT_EQ(std::string("goodbye"), util::getString(test, 1));
238 EXPECT_EQ(StringPiece16(u"goodbye"), util::getString16(test, 1));
239
240 EXPECT_EQ(StringPiece(sLongString), util::getString(test, 2));
241 EXPECT_EQ(util::utf8ToUtf16(sLongString), util::getString16(test, 2).toString());
242
Adam Lesinskie1fda9a2016-07-01 14:51:51 -0700243 size_t len;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700244 EXPECT_TRUE(test.stringAt(3, &len) != nullptr || test.string8At(3, &len) != nullptr);
245
246 EXPECT_EQ(std::string("style"), util::getString(test, 4));
247 EXPECT_EQ(StringPiece16(u"style"), util::getString16(test, 4));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248
Adam Lesinskie1fda9a2016-07-01 14:51:51 -0700249 const ResStringPool_span* span = test.styleAt(4);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250 ASSERT_NE(nullptr, span);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700251 EXPECT_EQ(std::string("b"), util::getString(test, span->name.index));
252 EXPECT_EQ(StringPiece16(u"b"), util::getString16(test, span->name.index));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800253 EXPECT_EQ(0u, span->firstChar);
254 EXPECT_EQ(1u, span->lastChar);
255 span++;
256
Adam Lesinskica2fc352015-04-03 12:08:26 -0700257 ASSERT_NE(ResStringPool_span::END, span->name.index);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700258 EXPECT_EQ(std::string("i"), util::getString(test, span->name.index));
259 EXPECT_EQ(StringPiece16(u"i"), util::getString16(test, span->name.index));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260 EXPECT_EQ(2u, span->firstChar);
261 EXPECT_EQ(3u, span->lastChar);
262 span++;
263
Adam Lesinskica2fc352015-04-03 12:08:26 -0700264 EXPECT_EQ(ResStringPool_span::END, span->name.index);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800266}
267
268} // namespace aapt