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 <log/log.h>
20 #include <utils/String16.h>
21 #include <utils/String8.h>
22 #include <compare>
23 #include <utility>
24
25 #include <gtest/gtest.h>
26
27 using namespace android;
28
29 class String8Test : public testing::Test {
30 protected:
SetUp()31 virtual void SetUp() {
32 }
33
TearDown()34 virtual void TearDown() {
35 }
36 };
37
TEST_F(String8Test,Cstr)38 TEST_F(String8Test, Cstr) {
39 String8 tmp("Hello, world!");
40
41 EXPECT_STREQ(tmp.c_str(), "Hello, world!");
42 }
43
TEST_F(String8Test,OperatorPlus)44 TEST_F(String8Test, OperatorPlus) {
45 String8 src1("Hello, ");
46
47 // Test adding String8 + const char*
48 const char* ccsrc2 = "world!";
49 String8 dst1 = src1 + ccsrc2;
50 EXPECT_STREQ(dst1.c_str(), "Hello, world!");
51 EXPECT_STREQ(src1.c_str(), "Hello, ");
52 EXPECT_STREQ(ccsrc2, "world!");
53
54 // Test adding String8 + String8
55 String8 ssrc2("world!");
56 String8 dst2 = src1 + ssrc2;
57 EXPECT_STREQ(dst2.c_str(), "Hello, world!");
58 EXPECT_STREQ(src1.c_str(), "Hello, ");
59 EXPECT_STREQ(ssrc2.c_str(), "world!");
60 }
61
TEST_F(String8Test,OperatorPlusEquals)62 TEST_F(String8Test, OperatorPlusEquals) {
63 String8 src1("My voice");
64
65 // Testing String8 += String8
66 String8 src2(" is my passport.");
67 src1 += src2;
68 EXPECT_STREQ(src1.c_str(), "My voice is my passport.");
69 EXPECT_STREQ(src2.c_str(), " is my passport.");
70
71 // Adding const char* to the previous string.
72 const char* src3 = " Verify me.";
73 src1 += src3;
74 EXPECT_STREQ(src1.c_str(), "My voice is my passport. Verify me.");
75 EXPECT_STREQ(src2.c_str(), " is my passport.");
76 EXPECT_STREQ(src3, " Verify me.");
77 }
78
TEST_F(String8Test,SetToSizeMaxReturnsNoMemory)79 TEST_F(String8Test, SetToSizeMaxReturnsNoMemory) {
80 const char *in = "some string";
81 EXPECT_EQ(NO_MEMORY, String8("").setTo(in, SIZE_MAX));
82 }
83
84 // http://b/29250543
TEST_F(String8Test,CorrectInvalidSurrogate)85 TEST_F(String8Test, CorrectInvalidSurrogate) {
86 // d841d8 is an invalid start for a surrogate pair. Make sure this is handled by ignoring the
87 // first character in the pair and handling the rest correctly.
88 String16 string16(u"\xd841\xd841\xdc41\x0000");
89 String8 string8(string16);
90
91 EXPECT_EQ(4U, string8.length());
92 }
93
TEST_F(String8Test,CheckUtf32Conversion)94 TEST_F(String8Test, CheckUtf32Conversion) {
95 // Since bound checks were added, check the conversion can be done without fatal errors.
96 // The utf8 lengths of these are chars are 1 + 2 + 3 + 4 = 10.
97 const char32_t string32[] = U"\x0000007f\x000007ff\x0000911\x0010fffe";
98 String8 string8(string32);
99 EXPECT_EQ(10U, string8.length());
100 }
101
TEST_F(String8Test,ValidUtf16Conversion)102 TEST_F(String8Test, ValidUtf16Conversion) {
103 char16_t tmp[] = u"abcdef";
104 String8 valid = String8(String16(tmp));
105 EXPECT_STREQ(valid.c_str(), "abcdef");
106 }
107
TEST_F(String8Test,append)108 TEST_F(String8Test, append) {
109 String8 s;
110 EXPECT_EQ(OK, s.append("foo"));
111 EXPECT_STREQ("foo", s.c_str());
112 EXPECT_EQ(OK, s.append("bar"));
113 EXPECT_STREQ("foobar", s.c_str());
114 EXPECT_EQ(OK, s.append("baz", 0));
115 EXPECT_STREQ("foobar", s.c_str());
116 EXPECT_EQ(NO_MEMORY, s.append("baz", SIZE_MAX));
117 EXPECT_STREQ("foobar", s.c_str());
118 }
119
TEST_F(String8Test,removeAll)120 TEST_F(String8Test, removeAll) {
121 String8 s("Hello, world!");
122
123 // NULL input should cause an assertion failure and error message in logcat
124 EXPECT_DEATH(s.removeAll(NULL), "");
125
126 // expect to return true and string content should remain unchanged
127 EXPECT_TRUE(s.removeAll(""));
128 EXPECT_STREQ("Hello, world!", s.c_str());
129
130 // expect to return false
131 EXPECT_FALSE(s.removeAll("x"));
132 EXPECT_STREQ("Hello, world!", s.c_str());
133
134 EXPECT_TRUE(s.removeAll("o"));
135 EXPECT_STREQ("Hell, wrld!", s.c_str());
136 }
137
TEST_F(String8Test,comparisons)138 TEST_F(String8Test, comparisons) {
139 const char* cstr1 = "abc";
140 const char* cstr2 = "def";
141
142 // str1 and str1b will point to different blocks of memory but with equal contents.
143 String8 str1(cstr1);
144 String8 str1b(cstr1);
145 String8 str2(cstr2);
146
147 EXPECT_TRUE((str1 <=> str1b) == 0);
148 EXPECT_FALSE(str1 != str1b);
149 EXPECT_FALSE(str1 < str1b);
150 EXPECT_TRUE(str1 <= str1b);
151 EXPECT_TRUE(str1 == str1b);
152 EXPECT_TRUE(str1 >= str1b);
153 EXPECT_FALSE(str1 > str1b);
154
155 EXPECT_TRUE((str1 <=> str2) < 0);
156 EXPECT_TRUE((str2 <=> str1) > 0);
157 EXPECT_TRUE(str1 != str2);
158 EXPECT_TRUE(str1 < str2);
159 EXPECT_TRUE(str1 <= str2);
160 EXPECT_FALSE(str1 == str2);
161 EXPECT_FALSE(str1 >= str2);
162 EXPECT_FALSE(str1 > str2);
163
164 // Verify that pre-C++20 comparison operators work with a std::pair of a String8, which only
165 // provides <=> in C++20 and up. See b/339775405.
166
167 std::pair<String8, int> pair1(str1, 13);
168 std::pair<String8, int> pair1b(str1b, 13);
169 std::pair<String8, int> pair2(str2, 13);
170
171 EXPECT_TRUE(pair1 == pair1b);
172 EXPECT_FALSE(pair1 < pair1b);
173 EXPECT_FALSE(pair1 > pair1b);
174
175 EXPECT_TRUE(pair1 != pair2);
176 EXPECT_TRUE(pair1 < pair2);
177 EXPECT_FALSE(pair1 > pair2);
178 }
179