Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:47

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 vprint_message = [](auto&& format, auto&&... args) {
0049   fmt::vprint(format, fmt::make_format_args(std::forward<decltype(args)>(args)...));
0050 };
0051 
0052 void test_fmt_external::test_fmt()
0053 
0054 {
0055   std::string s = fmt::format("The date is {}", date{2012, 12, 9});
0056   std::string s_check = "The date is 2012-12-9";
0057   CPPUNIT_ASSERT(s_check == s);
0058   auto args = capture("{} {}", 42, "foo");
0059   std::apply(vprint_message, args);
0060   auto buf = fmt::memory_buffer();
0061   format_to(std::back_inserter(buf), "{}", 42);  // replaces itoa(42, buffer, 10)
0062   fmt::vprint(to_string(buf), fmt::make_format_args());
0063   format_to(std::back_inserter(buf), "{:x}", 42);  // replaces itoa(42, buffer, 16)
0064   fmt::vprint(to_string(buf), fmt::make_format_args());
0065 }
0066 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>