core-test.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. // Formatting library for C++ - core tests
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. // clang-format off
  8. #include "test-assert.h"
  9. // clang-format on
  10. #define I 42 // simulate https://en.cppreference.com/w/c/numeric/complex/I
  11. #include "fmt/core.h"
  12. #undef I
  13. #include <algorithm> // std::copy_n
  14. #include <climits> // INT_MAX
  15. #include <cstring> // std::strlen
  16. #include <functional> // std::equal_to
  17. #include <iterator> // std::back_insert_iterator
  18. #include <limits> // std::numeric_limits
  19. #include <string> // std::string
  20. #include <type_traits> // std::is_same
  21. #include "gmock/gmock.h"
  22. using fmt::string_view;
  23. using fmt::detail::buffer;
  24. using testing::_;
  25. using testing::Invoke;
  26. using testing::Return;
  27. #ifdef FMT_FORMAT_H_
  28. # error core-test includes format.h
  29. #endif
  30. TEST(string_view_test, value_type) {
  31. static_assert(std::is_same<string_view::value_type, char>::value, "");
  32. }
  33. TEST(string_view_test, ctor) {
  34. EXPECT_STREQ("abc", fmt::string_view("abc").data());
  35. EXPECT_EQ(3u, fmt::string_view("abc").size());
  36. EXPECT_STREQ("defg", fmt::string_view(std::string("defg")).data());
  37. EXPECT_EQ(4u, fmt::string_view(std::string("defg")).size());
  38. }
  39. TEST(string_view_test, length) {
  40. // Test that string_view::size() returns string length, not buffer size.
  41. char str[100] = "some string";
  42. EXPECT_EQ(std::strlen(str), string_view(str).size());
  43. EXPECT_LT(std::strlen(str), sizeof(str));
  44. }
  45. // Check string_view's comparison operator.
  46. template <template <typename> class Op> void check_op() {
  47. const char* inputs[] = {"foo", "fop", "fo"};
  48. size_t num_inputs = sizeof(inputs) / sizeof(*inputs);
  49. for (size_t i = 0; i < num_inputs; ++i) {
  50. for (size_t j = 0; j < num_inputs; ++j) {
  51. string_view lhs(inputs[i]), rhs(inputs[j]);
  52. EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<string_view>()(lhs, rhs));
  53. }
  54. }
  55. }
  56. TEST(string_view_test, compare) {
  57. EXPECT_EQ(string_view("foo").compare(string_view("foo")), 0);
  58. EXPECT_GT(string_view("fop").compare(string_view("foo")), 0);
  59. EXPECT_LT(string_view("foo").compare(string_view("fop")), 0);
  60. EXPECT_GT(string_view("foo").compare(string_view("fo")), 0);
  61. EXPECT_LT(string_view("fo").compare(string_view("foo")), 0);
  62. check_op<std::equal_to>();
  63. check_op<std::not_equal_to>();
  64. check_op<std::less>();
  65. check_op<std::less_equal>();
  66. check_op<std::greater>();
  67. check_op<std::greater_equal>();
  68. }
  69. namespace test_ns {
  70. template <typename Char> class test_string {
  71. private:
  72. std::basic_string<Char> s_;
  73. public:
  74. test_string(const Char* s) : s_(s) {}
  75. const Char* data() const { return s_.data(); }
  76. size_t length() const { return s_.size(); }
  77. operator const Char*() const { return s_.c_str(); }
  78. };
  79. template <typename Char>
  80. fmt::basic_string_view<Char> to_string_view(const test_string<Char>& s) {
  81. return {s.data(), s.length()};
  82. }
  83. } // namespace test_ns
  84. TEST(core_test, is_output_iterator) {
  85. EXPECT_TRUE((fmt::detail::is_output_iterator<char*, char>::value));
  86. EXPECT_FALSE((fmt::detail::is_output_iterator<const char*, char>::value));
  87. EXPECT_FALSE((fmt::detail::is_output_iterator<std::string, char>::value));
  88. EXPECT_TRUE(
  89. (fmt::detail::is_output_iterator<std::back_insert_iterator<std::string>,
  90. char>::value));
  91. EXPECT_TRUE(
  92. (fmt::detail::is_output_iterator<std::string::iterator, char>::value));
  93. EXPECT_FALSE((fmt::detail::is_output_iterator<std::string::const_iterator,
  94. char>::value));
  95. }
  96. TEST(core_test, buffer_appender) {
  97. // back_insert_iterator is not default-constructible before C++20, so
  98. // buffer_appender can only be default-constructible when back_insert_iterator
  99. // is.
  100. static_assert(
  101. std::is_default_constructible<
  102. std::back_insert_iterator<fmt::detail::buffer<char>>>::value ==
  103. std::is_default_constructible<
  104. fmt::detail::buffer_appender<char>>::value,
  105. "");
  106. #ifdef __cpp_lib_ranges
  107. static_assert(std::output_iterator<fmt::detail::buffer_appender<char>, char>);
  108. #endif
  109. }
  110. #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 470
  111. TEST(buffer_test, noncopyable) {
  112. EXPECT_FALSE(std::is_copy_constructible<buffer<char>>::value);
  113. # if !FMT_MSC_VERSION
  114. // std::is_copy_assignable is broken in MSVC2013.
  115. EXPECT_FALSE(std::is_copy_assignable<buffer<char>>::value);
  116. # endif
  117. }
  118. TEST(buffer_test, nonmoveable) {
  119. EXPECT_FALSE(std::is_move_constructible<buffer<char>>::value);
  120. # if !FMT_MSC_VERSION
  121. // std::is_move_assignable is broken in MSVC2013.
  122. EXPECT_FALSE(std::is_move_assignable<buffer<char>>::value);
  123. # endif
  124. }
  125. #endif
  126. TEST(buffer_test, indestructible) {
  127. static_assert(!std::is_destructible<fmt::detail::buffer<int>>(),
  128. "buffer's destructor is protected");
  129. }
  130. template <typename T> struct mock_buffer final : buffer<T> {
  131. MOCK_METHOD1(do_grow, size_t(size_t capacity));
  132. void grow(size_t capacity) override {
  133. this->set(this->data(), do_grow(capacity));
  134. }
  135. mock_buffer(T* data = nullptr, size_t buf_capacity = 0) {
  136. this->set(data, buf_capacity);
  137. ON_CALL(*this, do_grow(_)).WillByDefault(Invoke([](size_t capacity) {
  138. return capacity;
  139. }));
  140. }
  141. };
  142. TEST(buffer_test, ctor) {
  143. {
  144. mock_buffer<int> buffer;
  145. EXPECT_EQ(nullptr, buffer.data());
  146. EXPECT_EQ(static_cast<size_t>(0), buffer.size());
  147. EXPECT_EQ(static_cast<size_t>(0), buffer.capacity());
  148. }
  149. {
  150. int dummy;
  151. mock_buffer<int> buffer(&dummy);
  152. EXPECT_EQ(&dummy, &buffer[0]);
  153. EXPECT_EQ(static_cast<size_t>(0), buffer.size());
  154. EXPECT_EQ(static_cast<size_t>(0), buffer.capacity());
  155. }
  156. {
  157. int dummy;
  158. size_t capacity = std::numeric_limits<size_t>::max();
  159. mock_buffer<int> buffer(&dummy, capacity);
  160. EXPECT_EQ(&dummy, &buffer[0]);
  161. EXPECT_EQ(static_cast<size_t>(0), buffer.size());
  162. EXPECT_EQ(capacity, buffer.capacity());
  163. }
  164. }
  165. TEST(buffer_test, access) {
  166. char data[10];
  167. mock_buffer<char> buffer(data, sizeof(data));
  168. buffer[0] = 11;
  169. EXPECT_EQ(11, buffer[0]);
  170. buffer[3] = 42;
  171. EXPECT_EQ(42, *(&buffer[0] + 3));
  172. const fmt::detail::buffer<char>& const_buffer = buffer;
  173. EXPECT_EQ(42, const_buffer[3]);
  174. }
  175. TEST(buffer_test, try_resize) {
  176. char data[123];
  177. mock_buffer<char> buffer(data, sizeof(data));
  178. buffer[10] = 42;
  179. EXPECT_EQ(42, buffer[10]);
  180. buffer.try_resize(20);
  181. EXPECT_EQ(20u, buffer.size());
  182. EXPECT_EQ(123u, buffer.capacity());
  183. EXPECT_EQ(42, buffer[10]);
  184. buffer.try_resize(5);
  185. EXPECT_EQ(5u, buffer.size());
  186. EXPECT_EQ(123u, buffer.capacity());
  187. EXPECT_EQ(42, buffer[10]);
  188. // Check if try_resize calls grow.
  189. EXPECT_CALL(buffer, do_grow(124));
  190. buffer.try_resize(124);
  191. EXPECT_CALL(buffer, do_grow(200));
  192. buffer.try_resize(200);
  193. }
  194. TEST(buffer_test, try_resize_partial) {
  195. char data[10];
  196. mock_buffer<char> buffer(data, sizeof(data));
  197. EXPECT_CALL(buffer, do_grow(20)).WillOnce(Return(15));
  198. buffer.try_resize(20);
  199. EXPECT_EQ(buffer.capacity(), 15);
  200. EXPECT_EQ(buffer.size(), 15);
  201. }
  202. TEST(buffer_test, clear) {
  203. mock_buffer<char> buffer;
  204. EXPECT_CALL(buffer, do_grow(20));
  205. buffer.try_resize(20);
  206. buffer.try_resize(0);
  207. EXPECT_EQ(static_cast<size_t>(0), buffer.size());
  208. EXPECT_EQ(20u, buffer.capacity());
  209. }
  210. TEST(buffer_test, append) {
  211. char data[15];
  212. mock_buffer<char> buffer(data, 10);
  213. auto test = "test";
  214. buffer.append(test, test + 5);
  215. EXPECT_STREQ(test, &buffer[0]);
  216. EXPECT_EQ(5u, buffer.size());
  217. buffer.try_resize(10);
  218. EXPECT_CALL(buffer, do_grow(12));
  219. buffer.append(test, test + 2);
  220. EXPECT_EQ('t', buffer[10]);
  221. EXPECT_EQ('e', buffer[11]);
  222. EXPECT_EQ(12u, buffer.size());
  223. }
  224. TEST(buffer_test, append_partial) {
  225. char data[10];
  226. mock_buffer<char> buffer(data, sizeof(data));
  227. testing::InSequence seq;
  228. EXPECT_CALL(buffer, do_grow(15)).WillOnce(Return(10));
  229. EXPECT_CALL(buffer, do_grow(15)).WillOnce(Invoke([&buffer](size_t) {
  230. EXPECT_EQ(fmt::string_view(buffer.data(), buffer.size()), "0123456789");
  231. buffer.clear();
  232. return 10;
  233. }));
  234. auto test = "0123456789abcde";
  235. buffer.append(test, test + 15);
  236. }
  237. TEST(buffer_test, append_allocates_enough_storage) {
  238. char data[19];
  239. mock_buffer<char> buffer(data, 10);
  240. auto test = "abcdefgh";
  241. buffer.try_resize(10);
  242. EXPECT_CALL(buffer, do_grow(19));
  243. buffer.append(test, test + 9);
  244. }
  245. struct custom_context {
  246. using char_type = char;
  247. using parse_context_type = fmt::format_parse_context;
  248. bool called = false;
  249. template <typename T> struct formatter_type {
  250. auto parse(fmt::format_parse_context& ctx) -> decltype(ctx.begin()) {
  251. return ctx.begin();
  252. }
  253. const char* format(const T&, custom_context& ctx) {
  254. ctx.called = true;
  255. return nullptr;
  256. }
  257. };
  258. void advance_to(const char*) {}
  259. };
  260. struct test_struct {};
  261. FMT_BEGIN_NAMESPACE
  262. template <typename Char> struct formatter<test_struct, Char> {
  263. auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  264. return ctx.begin();
  265. }
  266. auto format(test_struct, format_context& ctx) -> decltype(ctx.out()) {
  267. auto test = string_view("test");
  268. return std::copy_n(test.data(), test.size(), ctx.out());
  269. }
  270. };
  271. FMT_END_NAMESPACE
  272. TEST(arg_test, format_args) {
  273. auto args = fmt::format_args();
  274. EXPECT_FALSE(args.get(1));
  275. }
  276. TEST(arg_test, make_value_with_custom_context) {
  277. auto t = test_struct();
  278. fmt::detail::value<custom_context> arg(
  279. fmt::detail::arg_mapper<custom_context>().map(t));
  280. auto ctx = custom_context();
  281. auto parse_ctx = fmt::format_parse_context("");
  282. arg.custom.format(&t, parse_ctx, ctx);
  283. EXPECT_TRUE(ctx.called);
  284. }
  285. // Use a unique result type to make sure that there are no undesirable
  286. // conversions.
  287. struct test_result {};
  288. template <typename T> struct mock_visitor {
  289. template <typename U> struct result { using type = test_result; };
  290. mock_visitor() {
  291. ON_CALL(*this, visit(_)).WillByDefault(Return(test_result()));
  292. }
  293. MOCK_METHOD1_T(visit, test_result(T value));
  294. MOCK_METHOD0_T(unexpected, void());
  295. test_result operator()(T value) { return visit(value); }
  296. template <typename U> test_result operator()(U) {
  297. unexpected();
  298. return test_result();
  299. }
  300. };
  301. template <typename T> struct visit_type { using type = T; };
  302. #define VISIT_TYPE(type_, visit_type_) \
  303. template <> struct visit_type<type_> { using type = visit_type_; }
  304. VISIT_TYPE(signed char, int);
  305. VISIT_TYPE(unsigned char, unsigned);
  306. VISIT_TYPE(short, int);
  307. VISIT_TYPE(unsigned short, unsigned);
  308. #if LONG_MAX == INT_MAX
  309. VISIT_TYPE(long, int);
  310. VISIT_TYPE(unsigned long, unsigned);
  311. #else
  312. VISIT_TYPE(long, long long);
  313. VISIT_TYPE(unsigned long, unsigned long long);
  314. #endif
  315. #define CHECK_ARG(Char, expected, value) \
  316. { \
  317. testing::StrictMock<mock_visitor<decltype(expected)>> visitor; \
  318. EXPECT_CALL(visitor, visit(expected)); \
  319. using iterator = std::back_insert_iterator<buffer<Char>>; \
  320. fmt::visit_format_arg( \
  321. visitor, \
  322. fmt::detail::make_arg<fmt::basic_format_context<iterator, Char>>( \
  323. value)); \
  324. }
  325. #define CHECK_ARG_SIMPLE(value) \
  326. { \
  327. using value_type = decltype(value); \
  328. typename visit_type<value_type>::type expected = value; \
  329. CHECK_ARG(char, expected, value) \
  330. CHECK_ARG(wchar_t, expected, value) \
  331. }
  332. template <typename T> class numeric_arg_test : public testing::Test {};
  333. using test_types =
  334. testing::Types<bool, signed char, unsigned char, short, unsigned short, int,
  335. unsigned, long, unsigned long, long long, unsigned long long,
  336. float, double, long double>;
  337. TYPED_TEST_SUITE(numeric_arg_test, test_types);
  338. template <typename T, fmt::enable_if_t<std::is_integral<T>::value, int> = 0>
  339. T test_value() {
  340. return static_cast<T>(42);
  341. }
  342. template <typename T,
  343. fmt::enable_if_t<std::is_floating_point<T>::value, int> = 0>
  344. T test_value() {
  345. return static_cast<T>(4.2);
  346. }
  347. TYPED_TEST(numeric_arg_test, make_and_visit) {
  348. CHECK_ARG_SIMPLE(test_value<TypeParam>());
  349. CHECK_ARG_SIMPLE(std::numeric_limits<TypeParam>::min());
  350. CHECK_ARG_SIMPLE(std::numeric_limits<TypeParam>::max());
  351. }
  352. TEST(arg_test, char_arg) { CHECK_ARG(char, 'a', 'a'); }
  353. TEST(arg_test, string_arg) {
  354. char str_data[] = "test";
  355. char* str = str_data;
  356. const char* cstr = str;
  357. CHECK_ARG(char, cstr, str);
  358. auto sv = fmt::string_view(str);
  359. CHECK_ARG(char, sv, std::string(str));
  360. }
  361. TEST(arg_test, wstring_arg) {
  362. wchar_t str_data[] = L"test";
  363. wchar_t* str = str_data;
  364. const wchar_t* cstr = str;
  365. auto sv = fmt::basic_string_view<wchar_t>(str);
  366. CHECK_ARG(wchar_t, cstr, str);
  367. CHECK_ARG(wchar_t, cstr, cstr);
  368. CHECK_ARG(wchar_t, sv, std::wstring(str));
  369. CHECK_ARG(wchar_t, sv, fmt::basic_string_view<wchar_t>(str));
  370. }
  371. TEST(arg_test, pointer_arg) {
  372. void* p = nullptr;
  373. const void* cp = nullptr;
  374. CHECK_ARG(char, cp, p);
  375. CHECK_ARG(wchar_t, cp, p);
  376. CHECK_ARG_SIMPLE(cp);
  377. }
  378. struct check_custom {
  379. test_result operator()(
  380. fmt::basic_format_arg<fmt::format_context>::handle h) const {
  381. struct test_buffer final : fmt::detail::buffer<char> {
  382. char data[10];
  383. test_buffer() : fmt::detail::buffer<char>(data, 0, 10) {}
  384. void grow(size_t) override {}
  385. } buffer;
  386. auto parse_ctx = fmt::format_parse_context("");
  387. auto ctx = fmt::format_context(fmt::detail::buffer_appender<char>(buffer),
  388. fmt::format_args());
  389. h.format(parse_ctx, ctx);
  390. EXPECT_EQ("test", std::string(buffer.data, buffer.size()));
  391. return test_result();
  392. }
  393. };
  394. TEST(arg_test, custom_arg) {
  395. auto test = test_struct();
  396. using visitor =
  397. mock_visitor<fmt::basic_format_arg<fmt::format_context>::handle>;
  398. testing::StrictMock<visitor> v;
  399. EXPECT_CALL(v, visit(_)).WillOnce(Invoke(check_custom()));
  400. fmt::visit_format_arg(v, fmt::detail::make_arg<fmt::format_context>(test));
  401. }
  402. TEST(arg_test, visit_invalid_arg) {
  403. testing::StrictMock<mock_visitor<fmt::monostate>> visitor;
  404. EXPECT_CALL(visitor, visit(_));
  405. auto arg = fmt::basic_format_arg<fmt::format_context>();
  406. fmt::visit_format_arg(visitor, arg);
  407. }
  408. #if FMT_USE_CONSTEXPR
  409. enum class arg_id_result { none, empty, index, name, error };
  410. struct test_arg_id_handler {
  411. arg_id_result res = arg_id_result::none;
  412. int index = 0;
  413. string_view name;
  414. constexpr void operator()() { res = arg_id_result::empty; }
  415. constexpr void operator()(int i) {
  416. res = arg_id_result::index;
  417. index = i;
  418. }
  419. constexpr void operator()(string_view n) {
  420. res = arg_id_result::name;
  421. name = n;
  422. }
  423. constexpr void on_error(const char*) { res = arg_id_result::error; }
  424. };
  425. template <size_t N>
  426. constexpr test_arg_id_handler parse_arg_id(const char (&s)[N]) {
  427. test_arg_id_handler h;
  428. fmt::detail::parse_arg_id(s, s + N, h);
  429. return h;
  430. }
  431. TEST(format_test, constexpr_parse_arg_id) {
  432. static_assert(parse_arg_id(":").res == arg_id_result::empty, "");
  433. static_assert(parse_arg_id("}").res == arg_id_result::empty, "");
  434. static_assert(parse_arg_id("42:").res == arg_id_result::index, "");
  435. static_assert(parse_arg_id("42:").index == 42, "");
  436. static_assert(parse_arg_id("foo:").res == arg_id_result::name, "");
  437. static_assert(parse_arg_id("foo:").name.size() == 3, "");
  438. static_assert(parse_arg_id("!").res == arg_id_result::error, "");
  439. }
  440. struct test_format_specs_handler {
  441. enum result { none, hash, zero, loc, error };
  442. result res = none;
  443. fmt::align_t alignment = fmt::align::none;
  444. fmt::sign_t sign = fmt::sign::none;
  445. char fill = 0;
  446. int width = 0;
  447. fmt::detail::arg_ref<char> width_ref;
  448. int precision = 0;
  449. fmt::detail::arg_ref<char> precision_ref;
  450. fmt::presentation_type type = fmt::presentation_type::none;
  451. // Workaround for MSVC2017 bug that results in "expression did not evaluate
  452. // to a constant" with compiler-generated copy ctor.
  453. constexpr test_format_specs_handler() {}
  454. constexpr test_format_specs_handler(const test_format_specs_handler& other) =
  455. default;
  456. constexpr void on_align(fmt::align_t a) { alignment = a; }
  457. constexpr void on_fill(fmt::string_view f) { fill = f[0]; }
  458. constexpr void on_sign(fmt::sign_t s) { sign = s; }
  459. constexpr void on_hash() { res = hash; }
  460. constexpr void on_zero() { res = zero; }
  461. constexpr void on_localized() { res = loc; }
  462. constexpr void on_width(int w) { width = w; }
  463. constexpr void on_dynamic_width(fmt::detail::auto_id) {}
  464. constexpr void on_dynamic_width(int index) { width_ref = index; }
  465. constexpr void on_dynamic_width(string_view) {}
  466. constexpr void on_precision(int p) { precision = p; }
  467. constexpr void on_dynamic_precision(fmt::detail::auto_id) {}
  468. constexpr void on_dynamic_precision(int index) { precision_ref = index; }
  469. constexpr void on_dynamic_precision(string_view) {}
  470. constexpr void end_precision() {}
  471. constexpr void on_type(fmt::presentation_type t) { type = t; }
  472. constexpr void on_error(const char*) { res = error; }
  473. };
  474. template <size_t N>
  475. constexpr test_format_specs_handler parse_test_specs(const char (&s)[N]) {
  476. auto h = test_format_specs_handler();
  477. fmt::detail::parse_format_specs(s, s + N - 1, h);
  478. return h;
  479. }
  480. TEST(core_test, constexpr_parse_format_specs) {
  481. using handler = test_format_specs_handler;
  482. static_assert(parse_test_specs("<").alignment == fmt::align::left, "");
  483. static_assert(parse_test_specs("*^").fill == '*', "");
  484. static_assert(parse_test_specs("+").sign == fmt::sign::plus, "");
  485. static_assert(parse_test_specs("-").sign == fmt::sign::minus, "");
  486. static_assert(parse_test_specs(" ").sign == fmt::sign::space, "");
  487. static_assert(parse_test_specs("#").res == handler::hash, "");
  488. static_assert(parse_test_specs("0").res == handler::zero, "");
  489. static_assert(parse_test_specs("L").res == handler::loc, "");
  490. static_assert(parse_test_specs("42").width == 42, "");
  491. static_assert(parse_test_specs("{42}").width_ref.val.index == 42, "");
  492. static_assert(parse_test_specs(".42").precision == 42, "");
  493. static_assert(parse_test_specs(".{42}").precision_ref.val.index == 42, "");
  494. static_assert(parse_test_specs("d").type == fmt::presentation_type::dec, "");
  495. static_assert(parse_test_specs("{<").res == handler::error, "");
  496. }
  497. struct test_parse_context {
  498. using char_type = char;
  499. constexpr int next_arg_id() { return 11; }
  500. template <typename Id> FMT_CONSTEXPR void check_arg_id(Id) {}
  501. FMT_CONSTEXPR void check_dynamic_spec(int) {}
  502. constexpr const char* begin() { return nullptr; }
  503. constexpr const char* end() { return nullptr; }
  504. void on_error(const char*) {}
  505. };
  506. template <size_t N>
  507. constexpr fmt::detail::dynamic_format_specs<char> parse_dynamic_specs(
  508. const char (&s)[N]) {
  509. auto specs = fmt::detail::dynamic_format_specs<char>();
  510. auto ctx = test_parse_context();
  511. auto h = fmt::detail::dynamic_specs_handler<test_parse_context>(specs, ctx);
  512. parse_format_specs(s, s + N - 1, h);
  513. return specs;
  514. }
  515. TEST(format_test, constexpr_dynamic_specs_handler) {
  516. static_assert(parse_dynamic_specs("<").align == fmt::align::left, "");
  517. static_assert(parse_dynamic_specs("*^").fill[0] == '*', "");
  518. static_assert(parse_dynamic_specs("+").sign == fmt::sign::plus, "");
  519. static_assert(parse_dynamic_specs("-").sign == fmt::sign::minus, "");
  520. static_assert(parse_dynamic_specs(" ").sign == fmt::sign::space, "");
  521. static_assert(parse_dynamic_specs("#").alt, "");
  522. static_assert(parse_dynamic_specs("0").align == fmt::align::numeric, "");
  523. static_assert(parse_dynamic_specs("42").width == 42, "");
  524. static_assert(parse_dynamic_specs("{}").width_ref.val.index == 11, "");
  525. static_assert(parse_dynamic_specs("{42}").width_ref.val.index == 42, "");
  526. static_assert(parse_dynamic_specs(".42").precision == 42, "");
  527. static_assert(parse_dynamic_specs(".{}").precision_ref.val.index == 11, "");
  528. static_assert(parse_dynamic_specs(".{42}").precision_ref.val.index == 42, "");
  529. static_assert(parse_dynamic_specs("d").type == fmt::presentation_type::dec,
  530. "");
  531. }
  532. template <size_t N>
  533. constexpr test_format_specs_handler check_specs(const char (&s)[N]) {
  534. fmt::detail::specs_checker<test_format_specs_handler> checker(
  535. test_format_specs_handler(), fmt::detail::type::double_type);
  536. parse_format_specs(s, s + N - 1, checker);
  537. return checker;
  538. }
  539. TEST(format_test, constexpr_specs_checker) {
  540. using handler = test_format_specs_handler;
  541. static_assert(check_specs("<").alignment == fmt::align::left, "");
  542. static_assert(check_specs("*^").fill == '*', "");
  543. static_assert(check_specs("+").sign == fmt::sign::plus, "");
  544. static_assert(check_specs("-").sign == fmt::sign::minus, "");
  545. static_assert(check_specs(" ").sign == fmt::sign::space, "");
  546. static_assert(check_specs("#").res == handler::hash, "");
  547. static_assert(check_specs("0").res == handler::zero, "");
  548. static_assert(check_specs("42").width == 42, "");
  549. static_assert(check_specs("{42}").width_ref.val.index == 42, "");
  550. static_assert(check_specs(".42").precision == 42, "");
  551. static_assert(check_specs(".{42}").precision_ref.val.index == 42, "");
  552. static_assert(check_specs("d").type == fmt::presentation_type::dec, "");
  553. static_assert(check_specs("{<").res == handler::error, "");
  554. }
  555. struct test_format_string_handler {
  556. constexpr void on_text(const char*, const char*) {}
  557. constexpr int on_arg_id() { return 0; }
  558. template <typename T> constexpr int on_arg_id(T) { return 0; }
  559. constexpr void on_replacement_field(int, const char*) {}
  560. constexpr const char* on_format_specs(int, const char* begin, const char*) {
  561. return begin;
  562. }
  563. constexpr void on_error(const char*) { error = true; }
  564. bool error = false;
  565. };
  566. template <size_t N> constexpr bool parse_string(const char (&s)[N]) {
  567. auto h = test_format_string_handler();
  568. fmt::detail::parse_format_string<true>(fmt::string_view(s, N - 1), h);
  569. return !h.error;
  570. }
  571. TEST(format_test, constexpr_parse_format_string) {
  572. static_assert(parse_string("foo"), "");
  573. static_assert(!parse_string("}"), "");
  574. static_assert(parse_string("{}"), "");
  575. static_assert(parse_string("{42}"), "");
  576. static_assert(parse_string("{foo}"), "");
  577. static_assert(parse_string("{:}"), "");
  578. }
  579. #endif // FMT_USE_CONSTEXPR
  580. struct enabled_formatter {};
  581. struct enabled_ptr_formatter {};
  582. struct disabled_formatter {};
  583. struct disabled_formatter_convertible {
  584. operator int() const { return 42; }
  585. };
  586. FMT_BEGIN_NAMESPACE
  587. template <> struct formatter<enabled_formatter> {
  588. auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  589. return ctx.begin();
  590. }
  591. auto format(enabled_formatter, format_context& ctx) -> decltype(ctx.out()) {
  592. return ctx.out();
  593. }
  594. };
  595. template <> struct formatter<enabled_ptr_formatter*> {
  596. auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  597. return ctx.begin();
  598. }
  599. auto format(enabled_ptr_formatter*, format_context& ctx)
  600. -> decltype(ctx.out()) {
  601. return ctx.out();
  602. }
  603. };
  604. FMT_END_NAMESPACE
  605. TEST(core_test, has_formatter) {
  606. using fmt::has_formatter;
  607. using context = fmt::format_context;
  608. static_assert(has_formatter<enabled_formatter, context>::value, "");
  609. static_assert(!has_formatter<disabled_formatter, context>::value, "");
  610. static_assert(!has_formatter<disabled_formatter_convertible, context>::value,
  611. "");
  612. }
  613. struct const_formattable {};
  614. struct nonconst_formattable {};
  615. FMT_BEGIN_NAMESPACE
  616. template <> struct formatter<const_formattable> {
  617. auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  618. return ctx.begin();
  619. }
  620. auto format(const const_formattable&, format_context& ctx)
  621. -> decltype(ctx.out()) {
  622. auto test = string_view("test");
  623. return std::copy_n(test.data(), test.size(), ctx.out());
  624. }
  625. };
  626. template <> struct formatter<nonconst_formattable> {
  627. auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  628. return ctx.begin();
  629. }
  630. auto format(nonconst_formattable&, format_context& ctx)
  631. -> decltype(ctx.out()) {
  632. auto test = string_view("test");
  633. return std::copy_n(test.data(), test.size(), ctx.out());
  634. }
  635. };
  636. FMT_END_NAMESPACE
  637. struct convertible_to_pointer {
  638. operator const int*() const { return nullptr; }
  639. };
  640. struct convertible_to_pointer_formattable {
  641. operator const int*() const { return nullptr; }
  642. };
  643. FMT_BEGIN_NAMESPACE
  644. template <> struct formatter<convertible_to_pointer_formattable> {
  645. auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  646. return ctx.begin();
  647. }
  648. auto format(convertible_to_pointer_formattable, format_context& ctx) const
  649. -> decltype(ctx.out()) {
  650. auto test = string_view("test");
  651. return std::copy_n(test.data(), test.size(), ctx.out());
  652. }
  653. };
  654. FMT_END_NAMESPACE
  655. enum class unformattable_scoped_enum {};
  656. namespace test {
  657. enum class formattable_scoped_enum {};
  658. auto format_as(formattable_scoped_enum) -> int { return 42; }
  659. struct convertible_to_enum {
  660. operator formattable_scoped_enum() const { return {}; }
  661. };
  662. } // namespace test
  663. TEST(core_test, is_formattable) {
  664. #if 0
  665. // This should be enabled once corresponding map overloads are gone.
  666. static_assert(fmt::is_formattable<signed char*>::value, "");
  667. static_assert(fmt::is_formattable<unsigned char*>::value, "");
  668. static_assert(fmt::is_formattable<const signed char*>::value, "");
  669. static_assert(fmt::is_formattable<const unsigned char*>::value, "");
  670. #endif
  671. static_assert(!fmt::is_formattable<wchar_t>::value, "");
  672. #ifdef __cpp_char8_t
  673. static_assert(!fmt::is_formattable<char8_t>::value, "");
  674. #endif
  675. static_assert(!fmt::is_formattable<char16_t>::value, "");
  676. static_assert(!fmt::is_formattable<char32_t>::value, "");
  677. static_assert(!fmt::is_formattable<const wchar_t*>::value, "");
  678. static_assert(!fmt::is_formattable<const wchar_t[3]>::value, "");
  679. static_assert(!fmt::is_formattable<fmt::basic_string_view<wchar_t>>::value,
  680. "");
  681. static_assert(fmt::is_formattable<enabled_formatter>::value, "");
  682. static_assert(!fmt::is_formattable<enabled_ptr_formatter*>::value, "");
  683. static_assert(!fmt::is_formattable<disabled_formatter>::value, "");
  684. static_assert(fmt::is_formattable<disabled_formatter_convertible>::value, "");
  685. static_assert(fmt::is_formattable<const_formattable&>::value, "");
  686. static_assert(fmt::is_formattable<const const_formattable&>::value, "");
  687. static_assert(fmt::is_formattable<nonconst_formattable&>::value, "");
  688. #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910
  689. static_assert(!fmt::is_formattable<const nonconst_formattable&>::value, "");
  690. #endif
  691. static_assert(!fmt::is_formattable<convertible_to_pointer>::value, "");
  692. const auto f = convertible_to_pointer_formattable();
  693. EXPECT_EQ(fmt::format("{}", f), "test");
  694. static_assert(!fmt::is_formattable<void (*)()>::value, "");
  695. struct s;
  696. static_assert(!fmt::is_formattable<int(s::*)>::value, "");
  697. static_assert(!fmt::is_formattable<int (s::*)()>::value, "");
  698. static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, "");
  699. static_assert(fmt::is_formattable<test::formattable_scoped_enum>::value, "");
  700. static_assert(!fmt::is_formattable<test::convertible_to_enum>::value, "");
  701. }
  702. TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); }
  703. TEST(core_test, format_to) {
  704. std::string s;
  705. fmt::format_to(std::back_inserter(s), "{}", 42);
  706. EXPECT_EQ(s, "42");
  707. }
  708. TEST(core_test, format_as) {
  709. EXPECT_EQ(fmt::format("{}", test::formattable_scoped_enum()), "42");
  710. }
  711. struct convertible_to_int {
  712. operator int() const { return 42; }
  713. };
  714. struct convertible_to_c_string {
  715. operator const char*() const { return "foo"; }
  716. };
  717. FMT_BEGIN_NAMESPACE
  718. template <> struct formatter<convertible_to_int> {
  719. auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  720. return ctx.begin();
  721. }
  722. auto format(convertible_to_int, format_context& ctx) -> decltype(ctx.out()) {
  723. return std::copy_n("foo", 3, ctx.out());
  724. }
  725. };
  726. template <> struct formatter<convertible_to_c_string> {
  727. FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
  728. return ctx.begin();
  729. }
  730. auto format(convertible_to_c_string, format_context& ctx)
  731. -> decltype(ctx.out()) {
  732. return std::copy_n("bar", 3, ctx.out());
  733. }
  734. };
  735. FMT_END_NAMESPACE
  736. TEST(core_test, formatter_overrides_implicit_conversion) {
  737. EXPECT_EQ(fmt::format("{}", convertible_to_int()), "foo");
  738. EXPECT_EQ(fmt::format("{}", convertible_to_c_string()), "bar");
  739. }
  740. // Test that check is not found by ADL.
  741. template <typename T> void check(T);
  742. TEST(core_test, adl_check) {
  743. EXPECT_EQ(fmt::format("{}", test_struct()), "test");
  744. }
  745. TEST(core_test, to_string_view_foreign_strings) {
  746. using namespace test_ns;
  747. EXPECT_EQ(to_string_view(test_string<char>("42")), "42");
  748. fmt::detail::type type =
  749. fmt::detail::mapped_type_constant<test_string<char>,
  750. fmt::format_context>::value;
  751. EXPECT_EQ(type, fmt::detail::type::string_type);
  752. }
  753. struct implicitly_convertible_to_string {
  754. operator std::string() const { return "foo"; }
  755. };
  756. struct implicitly_convertible_to_string_view {
  757. operator fmt::string_view() const { return "foo"; }
  758. };
  759. TEST(core_test, format_implicitly_convertible_to_string_view) {
  760. EXPECT_EQ("foo", fmt::format("{}", implicitly_convertible_to_string_view()));
  761. }
  762. // std::is_constructible is broken in MSVC until version 2015.
  763. #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1900
  764. struct explicitly_convertible_to_string_view {
  765. explicit operator fmt::string_view() const { return "foo"; }
  766. };
  767. TEST(core_test, format_explicitly_convertible_to_string_view) {
  768. // Types explicitly convertible to string_view are not formattable by
  769. // default because it may introduce ODR violations.
  770. static_assert(
  771. !fmt::is_formattable<explicitly_convertible_to_string_view>::value, "");
  772. }
  773. # ifdef FMT_USE_STRING_VIEW
  774. struct explicitly_convertible_to_std_string_view {
  775. explicit operator std::string_view() const { return "foo"; }
  776. };
  777. TEST(core_test, format_explicitly_convertible_to_std_string_view) {
  778. // Types explicitly convertible to string_view are not formattable by
  779. // default because it may introduce ODR violations.
  780. static_assert(
  781. !fmt::is_formattable<explicitly_convertible_to_std_string_view>::value,
  782. "");
  783. }
  784. # endif
  785. #endif
  786. struct convertible_to_long_long {
  787. operator long long() const { return 1LL << 32; }
  788. };
  789. TEST(format_test, format_convertible_to_long_long) {
  790. EXPECT_EQ("100000000", fmt::format("{:x}", convertible_to_long_long()));
  791. }
  792. struct disabled_rvalue_conversion {
  793. operator const char*() const& { return "foo"; }
  794. operator const char*() & { return "foo"; }
  795. operator const char*() const&& = delete;
  796. operator const char*() && = delete;
  797. };
  798. TEST(core_test, disabled_rvalue_conversion) {
  799. EXPECT_EQ("foo", fmt::format("{}", disabled_rvalue_conversion()));
  800. }
  801. namespace adl_test {
  802. template <typename... T> void make_format_args(const T&...) = delete;
  803. struct string : std::string {};
  804. } // namespace adl_test
  805. // Test that formatting functions compile when make_format_args is found by ADL.
  806. TEST(core_test, adl) {
  807. // Only check compilation and don't run the code to avoid polluting the output
  808. // and since the output is tested elsewhere.
  809. if (fmt::detail::const_check(true)) return;
  810. auto s = adl_test::string();
  811. char buf[10];
  812. (void)fmt::format("{}", s);
  813. fmt::format_to(buf, "{}", s);
  814. fmt::format_to_n(buf, 10, "{}", s);
  815. (void)fmt::formatted_size("{}", s);
  816. fmt::print("{}", s);
  817. fmt::print(stdout, "{}", s);
  818. }
  819. TEST(core_test, has_const_formatter) {
  820. EXPECT_TRUE((fmt::detail::has_const_formatter<const_formattable,
  821. fmt::format_context>()));
  822. EXPECT_FALSE((fmt::detail::has_const_formatter<nonconst_formattable,
  823. fmt::format_context>()));
  824. }
  825. TEST(core_test, format_nonconst) {
  826. EXPECT_EQ(fmt::format("{}", nonconst_formattable()), "test");
  827. }