blob: c722fbeca690e144e693bd3972f3f2dc78fcc3d0 [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
Adam Lesinskica2fc352015-04-03 12:08:26 -070023using namespace android;
24
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025namespace aapt {
26
27TEST(StringPoolTest, InsertOneString) {
28 StringPool pool;
29
30 StringPool::Ref ref = pool.makeRef(u"wut");
31 EXPECT_EQ(*ref, u"wut");
32}
33
34TEST(StringPoolTest, InsertTwoUniqueStrings) {
35 StringPool pool;
36
37 StringPool::Ref ref = pool.makeRef(u"wut");
38 StringPool::Ref ref2 = pool.makeRef(u"hey");
39
40 EXPECT_EQ(*ref, u"wut");
41 EXPECT_EQ(*ref2, u"hey");
42}
43
44TEST(StringPoolTest, DoNotInsertNewDuplicateString) {
45 StringPool pool;
46
47 StringPool::Ref ref = pool.makeRef(u"wut");
48 StringPool::Ref ref2 = pool.makeRef(u"wut");
49
50 EXPECT_EQ(*ref, u"wut");
51 EXPECT_EQ(*ref2, u"wut");
52 EXPECT_EQ(1u, pool.size());
53}
54
55TEST(StringPoolTest, MaintainInsertionOrderIndex) {
56 StringPool pool;
57
58 StringPool::Ref ref = pool.makeRef(u"z");
59 StringPool::Ref ref2 = pool.makeRef(u"a");
60 StringPool::Ref ref3 = pool.makeRef(u"m");
61
62 EXPECT_EQ(0u, ref.getIndex());
63 EXPECT_EQ(1u, ref2.getIndex());
64 EXPECT_EQ(2u, ref3.getIndex());
65}
66
67TEST(StringPoolTest, PruneStringsWithNoReferences) {
68 StringPool pool;
69
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070 StringPool::Ref refA = pool.makeRef(u"foo");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071 {
72 StringPool::Ref ref = pool.makeRef(u"wut");
73 EXPECT_EQ(*ref, u"wut");
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074 EXPECT_EQ(2u, pool.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076 StringPool::Ref refB = pool.makeRef(u"bar");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078 EXPECT_EQ(3u, pool.size());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079 pool.prune();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080 EXPECT_EQ(2u, pool.size());
81 StringPool::const_iterator iter = begin(pool);
82 EXPECT_EQ((*iter)->value, u"foo");
83 EXPECT_LT((*iter)->index, 2u);
84 ++iter;
85 EXPECT_EQ((*iter)->value, u"bar");
86 EXPECT_LT((*iter)->index, 2u);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087}
88
89TEST(StringPoolTest, SortAndMaintainIndexesInReferences) {
90 StringPool pool;
91
92 StringPool::Ref ref = pool.makeRef(u"z");
93 StringPool::StyleRef ref2 = pool.makeRef(StyleString{ {u"a"} });
94 StringPool::Ref ref3 = pool.makeRef(u"m");
95
96 EXPECT_EQ(*ref, u"z");
97 EXPECT_EQ(0u, ref.getIndex());
98
99 EXPECT_EQ(*(ref2->str), u"a");
100 EXPECT_EQ(1u, ref2.getIndex());
101
102 EXPECT_EQ(*ref3, u"m");
103 EXPECT_EQ(2u, ref3.getIndex());
104
105 pool.sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
106 return a.value < b.value;
107 });
108
109
110 EXPECT_EQ(*ref, u"z");
111 EXPECT_EQ(2u, ref.getIndex());
112
113 EXPECT_EQ(*(ref2->str), u"a");
114 EXPECT_EQ(0u, ref2.getIndex());
115
116 EXPECT_EQ(*ref3, u"m");
117 EXPECT_EQ(1u, ref3.getIndex());
118}
119
120TEST(StringPoolTest, SortAndStillDedupe) {
121 StringPool pool;
122
123 StringPool::Ref ref = pool.makeRef(u"z");
124 StringPool::Ref ref2 = pool.makeRef(u"a");
125 StringPool::Ref ref3 = pool.makeRef(u"m");
126
127 pool.sort([](const StringPool::Entry& a, const StringPool::Entry& b) -> bool {
128 return a.value < b.value;
129 });
130
131 StringPool::Ref ref4 = pool.makeRef(u"z");
132 StringPool::Ref ref5 = pool.makeRef(u"a");
133 StringPool::Ref ref6 = pool.makeRef(u"m");
134
135 EXPECT_EQ(ref4.getIndex(), ref.getIndex());
136 EXPECT_EQ(ref5.getIndex(), ref2.getIndex());
137 EXPECT_EQ(ref6.getIndex(), ref3.getIndex());
138}
139
140TEST(StringPoolTest, AddStyles) {
141 StringPool pool;
142
143 StyleString str {
144 { u"android" },
145 {
146 Span{ { u"b" }, 2, 6 }
147 }
148 };
149
150 StringPool::StyleRef ref = pool.makeRef(str);
151
152 EXPECT_EQ(0u, ref.getIndex());
153 EXPECT_EQ(std::u16string(u"android"), *(ref->str));
154 ASSERT_EQ(1u, ref->spans.size());
155
156 const StringPool::Span& span = ref->spans.front();
157 EXPECT_EQ(*(span.name), u"b");
158 EXPECT_EQ(2u, span.firstChar);
159 EXPECT_EQ(6u, span.lastChar);
160}
161
162TEST(StringPoolTest, DoNotDedupeStyleWithSameStringAsNonStyle) {
163 StringPool pool;
164
165 StringPool::Ref ref = pool.makeRef(u"android");
166
167 StyleString str { { u"android" } };
168 StringPool::StyleRef styleRef = pool.makeRef(str);
169
170 EXPECT_NE(ref.getIndex(), styleRef.getIndex());
171}
172
Adam Lesinski769de982015-04-10 19:43:55 -0700173TEST(StringPoolTest, FlattenEmptyStringPoolUtf8) {
174 StringPool pool;
175 BigBuffer buffer(1024);
176 StringPool::flattenUtf8(&buffer, pool);
177
178 std::unique_ptr<uint8_t[]> data = util::copy(buffer);
179 android::ResStringPool test;
180 ASSERT_EQ(test.setTo(data.get(), buffer.size()), android::NO_ERROR);
181}
182
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183constexpr const char16_t* sLongString = u"バッテリーを長持ちさせるため、バッテリーセーバーは端末のパフォーマンスを抑え、バイブレーション、位置情報サービス、大半のバックグラウンドデータを制限します。メール、SMSや、同期を使 用するその他のアプリは、起動しても更新されないことがあります。バッテリーセーバーは端末の充電中は自動的にOFFになります。";
184
185TEST(StringPoolTest, FlattenUtf8) {
186 StringPool pool;
187
188 StringPool::Ref ref1 = pool.makeRef(u"hello");
189 StringPool::Ref ref2 = pool.makeRef(u"goodbye");
190 StringPool::Ref ref3 = pool.makeRef(sLongString);
191 StringPool::StyleRef ref4 = pool.makeRef(StyleString{
192 { u"style" },
193 { Span{ { u"b" }, 0, 1 }, Span{ { u"i" }, 2, 3 } }
194 });
195
196 EXPECT_EQ(0u, ref1.getIndex());
197 EXPECT_EQ(1u, ref2.getIndex());
198 EXPECT_EQ(2u, ref3.getIndex());
199 EXPECT_EQ(3u, ref4.getIndex());
200
201 BigBuffer buffer(1024);
202 StringPool::flattenUtf8(&buffer, pool);
203
Adam Lesinski769de982015-04-10 19:43:55 -0700204 std::unique_ptr<uint8_t[]> data = util::copy(buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205 {
Adam Lesinski769de982015-04-10 19:43:55 -0700206 android::ResStringPool test;
207 ASSERT_EQ(test.setTo(data.get(), buffer.size()), android::NO_ERROR);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208
209 EXPECT_EQ(util::getString(test, 0), u"hello");
210 EXPECT_EQ(util::getString(test, 1), u"goodbye");
211 EXPECT_EQ(util::getString(test, 2), sLongString);
212 EXPECT_EQ(util::getString(test, 3), u"style");
213
Adam Lesinskica2fc352015-04-03 12:08:26 -0700214 const ResStringPool_span* span = test.styleAt(3);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215 ASSERT_NE(nullptr, span);
216 EXPECT_EQ(util::getString(test, span->name.index), u"b");
217 EXPECT_EQ(0u, span->firstChar);
218 EXPECT_EQ(1u, span->lastChar);
219 span++;
220
Adam Lesinskica2fc352015-04-03 12:08:26 -0700221 ASSERT_NE(ResStringPool_span::END, span->name.index);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222 EXPECT_EQ(util::getString(test, span->name.index), u"i");
223 EXPECT_EQ(2u, span->firstChar);
224 EXPECT_EQ(3u, span->lastChar);
225 span++;
226
Adam Lesinskica2fc352015-04-03 12:08:26 -0700227 EXPECT_EQ(ResStringPool_span::END, span->name.index);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229}
230
231} // namespace aapt