[llvm-exegesis] Fix wrong index type.
[llvm-complete.git] / unittests / ADT / StringExtrasTest.cpp
blob02a45c3404f0c9d2a860f1505c1c95ba6e7d3488
1 //===- StringExtrasTest.cpp - Unit tests for String extras ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
14 using namespace llvm;
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, Join) {
28 std::vector<std::string> Items;
29 EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));
31 Items = {"foo"};
32 EXPECT_EQ("foo", join(Items.begin(), Items.end(), " <sep> "));
34 Items = {"foo", "bar"};
35 EXPECT_EQ("foo <sep> bar", join(Items.begin(), Items.end(), " <sep> "));
37 Items = {"foo", "bar", "baz"};
38 EXPECT_EQ("foo <sep> bar <sep> baz",
39 join(Items.begin(), Items.end(), " <sep> "));
42 TEST(StringExtrasTest, JoinItems) {
43 const char *Foo = "foo";
44 std::string Bar = "bar";
45 llvm::StringRef Baz = "baz";
46 char X = 'x';
48 EXPECT_EQ("", join_items(" <sep> "));
49 EXPECT_EQ("", join_items('/'));
51 EXPECT_EQ("foo", join_items(" <sep> ", Foo));
52 EXPECT_EQ("foo", join_items('/', Foo));
54 EXPECT_EQ("foo <sep> bar", join_items(" <sep> ", Foo, Bar));
55 EXPECT_EQ("foo/bar", join_items('/', Foo, Bar));
57 EXPECT_EQ("foo <sep> bar <sep> baz", join_items(" <sep> ", Foo, Bar, Baz));
58 EXPECT_EQ("foo/bar/baz", join_items('/', Foo, Bar, Baz));
60 EXPECT_EQ("foo <sep> bar <sep> baz <sep> x",
61 join_items(" <sep> ", Foo, Bar, Baz, X));
63 EXPECT_EQ("foo/bar/baz/x", join_items('/', Foo, Bar, Baz, X));
66 TEST(StringExtrasTest, ToAndFromHex) {
67 std::vector<uint8_t> OddBytes = {0x5, 0xBD, 0x0D, 0x3E, 0xCD};
68 std::string OddStr = "05BD0D3ECD";
69 StringRef OddData(reinterpret_cast<const char *>(OddBytes.data()),
70 OddBytes.size());
71 EXPECT_EQ(OddStr, toHex(OddData));
72 EXPECT_EQ(OddData, fromHex(StringRef(OddStr).drop_front()));
73 EXPECT_EQ(StringRef(OddStr).lower(), toHex(OddData, true));
75 std::vector<uint8_t> EvenBytes = {0xA5, 0xBD, 0x0D, 0x3E, 0xCD};
76 std::string EvenStr = "A5BD0D3ECD";
77 StringRef EvenData(reinterpret_cast<const char *>(EvenBytes.data()),
78 EvenBytes.size());
79 EXPECT_EQ(EvenStr, toHex(EvenData));
80 EXPECT_EQ(EvenData, fromHex(EvenStr));
81 EXPECT_EQ(StringRef(EvenStr).lower(), toHex(EvenData, true));
84 TEST(StringExtrasTest, to_float) {
85 float F;
86 EXPECT_TRUE(to_float("4.7", F));
87 EXPECT_FLOAT_EQ(4.7f, F);
89 double D;
90 EXPECT_TRUE(to_float("4.7", D));
91 EXPECT_DOUBLE_EQ(4.7, D);
93 long double LD;
94 EXPECT_TRUE(to_float("4.7", LD));
95 EXPECT_DOUBLE_EQ(4.7, LD);
97 EXPECT_FALSE(to_float("foo", F));
98 EXPECT_FALSE(to_float("7.4 foo", F));
99 EXPECT_FLOAT_EQ(4.7f, F); // F should be unchanged
102 TEST(StringExtrasTest, printLowerCase) {
103 std::string str;
104 raw_string_ostream OS(str);
105 printLowerCase("ABCdefg01234.,&!~`'}\"", OS);
106 EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS.str());
109 TEST(StringExtrasTest, printEscapedString) {
110 std::string str;
111 raw_string_ostream OS(str);
112 printEscapedString("ABCdef123&<>\\\"'\t", OS);
113 EXPECT_EQ("ABCdef123&<>\\5C\\22'\\09", OS.str());
116 TEST(StringExtrasTest, printHTMLEscaped) {
117 std::string str;
118 raw_string_ostream OS(str);
119 printHTMLEscaped("ABCdef123&<>\"'", OS);
120 EXPECT_EQ("ABCdef123&amp;&lt;&gt;&quot;&apos;", OS.str());