blob: 35fd512f442de37e15bd9d416778e24b6cf240f1 [file] [log] [blame]
Kenny Root23b4a092010-08-05 16:21:23 -07001/*
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 Hughes59682762021-06-10 16:42:20 -070018
Kenny Root23b4a092010-08-05 16:21:23 -070019#include <utils/Log.h>
20#include <utils/String8.h>
Sergio Giroc4966a32016-06-28 18:02:29 +010021#include <utils/String16.h>
Kenny Root23b4a092010-08-05 16:21:23 -070022
23#include <gtest/gtest.h>
24
Elliott Hughes59682762021-06-10 16:42:20 -070025using namespace android;
Kenny Root23b4a092010-08-05 16:21:23 -070026
27class String8Test : public testing::Test {
28protected:
29 virtual void SetUp() {
30 }
31
32 virtual void TearDown() {
33 }
34};
35
36TEST_F(String8Test, Cstr) {
37 String8 tmp("Hello, world!");
38
39 EXPECT_STREQ(tmp.string(), "Hello, world!");
40}
41
42TEST_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
60TEST_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 Girodf5151d2015-09-07 16:21:12 +010077TEST_F(String8Test, SetToSizeMaxReturnsNoMemory) {
78 const char *in = "some string";
79 EXPECT_EQ(NO_MEMORY, String8("").setTo(in, SIZE_MAX));
80}
81
Sergio Giroc4966a32016-06-28 18:02:29 +010082// http://b/29250543
83TEST_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
92TEST_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 Moore6bcbeea2020-10-21 17:25:42 -0700100TEST_F(String8Test, ValidUtf16Conversion) {
101 char16_t tmp[] = u"abcdef";
102 String8 valid = String8(String16(tmp));
103 EXPECT_STREQ(valid, "abcdef");
104}
Elliott Hughes59682762021-06-10 16:42:20 -0700105
106TEST_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 Root23b4a092010-08-05 16:21:23 -0700116}
Eric Miaoc6ce48e2023-07-14 16:35:09 -0700117
118TEST_F(String8Test, removeAll) {
119 String8 s("Hello, world!");
120
121 // NULL input should cause an assertion failure and error message in logcat
122 EXPECT_DEATH(s.removeAll(NULL), "");
123
124 // expect to return true and string content should remain unchanged
125 EXPECT_TRUE(s.removeAll(""));
126 EXPECT_STREQ("Hello, world!", s);
127
128 // expect to return false
129 EXPECT_FALSE(s.removeAll("x"));
130 EXPECT_STREQ("Hello, world!", s);
131
132 EXPECT_TRUE(s.removeAll("o"));
133 EXPECT_STREQ("Hell, wrld!", s);
134}