1d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner// Copyright 2014 The Android Open Source Project
2d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner//
3d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner// This software is licensed under the terms of the GNU General Public
4d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner// License version 2, as published by the Free Software Foundation, and
5d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner// may be copied, distributed, and modified under those terms.
6d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner//
7d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner// This program is distributed in the hope that it will be useful,
8d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner// but WITHOUT ANY WARRANTY; without even the implied warranty of
9d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner// GNU General Public License for more details.
11d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
12d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner#include "android/base/String.h"
13d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
14d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner#include "android/base/StringView.h"
15d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
16d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner#include <gtest/gtest.h>
17d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
18d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turnernamespace android {
19d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turnernamespace base {
20d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
21d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, DefaultConstructor) {
22d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
23d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(0U, s.size());
24d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_TRUE(s.empty());
25d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
26d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
27d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, SimpleConstructor) {
28d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    static const char kString[] = "hello you";
29d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const size_t kStringLen = sizeof(kString) - 1U;
30d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
31d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s(kString);
32d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_NE(kString, s.c_str());
33d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(kStringLen, s.size());
34d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_FALSE(s.empty());
35d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(kString, s.c_str());
36d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
37d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
38d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, SimpleConstructorWithLongString) {
39d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    static const char kString[] =
40d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner            "this is a long string that should exceed the "
41d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner            "small-string-optimization capacity";
42d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const size_t kStringLen = sizeof(kString) - 1U;
43d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
44d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s(kString);
45d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_NE(kString, s.c_str());
46d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(kStringLen, s.size());
47d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_FALSE(s.empty());
48d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(kString, s.c_str());
49d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
50d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
51d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, CopyConstructor) {
52d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s1("Hello World!");
53d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s2(s1);
54d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(s1.size(), s2.size());
55d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(s1.c_str(), s2.c_str());
56d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
57d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
58d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, ConstructorFromStringView) {
59d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    StringView view("Hello Cowboys");
60d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s(view);
61d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_NE(view.str(), s.c_str());
62d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(view.size(), s.size());
63d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(view.str(), s.c_str());
64d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
65d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
66d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, ConstructorWithCountAndFill) {
67d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const size_t kCount = 1024;
68d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const char kFill = 0x42;
69d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
70d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s(kCount, kFill);
71d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(kCount, s.size());
72d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < kCount; ++n)
73d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(kFill, s[n]);
74d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
75d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
76d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, empty) {
77d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s1("");
78d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_TRUE(s1.empty());
79d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
80d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s2("foo");
81d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_FALSE(s2.empty());
82d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
83d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
84d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, size) {
85d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const size_t kCount = 4096;
86d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
87d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < kCount; ++n) {
88d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(n, s.size());
89d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        s += 'x';
90d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
91d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(kCount, s.size());
92d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
93d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
9497502faffa2e99bf717749a2d249ba3e57d7da4dDavid 'Digit' TurnerTEST(String, clear) {
9597502faffa2e99bf717749a2d249ba3e57d7da4dDavid 'Digit' Turner    String s("foo bar");
9697502faffa2e99bf717749a2d249ba3e57d7da4dDavid 'Digit' Turner    EXPECT_FALSE(s.empty());
9797502faffa2e99bf717749a2d249ba3e57d7da4dDavid 'Digit' Turner    s.clear();
9897502faffa2e99bf717749a2d249ba3e57d7da4dDavid 'Digit' Turner    EXPECT_TRUE(s.empty());
9997502faffa2e99bf717749a2d249ba3e57d7da4dDavid 'Digit' Turner    EXPECT_EQ(0U, s.size());
10097502faffa2e99bf717749a2d249ba3e57d7da4dDavid 'Digit' Turner}
10197502faffa2e99bf717749a2d249ba3e57d7da4dDavid 'Digit' Turner
102d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, IndexedAccess) {
103d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s("foobar");
104d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ('f', s[0]);
105d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ('o', s[1]);
106d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ('o', s[2]);
107d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ('b', s[3]);
108d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ('a', s[4]);
109d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ('r', s[5]);
110d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ('\0', s[6]);
111d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
112d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
113d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, AssignOperatorWithCString) {
114d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    static const char kString[] = "Hello 10234";
115d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const size_t kStringLen = sizeof(kString) - 1U;
116d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
117d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
118d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s = kString;
119d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_NE(kString, s.c_str());
120d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(kStringLen, s.size());
121d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(kString, s.c_str());
122d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
123d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
124d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, AssignOperatorWithOtherString) {
125d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s0("A fish named Wanda");
126d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
127d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s = s0;
128d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(s0.size(), s.size());
129d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(s0.c_str(), s.c_str());
130d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
131d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
132d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, AssignOperatorWithStringView) {
133d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    StringView v0("What a beautiful world");
134d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
135d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s = v0;
136d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(v0.size(), s.size());
137d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(v0.str(), s.c_str());
138d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
139d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
140d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, AssignOperatorWithChar) {
141d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
142d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s = 'f';
143d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(1U, s.size());
144d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ('f', s[0]);
145d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
146d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
147d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, assignWithCString) {
148d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    static const char kString[] = "Hello";
149d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const size_t kStringLen = sizeof(kString) - 1U;
150d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
151d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String  s;
152d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s.assign(kString);
153d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(kStringLen, s.size());
154d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(kString, s.c_str());
155d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
156d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
157d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, assignWithString) {
158d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s0("Input string");
159d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
160d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s.assign(s0);
161d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(s0.size(), s.size());
162d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(s0.c_str(), s.c_str());
163d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
164d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
165d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, assignWithStringAndLen) {
166d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    static const char kString[] = "Who you're gonna call?";
167d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const size_t kStringLen = sizeof(kString) - 1U;
168d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
169d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
170d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s.assign(kString, kStringLen);
171d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_NE(kString, s.c_str());
172d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(kStringLen, s.size());
173d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(kString, s.c_str());
174d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
175d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
176d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, assignWithCountAndFill) {
177d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const size_t kCount = 1024;
178d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const char kFill = '\x7f';
179d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
180d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s.assign(kCount, kFill);
181d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(kCount, s.size());
182d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < kCount; ++n) {
183d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(kFill, s[n]) << "At index " << n;
184d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
185d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
186d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
187d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, assignWithStringView) {
188d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    StringView v0("Input string view");
189d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
190d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s.assign(v0);
191d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(v0.size(), s.size());
192d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ(v0.str(), s.c_str());
193d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
194d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
195d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, assignWithChar) {
196d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
197d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s.assign('f');
198d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_EQ(1U, s.size());
199d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    EXPECT_STREQ("f", s.c_str());
200d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
201d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
202d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turnertypedef struct {
203d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const char* s1;
204d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const char* s2;
205d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    int expected;
206d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner} Comparison;
207d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
208d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turnerstatic const Comparison kComparisons[] = {
209d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "", "", 0 },
210d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "", "foo", -1 },
211d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "foo", "", +1 },
212d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "foo", "zoo", -1 },
213d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "foo", "foo", 0 },
214d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "foo", "fo", +1 },
215d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "foo", "fooo", -1 },
216d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "foo", "fop", -1 },
217d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    { "foo", "fon", +1 },
218d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner};
219d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
220d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(x[0]))
221d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
222d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, compareWithCString) {
223d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
224d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
225d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s(comp.s1);
226d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(comp.expected, s.compare(comp.s2))
227d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
228d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
229d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
230d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
231d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
232d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, compareWithCStringWithLen) {
233d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
234d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
235d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s(comp.s1);
236d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        size_t len2 = ::strlen(comp.s2);
237d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(comp.expected, s.compare(comp.s2, len2))
238d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
239d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
240d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
241d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
242d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
243d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
244d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, compareWithString) {
245d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
246d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
247d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s1(comp.s1);
248d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s2(comp.s2);
249d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(comp.expected, s1.compare(s2))
250d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
251d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
252d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
253d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
254d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
255d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, compareWithStringView) {
256d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
257d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
258d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s1(comp.s1);
259d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        StringView s2(comp.s2);
260d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(comp.expected, s1.compare(s2))
261d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
262d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
263d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
264d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
265d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
266d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, equalsWithCString) {
267d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
268d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
269d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s(comp.s1);
270d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(!comp.expected, s.equals(comp.s2))
271d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
272d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
273d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
274d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
275d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
276d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, equalsWithCStringWithLen) {
277d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
278d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
279d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s(comp.s1);
280d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        size_t len2 = ::strlen(comp.s2);
281d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(!comp.expected, s.equals(comp.s2, len2))
282d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
283d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
284d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
285d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
286d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
287d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
288d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, equalsWithString) {
289d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
290d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
291d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s1(comp.s1);
292d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s2(comp.s2);
293d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(!comp.expected, s1.equals(s2))
294d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
295d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
296d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
297d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
298d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
299d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, equalsWithStringView) {
300d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
301d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
302d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s1(comp.s1);
303d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        StringView s2(comp.s2);
304d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(!comp.expected, s1.equals(s2))
305d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
306d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
307d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
308d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
309d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
310d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, EqualsOperatorWithCString) {
311d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
312d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
313d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s(comp.s1);
314d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(!comp.expected, s == comp.s2)
315d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
316d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
317d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
318d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
319d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
320d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, EqualsOperatorWithString) {
321d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
322d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
323d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s1(comp.s1);
324d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s2(comp.s2);
325d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(!comp.expected, s1 == s2)
326d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
327d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
328d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
329d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
330d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
331d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, EqualsOperatorWithStringView) {
332d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    for (size_t n = 0; n < ARRAY_SIZE(kComparisons); ++n) {
333d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        const Comparison& comp = kComparisons[n];
334d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String s1(comp.s1);
335d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        StringView s2(comp.s2);
336d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        EXPECT_EQ(!comp.expected, s1 == s2)
337d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << "Comparing '" << comp.s1 << "' with '"
338d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner                << comp.s2 << "'";
339d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    }
340d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
341d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
342d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, Reserve0OnEmptyString) {
343d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    String s;
344d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    // This is really to check that this doesn't crash.
345d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    ::memset(&s, 0, sizeof(s));
346d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    s.reserve(0U);
347d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner}
348d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
349d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' TurnerTEST(String, Swap) {
350d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const String kShortString1("Hello World!");
351d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const String kLongString1("A very long string that will likely "
352d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner            "exceed the small string optimization storage buffer");
353d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const String kShortString2("Menthe a l'eau");
354d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    const String kLongString2("Harold Ramis, Chicago actor, writer "
355d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner            "and director, dead at 69");
356d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner
357d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    // Swap two short strings.
358d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner    {
359d3f2c27ff9f611e5047a35cb20ed53f548214fedDavid 'Digit' Turner        String sa = kShortString1;
360