Mercurial > hg > CbC > CbC_llvm
diff unittests/Support/FormatVariadicTest.cpp @ 121:803732b1fca8
LLVM 5.0
author | kono |
---|---|
date | Fri, 27 Oct 2017 17:07:41 +0900 |
parents | 1172e4bd9c6f |
children | c2174574ed3a |
line wrap: on
line diff
--- a/unittests/Support/FormatVariadicTest.cpp Fri Nov 25 19:14:25 2016 +0900 +++ b/unittests/Support/FormatVariadicTest.cpp Fri Oct 27 17:07:41 2017 +0900 @@ -7,12 +7,34 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/FormatAdapters.h" -#include "llvm/Support/FormatVariadic.h" #include "gtest/gtest.h" using namespace llvm; +// Compile-time tests templates in the detail namespace. +namespace { +struct Format : public FormatAdapter<int> { + Format(int N) : FormatAdapter<int>(std::move(N)) {} + void format(raw_ostream &OS, StringRef Opt) override { OS << "Format"; } +}; + +using detail::uses_format_member; +using detail::uses_missing_provider; + +static_assert(uses_format_member<Format>::value, ""); +static_assert(uses_format_member<Format &>::value, ""); +static_assert(uses_format_member<Format &&>::value, ""); +static_assert(uses_format_member<const Format>::value, ""); +static_assert(uses_format_member<const Format &>::value, ""); +static_assert(uses_format_member<const volatile Format>::value, ""); +static_assert(uses_format_member<const volatile Format &>::value, ""); + +struct NoFormat {}; +static_assert(uses_missing_provider<NoFormat>::value, ""); +} + TEST(FormatVariadicTest, EmptyFormatString) { auto Replacements = formatv_object_base::parseFormatString(""); EXPECT_EQ(0U, Replacements.size()); @@ -302,11 +324,13 @@ const char FooArray[] = "FooArray"; const char *FooPtr = "FooPtr"; llvm::StringRef FooRef("FooRef"); + constexpr StringLiteral FooLiteral("FooLiteral"); std::string FooString("FooString"); // 1. Test that we can print various types of strings. EXPECT_EQ(FooArray, formatv("{0}", FooArray).str()); EXPECT_EQ(FooPtr, formatv("{0}", FooPtr).str()); EXPECT_EQ(FooRef, formatv("{0}", FooRef).str()); + EXPECT_EQ(FooLiteral, formatv("{0}", FooLiteral).str()); EXPECT_EQ(FooString, formatv("{0}", FooString).str()); // 2. Test that the precision specifier prints the correct number of @@ -506,12 +530,10 @@ } TEST(FormatVariadicTest, Adapter) { - class Negative { - int N; - + class Negative : public FormatAdapter<int> { public: - explicit Negative(int N) : N(N) {} - void format(raw_ostream &S, StringRef Options) { S << -N; } + explicit Negative(int N) : FormatAdapter<int>(std::move(N)) {} + void format(raw_ostream &S, StringRef Options) override { S << -Item; } }; EXPECT_EQ("-7", formatv("{0}", Negative(7)).str()); @@ -520,6 +542,8 @@ EXPECT_EQ(" 171 ", formatv("{0}", fmt_align(N, AlignStyle::Center, 7)).str()); + EXPECT_EQ("--171--", + formatv("{0}", fmt_align(N, AlignStyle::Center, 7, '-')).str()); EXPECT_EQ(" 171 ", formatv("{0}", fmt_pad(N, 1, 3)).str()); EXPECT_EQ("171171171171171", formatv("{0}", fmt_repeat(N, 5)).str()); @@ -529,10 +553,59 @@ formatv("{0,=34:X-}", fmt_repeat(fmt_pad(N, 1, 3), 5)).str()); } +TEST(FormatVariadicTest, MoveConstructor) { + auto fmt = formatv("{0} {1}", 1, 2); + auto fmt2 = std::move(fmt); + std::string S = fmt2; + EXPECT_EQ("1 2", S); +} TEST(FormatVariadicTest, ImplicitConversions) { std::string S = formatv("{0} {1}", 1, 2); EXPECT_EQ("1 2", S); SmallString<4> S2 = formatv("{0} {1}", 1, 2); EXPECT_EQ("1 2", S2); -} \ No newline at end of file +} + +TEST(FormatVariadicTest, FormatAdapter) { + EXPECT_EQ("Format", formatv("{0}", Format(1)).str()); + + Format var(1); + EXPECT_EQ("Format", formatv("{0}", var).str()); + EXPECT_EQ("Format", formatv("{0}", std::move(var)).str()); + + // Not supposed to compile + // const Format cvar(1); + // EXPECT_EQ("Format", formatv("{0}", cvar).str()); +} + +TEST(FormatVariadicTest, FormatFormatvObject) { + EXPECT_EQ("Format", formatv("F{0}t", formatv("o{0}a", "rm")).str()); + EXPECT_EQ("[ ! ]", formatv("[{0,+5}]", formatv("{0,-2}", "!")).str()); +} + +namespace { +struct Recorder { + int Copied = 0, Moved = 0; + Recorder() = default; + Recorder(const Recorder &Copy) : Copied(1 + Copy.Copied), Moved(Copy.Moved) {} + Recorder(const Recorder &&Move) + : Copied(Move.Copied), Moved(1 + Move.Moved) {} +}; +} // namespace +namespace llvm { +template <> struct format_provider<Recorder> { + static void format(const Recorder &R, raw_ostream &OS, StringRef style) { + OS << R.Copied << "C " << R.Moved << "M"; + } +}; +} // namespace + +TEST(FormatVariadicTest, CopiesAndMoves) { + Recorder R; + EXPECT_EQ("0C 0M", formatv("{0}", R).str()); + EXPECT_EQ("0C 3M", formatv("{0}", std::move(R)).str()); + EXPECT_EQ("0C 3M", formatv("{0}", Recorder()).str()); + EXPECT_EQ(0, R.Copied); + EXPECT_EQ(0, R.Moved); +}