1 //===-- Unittests for string ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/string.h"
10 #include "test/UnitTest/Test.h"
12 using LIBC_NAMESPACE::cpp::string
;
13 using LIBC_NAMESPACE::cpp::string_view
;
14 using LIBC_NAMESPACE::cpp::to_string
;
16 TEST(LlvmLibcStringTest
, InitializeEmpty
) {
18 ASSERT_EQ(s
.size(), size_t(0));
19 ASSERT_TRUE(s
.empty());
20 ASSERT_STREQ(s
.data(), "");
21 ASSERT_STREQ(s
.c_str(), "");
22 ASSERT_EQ(s
.data(), s
.c_str());
23 ASSERT_EQ(s
.capacity(), size_t(0));
26 TEST(LlvmLibcStringTest
, InitializeCString
) {
27 const char *const str
= "abc";
29 ASSERT_EQ(s
.size(), size_t(3));
30 ASSERT_FALSE(s
.empty());
31 ASSERT_NE(s
.data(), &str
[0]);
35 ASSERT_EQ(s
.front(), 'a');
36 ASSERT_EQ(s
.back(), 'c');
37 ASSERT_EQ(s
.data(), s
.c_str());
40 TEST(LlvmLibcStringTest
, ToCString
) {
41 const char *const str
= "abc";
43 const char *cstr
= s
.c_str();
44 ASSERT_EQ(s
.size(), size_t(3));
45 ASSERT_STREQ(str
, cstr
);
48 TEST(LlvmLibcStringTest
, ToStringView
) {
49 const char *const str
= "abc";
52 ASSERT_EQ(view
, string_view(str
));
55 TEST(LlvmLibcStringTest
, InitializeCStringWithSize
) {
56 const char *const str
= "abc";
57 const string
s(str
, 2);
58 ASSERT_EQ(s
.size(), size_t(2));
61 ASSERT_EQ(s
.front(), 'a');
62 ASSERT_EQ(s
.back(), 'b');
65 TEST(LlvmLibcStringTest
, InitializeStringView
) {
66 const string_view str
= "ab";
68 ASSERT_EQ(s
.size(), size_t(2));
71 ASSERT_EQ(s
.front(), 'a');
72 ASSERT_EQ(s
.back(), 'b');
75 TEST(LlvmLibcStringTest
, InitializeRepeatedChar
) {
76 const string
s(4, '1');
77 ASSERT_EQ(string_view(s
), string_view("1111"));
80 TEST(LlvmLibcStringTest
, InitializeZeorChar
) {
81 const string
s(0, '1');
82 ASSERT_TRUE(s
.empty());
85 TEST(LlvmLibcStringTest
, CopyConstruct
) {
86 const char *const str
= "abc";
90 ASSERT_STREQ(a
.c_str(), str
);
91 ASSERT_STREQ(b
.c_str(), str
);
93 ASSERT_NE(a
.data(), b
.data());
96 string
&&move(string
&value
) { return static_cast<string
&&>(value
); }
98 TEST(LlvmLibcStringTest
, CopyAssign
) {
99 const char *const str
= "abc";
104 ASSERT_STREQ(a
.c_str(), str
);
105 ASSERT_STREQ(b
.c_str(), str
);
106 // Different pointers
107 ASSERT_NE(a
.data(), b
.data());
110 TEST(LlvmLibcStringTest
, MoveConstruct
) {
111 const char *const str
= "abc";
114 ASSERT_STREQ(b
.c_str(), str
);
115 ASSERT_STREQ(a
.c_str(), "");
118 TEST(LlvmLibcStringTest
, MoveAssign
) {
119 const char *const str
= "abc";
123 ASSERT_STREQ(b
.c_str(), str
);
124 ASSERT_STREQ(a
.c_str(), "");
127 TEST(LlvmLibcStringTest
, StringViewAssign
) {
128 const string_view str
= "ab";
131 ASSERT_EQ(s
.size(), size_t(2));
132 ASSERT_EQ(s
[0], 'a');
133 ASSERT_EQ(s
[1], 'b');
134 ASSERT_EQ(s
.front(), 'a');
135 ASSERT_EQ(s
.back(), 'b');
138 TEST(LlvmLibcStringTest
, Concat
) {
139 const char *const str
= "abc";
143 ASSERT_STREQ(b
.c_str(), "abc");
145 ASSERT_STREQ(b
.c_str(), "abcabc");
148 TEST(LlvmLibcStringTest
, AddChar
) {
151 ASSERT_STREQ(a
.c_str(), "a");
153 ASSERT_STREQ(a
.c_str(), "ab");
156 TEST(LlvmLibcStringTest
, ResizeCapacityAndNullTermination
) {
159 ASSERT_EQ(a
.capacity(), size_t(0));
160 ASSERT_EQ(a
.data()[0], '\0');
163 ASSERT_EQ(a
.capacity(), size_t(0));
164 ASSERT_EQ(a
.data()[0], '\0');
167 ASSERT_EQ(a
.size(), size_t(1));
168 ASSERT_GE(a
.capacity(), size_t(2));
169 ASSERT_EQ(a
.data()[1], '\0');
172 ASSERT_EQ(a
.size(), size_t(0));
173 ASSERT_GE(a
.capacity(), size_t(2));
174 ASSERT_EQ(a
.data()[0], '\0');
175 // Resize and check zero initialized
177 ASSERT_EQ(a
.size(), size_t(10));
178 ASSERT_GE(a
.capacity(), size_t(10));
179 for (size_t i
= 0; i
< 10; ++i
)
180 ASSERT_EQ(a
[i
], '\0');
183 TEST(LlvmLibcStringTest
, ConcatWithCString
) {
184 ASSERT_STREQ((string("a") + string("b")).c_str(), "ab");
185 ASSERT_STREQ((string("a") + "b").c_str(), "ab");
186 ASSERT_STREQ(("a" + string("b")).c_str(), "ab");
189 TEST(LlvmLibcStringTest
, Comparison
) {
190 // Here we simply check that comparison of string and string_view have the
195 } kTestPairs
[] = {{"a", "b"}, {"", "xyz"}};
196 for (const auto [pa
, pb
] : kTestPairs
) {
199 const string_view
sva(pa
);
200 const string_view
svb(pb
);
201 ASSERT_EQ(sa
== sb
, sva
== svb
);
202 ASSERT_EQ(sa
!= sb
, sva
!= svb
);
203 ASSERT_EQ(sa
>= sb
, sva
>= svb
);
204 ASSERT_EQ(sa
<= sb
, sva
<= svb
);
205 ASSERT_EQ(sa
< sb
, sva
< svb
);
206 ASSERT_EQ(sa
> sb
, sva
> svb
);
210 TEST(LlvmLibcStringTest
, ToString
) {
214 } kTestPairs
[] = {{123, "123"}, {0, "0"}, {-321, "-321"}};
215 for (const auto &[value
, str
] : kTestPairs
) {
216 ASSERT_EQ(to_string((int)(value
)), str
);
217 ASSERT_EQ(to_string((long)(value
)), str
);
218 ASSERT_EQ(to_string((long long)(value
)), str
);
220 ASSERT_EQ(to_string((unsigned int)(value
)), str
);
221 ASSERT_EQ(to_string((unsigned long)(value
)), str
);
222 ASSERT_EQ(to_string((unsigned long long)(value
)), str
);