core-test.cc 30 KB

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