1 //===- StringExtrasTest.cpp - Unit tests for String extras ----------------===//
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 #include "llvm/ADT/StringExtras.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 
TEST(StringExtrasTest,isPrint)15 TEST(StringExtrasTest, isPrint) {
16   EXPECT_FALSE(isPrint('\0'));
17   EXPECT_FALSE(isPrint('\t'));
18   EXPECT_TRUE(isPrint('0'));
19   EXPECT_TRUE(isPrint('a'));
20   EXPECT_TRUE(isPrint('A'));
21   EXPECT_TRUE(isPrint(' '));
22   EXPECT_TRUE(isPrint('~'));
23   EXPECT_TRUE(isPrint('?'));
24 }
25 
TEST(StringExtrasTest,isSpace)26 TEST(StringExtrasTest, isSpace) {
27   EXPECT_TRUE(isSpace(' '));
28   EXPECT_TRUE(isSpace('\t'));
29   EXPECT_TRUE(isSpace('\n'));
30   EXPECT_TRUE(isSpace('\v'));
31   EXPECT_TRUE(isSpace('\f'));
32   EXPECT_TRUE(isSpace('\v'));
33   EXPECT_FALSE(isSpace('\0'));
34   EXPECT_FALSE(isSpace('_'));
35 }
36 
TEST(StringExtrasTest,Join)37 TEST(StringExtrasTest, Join) {
38   std::vector<std::string> Items;
39   EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));
40 
41   Items = {"foo"};
42   EXPECT_EQ("foo", join(Items.begin(), Items.end(), " <sep> "));
43 
44   Items = {"foo", "bar"};
45   EXPECT_EQ("foo <sep> bar", join(Items.begin(), Items.end(), " <sep> "));
46 
47   Items = {"foo", "bar", "baz"};
48   EXPECT_EQ("foo <sep> bar <sep> baz",
49             join(Items.begin(), Items.end(), " <sep> "));
50 }
51 
TEST(StringExtrasTest,JoinItems)52 TEST(StringExtrasTest, JoinItems) {
53   const char *Foo = "foo";
54   std::string Bar = "bar";
55   llvm::StringRef Baz = "baz";
56   char X = 'x';
57 
58   EXPECT_EQ("", join_items(" <sep> "));
59   EXPECT_EQ("", join_items('/'));
60 
61   EXPECT_EQ("foo", join_items(" <sep> ", Foo));
62   EXPECT_EQ("foo", join_items('/', Foo));
63 
64   EXPECT_EQ("foo <sep> bar", join_items(" <sep> ", Foo, Bar));
65   EXPECT_EQ("foo/bar", join_items('/', Foo, Bar));
66 
67   EXPECT_EQ("foo <sep> bar <sep> baz", join_items(" <sep> ", Foo, Bar, Baz));
68   EXPECT_EQ("foo/bar/baz", join_items('/', Foo, Bar, Baz));
69 
70   EXPECT_EQ("foo <sep> bar <sep> baz <sep> x",
71             join_items(" <sep> ", Foo, Bar, Baz, X));
72 
73   EXPECT_EQ("foo/bar/baz/x", join_items('/', Foo, Bar, Baz, X));
74 }
75 
TEST(StringExtrasTest,ToAndFromHex)76 TEST(StringExtrasTest, ToAndFromHex) {
77   std::vector<uint8_t> OddBytes = {0x5, 0xBD, 0x0D, 0x3E, 0xCD};
78   std::string OddStr = "05BD0D3ECD";
79   StringRef OddData(reinterpret_cast<const char *>(OddBytes.data()),
80                     OddBytes.size());
81   EXPECT_EQ(OddStr, toHex(OddData));
82   EXPECT_EQ(OddData, fromHex(StringRef(OddStr).drop_front()));
83   EXPECT_EQ(StringRef(OddStr).lower(), toHex(OddData, true));
84 
85   std::vector<uint8_t> EvenBytes = {0xA5, 0xBD, 0x0D, 0x3E, 0xCD};
86   std::string EvenStr = "A5BD0D3ECD";
87   StringRef EvenData(reinterpret_cast<const char *>(EvenBytes.data()),
88                      EvenBytes.size());
89   EXPECT_EQ(EvenStr, toHex(EvenData));
90   EXPECT_EQ(EvenData, fromHex(EvenStr));
91   EXPECT_EQ(StringRef(EvenStr).lower(), toHex(EvenData, true));
92 
93   std::string InvalidStr = "A5ZX";
94   std::string IgnoredOutput;
95   EXPECT_FALSE(tryGetFromHex(InvalidStr, IgnoredOutput));
96 }
97 
TEST(StringExtrasTest,to_float)98 TEST(StringExtrasTest, to_float) {
99   float F;
100   EXPECT_TRUE(to_float("4.7", F));
101   EXPECT_FLOAT_EQ(4.7f, F);
102 
103   double D;
104   EXPECT_TRUE(to_float("4.7", D));
105   EXPECT_DOUBLE_EQ(4.7, D);
106 
107   long double LD;
108   EXPECT_TRUE(to_float("4.7", LD));
109   EXPECT_DOUBLE_EQ(4.7, LD);
110 
111   EXPECT_FALSE(to_float("foo", F));
112   EXPECT_FALSE(to_float("7.4 foo", F));
113   EXPECT_FLOAT_EQ(4.7f, F); // F should be unchanged
114 }
115 
TEST(StringExtrasTest,printLowerCase)116 TEST(StringExtrasTest, printLowerCase) {
117   std::string str;
118   raw_string_ostream OS(str);
119   printLowerCase("ABCdefg01234.,&!~`'}\"", OS);
120   EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS.str());
121 }
122 
TEST(StringExtrasTest,printEscapedString)123 TEST(StringExtrasTest, printEscapedString) {
124   std::string str;
125   raw_string_ostream OS(str);
126   printEscapedString("ABCdef123&<>\\\"'\t", OS);
127   EXPECT_EQ("ABCdef123&<>\\\\\\22'\\09", OS.str());
128 }
129 
TEST(StringExtrasTest,printHTMLEscaped)130 TEST(StringExtrasTest, printHTMLEscaped) {
131   std::string str;
132   raw_string_ostream OS(str);
133   printHTMLEscaped("ABCdef123&<>\"'", OS);
134   EXPECT_EQ("ABCdef123&amp;&lt;&gt;&quot;&apos;", OS.str());
135 }
136 
TEST(StringExtras,ConvertToSnakeFromCamelCase)137 TEST(StringExtras, ConvertToSnakeFromCamelCase) {
138   auto testConvertToSnakeCase = [](llvm::StringRef input,
139                                    llvm::StringRef expected) {
140     EXPECT_EQ(convertToSnakeFromCamelCase(input), expected.str());
141   };
142 
143   testConvertToSnakeCase("OpName", "op_name");
144   testConvertToSnakeCase("opName", "op_name");
145   testConvertToSnakeCase("_OpName", "_op_name");
146   testConvertToSnakeCase("Op_Name", "op_name");
147   testConvertToSnakeCase("", "");
148   testConvertToSnakeCase("A", "a");
149   testConvertToSnakeCase("_", "_");
150   testConvertToSnakeCase("a", "a");
151   testConvertToSnakeCase("op_name", "op_name");
152   testConvertToSnakeCase("_op_name", "_op_name");
153   testConvertToSnakeCase("__op_name", "__op_name");
154   testConvertToSnakeCase("op__name", "op__name");
155 }
156 
TEST(StringExtras,ConvertToCamelFromSnakeCase)157 TEST(StringExtras, ConvertToCamelFromSnakeCase) {
158   auto testConvertToCamelCase = [](bool capitalizeFirst, llvm::StringRef input,
159                                    llvm::StringRef expected) {
160     EXPECT_EQ(convertToCamelFromSnakeCase(input, capitalizeFirst),
161               expected.str());
162   };
163 
164   testConvertToCamelCase(false, "op_name", "opName");
165   testConvertToCamelCase(false, "_op_name", "_opName");
166   testConvertToCamelCase(false, "__op_name", "_OpName");
167   testConvertToCamelCase(false, "op__name", "op_Name");
168   testConvertToCamelCase(false, "", "");
169   testConvertToCamelCase(false, "A", "A");
170   testConvertToCamelCase(false, "_", "_");
171   testConvertToCamelCase(false, "a", "a");
172   testConvertToCamelCase(false, "OpName", "OpName");
173   testConvertToCamelCase(false, "opName", "opName");
174   testConvertToCamelCase(false, "_OpName", "_OpName");
175   testConvertToCamelCase(false, "Op_Name", "Op_Name");
176   testConvertToCamelCase(true, "op_name", "OpName");
177   testConvertToCamelCase(true, "_op_name", "_opName");
178   testConvertToCamelCase(true, "__op_name", "_OpName");
179   testConvertToCamelCase(true, "op__name", "Op_Name");
180   testConvertToCamelCase(true, "", "");
181   testConvertToCamelCase(true, "A", "A");
182   testConvertToCamelCase(true, "_", "_");
183   testConvertToCamelCase(true, "a", "A");
184   testConvertToCamelCase(true, "OpName", "OpName");
185   testConvertToCamelCase(true, "_OpName", "_OpName");
186   testConvertToCamelCase(true, "Op_Name", "Op_Name");
187   testConvertToCamelCase(true, "opName", "OpName");
188 }
189 
190 constexpr uint64_t MaxUint64 = std::numeric_limits<uint64_t>::max();
191 constexpr int64_t MaxInt64 = std::numeric_limits<int64_t>::max();
192 constexpr int64_t MinInt64 = std::numeric_limits<int64_t>::min();
193 
TEST(StringExtras,UToStr)194 TEST(StringExtras, UToStr) {
195   EXPECT_EQ("0", utostr(0));
196   EXPECT_EQ("0", utostr(0, /*isNeg=*/false));
197   EXPECT_EQ("1", utostr(1));
198   EXPECT_EQ("1", utostr(1, /*isNeg=*/false));
199   EXPECT_EQ(std::to_string(MaxUint64), utostr(MaxUint64));
200   EXPECT_EQ(std::to_string(MaxUint64), utostr(MaxUint64, /*isNeg=*/false));
201 
202   EXPECT_EQ("-0", utostr(0, /*isNeg=*/true));
203   EXPECT_EQ("-1", utostr(1, /*isNeg=*/true));
204   EXPECT_EQ("-" + std::to_string(MaxInt64), utostr(MaxInt64, /*isNeg=*/true));
205   constexpr uint64_t MinusMinInt64 = -static_cast<uint64_t>(MinInt64);
206   EXPECT_EQ("-" + std::to_string(MinusMinInt64),
207             utostr(MinusMinInt64, /*isNeg=*/true));
208   EXPECT_EQ("-" + std::to_string(MaxUint64), utostr(MaxUint64, /*isNeg=*/true));
209 }
210 
TEST(StringExtras,IToStr)211 TEST(StringExtras, IToStr) {
212   EXPECT_EQ("0", itostr(0));
213   EXPECT_EQ("1", itostr(1));
214   EXPECT_EQ("-1", itostr(-1));
215   EXPECT_EQ(std::to_string(MinInt64), itostr(MinInt64));
216   EXPECT_EQ(std::to_string(MaxInt64), itostr(MaxInt64));
217 }
218