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