1 //===- StringExtrasTest.cpp - Unit tests for String extras ----------------===//
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 "llvm/ADT/StringExtras.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
16 TEST(StringExtrasTest
, isPrint
) {
17 EXPECT_FALSE(isPrint('\0'));
18 EXPECT_FALSE(isPrint('\t'));
19 EXPECT_TRUE(isPrint('0'));
20 EXPECT_TRUE(isPrint('a'));
21 EXPECT_TRUE(isPrint('A'));
22 EXPECT_TRUE(isPrint(' '));
23 EXPECT_TRUE(isPrint('~'));
24 EXPECT_TRUE(isPrint('?'));
27 TEST(StringExtrasTest
, isSpace
) {
28 EXPECT_TRUE(isSpace(' '));
29 EXPECT_TRUE(isSpace('\t'));
30 EXPECT_TRUE(isSpace('\n'));
31 EXPECT_TRUE(isSpace('\v'));
32 EXPECT_TRUE(isSpace('\f'));
33 EXPECT_TRUE(isSpace('\v'));
34 EXPECT_FALSE(isSpace('\0'));
35 EXPECT_FALSE(isSpace('_'));
38 TEST(StringExtrasTest
, Join
) {
39 std::vector
<std::string
> Items
;
40 EXPECT_EQ("", join(Items
.begin(), Items
.end(), " <sep> "));
43 EXPECT_EQ("foo", join(Items
.begin(), Items
.end(), " <sep> "));
45 Items
= {"foo", "bar"};
46 EXPECT_EQ("foo <sep> bar", join(Items
.begin(), Items
.end(), " <sep> "));
48 Items
= {"foo", "bar", "baz"};
49 EXPECT_EQ("foo <sep> bar <sep> baz",
50 join(Items
.begin(), Items
.end(), " <sep> "));
53 TEST(StringExtrasTest
, JoinItems
) {
54 const char *Foo
= "foo";
55 std::string Bar
= "bar";
56 llvm::StringRef Baz
= "baz";
59 EXPECT_EQ("", join_items(" <sep> "));
60 EXPECT_EQ("", join_items('/'));
62 EXPECT_EQ("foo", join_items(" <sep> ", Foo
));
63 EXPECT_EQ("foo", join_items('/', Foo
));
65 EXPECT_EQ("foo <sep> bar", join_items(" <sep> ", Foo
, Bar
));
66 EXPECT_EQ("foo/bar", join_items('/', Foo
, Bar
));
68 EXPECT_EQ("foo <sep> bar <sep> baz", join_items(" <sep> ", Foo
, Bar
, Baz
));
69 EXPECT_EQ("foo/bar/baz", join_items('/', Foo
, Bar
, Baz
));
71 EXPECT_EQ("foo <sep> bar <sep> baz <sep> x",
72 join_items(" <sep> ", Foo
, Bar
, Baz
, X
));
74 EXPECT_EQ("foo/bar/baz/x", join_items('/', Foo
, Bar
, Baz
, X
));
77 TEST(StringExtrasTest
, ToAndFromHex
) {
78 std::vector
<uint8_t> OddBytes
= {0x5, 0xBD, 0x0D, 0x3E, 0xCD};
79 std::string OddStr
= "05BD0D3ECD";
80 StringRef
OddData(reinterpret_cast<const char *>(OddBytes
.data()),
82 EXPECT_EQ(OddStr
, toHex(OddData
));
83 EXPECT_EQ(OddData
, fromHex(StringRef(OddStr
).drop_front()));
84 EXPECT_EQ(StringRef(OddStr
).lower(), toHex(OddData
, true));
86 std::vector
<uint8_t> EvenBytes
= {0xA5, 0xBD, 0x0D, 0x3E, 0xCD};
87 std::string EvenStr
= "A5BD0D3ECD";
88 StringRef
EvenData(reinterpret_cast<const char *>(EvenBytes
.data()),
90 EXPECT_EQ(EvenStr
, toHex(EvenData
));
91 EXPECT_EQ(EvenData
, fromHex(EvenStr
));
92 EXPECT_EQ(StringRef(EvenStr
).lower(), toHex(EvenData
, true));
94 std::string InvalidStr
= "A50\xFF";
95 std::string IgnoredOutput
;
96 EXPECT_FALSE(tryGetFromHex(InvalidStr
, IgnoredOutput
));
99 TEST(StringExtrasTest
, UINT64ToHex
) {
100 EXPECT_EQ(utohexstr(0xA0u
), "A0");
101 EXPECT_EQ(utohexstr(0xA0u
, false, 4), "00A0");
102 EXPECT_EQ(utohexstr(0xA0u
, false, 8), "000000A0");
105 TEST(StringExtrasTest
, to_float
) {
107 EXPECT_TRUE(to_float("4.7", F
));
108 EXPECT_FLOAT_EQ(4.7f
, F
);
111 EXPECT_TRUE(to_float("4.7", D
));
112 EXPECT_DOUBLE_EQ(4.7, D
);
115 EXPECT_TRUE(to_float("4.7", LD
));
116 EXPECT_DOUBLE_EQ(4.7, LD
);
118 EXPECT_FALSE(to_float("foo", F
));
119 EXPECT_FALSE(to_float("7.4 foo", F
));
120 EXPECT_FLOAT_EQ(4.7f
, F
); // F should be unchanged
123 TEST(StringExtrasTest
, printLowerCase
) {
125 raw_string_ostream
OS(str
);
126 printLowerCase("ABCdefg01234.,&!~`'}\"", OS
);
127 EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS
.str());
130 TEST(StringExtrasTest
, printEscapedString
) {
132 raw_string_ostream
OS(str
);
133 printEscapedString("ABCdef123&<>\\\"'\t", OS
);
134 EXPECT_EQ("ABCdef123&<>\\\\\\22'\\09", OS
.str());
137 TEST(StringExtrasTest
, printHTMLEscaped
) {
139 raw_string_ostream
OS(str
);
140 printHTMLEscaped("ABCdef123&<>\"'", OS
);
141 EXPECT_EQ("ABCdef123&<>"'", OS
.str());
144 TEST(StringExtrasTest
, ConvertToSnakeFromCamelCase
) {
145 auto testConvertToSnakeCase
= [](llvm::StringRef input
,
146 llvm::StringRef expected
) {
147 EXPECT_EQ(convertToSnakeFromCamelCase(input
), expected
.str());
150 testConvertToSnakeCase("OpName", "op_name");
151 testConvertToSnakeCase("opName", "op_name");
152 testConvertToSnakeCase("_OpName", "_op_name");
153 testConvertToSnakeCase("Op_Name", "op_name");
154 testConvertToSnakeCase("", "");
155 testConvertToSnakeCase("A", "a");
156 testConvertToSnakeCase("_", "_");
157 testConvertToSnakeCase("a", "a");
158 testConvertToSnakeCase("op_name", "op_name");
159 testConvertToSnakeCase("_op_name", "_op_name");
160 testConvertToSnakeCase("__op_name", "__op_name");
161 testConvertToSnakeCase("op__name", "op__name");
164 TEST(StringExtrasTest
, ConvertToCamelFromSnakeCase
) {
165 auto testConvertToCamelCase
= [](bool capitalizeFirst
, llvm::StringRef input
,
166 llvm::StringRef expected
) {
167 EXPECT_EQ(convertToCamelFromSnakeCase(input
, capitalizeFirst
),
171 testConvertToCamelCase(false, "op_name", "opName");
172 testConvertToCamelCase(false, "_op_name", "_opName");
173 testConvertToCamelCase(false, "__op_name", "_OpName");
174 testConvertToCamelCase(false, "op__name", "op_Name");
175 testConvertToCamelCase(false, "", "");
176 testConvertToCamelCase(false, "A", "A");
177 testConvertToCamelCase(false, "_", "_");
178 testConvertToCamelCase(false, "a", "a");
179 testConvertToCamelCase(false, "OpName", "OpName");
180 testConvertToCamelCase(false, "opName", "opName");
181 testConvertToCamelCase(false, "_OpName", "_OpName");
182 testConvertToCamelCase(false, "Op_Name", "Op_Name");
183 testConvertToCamelCase(true, "op_name", "OpName");
184 testConvertToCamelCase(true, "_op_name", "_opName");
185 testConvertToCamelCase(true, "__op_name", "_OpName");
186 testConvertToCamelCase(true, "op__name", "Op_Name");
187 testConvertToCamelCase(true, "", "");
188 testConvertToCamelCase(true, "A", "A");
189 testConvertToCamelCase(true, "_", "_");
190 testConvertToCamelCase(true, "a", "A");
191 testConvertToCamelCase(true, "OpName", "OpName");
192 testConvertToCamelCase(true, "_OpName", "_OpName");
193 testConvertToCamelCase(true, "Op_Name", "Op_Name");
194 testConvertToCamelCase(true, "opName", "OpName");
197 constexpr uint64_t MaxUint64
= std::numeric_limits
<uint64_t>::max();
198 constexpr int64_t MaxInt64
= std::numeric_limits
<int64_t>::max();
199 constexpr int64_t MinInt64
= std::numeric_limits
<int64_t>::min();
201 TEST(StringExtrasTest
, UToStr
) {
202 EXPECT_EQ("0", utostr(0));
203 EXPECT_EQ("0", utostr(0, /*isNeg=*/false));
204 EXPECT_EQ("1", utostr(1));
205 EXPECT_EQ("1", utostr(1, /*isNeg=*/false));
206 EXPECT_EQ(std::to_string(MaxUint64
), utostr(MaxUint64
));
207 EXPECT_EQ(std::to_string(MaxUint64
), utostr(MaxUint64
, /*isNeg=*/false));
209 EXPECT_EQ("-0", utostr(0, /*isNeg=*/true));
210 EXPECT_EQ("-1", utostr(1, /*isNeg=*/true));
211 EXPECT_EQ("-" + std::to_string(MaxInt64
), utostr(MaxInt64
, /*isNeg=*/true));
212 constexpr uint64_t MinusMinInt64
= -static_cast<uint64_t>(MinInt64
);
213 EXPECT_EQ("-" + std::to_string(MinusMinInt64
),
214 utostr(MinusMinInt64
, /*isNeg=*/true));
215 EXPECT_EQ("-" + std::to_string(MaxUint64
), utostr(MaxUint64
, /*isNeg=*/true));
218 TEST(StringExtrasTest
, IToStr
) {
219 EXPECT_EQ("0", itostr(0));
220 EXPECT_EQ("1", itostr(1));
221 EXPECT_EQ("-1", itostr(-1));
222 EXPECT_EQ(std::to_string(MinInt64
), itostr(MinInt64
));
223 EXPECT_EQ(std::to_string(MaxInt64
), itostr(MaxInt64
));
226 TEST(StringExtrasTest
, ListSeparator
) {
233 ListSeparator
LS2(" ");
240 TEST(StringExtrasTest
, toStringAPInt
) {
243 EXPECT_EQ(toString(APInt(8, 0), 2, true, true), "0b0");
244 EXPECT_EQ(toString(APInt(8, 0), 8, true, true), "00");
245 EXPECT_EQ(toString(APInt(8, 0), 10, true, true), "0");
246 EXPECT_EQ(toString(APInt(8, 0), 16, true, true), "0x0");
247 EXPECT_EQ(toString(APInt(8, 0), 36, true, false), "0");
250 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 2, isSigned
, true), "0b11111111");
251 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 8, isSigned
, true), "0377");
252 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 10, isSigned
, true), "255");
253 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 16, isSigned
, true), "0xFF");
254 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 36, isSigned
, false), "73");
257 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 2, isSigned
, true), "-0b1");
258 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 8, isSigned
, true), "-01");
259 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 10, isSigned
, true), "-1");
260 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 16, isSigned
, true), "-0x1");
261 EXPECT_EQ(toString(APInt(8, 255, isSigned
), 36, isSigned
, false), "-1");
264 TEST(StringExtrasTest
, toStringAPSInt
) {
267 EXPECT_EQ(toString(APSInt(APInt(8, 0), false), 2), "0");
268 EXPECT_EQ(toString(APSInt(APInt(8, 0), false), 8), "0");
269 EXPECT_EQ(toString(APSInt(APInt(8, 0), false), 10), "0");
270 EXPECT_EQ(toString(APSInt(APInt(8, 0), false), 16), "0");
273 EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned
), 2), "11111111");
274 EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned
), 8), "377");
275 EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned
), 10), "255");
276 EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned
), 16), "FF");
279 EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned
), 2), "-1");
280 EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned
), 8), "-1");
281 EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned
), 10), "-1");
282 EXPECT_EQ(toString(APSInt(APInt(8, 255), isUnsigned
), 16), "-1");
285 TEST(StringExtrasTest
, splitStringRef
) {
286 auto Spl
= split("foo<=>bar<=><=>baz", "<=>");
287 auto It
= Spl
.begin();
288 auto End
= Spl
.end();
291 EXPECT_EQ(*It
, StringRef("foo"));
292 ASSERT_NE(++It
, End
);
293 EXPECT_EQ(*It
, StringRef("bar"));
294 ASSERT_NE(++It
, End
);
295 EXPECT_EQ(*It
, StringRef(""));
296 ASSERT_NE(++It
, End
);
297 EXPECT_EQ(*It
, StringRef("baz"));
298 ASSERT_EQ(++It
, End
);
301 TEST(StringExtrasTest
, splitStringRefForLoop
) {
302 llvm::SmallVector
<StringRef
, 4> Result
;
303 for (StringRef x
: split("foo<=>bar<=><=>baz", "<=>"))
305 EXPECT_THAT(Result
, testing::ElementsAre("foo", "bar", "", "baz"));
308 TEST(StringExtrasTest
, splitChar
) {
309 auto Spl
= split("foo,bar,,baz", ',');
310 auto It
= Spl
.begin();
311 auto End
= Spl
.end();
314 EXPECT_EQ(*It
, StringRef("foo"));
315 ASSERT_NE(++It
, End
);
316 EXPECT_EQ(*It
, StringRef("bar"));
317 ASSERT_NE(++It
, End
);
318 EXPECT_EQ(*It
, StringRef(""));
319 ASSERT_NE(++It
, End
);
320 EXPECT_EQ(*It
, StringRef("baz"));
321 ASSERT_EQ(++It
, End
);
324 TEST(StringExtrasTest
, splitCharForLoop
) {
325 llvm::SmallVector
<StringRef
, 4> Result
;
326 for (StringRef x
: split("foo,bar,,baz", ','))
328 EXPECT_THAT(Result
, testing::ElementsAre("foo", "bar", "", "baz"));