| Kenny Root | 23b4a09 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2010 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 | #define LOG_TAG "String8_test" | 
| Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 18 |  | 
| Kenny Root | 23b4a09 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> | 
|  | 20 | #include <utils/String8.h> | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 21 | #include <utils/String16.h> | 
| Kenny Root | 23b4a09 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <gtest/gtest.h> | 
|  | 24 |  | 
| Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 25 | using namespace android; | 
| Kenny Root | 23b4a09 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | class String8Test : public testing::Test { | 
|  | 28 | protected: | 
|  | 29 | virtual void SetUp() { | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | virtual void TearDown() { | 
|  | 33 | } | 
|  | 34 | }; | 
|  | 35 |  | 
|  | 36 | TEST_F(String8Test, Cstr) { | 
|  | 37 | String8 tmp("Hello, world!"); | 
|  | 38 |  | 
|  | 39 | EXPECT_STREQ(tmp.string(), "Hello, world!"); | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | TEST_F(String8Test, OperatorPlus) { | 
|  | 43 | String8 src1("Hello, "); | 
|  | 44 |  | 
|  | 45 | // Test adding String8 + const char* | 
|  | 46 | const char* ccsrc2 = "world!"; | 
|  | 47 | String8 dst1 = src1 + ccsrc2; | 
|  | 48 | EXPECT_STREQ(dst1.string(), "Hello, world!"); | 
|  | 49 | EXPECT_STREQ(src1.string(), "Hello, "); | 
|  | 50 | EXPECT_STREQ(ccsrc2, "world!"); | 
|  | 51 |  | 
|  | 52 | // Test adding String8 + String8 | 
|  | 53 | String8 ssrc2("world!"); | 
|  | 54 | String8 dst2 = src1 + ssrc2; | 
|  | 55 | EXPECT_STREQ(dst2.string(), "Hello, world!"); | 
|  | 56 | EXPECT_STREQ(src1.string(), "Hello, "); | 
|  | 57 | EXPECT_STREQ(ssrc2.string(), "world!"); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | TEST_F(String8Test, OperatorPlusEquals) { | 
|  | 61 | String8 src1("My voice"); | 
|  | 62 |  | 
|  | 63 | // Testing String8 += String8 | 
|  | 64 | String8 src2(" is my passport."); | 
|  | 65 | src1 += src2; | 
|  | 66 | EXPECT_STREQ(src1.string(), "My voice is my passport."); | 
|  | 67 | EXPECT_STREQ(src2.string(), " is my passport."); | 
|  | 68 |  | 
|  | 69 | // Adding const char* to the previous string. | 
|  | 70 | const char* src3 = " Verify me."; | 
|  | 71 | src1 += src3; | 
|  | 72 | EXPECT_STREQ(src1.string(), "My voice is my passport. Verify me."); | 
|  | 73 | EXPECT_STREQ(src2.string(), " is my passport."); | 
|  | 74 | EXPECT_STREQ(src3, " Verify me."); | 
|  | 75 | } | 
|  | 76 |  | 
| Sergio Giro | df5151d | 2015-09-07 16:21:12 +0100 | [diff] [blame] | 77 | TEST_F(String8Test, SetToSizeMaxReturnsNoMemory) { | 
|  | 78 | const char *in = "some string"; | 
|  | 79 | EXPECT_EQ(NO_MEMORY, String8("").setTo(in, SIZE_MAX)); | 
|  | 80 | } | 
|  | 81 |  | 
| Sergio Giro | c4966a3 | 2016-06-28 18:02:29 +0100 | [diff] [blame] | 82 | // http://b/29250543 | 
|  | 83 | TEST_F(String8Test, CorrectInvalidSurrogate) { | 
|  | 84 | // d841d8 is an invalid start for a surrogate pair. Make sure this is handled by ignoring the | 
|  | 85 | // first character in the pair and handling the rest correctly. | 
|  | 86 | String16 string16(u"\xd841\xd841\xdc41\x0000"); | 
|  | 87 | String8 string8(string16); | 
|  | 88 |  | 
|  | 89 | EXPECT_EQ(4U, string8.length()); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | TEST_F(String8Test, CheckUtf32Conversion) { | 
|  | 93 | // Since bound checks were added, check the conversion can be done without fatal errors. | 
|  | 94 | // The utf8 lengths of these are chars are 1 + 2 + 3 + 4 = 10. | 
|  | 95 | const char32_t string32[] = U"\x0000007f\x000007ff\x0000911\x0010fffe"; | 
|  | 96 | String8 string8(string32); | 
|  | 97 | EXPECT_EQ(10U, string8.length()); | 
|  | 98 | } | 
|  | 99 |  | 
| Devin Moore | 6bcbeea | 2020-10-21 17:25:42 -0700 | [diff] [blame] | 100 | TEST_F(String8Test, ValidUtf16Conversion) { | 
|  | 101 | char16_t tmp[] = u"abcdef"; | 
|  | 102 | String8 valid = String8(String16(tmp)); | 
|  | 103 | EXPECT_STREQ(valid, "abcdef"); | 
|  | 104 | } | 
| Elliott Hughes | 5968276 | 2021-06-10 16:42:20 -0700 | [diff] [blame] | 105 |  | 
|  | 106 | TEST_F(String8Test, append) { | 
|  | 107 | String8 s; | 
|  | 108 | EXPECT_EQ(OK, s.append("foo")); | 
|  | 109 | EXPECT_STREQ("foo", s); | 
|  | 110 | EXPECT_EQ(OK, s.append("bar")); | 
|  | 111 | EXPECT_STREQ("foobar", s); | 
|  | 112 | EXPECT_EQ(OK, s.append("baz", 0)); | 
|  | 113 | EXPECT_STREQ("foobar", s); | 
|  | 114 | EXPECT_EQ(NO_MEMORY, s.append("baz", SIZE_MAX)); | 
|  | 115 | EXPECT_STREQ("foobar", s); | 
| Kenny Root | 23b4a09 | 2010-08-05 16:21:23 -0700 | [diff] [blame] | 116 | } |