blob: e1fd13a9e1374eef638d91ef48f8a1a4b6d37a3d [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
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +000039 EXPECT_STREQ(tmp.c_str(), "Hello, world!");
Kenny Root23b4a092010-08-05 16:21:23 -070040}
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;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +000048 EXPECT_STREQ(dst1.c_str(), "Hello, world!");
49 EXPECT_STREQ(src1.c_str(), "Hello, ");
Kenny Root23b4a092010-08-05 16:21:23 -070050 EXPECT_STREQ(ccsrc2, "world!");
51
52 // Test adding String8 + String8
53 String8 ssrc2("world!");
54 String8 dst2 = src1 + ssrc2;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +000055 EXPECT_STREQ(dst2.c_str(), "Hello, world!");
56 EXPECT_STREQ(src1.c_str(), "Hello, ");
57 EXPECT_STREQ(ssrc2.c_str(), "world!");
Kenny Root23b4a092010-08-05 16:21:23 -070058}
59
60TEST_F(String8Test, OperatorPlusEquals) {
61 String8 src1("My voice");
62
63 // Testing String8 += String8
64 String8 src2(" is my passport.");
65 src1 += src2;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +000066 EXPECT_STREQ(src1.c_str(), "My voice is my passport.");
67 EXPECT_STREQ(src2.c_str(), " is my passport.");
Kenny Root23b4a092010-08-05 16:21:23 -070068
69 // Adding const char* to the previous string.
70 const char* src3 = " Verify me.";
71 src1 += src3;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +000072 EXPECT_STREQ(src1.c_str(), "My voice is my passport. Verify me.");
73 EXPECT_STREQ(src2.c_str(), " is my passport.");
Kenny Root23b4a092010-08-05 16:21:23 -070074 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));
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000103 EXPECT_STREQ(valid.c_str(), "abcdef");
Devin Moore6bcbeea2020-10-21 17:25:42 -0700104}
Elliott Hughes59682762021-06-10 16:42:20 -0700105
106TEST_F(String8Test, append) {
107 String8 s;
108 EXPECT_EQ(OK, s.append("foo"));
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000109 EXPECT_STREQ("foo", s.c_str());
Elliott Hughes59682762021-06-10 16:42:20 -0700110 EXPECT_EQ(OK, s.append("bar"));
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000111 EXPECT_STREQ("foobar", s.c_str());
Elliott Hughes59682762021-06-10 16:42:20 -0700112 EXPECT_EQ(OK, s.append("baz", 0));
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000113 EXPECT_STREQ("foobar", s.c_str());
Elliott Hughes59682762021-06-10 16:42:20 -0700114 EXPECT_EQ(NO_MEMORY, s.append("baz", SIZE_MAX));
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000115 EXPECT_STREQ("foobar", s.c_str());
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(""));
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000126 EXPECT_STREQ("Hello, world!", s.c_str());
Eric Miaoc6ce48e2023-07-14 16:35:09 -0700127
128 // expect to return false
129 EXPECT_FALSE(s.removeAll("x"));
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000130 EXPECT_STREQ("Hello, world!", s.c_str());
Eric Miaoc6ce48e2023-07-14 16:35:09 -0700131
132 EXPECT_TRUE(s.removeAll("o"));
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000133 EXPECT_STREQ("Hell, wrld!", s.c_str());
Eric Miaoc6ce48e2023-07-14 16:35:09 -0700134}