Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 <utils/String16.h> |
| 18 | #include <utils/String8.h> |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | |
Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 22 | using namespace android; |
Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 23 | |
| 24 | ::testing::AssertionResult Char16_tStringEquals(const char16_t* a, const char16_t* b) { |
| 25 | if (strcmp16(a, b) != 0) { |
| 26 | return ::testing::AssertionFailure() |
| 27 | << "\"" << String8(a).c_str() << "\" not equal to \"" << String8(b).c_str() << "\""; |
| 28 | } |
| 29 | return ::testing::AssertionSuccess(); |
| 30 | } |
| 31 | |
| 32 | #define EXPECT_STR16EQ(a, b) EXPECT_TRUE(Char16_tStringEquals(a, b)) |
| 33 | |
| 34 | TEST(String16Test, FromChar16_t) { |
| 35 | String16 tmp(u"Verify me"); |
| 36 | EXPECT_STR16EQ(u"Verify me", tmp); |
| 37 | } |
| 38 | |
| 39 | TEST(String16Test, FromChar16_tSized) { |
| 40 | String16 tmp(u"Verify me", 7); |
| 41 | EXPECT_STR16EQ(u"Verify ", tmp); |
| 42 | } |
| 43 | |
| 44 | TEST(String16Test, FromChar) { |
| 45 | String16 tmp("Verify me"); |
| 46 | EXPECT_STR16EQ(u"Verify me", tmp); |
| 47 | } |
| 48 | |
| 49 | TEST(String16Test, FromCharSized) { |
| 50 | String16 tmp("Verify me", 7); |
| 51 | EXPECT_STR16EQ(u"Verify ", tmp); |
| 52 | } |
| 53 | |
| 54 | TEST(String16Test, Copy) { |
| 55 | String16 tmp("Verify me"); |
| 56 | String16 another = tmp; |
| 57 | EXPECT_STR16EQ(u"Verify me", tmp); |
| 58 | EXPECT_STR16EQ(u"Verify me", another); |
| 59 | } |
| 60 | |
Jooyung Han | 6f92640 | 2021-06-30 09:04:29 +0900 | [diff] [blame] | 61 | TEST(String16Test, CopyAssign) { |
| 62 | String16 tmp("Verify me"); |
| 63 | String16 another; |
| 64 | another = tmp; |
| 65 | EXPECT_STR16EQ(u"Verify me", tmp); |
| 66 | EXPECT_STR16EQ(u"Verify me", another); |
| 67 | } |
| 68 | |
Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 69 | TEST(String16Test, Move) { |
| 70 | String16 tmp("Verify me"); |
| 71 | String16 another(std::move(tmp)); |
| 72 | EXPECT_STR16EQ(u"Verify me", another); |
| 73 | } |
| 74 | |
Jooyung Han | 98b396e | 2021-06-27 03:30:42 +0900 | [diff] [blame] | 75 | TEST(String16Test, MoveAssign) { |
| 76 | String16 tmp("Verify me"); |
| 77 | String16 another; |
| 78 | another = std::move(tmp); |
| 79 | EXPECT_STR16EQ(u"Verify me", another); |
| 80 | } |
| 81 | |
Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 82 | TEST(String16Test, Size) { |
| 83 | String16 tmp("Verify me"); |
| 84 | EXPECT_EQ(9U, tmp.size()); |
| 85 | } |
| 86 | |
| 87 | TEST(String16Test, setTo) { |
| 88 | String16 tmp("Verify me"); |
| 89 | tmp.setTo(u"New content"); |
| 90 | EXPECT_EQ(11U, tmp.size()); |
| 91 | EXPECT_STR16EQ(u"New content", tmp); |
| 92 | } |
| 93 | |
| 94 | TEST(String16Test, Append) { |
| 95 | String16 tmp("Verify me"); |
| 96 | tmp.append(String16("Hello")); |
| 97 | EXPECT_EQ(14U, tmp.size()); |
| 98 | EXPECT_STR16EQ(u"Verify meHello", tmp); |
| 99 | } |
| 100 | |
| 101 | TEST(String16Test, Insert) { |
| 102 | String16 tmp("Verify me"); |
| 103 | tmp.insert(6, u"Insert"); |
| 104 | EXPECT_EQ(15U, tmp.size()); |
| 105 | EXPECT_STR16EQ(u"VerifyInsert me", tmp); |
| 106 | } |
| 107 | |
Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 108 | TEST(String16Test, ReplaceAll) { |
| 109 | String16 tmp("Verify verify Verify"); |
| 110 | tmp.replaceAll(u'r', u'!'); |
| 111 | EXPECT_STR16EQ(u"Ve!ify ve!ify Ve!ify", tmp); |
| 112 | } |
| 113 | |
| 114 | TEST(String16Test, Compare) { |
| 115 | String16 tmp("Verify me"); |
| 116 | EXPECT_EQ(String16(u"Verify me"), tmp); |
| 117 | } |
| 118 | |
| 119 | TEST(String16Test, StaticString) { |
| 120 | String16 nonStaticString("NonStatic"); |
| 121 | StaticString16 staticString(u"Static"); |
| 122 | |
| 123 | EXPECT_TRUE(staticString.isStaticString()); |
| 124 | EXPECT_FALSE(nonStaticString.isStaticString()); |
| 125 | } |
| 126 | |
| 127 | TEST(String16Test, StaticStringCopy) { |
| 128 | StaticString16 tmp(u"Verify me"); |
| 129 | String16 another = tmp; |
| 130 | EXPECT_STR16EQ(u"Verify me", tmp); |
| 131 | EXPECT_STR16EQ(u"Verify me", another); |
| 132 | EXPECT_TRUE(tmp.isStaticString()); |
| 133 | EXPECT_TRUE(another.isStaticString()); |
| 134 | } |
| 135 | |
| 136 | TEST(String16Test, StaticStringMove) { |
| 137 | StaticString16 tmp(u"Verify me"); |
| 138 | String16 another(std::move(tmp)); |
| 139 | EXPECT_STR16EQ(u"Verify me", another); |
| 140 | EXPECT_TRUE(another.isStaticString()); |
| 141 | } |
| 142 | |
| 143 | TEST(String16Test, StaticStringSize) { |
| 144 | StaticString16 tmp(u"Verify me"); |
| 145 | EXPECT_EQ(9U, tmp.size()); |
| 146 | } |
| 147 | |
| 148 | TEST(String16Test, StaticStringSetTo) { |
| 149 | StaticString16 tmp(u"Verify me"); |
| 150 | tmp.setTo(u"New content"); |
| 151 | EXPECT_EQ(11U, tmp.size()); |
| 152 | EXPECT_STR16EQ(u"New content", tmp); |
| 153 | EXPECT_FALSE(tmp.isStaticString()); |
| 154 | } |
| 155 | |
| 156 | TEST(String16Test, StaticStringAppend) { |
| 157 | StaticString16 tmp(u"Verify me"); |
| 158 | tmp.append(String16("Hello")); |
| 159 | EXPECT_EQ(14U, tmp.size()); |
| 160 | EXPECT_STR16EQ(u"Verify meHello", tmp); |
| 161 | EXPECT_FALSE(tmp.isStaticString()); |
| 162 | } |
| 163 | |
| 164 | TEST(String16Test, StaticStringInsert) { |
| 165 | StaticString16 tmp(u"Verify me"); |
| 166 | tmp.insert(6, u"Insert"); |
| 167 | EXPECT_EQ(15U, tmp.size()); |
| 168 | EXPECT_STR16EQ(u"VerifyInsert me", tmp); |
| 169 | EXPECT_FALSE(tmp.isStaticString()); |
| 170 | } |
| 171 | |
Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 172 | TEST(String16Test, StaticStringReplaceAll) { |
| 173 | StaticString16 tmp(u"Verify verify Verify"); |
| 174 | tmp.replaceAll(u'r', u'!'); |
| 175 | EXPECT_STR16EQ(u"Ve!ify ve!ify Ve!ify", tmp); |
| 176 | EXPECT_FALSE(tmp.isStaticString()); |
| 177 | } |
| 178 | |
| 179 | TEST(String16Test, StaticStringCompare) { |
| 180 | StaticString16 tmp(u"Verify me"); |
| 181 | EXPECT_EQ(String16(u"Verify me"), tmp); |
| 182 | } |
| 183 | |
| 184 | TEST(String16Test, StringSetToStaticString) { |
| 185 | StaticString16 tmp(u"Verify me"); |
| 186 | String16 another(u"nonstatic"); |
| 187 | another = tmp; |
| 188 | EXPECT_STR16EQ(u"Verify me", tmp); |
| 189 | EXPECT_STR16EQ(u"Verify me", another); |
| 190 | } |
| 191 | |
Jooyung Han | 6f92640 | 2021-06-30 09:04:29 +0900 | [diff] [blame] | 192 | TEST(String16Test, StringCopyAssignFromStaticString) { |
| 193 | StaticString16 tmp(u"Verify me"); |
| 194 | String16 another(u"nonstatic"); |
| 195 | another = tmp; |
| 196 | EXPECT_STR16EQ(u"Verify me", another); |
| 197 | EXPECT_TRUE(another.isStaticString()); |
| 198 | EXPECT_STR16EQ(u"Verify me", tmp); |
| 199 | EXPECT_TRUE(tmp.isStaticString()); |
| 200 | } |
| 201 | |
Jooyung Han | 98b396e | 2021-06-27 03:30:42 +0900 | [diff] [blame] | 202 | TEST(String16Test, StringMoveAssignFromStaticString) { |
Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 203 | StaticString16 tmp(u"Verify me"); |
Jooyung Han | 98b396e | 2021-06-27 03:30:42 +0900 | [diff] [blame] | 204 | String16 another(u"nonstatic"); |
| 205 | another = std::move(tmp); |
Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 206 | EXPECT_STR16EQ(u"Verify me", another); |
Jooyung Han | 98b396e | 2021-06-27 03:30:42 +0900 | [diff] [blame] | 207 | EXPECT_TRUE(another.isStaticString()); |
Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | TEST(String16Test, EmptyStringIsStatic) { |
| 211 | String16 tmp(""); |
| 212 | EXPECT_TRUE(tmp.isStaticString()); |
| 213 | } |
| 214 | |
Devin Moore | 6bcbeea | 2020-10-21 17:25:42 -0700 | [diff] [blame] | 215 | TEST(String16Test, OverreadUtf8Conversion) { |
| 216 | char tmp[] = {'a', static_cast<char>(0xe0), '\0'}; |
| 217 | String16 another(tmp); |
| 218 | EXPECT_TRUE(another.size() == 0); |
| 219 | } |
| 220 | |
| 221 | TEST(String16Test, ValidUtf8Conversion) { |
| 222 | String16 another("abcdef"); |
| 223 | EXPECT_EQ(6U, another.size()); |
| 224 | EXPECT_STR16EQ(another, u"abcdef"); |
| 225 | } |
| 226 | |
Elliott Hughes | a6be6f0 | 2021-06-10 17:06:26 -0700 | [diff] [blame] | 227 | TEST(String16Test, append) { |
| 228 | String16 s; |
| 229 | EXPECT_EQ(OK, s.append(String16(u"foo"))); |
| 230 | EXPECT_STR16EQ(u"foo", s); |
| 231 | EXPECT_EQ(OK, s.append(String16(u"bar"))); |
| 232 | EXPECT_STR16EQ(u"foobar", s); |
| 233 | EXPECT_EQ(OK, s.append(u"baz", 0)); |
| 234 | EXPECT_STR16EQ(u"foobar", s); |
| 235 | EXPECT_EQ(NO_MEMORY, s.append(u"baz", SIZE_MAX)); |
| 236 | EXPECT_STR16EQ(u"foobar", s); |
| 237 | } |
| 238 | |
| 239 | TEST(String16Test, insert) { |
| 240 | String16 s; |
| 241 | |
| 242 | // Inserting into the empty string inserts at the start. |
| 243 | EXPECT_EQ(OK, s.insert(123, u"foo")); |
| 244 | EXPECT_STR16EQ(u"foo", s); |
| 245 | |
| 246 | // Inserting zero characters at any position is okay, but won't expand the string. |
| 247 | EXPECT_EQ(OK, s.insert(123, u"foo", 0)); |
| 248 | EXPECT_STR16EQ(u"foo", s); |
| 249 | |
| 250 | // Inserting past the end of a non-empty string appends. |
| 251 | EXPECT_EQ(OK, s.insert(123, u"bar")); |
| 252 | EXPECT_STR16EQ(u"foobar", s); |
| 253 | |
| 254 | EXPECT_EQ(OK, s.insert(3, u"!")); |
| 255 | EXPECT_STR16EQ(u"foo!bar", s); |
| 256 | |
| 257 | EXPECT_EQ(NO_MEMORY, s.insert(3, u"", SIZE_MAX)); |
| 258 | EXPECT_STR16EQ(u"foo!bar", s); |
| 259 | } |