Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 21 | #include "androidfw/StringPiece.h" |
| 22 | |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 23 | #include "Diagnostics.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | #include "test/Test.h" |
| 25 | #include "util/Util.h" |
| 26 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 27 | using ::android::StringPiece; |
| 28 | using ::android::StringPiece16; |
| 29 | using ::testing::Eq; |
| 30 | using ::testing::Ne; |
| 31 | using ::testing::NotNull; |
| 32 | using ::testing::Pointee; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | namespace aapt { |
| 35 | |
| 36 | TEST(StringPoolTest, InsertOneString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | StringPool::Ref ref = pool.MakeRef("wut"); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 40 | EXPECT_THAT(*ref, Eq("wut")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | TEST(StringPoolTest, InsertTwoUniqueStrings) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 45 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 46 | StringPool::Ref ref_a = pool.MakeRef("wut"); |
| 47 | StringPool::Ref ref_b = pool.MakeRef("hey"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 49 | EXPECT_THAT(*ref_a, Eq("wut")); |
| 50 | EXPECT_THAT(*ref_b, Eq("hey")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST(StringPoolTest, DoNotInsertNewDuplicateString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 56 | StringPool::Ref ref_a = pool.MakeRef("wut"); |
| 57 | StringPool::Ref ref_b = pool.MakeRef("wut"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 59 | EXPECT_THAT(*ref_a, Eq("wut")); |
| 60 | EXPECT_THAT(*ref_b, Eq("wut")); |
| 61 | EXPECT_THAT(pool.size(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | } |
| 63 | |
y | 4602926 | 2018-04-16 18:13:14 -0700 | [diff] [blame] | 64 | TEST(StringPoolTest, DoNotDedupeSameStringDifferentPriority) { |
| 65 | StringPool pool; |
| 66 | |
| 67 | StringPool::Ref ref_a = pool.MakeRef("wut", StringPool::Context(0x81010001)); |
| 68 | StringPool::Ref ref_b = pool.MakeRef("wut", StringPool::Context(0x81010002)); |
| 69 | |
| 70 | EXPECT_THAT(*ref_a, Eq("wut")); |
| 71 | EXPECT_THAT(*ref_b, Eq("wut")); |
| 72 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 73 | } |
| 74 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 75 | TEST(StringPoolTest, MaintainInsertionOrderIndex) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 78 | StringPool::Ref ref_a = pool.MakeRef("z"); |
| 79 | StringPool::Ref ref_b = pool.MakeRef("a"); |
| 80 | StringPool::Ref ref_c = pool.MakeRef("m"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 82 | EXPECT_THAT(ref_a.index(), Eq(0u)); |
| 83 | EXPECT_THAT(ref_b.index(), Eq(1u)); |
| 84 | EXPECT_THAT(ref_c.index(), Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST(StringPoolTest, PruneStringsWithNoReferences) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 89 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 90 | StringPool::Ref ref_a = pool.MakeRef("foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 92 | { |
| 93 | StringPool::Ref ref_b = pool.MakeRef("wut"); |
| 94 | EXPECT_THAT(*ref_b, Eq("wut")); |
| 95 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 96 | pool.Prune(); |
| 97 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 98 | } |
| 99 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 100 | |
| 101 | { |
| 102 | StringPool::Ref ref_c = pool.MakeRef("bar"); |
| 103 | EXPECT_THAT(pool.size(), Eq(3u)); |
| 104 | |
| 105 | pool.Prune(); |
| 106 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 107 | } |
| 108 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 109 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | pool.Prune(); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 111 | EXPECT_THAT(pool.size(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 114 | TEST(StringPoolTest, SortAndMaintainIndexesInStringReferences) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 117 | StringPool::Ref ref_a = pool.MakeRef("z"); |
| 118 | StringPool::Ref ref_b = pool.MakeRef("a"); |
| 119 | StringPool::Ref ref_c = pool.MakeRef("m"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 120 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 121 | EXPECT_THAT(*ref_a, Eq("z")); |
| 122 | EXPECT_THAT(ref_a.index(), Eq(0u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 123 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 124 | EXPECT_THAT(*ref_b, Eq("a")); |
| 125 | EXPECT_THAT(ref_b.index(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 127 | EXPECT_THAT(*ref_c, Eq("m")); |
| 128 | EXPECT_THAT(ref_c.index(), Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 129 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 130 | pool.Sort(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 131 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 132 | EXPECT_THAT(*ref_a, Eq("z")); |
| 133 | EXPECT_THAT(ref_a.index(), Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 135 | EXPECT_THAT(*ref_b, Eq("a")); |
| 136 | EXPECT_THAT(ref_b.index(), Eq(0u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 137 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 138 | EXPECT_THAT(*ref_c, Eq("m")); |
| 139 | EXPECT_THAT(ref_c.index(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TEST(StringPoolTest, SortAndStillDedupe) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 144 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 145 | StringPool::Ref ref_a = pool.MakeRef("z"); |
| 146 | StringPool::Ref ref_b = pool.MakeRef("a"); |
| 147 | StringPool::Ref ref_c = pool.MakeRef("m"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 148 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 149 | pool.Sort(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 151 | StringPool::Ref ref_d = pool.MakeRef("z"); |
| 152 | StringPool::Ref ref_e = pool.MakeRef("a"); |
| 153 | StringPool::Ref ref_f = pool.MakeRef("m"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 154 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 155 | EXPECT_THAT(ref_d.index(), Eq(ref_a.index())); |
| 156 | EXPECT_THAT(ref_e.index(), Eq(ref_b.index())); |
| 157 | EXPECT_THAT(ref_f.index(), Eq(ref_c.index())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | TEST(StringPoolTest, AddStyles) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 162 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 163 | StringPool::StyleRef ref = pool.MakeRef(StyleString{{"android"}, {Span{{"b"}, 2, 6}}}); |
| 164 | EXPECT_THAT(ref.index(), Eq(0u)); |
| 165 | EXPECT_THAT(ref->value, Eq("android")); |
| 166 | ASSERT_THAT(ref->spans.size(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 167 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | const StringPool::Span& span = ref->spans.front(); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 169 | EXPECT_THAT(*span.name, Eq("b")); |
| 170 | EXPECT_THAT(span.first_char, Eq(2u)); |
| 171 | EXPECT_THAT(span.last_char, Eq(6u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TEST(StringPoolTest, DoNotDedupeStyleWithSameStringAsNonStyle) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 175 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 176 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | StringPool::Ref ref = pool.MakeRef("android"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 178 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | StyleString str{{"android"}}; |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 180 | StringPool::StyleRef style_ref = pool.MakeRef(StyleString{{"android"}}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 181 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 182 | EXPECT_THAT(ref.index(), Ne(style_ref.index())); |
| 183 | } |
| 184 | |
| 185 | TEST(StringPoolTest, StylesAndStringsAreSeparateAfterSorting) { |
| 186 | StringPool pool; |
| 187 | |
| 188 | StringPool::StyleRef ref_a = pool.MakeRef(StyleString{{"beta"}}); |
| 189 | StringPool::Ref ref_b = pool.MakeRef("alpha"); |
| 190 | StringPool::StyleRef ref_c = pool.MakeRef(StyleString{{"alpha"}}); |
| 191 | |
| 192 | EXPECT_THAT(ref_b.index(), Ne(ref_c.index())); |
| 193 | |
| 194 | pool.Sort(); |
| 195 | |
| 196 | EXPECT_THAT(ref_c.index(), Eq(0u)); |
| 197 | EXPECT_THAT(ref_a.index(), Eq(1u)); |
| 198 | EXPECT_THAT(ref_b.index(), Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 201 | TEST(StringPoolTest, FlattenEmptyStringPoolUtf8) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | using namespace android; // For NO_ERROR on Windows. |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 203 | StdErrDiagnostics diag; |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 204 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | StringPool pool; |
| 206 | BigBuffer buffer(1024); |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 207 | StringPool::FlattenUtf8(&buffer, pool, &diag); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 208 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | std::unique_ptr<uint8_t[]> data = util::Copy(buffer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | ResStringPool test; |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 211 | ASSERT_THAT(test.setTo(data.get(), buffer.size()), Eq(NO_ERROR)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 214 | TEST(StringPoolTest, FlattenOddCharactersUtf16) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | using namespace android; // For NO_ERROR on Windows. |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 216 | StdErrDiagnostics diag; |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 217 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | StringPool pool; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | pool.MakeRef("\u093f"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | BigBuffer buffer(1024); |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 221 | StringPool::FlattenUtf16(&buffer, pool, &diag); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 222 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | std::unique_ptr<uint8_t[]> data = util::Copy(buffer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 224 | ResStringPool test; |
| 225 | ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR); |
| 226 | size_t len = 0; |
| 227 | const char16_t* str = test.stringAt(0, &len); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 228 | EXPECT_THAT(len, Eq(1u)); |
| 229 | EXPECT_THAT(str, Pointee(Eq(u'\u093f'))); |
| 230 | EXPECT_THAT(str[1], Eq(0u)); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | constexpr const char* sLongString = |
| 234 | "バッテリーを長持ちさせるため、バッテリーセーバーは端末のパフォーマンスを抑" |
| 235 | "え、バイブレーション、位置情報サービス、大半のバックグラウンドデータを制限" |
| 236 | "します。メール、SMSや、同期を使 " |
| 237 | "用するその他のアプリは、起動しても更新されないことがあります。バッテリーセ" |
| 238 | "ーバーは端末の充電中は自動的にOFFになります。"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 239 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 240 | TEST(StringPoolTest, Flatten) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 241 | using namespace android; // For NO_ERROR on Windows. |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 242 | StdErrDiagnostics diag; |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 243 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 245 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 246 | StringPool::Ref ref_a = pool.MakeRef("hello"); |
| 247 | StringPool::Ref ref_b = pool.MakeRef("goodbye"); |
| 248 | StringPool::Ref ref_c = pool.MakeRef(sLongString); |
| 249 | StringPool::Ref ref_d = pool.MakeRef(""); |
| 250 | StringPool::StyleRef ref_e = |
| 251 | pool.MakeRef(StyleString{{"style"}, {Span{{"b"}, 0, 1}, Span{{"i"}, 2, 3}}}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 252 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 253 | // Styles are always first. |
| 254 | EXPECT_THAT(ref_e.index(), Eq(0u)); |
| 255 | |
| 256 | EXPECT_THAT(ref_a.index(), Eq(1u)); |
| 257 | EXPECT_THAT(ref_b.index(), Eq(2u)); |
| 258 | EXPECT_THAT(ref_c.index(), Eq(3u)); |
| 259 | EXPECT_THAT(ref_d.index(), Eq(4u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 260 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | BigBuffer buffers[2] = {BigBuffer(1024), BigBuffer(1024)}; |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 262 | StringPool::FlattenUtf8(&buffers[0], pool, &diag); |
| 263 | StringPool::FlattenUtf16(&buffers[1], pool, &diag); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 264 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | // Test both UTF-8 and UTF-16 buffers. |
| 266 | for (const BigBuffer& buffer : buffers) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 267 | std::unique_ptr<uint8_t[]> data = util::Copy(buffer); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 268 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 269 | ResStringPool test; |
| 270 | ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 271 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 272 | EXPECT_THAT(util::GetString(test, 1), Eq("hello")); |
| 273 | EXPECT_THAT(util::GetString16(test, 1), Eq(u"hello")); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 274 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 275 | EXPECT_THAT(util::GetString(test, 2), Eq("goodbye")); |
| 276 | EXPECT_THAT(util::GetString16(test, 2), Eq(u"goodbye")); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 277 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 278 | EXPECT_THAT(util::GetString(test, 3), Eq(sLongString)); |
| 279 | EXPECT_THAT(util::GetString16(test, 3), Eq(util::Utf8ToUtf16(sLongString))); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 280 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 281 | size_t len; |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 282 | EXPECT_TRUE(test.stringAt(4, &len) != nullptr || test.string8At(4, &len) != nullptr); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 283 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 284 | EXPECT_THAT(util::GetString(test, 0), Eq("style")); |
| 285 | EXPECT_THAT(util::GetString16(test, 0), Eq(u"style")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 286 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 287 | const ResStringPool_span* span = test.styleAt(0); |
| 288 | ASSERT_THAT(span, NotNull()); |
| 289 | EXPECT_THAT(util::GetString(test, span->name.index), Eq("b")); |
| 290 | EXPECT_THAT(util::GetString16(test, span->name.index), Eq(u"b")); |
| 291 | EXPECT_THAT(span->firstChar, Eq(0u)); |
| 292 | EXPECT_THAT(span->lastChar, Eq(1u)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | span++; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 294 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 295 | ASSERT_THAT(span->name.index, Ne(ResStringPool_span::END)); |
| 296 | EXPECT_THAT(util::GetString(test, span->name.index), Eq("i")); |
| 297 | EXPECT_THAT(util::GetString16(test, span->name.index), Eq(u"i")); |
| 298 | EXPECT_THAT(span->firstChar, Eq(2u)); |
| 299 | EXPECT_THAT(span->lastChar, Eq(3u)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 300 | span++; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 301 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 302 | EXPECT_THAT(span->name.index, Eq(ResStringPool_span::END)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 303 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Ryan Mitchell | 4353d61b | 2018-09-10 17:09:12 -0700 | [diff] [blame] | 306 | TEST(StringPoolTest, ModifiedUTF8) { |
Ryan Mitchell | d86ea58 | 2018-06-27 11:57:18 -0700 | [diff] [blame] | 307 | using namespace android; // For NO_ERROR on Windows. |
| 308 | StdErrDiagnostics diag; |
| 309 | StringPool pool; |
| 310 | StringPool::Ref ref_a = pool.MakeRef("\xF0\x90\x90\x80"); // 𐐀 (U+10400) |
| 311 | StringPool::Ref ref_b = pool.MakeRef("foo \xF0\x90\x90\xB7 bar"); // 𐐷 (U+10437) |
| 312 | StringPool::Ref ref_c = pool.MakeRef("\xF0\x90\x90\x80\xF0\x90\x90\xB7"); |
| 313 | |
| 314 | BigBuffer buffer(1024); |
| 315 | StringPool::FlattenUtf8(&buffer, pool, &diag); |
| 316 | std::unique_ptr<uint8_t[]> data = util::Copy(buffer); |
| 317 | |
Ryan Mitchell | 4353d61b | 2018-09-10 17:09:12 -0700 | [diff] [blame] | 318 | // Check that the codepoints are encoded using two three-byte surrogate pairs |
Ryan Mitchell | d86ea58 | 2018-06-27 11:57:18 -0700 | [diff] [blame] | 319 | ResStringPool test; |
| 320 | ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR); |
Ryan Mitchell | 4353d61b | 2018-09-10 17:09:12 -0700 | [diff] [blame] | 321 | size_t len; |
| 322 | const char* str = test.string8At(0, &len); |
| 323 | ASSERT_THAT(str, NotNull()); |
| 324 | EXPECT_THAT(std::string(str, len), Eq("\xED\xA0\x81\xED\xB0\x80")); |
| 325 | str = test.string8At(1, &len); |
| 326 | ASSERT_THAT(str, NotNull()); |
| 327 | EXPECT_THAT(std::string(str, len), Eq("foo \xED\xA0\x81\xED\xB0\xB7 bar")); |
| 328 | str = test.string8At(2, &len); |
| 329 | ASSERT_THAT(str, NotNull()); |
| 330 | EXPECT_THAT(std::string(str, len), Eq("\xED\xA0\x81\xED\xB0\x80\xED\xA0\x81\xED\xB0\xB7")); |
| 331 | |
| 332 | // Check that retrieving the strings returns the original UTF-8 character bytes |
| 333 | EXPECT_THAT(util::GetString(test, 0), Eq("\xF0\x90\x90\x80")); |
| 334 | EXPECT_THAT(util::GetString(test, 1), Eq("foo \xF0\x90\x90\xB7 bar")); |
| 335 | EXPECT_THAT(util::GetString(test, 2), Eq("\xF0\x90\x90\x80\xF0\x90\x90\xB7")); |
Ryan Mitchell | d86ea58 | 2018-06-27 11:57:18 -0700 | [diff] [blame] | 336 | } |
Ryan Mitchell | 61ffd40 | 2018-04-16 18:21:14 +0000 | [diff] [blame] | 337 | |
Ryan Mitchell | 70414f2 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 338 | TEST(StringPoolTest, MaxEncodingLength) { |
| 339 | StdErrDiagnostics diag; |
| 340 | using namespace android; // For NO_ERROR on Windows. |
| 341 | ResStringPool test; |
| 342 | |
| 343 | StringPool pool; |
| 344 | pool.MakeRef("aaaaaaaaaa"); |
| 345 | BigBuffer buffers[2] = {BigBuffer(1024), BigBuffer(1024)}; |
| 346 | |
| 347 | // Make sure a UTF-8 string under the maximum length does not produce an error |
| 348 | EXPECT_THAT(StringPool::FlattenUtf8(&buffers[0], pool, &diag), Eq(true)); |
| 349 | std::unique_ptr<uint8_t[]> data = util::Copy(buffers[0]); |
| 350 | test.setTo(data.get(), buffers[0].size()); |
| 351 | EXPECT_THAT(util::GetString(test, 0), Eq("aaaaaaaaaa")); |
| 352 | |
| 353 | // Make sure a UTF-16 string under the maximum length does not produce an error |
| 354 | EXPECT_THAT(StringPool::FlattenUtf16(&buffers[1], pool, &diag), Eq(true)); |
| 355 | data = util::Copy(buffers[1]); |
| 356 | test.setTo(data.get(), buffers[1].size()); |
| 357 | EXPECT_THAT(util::GetString16(test, 0), Eq(u"aaaaaaaaaa")); |
| 358 | |
| 359 | StringPool pool2; |
| 360 | std::string longStr(50000, 'a'); |
| 361 | pool2.MakeRef("this fits1"); |
| 362 | pool2.MakeRef(longStr); |
| 363 | pool2.MakeRef("this fits2"); |
| 364 | BigBuffer buffers2[2] = {BigBuffer(1024), BigBuffer(1024)}; |
| 365 | |
| 366 | // Make sure a string that exceeds the maximum length of UTF-8 produces an |
| 367 | // error and writes a shorter error string instead |
| 368 | EXPECT_THAT(StringPool::FlattenUtf8(&buffers2[0], pool2, &diag), Eq(false)); |
| 369 | data = util::Copy(buffers2[0]); |
| 370 | test.setTo(data.get(), buffers2[0].size()); |
| 371 | EXPECT_THAT(util::GetString(test, 0), "this fits1"); |
| 372 | EXPECT_THAT(util::GetString(test, 1), "STRING_TOO_LARGE"); |
| 373 | EXPECT_THAT(util::GetString(test, 2), "this fits2"); |
| 374 | |
| 375 | // Make sure a string that a string that exceeds the maximum length of UTF-8 |
| 376 | // but not UTF-16 does not error for UTF-16 |
| 377 | StringPool pool3; |
| 378 | std::u16string longStr16(50000, 'a'); |
| 379 | pool3.MakeRef(longStr); |
| 380 | EXPECT_THAT(StringPool::FlattenUtf16(&buffers2[1], pool3, &diag), Eq(true)); |
| 381 | data = util::Copy(buffers2[1]); |
| 382 | test.setTo(data.get(), buffers2[1].size()); |
| 383 | EXPECT_THAT(util::GetString16(test, 0), Eq(longStr16)); |
| 384 | } |
| 385 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 386 | } // namespace aapt |