Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-25 01:45:09

0001 /*----------------------------------------------------------------------
0002 
0003 Test program for fmt external.
0004 
0005  ----------------------------------------------------------------------*/
0006 
0007 #include <fmt/format.h>
0008 #include <cppunit/extensions/HelperMacros.h>
0009 
0010 class test_fmt_external : public CppUnit::TestFixture {
0011   CPPUNIT_TEST_SUITE(test_fmt_external);
0012 
0013   CPPUNIT_TEST(test_fmt);
0014 
0015   CPPUNIT_TEST_SUITE_END();
0016 
0017 public:
0018   void setUp() {}
0019   void tearDown() {}
0020   void test_fmt();
0021 };
0022 
0023 ///registration of the test so that the runner can find it
0024 CPPUNIT_TEST_SUITE_REGISTRATION(test_fmt_external);
0025 
0026 //using std::cerr;
0027 //using std::endl;
0028 
0029 struct date {
0030   int year, month, day;
0031 };
0032 
0033 template <>
0034 struct fmt::formatter<date> {
0035   constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
0036 
0037   template <typename FormatContext>
0038   auto format(const date& d, FormatContext& ctx) {
0039     return format_to(ctx.out(), "{}-{}-{}", d.year, d.month, d.day);
0040   }
0041 };
0042 
0043 template <typename... Args>
0044 auto capture(const Args&... args) {
0045   return std::make_tuple(args...);
0046 }
0047 
0048 auto print_message = [](const auto&... args) { fmt::print(args...); };
0049 
0050 void test_fmt_external::test_fmt()
0051 
0052 {
0053   std::string s = fmt::format("The date is {}", date{2012, 12, 9});
0054   std::string s_check = "The date is 2012-12-9";
0055   CPPUNIT_ASSERT(s_check == s);
0056   auto args = capture("{} {}", 42, "foo");
0057   std::apply(print_message, args);
0058   auto buf = fmt::memory_buffer();
0059   format_to(std::back_inserter(buf), "{}", 42);  // replaces itoa(42, buffer, 10)
0060   fmt::print(to_string(buf));
0061   format_to(std::back_inserter(buf), "{:x}", 42);  // replaces itoa(42, buffer, 16)
0062   fmt::print(to_string(buf));
0063 }
0064 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>