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