2016-09-27 18:37:30 +02:00
|
|
|
//===- StringExtrasTest.cpp - Unit tests for String extras ----------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-09-27 18:37:30 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2017-11-28 17:11:56 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-09-27 18:37:30 +02:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-07-26 17:31:41 +02:00
|
|
|
TEST(StringExtrasTest, isPrint) {
|
|
|
|
EXPECT_FALSE(isPrint('\0'));
|
|
|
|
EXPECT_FALSE(isPrint('\t'));
|
|
|
|
EXPECT_TRUE(isPrint('0'));
|
|
|
|
EXPECT_TRUE(isPrint('a'));
|
|
|
|
EXPECT_TRUE(isPrint('A'));
|
|
|
|
EXPECT_TRUE(isPrint(' '));
|
|
|
|
EXPECT_TRUE(isPrint('~'));
|
|
|
|
EXPECT_TRUE(isPrint('?'));
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:18:35 +02:00
|
|
|
TEST(StringExtrasTest, isSpace) {
|
|
|
|
EXPECT_TRUE(isSpace(' '));
|
|
|
|
EXPECT_TRUE(isSpace('\t'));
|
|
|
|
EXPECT_TRUE(isSpace('\n'));
|
|
|
|
EXPECT_TRUE(isSpace('\v'));
|
|
|
|
EXPECT_TRUE(isSpace('\f'));
|
|
|
|
EXPECT_TRUE(isSpace('\v'));
|
|
|
|
EXPECT_FALSE(isSpace('\0'));
|
|
|
|
EXPECT_FALSE(isSpace('_'));
|
|
|
|
}
|
|
|
|
|
2016-09-27 18:37:30 +02:00
|
|
|
TEST(StringExtrasTest, Join) {
|
|
|
|
std::vector<std::string> Items;
|
|
|
|
EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));
|
|
|
|
|
|
|
|
Items = {"foo"};
|
|
|
|
EXPECT_EQ("foo", join(Items.begin(), Items.end(), " <sep> "));
|
|
|
|
|
|
|
|
Items = {"foo", "bar"};
|
|
|
|
EXPECT_EQ("foo <sep> bar", join(Items.begin(), Items.end(), " <sep> "));
|
|
|
|
|
|
|
|
Items = {"foo", "bar", "baz"};
|
|
|
|
EXPECT_EQ("foo <sep> bar <sep> baz",
|
|
|
|
join(Items.begin(), Items.end(), " <sep> "));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringExtrasTest, JoinItems) {
|
|
|
|
const char *Foo = "foo";
|
|
|
|
std::string Bar = "bar";
|
|
|
|
llvm::StringRef Baz = "baz";
|
|
|
|
char X = 'x';
|
|
|
|
|
|
|
|
EXPECT_EQ("", join_items(" <sep> "));
|
|
|
|
EXPECT_EQ("", join_items('/'));
|
|
|
|
|
|
|
|
EXPECT_EQ("foo", join_items(" <sep> ", Foo));
|
|
|
|
EXPECT_EQ("foo", join_items('/', Foo));
|
|
|
|
|
|
|
|
EXPECT_EQ("foo <sep> bar", join_items(" <sep> ", Foo, Bar));
|
|
|
|
EXPECT_EQ("foo/bar", join_items('/', Foo, Bar));
|
|
|
|
|
|
|
|
EXPECT_EQ("foo <sep> bar <sep> baz", join_items(" <sep> ", Foo, Bar, Baz));
|
|
|
|
EXPECT_EQ("foo/bar/baz", join_items('/', Foo, Bar, Baz));
|
|
|
|
|
|
|
|
EXPECT_EQ("foo <sep> bar <sep> baz <sep> x",
|
|
|
|
join_items(" <sep> ", Foo, Bar, Baz, X));
|
|
|
|
|
|
|
|
EXPECT_EQ("foo/bar/baz/x", join_items('/', Foo, Bar, Baz, X));
|
|
|
|
}
|
2017-04-25 22:21:35 +02:00
|
|
|
|
|
|
|
TEST(StringExtrasTest, ToAndFromHex) {
|
|
|
|
std::vector<uint8_t> OddBytes = {0x5, 0xBD, 0x0D, 0x3E, 0xCD};
|
|
|
|
std::string OddStr = "05BD0D3ECD";
|
|
|
|
StringRef OddData(reinterpret_cast<const char *>(OddBytes.data()),
|
|
|
|
OddBytes.size());
|
|
|
|
EXPECT_EQ(OddStr, toHex(OddData));
|
|
|
|
EXPECT_EQ(OddData, fromHex(StringRef(OddStr).drop_front()));
|
2018-09-10 21:34:44 +02:00
|
|
|
EXPECT_EQ(StringRef(OddStr).lower(), toHex(OddData, true));
|
2017-04-25 22:21:35 +02:00
|
|
|
|
|
|
|
std::vector<uint8_t> EvenBytes = {0xA5, 0xBD, 0x0D, 0x3E, 0xCD};
|
|
|
|
std::string EvenStr = "A5BD0D3ECD";
|
|
|
|
StringRef EvenData(reinterpret_cast<const char *>(EvenBytes.data()),
|
|
|
|
EvenBytes.size());
|
|
|
|
EXPECT_EQ(EvenStr, toHex(EvenData));
|
|
|
|
EXPECT_EQ(EvenData, fromHex(EvenStr));
|
2018-09-10 21:34:44 +02:00
|
|
|
EXPECT_EQ(StringRef(EvenStr).lower(), toHex(EvenData, true));
|
2020-10-29 00:46:25 +01:00
|
|
|
|
|
|
|
std::string InvalidStr = "A5ZX";
|
|
|
|
std::string IgnoredOutput;
|
|
|
|
EXPECT_FALSE(tryGetFromHex(InvalidStr, IgnoredOutput));
|
2017-06-23 14:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringExtrasTest, to_float) {
|
|
|
|
float F;
|
|
|
|
EXPECT_TRUE(to_float("4.7", F));
|
2017-06-23 15:53:55 +02:00
|
|
|
EXPECT_FLOAT_EQ(4.7f, F);
|
2017-06-23 14:55:02 +02:00
|
|
|
|
|
|
|
double D;
|
|
|
|
EXPECT_TRUE(to_float("4.7", D));
|
|
|
|
EXPECT_DOUBLE_EQ(4.7, D);
|
|
|
|
|
|
|
|
long double LD;
|
|
|
|
EXPECT_TRUE(to_float("4.7", LD));
|
|
|
|
EXPECT_DOUBLE_EQ(4.7, LD);
|
|
|
|
|
|
|
|
EXPECT_FALSE(to_float("foo", F));
|
|
|
|
EXPECT_FALSE(to_float("7.4 foo", F));
|
2017-06-23 15:53:55 +02:00
|
|
|
EXPECT_FLOAT_EQ(4.7f, F); // F should be unchanged
|
2017-06-23 14:55:02 +02:00
|
|
|
}
|
2017-11-28 17:11:56 +01:00
|
|
|
|
|
|
|
TEST(StringExtrasTest, printLowerCase) {
|
|
|
|
std::string str;
|
|
|
|
raw_string_ostream OS(str);
|
|
|
|
printLowerCase("ABCdefg01234.,&!~`'}\"", OS);
|
|
|
|
EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS.str());
|
|
|
|
}
|
2018-05-30 22:47:18 +02:00
|
|
|
|
2018-07-26 17:31:41 +02:00
|
|
|
TEST(StringExtrasTest, printEscapedString) {
|
|
|
|
std::string str;
|
|
|
|
raw_string_ostream OS(str);
|
|
|
|
printEscapedString("ABCdef123&<>\\\"'\t", OS);
|
2019-10-10 20:31:57 +02:00
|
|
|
EXPECT_EQ("ABCdef123&<>\\\\\\22'\\09", OS.str());
|
2018-07-26 17:31:41 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 19:01:42 +02:00
|
|
|
TEST(StringExtrasTest, printHTMLEscaped) {
|
2018-05-30 22:47:18 +02:00
|
|
|
std::string str;
|
|
|
|
raw_string_ostream OS(str);
|
2018-05-31 19:01:42 +02:00
|
|
|
printHTMLEscaped("ABCdef123&<>\"'", OS);
|
2018-05-30 22:47:18 +02:00
|
|
|
EXPECT_EQ("ABCdef123&<>"'", OS.str());
|
|
|
|
}
|
2020-04-15 03:54:23 +02:00
|
|
|
|
|
|
|
TEST(StringExtras, ConvertToSnakeFromCamelCase) {
|
|
|
|
auto testConvertToSnakeCase = [](llvm::StringRef input,
|
|
|
|
llvm::StringRef expected) {
|
|
|
|
EXPECT_EQ(convertToSnakeFromCamelCase(input), expected.str());
|
|
|
|
};
|
|
|
|
|
|
|
|
testConvertToSnakeCase("OpName", "op_name");
|
|
|
|
testConvertToSnakeCase("opName", "op_name");
|
|
|
|
testConvertToSnakeCase("_OpName", "_op_name");
|
|
|
|
testConvertToSnakeCase("Op_Name", "op_name");
|
|
|
|
testConvertToSnakeCase("", "");
|
|
|
|
testConvertToSnakeCase("A", "a");
|
|
|
|
testConvertToSnakeCase("_", "_");
|
|
|
|
testConvertToSnakeCase("a", "a");
|
|
|
|
testConvertToSnakeCase("op_name", "op_name");
|
|
|
|
testConvertToSnakeCase("_op_name", "_op_name");
|
|
|
|
testConvertToSnakeCase("__op_name", "__op_name");
|
|
|
|
testConvertToSnakeCase("op__name", "op__name");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringExtras, ConvertToCamelFromSnakeCase) {
|
|
|
|
auto testConvertToCamelCase = [](bool capitalizeFirst, llvm::StringRef input,
|
|
|
|
llvm::StringRef expected) {
|
|
|
|
EXPECT_EQ(convertToCamelFromSnakeCase(input, capitalizeFirst),
|
|
|
|
expected.str());
|
|
|
|
};
|
|
|
|
|
|
|
|
testConvertToCamelCase(false, "op_name", "opName");
|
|
|
|
testConvertToCamelCase(false, "_op_name", "_opName");
|
|
|
|
testConvertToCamelCase(false, "__op_name", "_OpName");
|
|
|
|
testConvertToCamelCase(false, "op__name", "op_Name");
|
|
|
|
testConvertToCamelCase(false, "", "");
|
|
|
|
testConvertToCamelCase(false, "A", "A");
|
|
|
|
testConvertToCamelCase(false, "_", "_");
|
|
|
|
testConvertToCamelCase(false, "a", "a");
|
|
|
|
testConvertToCamelCase(false, "OpName", "OpName");
|
|
|
|
testConvertToCamelCase(false, "opName", "opName");
|
|
|
|
testConvertToCamelCase(false, "_OpName", "_OpName");
|
|
|
|
testConvertToCamelCase(false, "Op_Name", "Op_Name");
|
|
|
|
testConvertToCamelCase(true, "op_name", "OpName");
|
|
|
|
testConvertToCamelCase(true, "_op_name", "_opName");
|
|
|
|
testConvertToCamelCase(true, "__op_name", "_OpName");
|
|
|
|
testConvertToCamelCase(true, "op__name", "Op_Name");
|
|
|
|
testConvertToCamelCase(true, "", "");
|
|
|
|
testConvertToCamelCase(true, "A", "A");
|
|
|
|
testConvertToCamelCase(true, "_", "_");
|
|
|
|
testConvertToCamelCase(true, "a", "A");
|
|
|
|
testConvertToCamelCase(true, "OpName", "OpName");
|
|
|
|
testConvertToCamelCase(true, "_OpName", "_OpName");
|
|
|
|
testConvertToCamelCase(true, "Op_Name", "Op_Name");
|
|
|
|
testConvertToCamelCase(true, "opName", "OpName");
|
|
|
|
}
|
2020-06-22 16:00:00 +02:00
|
|
|
|
|
|
|
constexpr uint64_t MaxUint64 = std::numeric_limits<uint64_t>::max();
|
|
|
|
constexpr int64_t MaxInt64 = std::numeric_limits<int64_t>::max();
|
|
|
|
constexpr int64_t MinInt64 = std::numeric_limits<int64_t>::min();
|
|
|
|
|
|
|
|
TEST(StringExtras, UToStr) {
|
|
|
|
EXPECT_EQ("0", utostr(0));
|
|
|
|
EXPECT_EQ("0", utostr(0, /*isNeg=*/false));
|
|
|
|
EXPECT_EQ("1", utostr(1));
|
|
|
|
EXPECT_EQ("1", utostr(1, /*isNeg=*/false));
|
|
|
|
EXPECT_EQ(std::to_string(MaxUint64), utostr(MaxUint64));
|
|
|
|
EXPECT_EQ(std::to_string(MaxUint64), utostr(MaxUint64, /*isNeg=*/false));
|
|
|
|
|
|
|
|
EXPECT_EQ("-0", utostr(0, /*isNeg=*/true));
|
|
|
|
EXPECT_EQ("-1", utostr(1, /*isNeg=*/true));
|
|
|
|
EXPECT_EQ("-" + std::to_string(MaxInt64), utostr(MaxInt64, /*isNeg=*/true));
|
|
|
|
constexpr uint64_t MinusMinInt64 = -static_cast<uint64_t>(MinInt64);
|
|
|
|
EXPECT_EQ("-" + std::to_string(MinusMinInt64),
|
|
|
|
utostr(MinusMinInt64, /*isNeg=*/true));
|
|
|
|
EXPECT_EQ("-" + std::to_string(MaxUint64), utostr(MaxUint64, /*isNeg=*/true));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringExtras, IToStr) {
|
|
|
|
EXPECT_EQ("0", itostr(0));
|
|
|
|
EXPECT_EQ("1", itostr(1));
|
|
|
|
EXPECT_EQ("-1", itostr(-1));
|
|
|
|
EXPECT_EQ(std::to_string(MinInt64), itostr(MinInt64));
|
|
|
|
EXPECT_EQ(std::to_string(MaxInt64), itostr(MaxInt64));
|
|
|
|
}
|
[StringExtras] Add a helper class for comma-separated lists
This patch introduces a helper class SubsequentDelim to simplify loops
that generate a comma-separated lists.
For example, consider the following loop, taken from
llvm/lib/CodeGen/MachineBasicBlock.cpp:
for (auto I = pred_begin(), E = pred_end(); I != E; ++I) {
if (I != pred_begin())
OS << ", ";
OS << printMBBReference(**I);
}
The new class allows us to rewrite the loop as:
SubsequentDelim SD;
for (auto I = pred_begin(), E = pred_end(); I != E; ++I)
OS << SD << printMBBReference(**I);
where SD evaluates to the empty string for the first time and ", " for
subsequent iterations.
Unlike interleaveComma, defined in llvm/include/llvm/ADT/STLExtras.h,
SubsequentDelim can accommodate a wider variety of loops, including:
- those that conditionally skip certain items,
- those that need iterators to call getSuccProbability(I), and
- those that iterate over integer ranges.
As an example, this patch cleans up MachineBasicBlock::print.
Differential Revision: https://reviews.llvm.org/D94377
2021-01-10 23:32:02 +01:00
|
|
|
|
2021-01-16 06:00:55 +01:00
|
|
|
TEST(StringExtras, ListSeparator) {
|
|
|
|
ListSeparator LS;
|
|
|
|
StringRef S = LS;
|
[StringExtras] Add a helper class for comma-separated lists
This patch introduces a helper class SubsequentDelim to simplify loops
that generate a comma-separated lists.
For example, consider the following loop, taken from
llvm/lib/CodeGen/MachineBasicBlock.cpp:
for (auto I = pred_begin(), E = pred_end(); I != E; ++I) {
if (I != pred_begin())
OS << ", ";
OS << printMBBReference(**I);
}
The new class allows us to rewrite the loop as:
SubsequentDelim SD;
for (auto I = pred_begin(), E = pred_end(); I != E; ++I)
OS << SD << printMBBReference(**I);
where SD evaluates to the empty string for the first time and ", " for
subsequent iterations.
Unlike interleaveComma, defined in llvm/include/llvm/ADT/STLExtras.h,
SubsequentDelim can accommodate a wider variety of loops, including:
- those that conditionally skip certain items,
- those that need iterators to call getSuccProbability(I), and
- those that iterate over integer ranges.
As an example, this patch cleans up MachineBasicBlock::print.
Differential Revision: https://reviews.llvm.org/D94377
2021-01-10 23:32:02 +01:00
|
|
|
EXPECT_EQ(S, "");
|
2021-01-16 06:00:55 +01:00
|
|
|
S = LS;
|
[StringExtras] Add a helper class for comma-separated lists
This patch introduces a helper class SubsequentDelim to simplify loops
that generate a comma-separated lists.
For example, consider the following loop, taken from
llvm/lib/CodeGen/MachineBasicBlock.cpp:
for (auto I = pred_begin(), E = pred_end(); I != E; ++I) {
if (I != pred_begin())
OS << ", ";
OS << printMBBReference(**I);
}
The new class allows us to rewrite the loop as:
SubsequentDelim SD;
for (auto I = pred_begin(), E = pred_end(); I != E; ++I)
OS << SD << printMBBReference(**I);
where SD evaluates to the empty string for the first time and ", " for
subsequent iterations.
Unlike interleaveComma, defined in llvm/include/llvm/ADT/STLExtras.h,
SubsequentDelim can accommodate a wider variety of loops, including:
- those that conditionally skip certain items,
- those that need iterators to call getSuccProbability(I), and
- those that iterate over integer ranges.
As an example, this patch cleans up MachineBasicBlock::print.
Differential Revision: https://reviews.llvm.org/D94377
2021-01-10 23:32:02 +01:00
|
|
|
EXPECT_EQ(S, ", ");
|
|
|
|
|
2021-01-16 06:00:55 +01:00
|
|
|
ListSeparator LS2(" ");
|
|
|
|
S = LS2;
|
[StringExtras] Add a helper class for comma-separated lists
This patch introduces a helper class SubsequentDelim to simplify loops
that generate a comma-separated lists.
For example, consider the following loop, taken from
llvm/lib/CodeGen/MachineBasicBlock.cpp:
for (auto I = pred_begin(), E = pred_end(); I != E; ++I) {
if (I != pred_begin())
OS << ", ";
OS << printMBBReference(**I);
}
The new class allows us to rewrite the loop as:
SubsequentDelim SD;
for (auto I = pred_begin(), E = pred_end(); I != E; ++I)
OS << SD << printMBBReference(**I);
where SD evaluates to the empty string for the first time and ", " for
subsequent iterations.
Unlike interleaveComma, defined in llvm/include/llvm/ADT/STLExtras.h,
SubsequentDelim can accommodate a wider variety of loops, including:
- those that conditionally skip certain items,
- those that need iterators to call getSuccProbability(I), and
- those that iterate over integer ranges.
As an example, this patch cleans up MachineBasicBlock::print.
Differential Revision: https://reviews.llvm.org/D94377
2021-01-10 23:32:02 +01:00
|
|
|
EXPECT_EQ(S, "");
|
2021-01-16 06:00:55 +01:00
|
|
|
S = LS2;
|
[StringExtras] Add a helper class for comma-separated lists
This patch introduces a helper class SubsequentDelim to simplify loops
that generate a comma-separated lists.
For example, consider the following loop, taken from
llvm/lib/CodeGen/MachineBasicBlock.cpp:
for (auto I = pred_begin(), E = pred_end(); I != E; ++I) {
if (I != pred_begin())
OS << ", ";
OS << printMBBReference(**I);
}
The new class allows us to rewrite the loop as:
SubsequentDelim SD;
for (auto I = pred_begin(), E = pred_end(); I != E; ++I)
OS << SD << printMBBReference(**I);
where SD evaluates to the empty string for the first time and ", " for
subsequent iterations.
Unlike interleaveComma, defined in llvm/include/llvm/ADT/STLExtras.h,
SubsequentDelim can accommodate a wider variety of loops, including:
- those that conditionally skip certain items,
- those that need iterators to call getSuccProbability(I), and
- those that iterate over integer ranges.
As an example, this patch cleans up MachineBasicBlock::print.
Differential Revision: https://reviews.llvm.org/D94377
2021-01-10 23:32:02 +01:00
|
|
|
EXPECT_EQ(S, " ");
|
|
|
|
}
|