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 "gtest/gtest.h"
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('?'));
26 TEST(StringExtrasTest
, Join
) {
27 std::vector
<std::string
> Items
;
28 EXPECT_EQ("", join(Items
.begin(), Items
.end(), " <sep> "));
31 EXPECT_EQ("foo", join(Items
.begin(), Items
.end(), " <sep> "));
33 Items
= {"foo", "bar"};
34 EXPECT_EQ("foo <sep> bar", join(Items
.begin(), Items
.end(), " <sep> "));
36 Items
= {"foo", "bar", "baz"};
37 EXPECT_EQ("foo <sep> bar <sep> baz",
38 join(Items
.begin(), Items
.end(), " <sep> "));
41 TEST(StringExtrasTest
, JoinItems
) {
42 const char *Foo
= "foo";
43 std::string Bar
= "bar";
44 llvm::StringRef Baz
= "baz";
47 EXPECT_EQ("", join_items(" <sep> "));
48 EXPECT_EQ("", join_items('/'));
50 EXPECT_EQ("foo", join_items(" <sep> ", Foo
));
51 EXPECT_EQ("foo", join_items('/', Foo
));
53 EXPECT_EQ("foo <sep> bar", join_items(" <sep> ", Foo
, Bar
));
54 EXPECT_EQ("foo/bar", join_items('/', Foo
, Bar
));
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
));
59 EXPECT_EQ("foo <sep> bar <sep> baz <sep> x",
60 join_items(" <sep> ", Foo
, Bar
, Baz
, X
));
62 EXPECT_EQ("foo/bar/baz/x", join_items('/', Foo
, Bar
, Baz
, X
));
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()),
70 EXPECT_EQ(OddStr
, toHex(OddData
));
71 EXPECT_EQ(OddData
, fromHex(StringRef(OddStr
).drop_front()));
72 EXPECT_EQ(StringRef(OddStr
).lower(), toHex(OddData
, true));
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()),
78 EXPECT_EQ(EvenStr
, toHex(EvenData
));
79 EXPECT_EQ(EvenData
, fromHex(EvenStr
));
80 EXPECT_EQ(StringRef(EvenStr
).lower(), toHex(EvenData
, true));
83 TEST(StringExtrasTest
, to_float
) {
85 EXPECT_TRUE(to_float("4.7", F
));
86 EXPECT_FLOAT_EQ(4.7f
, F
);
89 EXPECT_TRUE(to_float("4.7", D
));
90 EXPECT_DOUBLE_EQ(4.7, D
);
93 EXPECT_TRUE(to_float("4.7", LD
));
94 EXPECT_DOUBLE_EQ(4.7, LD
);
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
101 TEST(StringExtrasTest
, printLowerCase
) {
103 raw_string_ostream
OS(str
);
104 printLowerCase("ABCdefg01234.,&!~`'}\"", OS
);
105 EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS
.str());
108 TEST(StringExtrasTest
, printEscapedString
) {
110 raw_string_ostream
OS(str
);
111 printEscapedString("ABCdef123&<>\\\"'\t", OS
);
112 EXPECT_EQ("ABCdef123&<>\\5C\\22'\\09", OS
.str());
115 TEST(StringExtrasTest
, printHTMLEscaped
) {
117 raw_string_ostream
OS(str
);
118 printHTMLEscaped("ABCdef123&<>\"'", OS
);
119 EXPECT_EQ("ABCdef123&<>"'", OS
.str());