[InstCombine] Signed saturation patterns
[llvm-core.git] / unittests / ADT / SmallStringTest.cpp
blob686215fd223896259f16c0656b6a726b561f4393
1 //===- llvm/unittest/ADT/SmallStringTest.cpp ------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // SmallString unit tests.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/SmallString.h"
14 #include "gtest/gtest.h"
15 #include <climits>
16 #include <cstring>
17 #include <stdarg.h>
19 using namespace llvm;
21 namespace {
23 // Test fixture class
24 class SmallStringTest : public testing::Test {
25 protected:
26 typedef SmallString<40> StringType;
28 StringType theString;
30 void assertEmpty(StringType & v) {
31 // Size tests
32 EXPECT_EQ(0u, v.size());
33 EXPECT_TRUE(v.empty());
34 // Iterator tests
35 EXPECT_TRUE(v.begin() == v.end());
39 // New string test.
40 TEST_F(SmallStringTest, EmptyStringTest) {
41 SCOPED_TRACE("EmptyStringTest");
42 assertEmpty(theString);
43 EXPECT_TRUE(theString.rbegin() == theString.rend());
46 TEST_F(SmallStringTest, AssignRepeated) {
47 theString.assign(3, 'a');
48 EXPECT_EQ(3u, theString.size());
49 EXPECT_STREQ("aaa", theString.c_str());
52 TEST_F(SmallStringTest, AssignIterPair) {
53 StringRef abc = "abc";
54 theString.assign(abc.begin(), abc.end());
55 EXPECT_EQ(3u, theString.size());
56 EXPECT_STREQ("abc", theString.c_str());
59 TEST_F(SmallStringTest, AssignStringRef) {
60 StringRef abc = "abc";
61 theString.assign(abc);
62 EXPECT_EQ(3u, theString.size());
63 EXPECT_STREQ("abc", theString.c_str());
66 TEST_F(SmallStringTest, AssignSmallVector) {
67 StringRef abc = "abc";
68 SmallVector<char, 10> abcVec(abc.begin(), abc.end());
69 theString.assign(abcVec);
70 EXPECT_EQ(3u, theString.size());
71 EXPECT_STREQ("abc", theString.c_str());
74 TEST_F(SmallStringTest, AppendIterPair) {
75 StringRef abc = "abc";
76 theString.append(abc.begin(), abc.end());
77 theString.append(abc.begin(), abc.end());
78 EXPECT_EQ(6u, theString.size());
79 EXPECT_STREQ("abcabc", theString.c_str());
82 TEST_F(SmallStringTest, AppendStringRef) {
83 StringRef abc = "abc";
84 theString.append(abc);
85 theString.append(abc);
86 EXPECT_EQ(6u, theString.size());
87 EXPECT_STREQ("abcabc", theString.c_str());
90 TEST_F(SmallStringTest, AppendSmallVector) {
91 StringRef abc = "abc";
92 SmallVector<char, 10> abcVec(abc.begin(), abc.end());
93 theString.append(abcVec);
94 theString.append(abcVec);
95 EXPECT_EQ(6u, theString.size());
96 EXPECT_STREQ("abcabc", theString.c_str());
99 TEST_F(SmallStringTest, Substr) {
100 theString = "hello";
101 EXPECT_EQ("lo", theString.substr(3));
102 EXPECT_EQ("", theString.substr(100));
103 EXPECT_EQ("hello", theString.substr(0, 100));
104 EXPECT_EQ("o", theString.substr(4, 10));
107 TEST_F(SmallStringTest, Slice) {
108 theString = "hello";
109 EXPECT_EQ("l", theString.slice(2, 3));
110 EXPECT_EQ("ell", theString.slice(1, 4));
111 EXPECT_EQ("llo", theString.slice(2, 100));
112 EXPECT_EQ("", theString.slice(2, 1));
113 EXPECT_EQ("", theString.slice(10, 20));
116 TEST_F(SmallStringTest, Find) {
117 theString = "hello";
118 EXPECT_EQ(2U, theString.find('l'));
119 EXPECT_EQ(StringRef::npos, theString.find('z'));
120 EXPECT_EQ(StringRef::npos, theString.find("helloworld"));
121 EXPECT_EQ(0U, theString.find("hello"));
122 EXPECT_EQ(1U, theString.find("ello"));
123 EXPECT_EQ(StringRef::npos, theString.find("zz"));
124 EXPECT_EQ(2U, theString.find("ll", 2));
125 EXPECT_EQ(StringRef::npos, theString.find("ll", 3));
126 EXPECT_EQ(0U, theString.find(""));
128 EXPECT_EQ(3U, theString.rfind('l'));
129 EXPECT_EQ(StringRef::npos, theString.rfind('z'));
130 EXPECT_EQ(StringRef::npos, theString.rfind("helloworld"));
131 EXPECT_EQ(0U, theString.rfind("hello"));
132 EXPECT_EQ(1U, theString.rfind("ello"));
133 EXPECT_EQ(StringRef::npos, theString.rfind("zz"));
135 EXPECT_EQ(2U, theString.find_first_of('l'));
136 EXPECT_EQ(1U, theString.find_first_of("el"));
137 EXPECT_EQ(StringRef::npos, theString.find_first_of("xyz"));
139 EXPECT_EQ(1U, theString.find_first_not_of('h'));
140 EXPECT_EQ(4U, theString.find_first_not_of("hel"));
141 EXPECT_EQ(StringRef::npos, theString.find_first_not_of("hello"));
143 theString = "hellx xello hell ello world foo bar hello";
144 EXPECT_EQ(36U, theString.find("hello"));
145 EXPECT_EQ(28U, theString.find("foo"));
146 EXPECT_EQ(12U, theString.find("hell", 2));
147 EXPECT_EQ(0U, theString.find(""));
150 TEST_F(SmallStringTest, Count) {
151 theString = "hello";
152 EXPECT_EQ(2U, theString.count('l'));
153 EXPECT_EQ(1U, theString.count('o'));
154 EXPECT_EQ(0U, theString.count('z'));
155 EXPECT_EQ(0U, theString.count("helloworld"));
156 EXPECT_EQ(1U, theString.count("hello"));
157 EXPECT_EQ(1U, theString.count("ello"));
158 EXPECT_EQ(0U, theString.count("zz"));
161 TEST_F(SmallStringTest, Realloc) {
162 theString = "abcd";
163 theString.reserve(100);
164 EXPECT_EQ("abcd", theString);
165 unsigned const N = 100000;
166 theString.reserve(N);
167 for (unsigned i = 0; i < N - 4; ++i)
168 theString.push_back('y');
169 EXPECT_EQ("abcdyyy", theString.slice(0, 7));
172 TEST_F(SmallStringTest, Comparisons) {
173 EXPECT_EQ(-1, SmallString<10>("aab").compare("aad"));
174 EXPECT_EQ( 0, SmallString<10>("aab").compare("aab"));
175 EXPECT_EQ( 1, SmallString<10>("aab").compare("aaa"));
176 EXPECT_EQ(-1, SmallString<10>("aab").compare("aabb"));
177 EXPECT_EQ( 1, SmallString<10>("aab").compare("aa"));
178 EXPECT_EQ( 1, SmallString<10>("\xFF").compare("\1"));
180 EXPECT_EQ(-1, SmallString<10>("AaB").compare_lower("aAd"));
181 EXPECT_EQ( 0, SmallString<10>("AaB").compare_lower("aab"));
182 EXPECT_EQ( 1, SmallString<10>("AaB").compare_lower("AAA"));
183 EXPECT_EQ(-1, SmallString<10>("AaB").compare_lower("aaBb"));
184 EXPECT_EQ( 1, SmallString<10>("AaB").compare_lower("aA"));
185 EXPECT_EQ( 1, SmallString<10>("\xFF").compare_lower("\1"));
187 EXPECT_EQ(-1, SmallString<10>("aab").compare_numeric("aad"));
188 EXPECT_EQ( 0, SmallString<10>("aab").compare_numeric("aab"));
189 EXPECT_EQ( 1, SmallString<10>("aab").compare_numeric("aaa"));
190 EXPECT_EQ(-1, SmallString<10>("aab").compare_numeric("aabb"));
191 EXPECT_EQ( 1, SmallString<10>("aab").compare_numeric("aa"));
192 EXPECT_EQ(-1, SmallString<10>("1").compare_numeric("10"));
193 EXPECT_EQ( 0, SmallString<10>("10").compare_numeric("10"));
194 EXPECT_EQ( 0, SmallString<10>("10a").compare_numeric("10a"));
195 EXPECT_EQ( 1, SmallString<10>("2").compare_numeric("1"));
196 EXPECT_EQ( 0, SmallString<10>("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
197 EXPECT_EQ( 1, SmallString<10>("\xFF").compare_numeric("\1"));
198 EXPECT_EQ( 1, SmallString<10>("V16").compare_numeric("V1_q0"));
199 EXPECT_EQ(-1, SmallString<10>("V1_q0").compare_numeric("V16"));
200 EXPECT_EQ(-1, SmallString<10>("V8_q0").compare_numeric("V16"));
201 EXPECT_EQ( 1, SmallString<10>("V16").compare_numeric("V8_q0"));
202 EXPECT_EQ(-1, SmallString<10>("V1_q0").compare_numeric("V8_q0"));
203 EXPECT_EQ( 1, SmallString<10>("V8_q0").compare_numeric("V1_q0"));
206 // Check gtest prints SmallString as a string instead of a container of chars.
207 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h
208 TEST_F(SmallStringTest, GTestPrinter) {
209 EXPECT_EQ(R"("foo")", ::testing::PrintToString(SmallString<1>("foo")));
210 const SmallVectorImpl<char> &ErasedSmallString = SmallString<1>("foo");
211 EXPECT_EQ(R"("foo")", ::testing::PrintToString(ErasedSmallString));
214 } // namespace