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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "test/Test.h" |
| 24 | #include "util/Util.h" |
| 25 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 26 | using ::android::StringPiece; |
| 27 | using ::android::StringPiece16; |
| 28 | using ::testing::Eq; |
| 29 | using ::testing::Ne; |
| 30 | using ::testing::NotNull; |
| 31 | using ::testing::Pointee; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 32 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | namespace aapt { |
| 34 | |
| 35 | TEST(StringPoolTest, InsertOneString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | StringPool::Ref ref = pool.MakeRef("wut"); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 39 | EXPECT_THAT(*ref, Eq("wut")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST(StringPoolTest, InsertTwoUniqueStrings) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 45 | StringPool::Ref ref_a = pool.MakeRef("wut"); |
| 46 | StringPool::Ref ref_b = pool.MakeRef("hey"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 48 | EXPECT_THAT(*ref_a, Eq("wut")); |
| 49 | EXPECT_THAT(*ref_b, Eq("hey")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(StringPoolTest, DoNotInsertNewDuplicateString) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 55 | StringPool::Ref ref_a = pool.MakeRef("wut"); |
| 56 | StringPool::Ref ref_b = pool.MakeRef("wut"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 58 | EXPECT_THAT(*ref_a, Eq("wut")); |
| 59 | EXPECT_THAT(*ref_b, Eq("wut")); |
| 60 | EXPECT_THAT(pool.size(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST(StringPoolTest, MaintainInsertionOrderIndex) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 65 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 66 | StringPool::Ref ref_a = pool.MakeRef("z"); |
| 67 | StringPool::Ref ref_b = pool.MakeRef("a"); |
| 68 | StringPool::Ref ref_c = pool.MakeRef("m"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 69 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 70 | EXPECT_THAT(ref_a.index(), Eq(0u)); |
| 71 | EXPECT_THAT(ref_b.index(), Eq(1u)); |
| 72 | EXPECT_THAT(ref_c.index(), Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST(StringPoolTest, PruneStringsWithNoReferences) { |
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("foo"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 80 | { |
| 81 | StringPool::Ref ref_b = pool.MakeRef("wut"); |
| 82 | EXPECT_THAT(*ref_b, Eq("wut")); |
| 83 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 84 | pool.Prune(); |
| 85 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 86 | } |
| 87 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 88 | |
| 89 | { |
| 90 | StringPool::Ref ref_c = pool.MakeRef("bar"); |
| 91 | EXPECT_THAT(pool.size(), Eq(3u)); |
| 92 | |
| 93 | pool.Prune(); |
| 94 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 95 | } |
| 96 | EXPECT_THAT(pool.size(), Eq(2u)); |
| 97 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | pool.Prune(); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 99 | EXPECT_THAT(pool.size(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 102 | TEST(StringPoolTest, SortAndMaintainIndexesInStringReferences) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 104 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 105 | StringPool::Ref ref_a = pool.MakeRef("z"); |
| 106 | StringPool::Ref ref_b = pool.MakeRef("a"); |
| 107 | StringPool::Ref ref_c = pool.MakeRef("m"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 109 | EXPECT_THAT(*ref_a, Eq("z")); |
| 110 | EXPECT_THAT(ref_a.index(), Eq(0u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 112 | EXPECT_THAT(*ref_b, Eq("a")); |
| 113 | EXPECT_THAT(ref_b.index(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 115 | EXPECT_THAT(*ref_c, Eq("m")); |
| 116 | EXPECT_THAT(ref_c.index(), Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 117 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 118 | pool.Sort(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 119 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 120 | EXPECT_THAT(*ref_a, Eq("z")); |
| 121 | EXPECT_THAT(ref_a.index(), Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 123 | EXPECT_THAT(*ref_b, Eq("a")); |
| 124 | EXPECT_THAT(ref_b.index(), Eq(0u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 125 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 126 | EXPECT_THAT(*ref_c, Eq("m")); |
| 127 | EXPECT_THAT(ref_c.index(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST(StringPoolTest, SortAndStillDedupe) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 132 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 133 | StringPool::Ref ref_a = pool.MakeRef("z"); |
| 134 | StringPool::Ref ref_b = pool.MakeRef("a"); |
| 135 | StringPool::Ref ref_c = pool.MakeRef("m"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 136 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 137 | pool.Sort(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 139 | StringPool::Ref ref_d = pool.MakeRef("z"); |
| 140 | StringPool::Ref ref_e = pool.MakeRef("a"); |
| 141 | StringPool::Ref ref_f = pool.MakeRef("m"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 142 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 143 | EXPECT_THAT(ref_d.index(), Eq(ref_a.index())); |
| 144 | EXPECT_THAT(ref_e.index(), Eq(ref_b.index())); |
| 145 | EXPECT_THAT(ref_f.index(), Eq(ref_c.index())); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST(StringPoolTest, AddStyles) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | StringPool pool; |
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::StyleRef ref = pool.MakeRef(StyleString{{"android"}, {Span{{"b"}, 2, 6}}}); |
| 152 | EXPECT_THAT(ref.index(), Eq(0u)); |
| 153 | EXPECT_THAT(ref->value, Eq("android")); |
| 154 | ASSERT_THAT(ref->spans.size(), Eq(1u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 155 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | const StringPool::Span& span = ref->spans.front(); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 157 | EXPECT_THAT(*span.name, Eq("b")); |
| 158 | EXPECT_THAT(span.first_char, Eq(2u)); |
| 159 | EXPECT_THAT(span.last_char, Eq(6u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TEST(StringPoolTest, DoNotDedupeStyleWithSameStringAsNonStyle) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | StringPool::Ref ref = pool.MakeRef("android"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 166 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | StyleString str{{"android"}}; |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 168 | StringPool::StyleRef style_ref = pool.MakeRef(StyleString{{"android"}}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 169 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 170 | EXPECT_THAT(ref.index(), Ne(style_ref.index())); |
| 171 | } |
| 172 | |
| 173 | TEST(StringPoolTest, StylesAndStringsAreSeparateAfterSorting) { |
| 174 | StringPool pool; |
| 175 | |
| 176 | StringPool::StyleRef ref_a = pool.MakeRef(StyleString{{"beta"}}); |
| 177 | StringPool::Ref ref_b = pool.MakeRef("alpha"); |
| 178 | StringPool::StyleRef ref_c = pool.MakeRef(StyleString{{"alpha"}}); |
| 179 | |
| 180 | EXPECT_THAT(ref_b.index(), Ne(ref_c.index())); |
| 181 | |
| 182 | pool.Sort(); |
| 183 | |
| 184 | EXPECT_THAT(ref_c.index(), Eq(0u)); |
| 185 | EXPECT_THAT(ref_a.index(), Eq(1u)); |
| 186 | EXPECT_THAT(ref_b.index(), Eq(2u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 189 | TEST(StringPoolTest, FlattenEmptyStringPoolUtf8) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | using namespace android; // For NO_ERROR on Windows. |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 191 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | StringPool pool; |
| 193 | BigBuffer buffer(1024); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 194 | StringPool::FlattenUtf8(&buffer, pool); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 195 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | std::unique_ptr<uint8_t[]> data = util::Copy(buffer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | ResStringPool test; |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 198 | ASSERT_THAT(test.setTo(data.get(), buffer.size()), Eq(NO_ERROR)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 201 | TEST(StringPoolTest, FlattenOddCharactersUtf16) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | using namespace android; // For NO_ERROR on Windows. |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 203 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | StringPool pool; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | pool.MakeRef("\u093f"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | BigBuffer buffer(1024); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | StringPool::FlattenUtf16(&buffer, pool); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [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; |
| 211 | ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR); |
| 212 | size_t len = 0; |
| 213 | const char16_t* str = test.stringAt(0, &len); |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 214 | EXPECT_THAT(len, Eq(1u)); |
| 215 | EXPECT_THAT(str, Pointee(Eq(u'\u093f'))); |
| 216 | EXPECT_THAT(str[1], Eq(0u)); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 219 | constexpr const char* sLongString = |
| 220 | "バッテリーを長持ちさせるため、バッテリーセーバーは端末のパフォーマンスを抑" |
| 221 | "え、バイブレーション、位置情報サービス、大半のバックグラウンドデータを制限" |
| 222 | "します。メール、SMSや、同期を使 " |
| 223 | "用するその他のアプリは、起動しても更新されないことがあります。バッテリーセ" |
| 224 | "ーバーは端末の充電中は自動的にOFFになります。"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 225 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 226 | TEST(StringPoolTest, Flatten) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | using namespace android; // For NO_ERROR on Windows. |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 228 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | StringPool pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 230 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 231 | StringPool::Ref ref_a = pool.MakeRef("hello"); |
| 232 | StringPool::Ref ref_b = pool.MakeRef("goodbye"); |
| 233 | StringPool::Ref ref_c = pool.MakeRef(sLongString); |
| 234 | StringPool::Ref ref_d = pool.MakeRef(""); |
| 235 | StringPool::StyleRef ref_e = |
| 236 | pool.MakeRef(StyleString{{"style"}, {Span{{"b"}, 0, 1}, Span{{"i"}, 2, 3}}}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 237 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 238 | // Styles are always first. |
| 239 | EXPECT_THAT(ref_e.index(), Eq(0u)); |
| 240 | |
| 241 | EXPECT_THAT(ref_a.index(), Eq(1u)); |
| 242 | EXPECT_THAT(ref_b.index(), Eq(2u)); |
| 243 | EXPECT_THAT(ref_c.index(), Eq(3u)); |
| 244 | EXPECT_THAT(ref_d.index(), Eq(4u)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 245 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 246 | BigBuffer buffers[2] = {BigBuffer(1024), BigBuffer(1024)}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | StringPool::FlattenUtf8(&buffers[0], pool); |
| 248 | StringPool::FlattenUtf16(&buffers[1], pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 249 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | // Test both UTF-8 and UTF-16 buffers. |
| 251 | for (const BigBuffer& buffer : buffers) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | std::unique_ptr<uint8_t[]> data = util::Copy(buffer); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 253 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 254 | ResStringPool test; |
| 255 | ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 256 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 257 | EXPECT_THAT(util::GetString(test, 1), Eq("hello")); |
| 258 | EXPECT_THAT(util::GetString16(test, 1), Eq(u"hello")); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 259 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 260 | EXPECT_THAT(util::GetString(test, 2), Eq("goodbye")); |
| 261 | EXPECT_THAT(util::GetString16(test, 2), Eq(u"goodbye")); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 262 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 263 | EXPECT_THAT(util::GetString(test, 3), Eq(sLongString)); |
| 264 | EXPECT_THAT(util::GetString16(test, 3), Eq(util::Utf8ToUtf16(sLongString))); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 265 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | size_t len; |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 267 | EXPECT_TRUE(test.stringAt(4, &len) != nullptr || test.string8At(4, &len) != nullptr); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 268 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 269 | EXPECT_THAT(util::GetString(test, 0), Eq("style")); |
| 270 | EXPECT_THAT(util::GetString16(test, 0), Eq(u"style")); |
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 | const ResStringPool_span* span = test.styleAt(0); |
| 273 | ASSERT_THAT(span, NotNull()); |
| 274 | EXPECT_THAT(util::GetString(test, span->name.index), Eq("b")); |
| 275 | EXPECT_THAT(util::GetString16(test, span->name.index), Eq(u"b")); |
| 276 | EXPECT_THAT(span->firstChar, Eq(0u)); |
| 277 | EXPECT_THAT(span->lastChar, Eq(1u)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 278 | span++; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 279 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 280 | ASSERT_THAT(span->name.index, Ne(ResStringPool_span::END)); |
| 281 | EXPECT_THAT(util::GetString(test, span->name.index), Eq("i")); |
| 282 | EXPECT_THAT(util::GetString16(test, span->name.index), Eq(u"i")); |
| 283 | EXPECT_THAT(span->firstChar, Eq(2u)); |
| 284 | EXPECT_THAT(span->lastChar, Eq(3u)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 285 | span++; |
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 | EXPECT_THAT(span->name.index, Eq(ResStringPool_span::END)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 288 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | } // namespace aapt |