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 //===----------------------------------------------------------------------===// 9 10 #include "llvm/ADT/StringExtras.h" 11 #include "llvm/Support/raw_ostream.h" 12 #include "gtest/gtest.h" 13 14 using namespace llvm; 15 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('?')); 25 } 26 27 TEST(StringExtrasTest, Join) { 28 std::vector<std::string> Items; 29 EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> ")); 30 31 Items = {"foo"}; 32 EXPECT_EQ("foo", join(Items.begin(), Items.end(), " <sep> ")); 33 34 Items = {"foo", "bar"}; 35 EXPECT_EQ("foo <sep> bar", join(Items.begin(), Items.end(), " <sep> ")); 36 37 Items = {"foo", "bar", "baz"}; 38 EXPECT_EQ("foo <sep> bar <sep> baz", 39 join(Items.begin(), Items.end(), " <sep> ")); 40 } 41 42 TEST(StringExtrasTest, JoinItems) { 43 const char *Foo = "foo"; 44 std::string Bar = "bar"; 45 llvm::StringRef Baz = "baz"; 46 char X = 'x'; 47 48 EXPECT_EQ("", join_items(" <sep> ")); 49 EXPECT_EQ("", join_items('/')); 50 51 EXPECT_EQ("foo", join_items(" <sep> ", Foo)); 52 EXPECT_EQ("foo", join_items('/', Foo)); 53 54 EXPECT_EQ("foo <sep> bar", join_items(" <sep> ", Foo, Bar)); 55 EXPECT_EQ("foo/bar", join_items('/', Foo, Bar)); 56 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)); 59 60 EXPECT_EQ("foo <sep> bar <sep> baz <sep> x", 61 join_items(" <sep> ", Foo, Bar, Baz, X)); 62 63 EXPECT_EQ("foo/bar/baz/x", join_items('/', Foo, Bar, Baz, X)); 64 } 65 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 74 std::vector<uint8_t> EvenBytes = {0xA5, 0xBD, 0x0D, 0x3E, 0xCD}; 75 std::string EvenStr = "A5BD0D3ECD"; 76 StringRef EvenData(reinterpret_cast<const char *>(EvenBytes.data()), 77 EvenBytes.size()); 78 EXPECT_EQ(EvenStr, toHex(EvenData)); 79 EXPECT_EQ(EvenData, fromHex(EvenStr)); 80 } 81 82 TEST(StringExtrasTest, to_float) { 83 float F; 84 EXPECT_TRUE(to_float("4.7", F)); 85 EXPECT_FLOAT_EQ(4.7f, F); 86 87 double D; 88 EXPECT_TRUE(to_float("4.7", D)); 89 EXPECT_DOUBLE_EQ(4.7, D); 90 91 long double LD; 92 EXPECT_TRUE(to_float("4.7", LD)); 93 EXPECT_DOUBLE_EQ(4.7, LD); 94 95 EXPECT_FALSE(to_float("foo", F)); 96 EXPECT_FALSE(to_float("7.4 foo", F)); 97 EXPECT_FLOAT_EQ(4.7f, F); // F should be unchanged 98 } 99 100 TEST(StringExtrasTest, printLowerCase) { 101 std::string str; 102 raw_string_ostream OS(str); 103 printLowerCase("ABCdefg01234.,&!~`'}\"", OS); 104 EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS.str()); 105 } 106 107 TEST(StringExtrasTest, printEscapedString) { 108 std::string str; 109 raw_string_ostream OS(str); 110 printEscapedString("ABCdef123&<>\\\"'\t", OS); 111 EXPECT_EQ("ABCdef123&<>\\5C\\22'\\09", OS.str()); 112 } 113 114 TEST(StringExtrasTest, printHTMLEscaped) { 115 std::string str; 116 raw_string_ostream OS(str); 117 printHTMLEscaped("ABCdef123&<>\"'", OS); 118 EXPECT_EQ("ABCdef123&<>"'", OS.str()); 119 } 120