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 
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 
26 TEST(StringExtrasTest, Join) {
27   std::vector<std::string> Items;
28   EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));
29 
30   Items = {"foo"};
31   EXPECT_EQ("foo", join(Items.begin(), Items.end(), " <sep> "));
32 
33   Items = {"foo", "bar"};
34   EXPECT_EQ("foo <sep> bar", join(Items.begin(), Items.end(), " <sep> "));
35 
36   Items = {"foo", "bar", "baz"};
37   EXPECT_EQ("foo <sep> bar <sep> baz",
38             join(Items.begin(), Items.end(), " <sep> "));
39 }
40 
41 TEST(StringExtrasTest, JoinItems) {
42   const char *Foo = "foo";
43   std::string Bar = "bar";
44   llvm::StringRef Baz = "baz";
45   char X = 'x';
46 
47   EXPECT_EQ("", join_items(" <sep> "));
48   EXPECT_EQ("", join_items('/'));
49 
50   EXPECT_EQ("foo", join_items(" <sep> ", Foo));
51   EXPECT_EQ("foo", join_items('/', Foo));
52 
53   EXPECT_EQ("foo <sep> bar", join_items(" <sep> ", Foo, Bar));
54   EXPECT_EQ("foo/bar", join_items('/', Foo, Bar));
55 
56   EXPECT_EQ("foo <sep> bar <sep> baz", join_items(" <sep> ", Foo, Bar, Baz));
57   EXPECT_EQ("foo/bar/baz", join_items('/', Foo, Bar, Baz));
58 
59   EXPECT_EQ("foo <sep> bar <sep> baz <sep> x",
60             join_items(" <sep> ", Foo, Bar, Baz, X));
61 
62   EXPECT_EQ("foo/bar/baz/x", join_items('/', Foo, Bar, Baz, X));
63 }
64 
65 TEST(StringExtrasTest, ToAndFromHex) {
66   std::vector<uint8_t> OddBytes = {0x5, 0xBD, 0x0D, 0x3E, 0xCD};
67   std::string OddStr = "05BD0D3ECD";
68   StringRef OddData(reinterpret_cast<const char *>(OddBytes.data()),
69                     OddBytes.size());
70   EXPECT_EQ(OddStr, toHex(OddData));
71   EXPECT_EQ(OddData, fromHex(StringRef(OddStr).drop_front()));
72   EXPECT_EQ(StringRef(OddStr).lower(), toHex(OddData, true));
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   EXPECT_EQ(StringRef(EvenStr).lower(), toHex(EvenData, true));
81 }
82 
83 TEST(StringExtrasTest, to_float) {
84   float F;
85   EXPECT_TRUE(to_float("4.7", F));
86   EXPECT_FLOAT_EQ(4.7f, F);
87 
88   double D;
89   EXPECT_TRUE(to_float("4.7", D));
90   EXPECT_DOUBLE_EQ(4.7, D);
91 
92   long double LD;
93   EXPECT_TRUE(to_float("4.7", LD));
94   EXPECT_DOUBLE_EQ(4.7, LD);
95 
96   EXPECT_FALSE(to_float("foo", F));
97   EXPECT_FALSE(to_float("7.4 foo", F));
98   EXPECT_FLOAT_EQ(4.7f, F); // F should be unchanged
99 }
100 
101 TEST(StringExtrasTest, printLowerCase) {
102   std::string str;
103   raw_string_ostream OS(str);
104   printLowerCase("ABCdefg01234.,&!~`'}\"", OS);
105   EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS.str());
106 }
107 
108 TEST(StringExtrasTest, printEscapedString) {
109   std::string str;
110   raw_string_ostream OS(str);
111   printEscapedString("ABCdef123&<>\\\"'\t", OS);
112   EXPECT_EQ("ABCdef123&<>\\\\\\22'\\09", OS.str());
113 }
114 
115 TEST(StringExtrasTest, printHTMLEscaped) {
116   std::string str;
117   raw_string_ostream OS(str);
118   printHTMLEscaped("ABCdef123&<>\"'", OS);
119   EXPECT_EQ("ABCdef123&amp;&lt;&gt;&quot;&apos;", OS.str());
120 }
121