Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,154 @@
//===-- ArchSpecTest.cpp ----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "lldb/Utility/ArchSpec.h"
#include "llvm/BinaryFormat/MachO.h"
using namespace lldb;
using namespace lldb_private;
TEST(ArchSpecTest, TestParseMachCPUDashSubtypeTripleSimple) {
// Success conditions. Valid cpu/subtype combinations using both - and .
ArchSpec AS;
EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-10", AS));
EXPECT_EQ(12u, AS.GetMachOCPUType());
EXPECT_EQ(10u, AS.GetMachOCPUSubType());
AS = ArchSpec();
EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-15", AS));
EXPECT_EQ(12u, AS.GetMachOCPUType());
EXPECT_EQ(15u, AS.GetMachOCPUSubType());
AS = ArchSpec();
EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12.15", AS));
EXPECT_EQ(12u, AS.GetMachOCPUType());
EXPECT_EQ(15u, AS.GetMachOCPUSubType());
// Failure conditions.
// Valid string, unknown cpu/subtype.
AS = ArchSpec();
EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("13.11", AS));
EXPECT_EQ(0u, AS.GetMachOCPUType());
EXPECT_EQ(0u, AS.GetMachOCPUSubType());
// Missing / invalid cpu or subtype
AS = ArchSpec();
EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("13", AS));
AS = ArchSpec();
EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("13.A", AS));
AS = ArchSpec();
EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("A.13", AS));
// Empty string.
AS = ArchSpec();
EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("", AS));
}
TEST(ArchSpecTest, TestParseMachCPUDashSubtypeTripleExtra) {
ArchSpec AS;
EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-15-vendor-os", AS));
EXPECT_EQ(12u, AS.GetMachOCPUType());
EXPECT_EQ(15u, AS.GetMachOCPUSubType());
EXPECT_EQ("vendor", AS.GetTriple().getVendorName());
EXPECT_EQ("os", AS.GetTriple().getOSName());
AS = ArchSpec();
EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-10-vendor-os-name", AS));
EXPECT_EQ(12u, AS.GetMachOCPUType());
EXPECT_EQ(10u, AS.GetMachOCPUSubType());
EXPECT_EQ("vendor", AS.GetTriple().getVendorName());
EXPECT_EQ("os", AS.GetTriple().getOSName());
AS = ArchSpec();
EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-15-vendor.os-name", AS));
EXPECT_EQ(12u, AS.GetMachOCPUType());
EXPECT_EQ(15u, AS.GetMachOCPUSubType());
EXPECT_EQ("vendor.os", AS.GetTriple().getVendorName());
EXPECT_EQ("name", AS.GetTriple().getOSName());
// These there should parse correctly, but the vendor / OS should be defaulted
// since they are unrecognized.
AS = ArchSpec();
EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-10-vendor", AS));
EXPECT_EQ(12u, AS.GetMachOCPUType());
EXPECT_EQ(10u, AS.GetMachOCPUSubType());
EXPECT_EQ("apple", AS.GetTriple().getVendorName());
EXPECT_EQ("", AS.GetTriple().getOSName());
AS = ArchSpec();
EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("12.10.10", AS));
AS = ArchSpec();
EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("12-10.10", AS));
}
TEST(ArchSpecTest, TestSetTriple) {
ArchSpec AS;
// Various flavors of valid triples.
EXPECT_TRUE(AS.SetTriple("12-10-apple-darwin"));
EXPECT_EQ(uint32_t(llvm::MachO::CPU_TYPE_ARM), AS.GetMachOCPUType());
EXPECT_EQ(10u, AS.GetMachOCPUSubType());
EXPECT_TRUE(llvm::StringRef(AS.GetTriple().str())
.consume_front("armv7f-apple-darwin"));
EXPECT_EQ(ArchSpec::eCore_arm_armv7f, AS.GetCore());
AS = ArchSpec();
EXPECT_TRUE(AS.SetTriple("18.100-apple-darwin"));
EXPECT_EQ(uint32_t(llvm::MachO::CPU_TYPE_POWERPC), AS.GetMachOCPUType());
EXPECT_EQ(100u, AS.GetMachOCPUSubType());
EXPECT_TRUE(llvm::StringRef(AS.GetTriple().str())
.consume_front("powerpc-apple-darwin"));
EXPECT_EQ(ArchSpec::eCore_ppc_ppc970, AS.GetCore());
AS = ArchSpec();
EXPECT_TRUE(AS.SetTriple("i686-pc-windows"));
EXPECT_EQ(llvm::Triple::x86, AS.GetTriple().getArch());
EXPECT_EQ(llvm::Triple::PC, AS.GetTriple().getVendor());
EXPECT_EQ(llvm::Triple::Win32, AS.GetTriple().getOS());
EXPECT_TRUE(
llvm::StringRef(AS.GetTriple().str()).consume_front("i686-pc-windows"));
EXPECT_STREQ("i686", AS.GetArchitectureName());
EXPECT_EQ(ArchSpec::eCore_x86_32_i686, AS.GetCore());
// Various flavors of invalid triples.
AS = ArchSpec();
EXPECT_FALSE(AS.SetTriple("unknown-unknown-unknown"));
AS = ArchSpec();
EXPECT_FALSE(AS.SetTriple("unknown"));
AS = ArchSpec();
EXPECT_FALSE(AS.SetTriple(""));
}
TEST(ArchSpecTest, MergeFrom) {
ArchSpec A;
ArchSpec B("x86_64-pc-linux");
EXPECT_FALSE(A.IsValid());
ASSERT_TRUE(B.IsValid());
EXPECT_EQ(llvm::Triple::ArchType::x86_64, B.GetTriple().getArch());
EXPECT_EQ(llvm::Triple::VendorType::PC, B.GetTriple().getVendor());
EXPECT_EQ(llvm::Triple::OSType::Linux, B.GetTriple().getOS());
EXPECT_EQ(ArchSpec::eCore_x86_64_x86_64, B.GetCore());
A.MergeFrom(B);
ASSERT_TRUE(A.IsValid());
EXPECT_EQ(llvm::Triple::ArchType::x86_64, A.GetTriple().getArch());
EXPECT_EQ(llvm::Triple::VendorType::PC, A.GetTriple().getVendor());
EXPECT_EQ(llvm::Triple::OSType::Linux, A.GetTriple().getOS());
EXPECT_EQ(ArchSpec::eCore_x86_64_x86_64, A.GetCore());
}

View File

@@ -0,0 +1,25 @@
add_lldb_unittest(UtilityTests
ArchSpecTest.cpp
ConstStringTest.cpp
JSONTest.cpp
LogTest.cpp
NameMatchesTest.cpp
StatusTest.cpp
StringExtractorTest.cpp
StructuredDataTest.cpp
TildeExpressionResolverTest.cpp
TimeoutTest.cpp
TimerTest.cpp
UriParserTest.cpp
VASprintfTest.cpp
LINK_LIBS
lldbUtility
lldbUtilityHelpers
LINK_COMPONENTS
Support
)
add_unittest_inputs(UtilityTests
StructuredData-basic.json
)

View File

@@ -0,0 +1,18 @@
//===-- ConstStringTest.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/ConstString.h"
#include "llvm/Support/FormatVariadic.h"
#include "gtest/gtest.h"
using namespace lldb_private;
TEST(ConstStringTest, format_provider) {
EXPECT_EQ("foo", llvm::formatv("{0}", ConstString("foo")).str());
}

View File

@@ -0,0 +1 @@
[1, 2, 3]

View File

@@ -0,0 +1,26 @@
#include "gtest/gtest.h"
#include "lldb/Utility/JSON.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb_private;
TEST(JSONTest, Dictionary) {
JSONObject o;
o.SetObject("key", std::make_shared<JSONString>("value"));
StreamString stream;
o.Write(stream);
ASSERT_EQ(stream.GetString(), R"({"key":"value"})");
}
TEST(JSONTest, Newlines) {
JSONObject o;
o.SetObject("key", std::make_shared<JSONString>("hello\nworld"));
StreamString stream;
o.Write(stream);
ASSERT_EQ(stream.GetString(), R"({"key":"hello\nworld"})");
}

View File

@@ -0,0 +1,280 @@
//===-- LogTest.cpp ---------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Threading.h"
#include <thread>
using namespace lldb;
using namespace lldb_private;
enum { FOO = 1, BAR = 2 };
static constexpr Log::Category test_categories[] = {
{{"foo"}, {"log foo"}, FOO}, {{"bar"}, {"log bar"}, BAR},
};
static constexpr uint32_t default_flags = FOO;
static Log::Channel test_channel(test_categories, default_flags);
struct LogChannelTest : public ::testing::Test {
void TearDown() override { Log::DisableAllLogChannels(); }
static void SetUpTestCase() {
Log::Register("chan", test_channel);
}
static void TearDownTestCase() {
Log::Unregister("chan");
llvm::llvm_shutdown();
}
};
// Wrap enable, disable and list functions to make them easier to test.
static bool EnableChannel(std::shared_ptr<llvm::raw_ostream> stream_sp,
uint32_t log_options, llvm::StringRef channel,
llvm::ArrayRef<const char *> categories,
std::string &error) {
error.clear();
llvm::raw_string_ostream error_stream(error);
return Log::EnableLogChannel(stream_sp, log_options, channel, categories,
error_stream);
}
static bool DisableChannel(llvm::StringRef channel,
llvm::ArrayRef<const char *> categories,
std::string &error) {
error.clear();
llvm::raw_string_ostream error_stream(error);
return Log::DisableLogChannel(channel, categories, error_stream);
}
static bool ListCategories(llvm::StringRef channel, std::string &result) {
result.clear();
llvm::raw_string_ostream result_stream(result);
return Log::ListChannelCategories(channel, result_stream);
}
TEST(LogTest, LLDB_LOG_nullptr) {
Log *log = nullptr;
LLDB_LOG(log, "{0}", 0); // Shouldn't crash
}
TEST(LogTest, Register) {
llvm::llvm_shutdown_obj obj;
Log::Register("chan", test_channel);
Log::Unregister("chan");
Log::Register("chan", test_channel);
Log::Unregister("chan");
}
TEST(LogTest, Unregister) {
llvm::llvm_shutdown_obj obj;
Log::Register("chan", test_channel);
EXPECT_EQ(nullptr, test_channel.GetLogIfAny(FOO));
std::string message;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(message));
EXPECT_TRUE(Log::EnableLogChannel(stream_sp, 0, "chan", {"foo"}, llvm::nulls()));
EXPECT_NE(nullptr, test_channel.GetLogIfAny(FOO));
Log::Unregister("chan");
EXPECT_EQ(nullptr, test_channel.GetLogIfAny(FOO));
}
TEST_F(LogChannelTest, Enable) {
EXPECT_EQ(nullptr, test_channel.GetLogIfAll(FOO));
std::string message;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(message));
std::string error;
ASSERT_FALSE(EnableChannel(stream_sp, 0, "chanchan", {}, error));
EXPECT_EQ("Invalid log channel 'chanchan'.\n", error);
EXPECT_TRUE(EnableChannel(stream_sp, 0, "chan", {}, error));
EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO));
EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR));
EXPECT_TRUE(EnableChannel(stream_sp, 0, "chan", {"bar"}, error));
EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO | BAR));
EXPECT_TRUE(EnableChannel(stream_sp, 0, "chan", {"baz"}, error));
EXPECT_NE(std::string::npos, error.find("unrecognized log category 'baz'"))
<< "error: " << error;
EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO | BAR));
}
TEST_F(LogChannelTest, EnableOptions) {
EXPECT_EQ(nullptr, test_channel.GetLogIfAll(FOO));
std::string message;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(message));
std::string error;
EXPECT_TRUE(
EnableChannel(stream_sp, LLDB_LOG_OPTION_VERBOSE, "chan", {}, error));
Log *log = test_channel.GetLogIfAll(FOO);
ASSERT_NE(nullptr, log);
EXPECT_TRUE(log->GetVerbose());
}
TEST_F(LogChannelTest, Disable) {
EXPECT_EQ(nullptr, test_channel.GetLogIfAll(FOO));
std::string message;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(message));
std::string error;
EXPECT_TRUE(EnableChannel(stream_sp, 0, "chan", {"foo", "bar"}, error));
EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO | BAR));
EXPECT_TRUE(DisableChannel("chan", {"bar"}, error));
EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO));
EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR));
EXPECT_TRUE(DisableChannel("chan", {"baz"}, error));
EXPECT_NE(std::string::npos, error.find("unrecognized log category 'baz'"))
<< "error: " << error;
EXPECT_NE(nullptr, test_channel.GetLogIfAll(FOO));
EXPECT_EQ(nullptr, test_channel.GetLogIfAll(BAR));
EXPECT_TRUE(DisableChannel("chan", {}, error));
EXPECT_EQ(nullptr, test_channel.GetLogIfAny(FOO | BAR));
}
TEST_F(LogChannelTest, List) {
std::string list;
EXPECT_TRUE(ListCategories("chan", list));
std::string expected =
R"(Logging categories for 'chan':
all - all available logging categories
default - default set of logging categories
foo - log foo
bar - log bar
)";
EXPECT_EQ(expected, list);
EXPECT_FALSE(ListCategories("chanchan", list));
EXPECT_EQ("Invalid log channel 'chanchan'.\n", list);
}
static std::string GetLogString(uint32_t log_options, const char *format,
int arg) {
std::string message;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(message));
std::string error;
llvm::raw_string_ostream error_stream(error);
EXPECT_TRUE(
Log::EnableLogChannel(stream_sp, log_options, "chan", {}, error_stream));
Log *log = test_channel.GetLogIfAll(FOO);
EXPECT_NE(nullptr, log);
LLDB_LOG(log, format, arg);
EXPECT_TRUE(Log::DisableLogChannel("chan", {}, error_stream));
return stream_sp->str();
}
TEST_F(LogChannelTest, log_options) {
EXPECT_EQ("Hello World 47\n", GetLogString(0, "Hello World {0}", 47));
EXPECT_EQ("Hello World 47\n",
GetLogString(LLDB_LOG_OPTION_THREADSAFE, "Hello World {0}", 47));
{
std::string msg =
GetLogString(LLDB_LOG_OPTION_PREPEND_SEQUENCE, "Hello World {0}", 47);
int seq_no;
EXPECT_EQ(1, sscanf(msg.c_str(), "%d Hello World 47", &seq_no));
}
EXPECT_EQ(
"LogTest.cpp:GetLogString Hello "
"World 47\n",
GetLogString(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION, "Hello World {0}", 47));
EXPECT_EQ(llvm::formatv("[{0,0+4}/{1,0+4}] Hello World 47\n", ::getpid(),
llvm::get_threadid())
.str(),
GetLogString(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD,
"Hello World {0}", 47));
}
TEST_F(LogChannelTest, LogThread) {
// Test that we are able to concurrently write to a log channel and disable
// it.
std::string message;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(message));
std::string err;
EXPECT_TRUE(EnableChannel(stream_sp, 0, "chan", {}, err));
Log *log = test_channel.GetLogIfAll(FOO);
// Start logging on one thread. Concurrently, try disabling the log channel.
std::thread log_thread([log] { LLDB_LOG(log, "Hello World"); });
EXPECT_TRUE(DisableChannel("chan", {}, err));
log_thread.join();
// The log thread either managed to write to the log in time, or it didn't. In
// either case, we should not trip any undefined behavior (run the test under
// TSAN to verify this).
EXPECT_TRUE(stream_sp->str() == "" || stream_sp->str() == "Hello World\n")
<< "str(): " << stream_sp->str();
}
TEST_F(LogChannelTest, LogVerboseThread) {
// Test that we are able to concurrently check the verbose flag of a log
// channel and enable it.
std::string message;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(message));
std::string err;
EXPECT_TRUE(EnableChannel(stream_sp, 0, "chan", {}, err));
Log *log = test_channel.GetLogIfAll(FOO);
// Start logging on one thread. Concurrently, try enabling the log channel
// (with different log options).
std::thread log_thread([log] { LLDB_LOGV(log, "Hello World"); });
EXPECT_TRUE(EnableChannel(stream_sp, LLDB_LOG_OPTION_VERBOSE, "chan",
{}, err));
log_thread.join();
EXPECT_TRUE(DisableChannel("chan", {}, err));
// The log thread either managed to write to the log, or it didn't. In either
// case, we should not trip any undefined behavior (run the test under TSAN to
// verify this).
EXPECT_TRUE(stream_sp->str() == "" || stream_sp->str() == "Hello World\n")
<< "str(): " << stream_sp->str();
}
TEST_F(LogChannelTest, LogGetLogThread) {
// Test that we are able to concurrently get mask of a Log object and disable
// it.
std::string message;
std::shared_ptr<llvm::raw_string_ostream> stream_sp(
new llvm::raw_string_ostream(message));
std::string err;
EXPECT_TRUE(EnableChannel(stream_sp, 0, "chan", {}, err));
Log *log = test_channel.GetLogIfAll(FOO);
// Try fetching the log on one thread. Concurrently, try disabling the log
// channel.
uint32_t mask;
std::thread log_thread([log, &mask] { mask = log->GetMask().Get(); });
EXPECT_TRUE(DisableChannel("chan", {}, err));
log_thread.join();
// The mask should be either zero of "FOO". In either case, we should not trip
// any undefined behavior (run the test under TSAN to verify this).
EXPECT_TRUE(mask == 0 || mask == FOO) << "mask: " << mask;
}

View File

@@ -0,0 +1,58 @@
//===-- NameMatchesTest.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/NameMatches.h"
#include "gtest/gtest.h"
using namespace lldb_private;
TEST(NameMatchesTest, Ignore) {
EXPECT_TRUE(NameMatches("foo", NameMatch::Ignore, "bar"));
}
TEST(NameMatchesTest, Equals) {
EXPECT_TRUE(NameMatches("foo", NameMatch::Equals, "foo"));
EXPECT_FALSE(NameMatches("foo", NameMatch::Equals, "bar"));
}
TEST(NameMatchesTest, Contains) {
EXPECT_TRUE(NameMatches("foobar", NameMatch::Contains, "foo"));
EXPECT_TRUE(NameMatches("foobar", NameMatch::Contains, "oob"));
EXPECT_TRUE(NameMatches("foobar", NameMatch::Contains, "bar"));
EXPECT_TRUE(NameMatches("foobar", NameMatch::Contains, "foobar"));
EXPECT_TRUE(NameMatches("", NameMatch::Contains, ""));
EXPECT_FALSE(NameMatches("", NameMatch::Contains, "foo"));
EXPECT_FALSE(NameMatches("foobar", NameMatch::Contains, "baz"));
}
TEST(NameMatchesTest, StartsWith) {
EXPECT_TRUE(NameMatches("foo", NameMatch::StartsWith, "f"));
EXPECT_TRUE(NameMatches("foo", NameMatch::StartsWith, ""));
EXPECT_TRUE(NameMatches("", NameMatch::StartsWith, ""));
EXPECT_FALSE(NameMatches("foo", NameMatch::StartsWith, "b"));
EXPECT_FALSE(NameMatches("", NameMatch::StartsWith, "b"));
}
TEST(NameMatchesTest, EndsWith) {
EXPECT_TRUE(NameMatches("foo", NameMatch::EndsWith, "o"));
EXPECT_TRUE(NameMatches("foo", NameMatch::EndsWith, ""));
EXPECT_TRUE(NameMatches("", NameMatch::EndsWith, ""));
EXPECT_FALSE(NameMatches("foo", NameMatch::EndsWith, "b"));
EXPECT_FALSE(NameMatches("", NameMatch::EndsWith, "b"));
}
TEST(NameMatchesTest, RegularExpression) {
EXPECT_TRUE(NameMatches("foobar", NameMatch::RegularExpression, "foo"));
EXPECT_TRUE(NameMatches("foobar", NameMatch::RegularExpression, "f[oa]o"));
EXPECT_TRUE(NameMatches("foo", NameMatch::RegularExpression, ""));
EXPECT_TRUE(NameMatches("", NameMatch::RegularExpression, ""));
EXPECT_FALSE(NameMatches("foo", NameMatch::RegularExpression, "b"));
EXPECT_FALSE(NameMatches("", NameMatch::RegularExpression, "b"));
EXPECT_FALSE(NameMatches("^a", NameMatch::RegularExpression, "^a"));
}

View File

@@ -0,0 +1,53 @@
//===-- StatusTest.cpp ------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/Status.h"
#include "gtest/gtest.h"
using namespace lldb_private;
using namespace lldb;
TEST(StatusTest, Formatv) {
EXPECT_EQ("", llvm::formatv("{0}", Status()).str());
EXPECT_EQ("Hello Status", llvm::formatv("{0}", Status("Hello Status")).str());
EXPECT_EQ("Hello", llvm::formatv("{0:5}", Status("Hello Error")).str());
}
TEST(StatusTest, ErrorConstructor) {
EXPECT_TRUE(Status(llvm::Error::success()).Success());
Status eagain(
llvm::errorCodeToError(std::error_code(EAGAIN, std::generic_category())));
EXPECT_TRUE(eagain.Fail());
EXPECT_EQ(eErrorTypePOSIX, eagain.GetType());
EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError());
Status foo(llvm::make_error<llvm::StringError>(
"foo", llvm::inconvertibleErrorCode()));
EXPECT_TRUE(foo.Fail());
EXPECT_EQ(eErrorTypeGeneric, foo.GetType());
EXPECT_STREQ("foo", foo.AsCString());
foo = llvm::Error::success();
EXPECT_TRUE(foo.Success());
}
TEST(StatusTest, ErrorConversion) {
EXPECT_FALSE(bool(Status().ToError()));
llvm::Error eagain = Status(EAGAIN, ErrorType::eErrorTypePOSIX).ToError();
EXPECT_TRUE(bool(eagain));
std::error_code ec = llvm::errorToErrorCode(std::move(eagain));
EXPECT_EQ(EAGAIN, ec.value());
EXPECT_EQ(std::generic_category(), ec.category());
llvm::Error foo = Status("foo").ToError();
EXPECT_TRUE(bool(foo));
EXPECT_EQ("foo", llvm::toString(std::move(foo)));
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,48 @@
//===-- StructuredDataTest.cpp ----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "TestingSupport/TestUtilities.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StructuredData.h"
#include "llvm/Support/Path.h"
using namespace lldb;
using namespace lldb_private;
TEST(StructuredDataTest, StringDump) {
std::pair<llvm::StringRef, llvm::StringRef> TestCases[] = {
{R"(asdfg)", R"("asdfg")"},
{R"(as"df)", R"("as\"df")"},
{R"(as\df)", R"("as\\df")"},
};
for (auto P : TestCases) {
StreamString S;
const bool pretty_print = false;
StructuredData::String(P.first).Dump(S, pretty_print);
EXPECT_EQ(P.second, S.GetString());
}
}
TEST(StructuredDataTest, ParseJSONFromFile) {
Status status;
auto object_sp = StructuredData::ParseJSONFromFile(
FileSpec("non-existing-file.json", false), status);
EXPECT_EQ(nullptr, object_sp);
std::string input = GetInputFilePath("StructuredData-basic.json");
object_sp = StructuredData::ParseJSONFromFile(FileSpec(input, false), status);
ASSERT_NE(nullptr, object_sp);
StreamString S;
object_sp->Dump(S, false);
EXPECT_EQ("[1,2,3]", S.GetString());
}

View File

@@ -0,0 +1,36 @@
#include "gtest/gtest.h"
#include "TestingSupport/MockTildeExpressionResolver.h"
#include "lldb/Utility/TildeExpressionResolver.h"
#include "llvm/ADT/SmallString.h"
using namespace llvm;
using namespace lldb_private;
TEST(TildeExpressionResolver, ResolveFullPath) {
MockTildeExpressionResolver Resolver("James", "/james");
Resolver.AddKnownUser("Kirk", "/kirk");
Resolver.AddKnownUser("Lars", "/lars");
Resolver.AddKnownUser("Jason", "/jason");
Resolver.AddKnownUser("Larry", "/larry");
SmallString<32> Result;
ASSERT_TRUE(Resolver.ResolveFullPath("~", Result));
EXPECT_EQ("/james", Result);
ASSERT_TRUE(Resolver.ResolveFullPath("~/", Result));
EXPECT_EQ("/james/", Result);
ASSERT_TRUE(Resolver.ResolveFullPath("~James/bar/baz", Result));
EXPECT_EQ("/james/bar/baz", Result);
ASSERT_TRUE(Resolver.ResolveFullPath("~Jason/", Result));
EXPECT_EQ("/jason/", Result);
ASSERT_TRUE(Resolver.ResolveFullPath("~Lars", Result));
EXPECT_EQ("/lars", Result);
ASSERT_FALSE(Resolver.ResolveFullPath("~Jaso", Result));
ASSERT_FALSE(Resolver.ResolveFullPath("", Result));
ASSERT_FALSE(Resolver.ResolveFullPath("Jason", Result));
}

View File

@@ -0,0 +1,30 @@
//===-- TimeoutTest.cpp -----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/Timeout.h"
#include "llvm/Support/FormatVariadic.h"
#include "gtest/gtest.h"
using namespace lldb_private;
using namespace std::chrono;
TEST(TimeoutTest, Construction) {
EXPECT_FALSE(Timeout<std::micro>(llvm::None));
EXPECT_TRUE(bool(Timeout<std::micro>(seconds(0))));
EXPECT_EQ(seconds(0), *Timeout<std::micro>(seconds(0)));
EXPECT_EQ(seconds(3), *Timeout<std::micro>(seconds(3)));
EXPECT_TRUE(bool(Timeout<std::micro>(Timeout<std::milli>(seconds(0)))));
}
TEST(TimeoutTest, Format) {
EXPECT_EQ("<infinite>",
llvm::formatv("{0}", Timeout<std::milli>(llvm::None)).str());
EXPECT_EQ("1000 ms",
llvm::formatv("{0}", Timeout<std::milli>(seconds(1))).str());
}

View File

@@ -0,0 +1,72 @@
//===-- TimerTest.cpp -------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/Timer.h"
#include "gtest/gtest.h"
#include <thread>
using namespace lldb_private;
TEST(TimerTest, CategoryTimes) {
Timer::ResetCategoryTimes();
{
static Timer::Category tcat("CAT1");
Timer t(tcat, "");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
StreamString ss;
Timer::DumpCategoryTimes(&ss);
double seconds;
ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
EXPECT_LT(0.001, seconds);
EXPECT_GT(0.1, seconds);
}
TEST(TimerTest, CategoryTimesNested) {
Timer::ResetCategoryTimes();
{
static Timer::Category tcat1("CAT1");
Timer t1(tcat1, "");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
// Explicitly testing the same category as above.
Timer t2(tcat1, "");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
StreamString ss;
Timer::DumpCategoryTimes(&ss);
double seconds;
// It should only appear once.
ASSERT_EQ(ss.GetString().count("CAT1"), 1U);
ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
EXPECT_LT(0.002, seconds);
EXPECT_GT(0.2, seconds);
}
TEST(TimerTest, CategoryTimes2) {
Timer::ResetCategoryTimes();
{
static Timer::Category tcat1("CAT1");
Timer t1(tcat1, "");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
static Timer::Category tcat2("CAT2");
Timer t2(tcat2, "");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
StreamString ss;
Timer::DumpCategoryTimes(&ss);
double seconds1, seconds2;
ASSERT_EQ(2, sscanf(ss.GetData(), "%lf sec for CAT1%*[\n ]%lf sec for CAT2",
&seconds1, &seconds2))
<< "String: " << ss.GetData();
EXPECT_LT(0.01, seconds1);
EXPECT_GT(1, seconds1);
EXPECT_LT(0.001, seconds2);
EXPECT_GT(0.1, seconds2);
}

View File

@@ -0,0 +1,153 @@
#include "lldb/Utility/UriParser.h"
#include "gtest/gtest.h"
using namespace lldb_private;
// result strings (scheme/hostname/port/path) passed into UriParser::Parse
// are initialized to kAsdf so we can verify that they are unmodified if the
// URI is invalid
static const char *kAsdf = "asdf";
class UriTestCase {
public:
UriTestCase(const char *uri, const char *scheme, const char *hostname,
int port, const char *path)
: m_uri(uri), m_result(true), m_scheme(scheme), m_hostname(hostname),
m_port(port), m_path(path) {}
UriTestCase(const char *uri)
: m_uri(uri), m_result(false), m_scheme(kAsdf), m_hostname(kAsdf),
m_port(1138), m_path(kAsdf) {}
const char *m_uri;
bool m_result;
const char *m_scheme;
const char *m_hostname;
int m_port;
const char *m_path;
};
#define VALIDATE \
llvm::StringRef scheme(kAsdf); \
llvm::StringRef hostname(kAsdf); \
int port(1138); \
llvm::StringRef path(kAsdf); \
EXPECT_EQ(testCase.m_result, \
UriParser::Parse(testCase.m_uri, scheme, hostname, port, path)); \
EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str()); \
EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str()); \
EXPECT_EQ(testCase.m_port, port); \
EXPECT_STREQ(testCase.m_path, path.str().c_str());
TEST(UriParserTest, Minimal) {
const UriTestCase testCase("x://y", "x", "y", -1, "/");
VALIDATE
}
TEST(UriParserTest, MinimalPort) {
const UriTestCase testCase("x://y:1", "x", "y", 1, "/");
llvm::StringRef scheme(kAsdf);
llvm::StringRef hostname(kAsdf);
int port(1138);
llvm::StringRef path(kAsdf);
bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
EXPECT_EQ(testCase.m_result, result);
EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
EXPECT_EQ(testCase.m_port, port);
EXPECT_STREQ(testCase.m_path, path.str().c_str());
}
TEST(UriParserTest, MinimalPath) {
const UriTestCase testCase("x://y/", "x", "y", -1, "/");
VALIDATE
}
TEST(UriParserTest, MinimalPortPath) {
const UriTestCase testCase("x://y:1/", "x", "y", 1, "/");
VALIDATE
}
TEST(UriParserTest, LongPath) {
const UriTestCase testCase("x://y/abc/def/xyz", "x", "y", -1, "/abc/def/xyz");
VALIDATE
}
TEST(UriParserTest, TypicalPortPath) {
const UriTestCase testCase("connect://192.168.100.132:5432/", "connect",
"192.168.100.132", 5432, "/");
VALIDATE;
}
TEST(UriParserTest, BracketedHostnamePort) {
const UriTestCase testCase("connect://[192.168.100.132]:5432/", "connect",
"192.168.100.132", 5432, "/");
llvm::StringRef scheme(kAsdf);
llvm::StringRef hostname(kAsdf);
int port(1138);
llvm::StringRef path(kAsdf);
bool result = UriParser::Parse(testCase.m_uri, scheme, hostname, port, path);
EXPECT_EQ(testCase.m_result, result);
EXPECT_STREQ(testCase.m_scheme, scheme.str().c_str());
EXPECT_STREQ(testCase.m_hostname, hostname.str().c_str());
EXPECT_EQ(testCase.m_port, port);
EXPECT_STREQ(testCase.m_path, path.str().c_str());
}
TEST(UriParserTest, BracketedHostname) {
const UriTestCase testCase("connect://[192.168.100.132]", "connect",
"192.168.100.132", -1, "/");
VALIDATE
}
TEST(UriParserTest, BracketedHostnameWithColon) {
const UriTestCase testCase("connect://[192.168.100.132:5555]:1234", "connect",
"192.168.100.132:5555", 1234, "/");
VALIDATE
}
TEST(UriParserTest, SchemeHostSeparator) {
const UriTestCase testCase("x:/y");
VALIDATE
}
TEST(UriParserTest, SchemeHostSeparator2) {
const UriTestCase testCase("x:y");
VALIDATE
}
TEST(UriParserTest, SchemeHostSeparator3) {
const UriTestCase testCase("x//y");
VALIDATE
}
TEST(UriParserTest, SchemeHostSeparator4) {
const UriTestCase testCase("x/y");
VALIDATE
}
TEST(UriParserTest, BadPort) {
const UriTestCase testCase("x://y:a/");
VALIDATE
}
TEST(UriParserTest, BadPort2) {
const UriTestCase testCase("x://y:5432a/");
VALIDATE
}
TEST(UriParserTest, Empty) {
const UriTestCase testCase("");
VALIDATE
}
TEST(UriParserTest, PortOverflow) {
const UriTestCase testCase("x://"
"y:"
"0123456789012345678901234567890123456789012345678"
"9012345678901234567890123456789012345678901234567"
"89/");
VALIDATE
}

View File

@@ -0,0 +1,67 @@
//===-- VASprintfTest.cpp ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/VASPrintf.h"
#include "llvm/ADT/SmallString.h"
#include "gtest/gtest.h"
#include <locale.h>
#if defined (_WIN32)
#define TEST_ENCODING ".932" // On Windows, test codepage 932
#else
#define TEST_ENCODING "C" // ...otherwise, any widely available uni-byte LC
#endif
using namespace lldb_private;
using namespace llvm;
static bool Sprintf(llvm::SmallVectorImpl<char> &Buffer, const char *Fmt, ...) {
va_list args;
va_start(args, Fmt);
bool Result = VASprintf(Buffer, Fmt, args);
va_end(args);
return Result;
}
TEST(VASprintfTest, NoBufferResize) {
std::string TestStr("small");
llvm::SmallString<32> BigBuffer;
ASSERT_TRUE(Sprintf(BigBuffer, "%s", TestStr.c_str()));
EXPECT_STREQ(TestStr.c_str(), BigBuffer.c_str());
EXPECT_EQ(TestStr.size(), BigBuffer.size());
}
TEST(VASprintfTest, BufferResize) {
std::string TestStr("bigger");
llvm::SmallString<4> SmallBuffer;
ASSERT_TRUE(Sprintf(SmallBuffer, "%s", TestStr.c_str()));
EXPECT_STREQ(TestStr.c_str(), SmallBuffer.c_str());
EXPECT_EQ(TestStr.size(), SmallBuffer.size());
}
TEST(VASprintfTest, EncodingError) {
// Save the current locale first.
std::string Current(::setlocale(LC_ALL, nullptr));
// Ensure tested locale is successfully set
ASSERT_TRUE(setlocale(LC_ALL, TEST_ENCODING));
wchar_t Invalid[2];
Invalid[0] = 0x100;
Invalid[1] = 0;
llvm::SmallString<32> Buffer;
EXPECT_FALSE(Sprintf(Buffer, "%ls", Invalid));
EXPECT_EQ("<Encoding error>", Buffer);
// Ensure we've restored the original locale once tested
ASSERT_TRUE(setlocale(LC_ALL, Current.c_str()));
}