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
19 #include <utils/Log.h>
20 #include <utils/String8.h>
21 #include <utils/String16.h>
22
23 #include <gtest/gtest.h>
24
25 using namespace android;
26
27 class String8Test : public testing::Test {
28 protected:
SetUp()29 virtual void SetUp() {
30 }
31
TearDown()32 virtual void TearDown() {
33 }
34 };
35
TEST_F(String8Test,Cstr)36 TEST_F(String8Test, Cstr) {
37 String8 tmp("Hello, world!");
38
39 EXPECT_STREQ(tmp.string(), "Hello, world!");
40 }
41
TEST_F(String8Test,OperatorPlus)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
TEST_F(String8Test,OperatorPlusEquals)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
TEST_F(String8Test,SetToSizeMaxReturnsNoMemory)77 TEST_F(String8Test, SetToSizeMaxReturnsNoMemory) {
78 const char *in = "some string";
79 EXPECT_EQ(NO_MEMORY, String8("").setTo(in, SIZE_MAX));
80 }
81
82 // http://b/29250543
TEST_F(String8Test,CorrectInvalidSurrogate)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
TEST_F(String8Test,CheckUtf32Conversion)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
TEST_F(String8Test,ValidUtf16Conversion)100 TEST_F(String8Test, ValidUtf16Conversion) {
101 char16_t tmp[] = u"abcdef";
102 String8 valid = String8(String16(tmp));
103 EXPECT_STREQ(valid, "abcdef");
104 }
105
TEST_F(String8Test,append)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);
116 }
117