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