format-impl-test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Formatting library for C++ - formatting library implementation tests
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #include <algorithm>
  8. #include <cstring>
  9. // clang-format off
  10. #include "test-assert.h"
  11. // clang-format on
  12. #include "fmt/format.h"
  13. #include "gmock/gmock.h"
  14. #include "util.h"
  15. using fmt::detail::bigint;
  16. using fmt::detail::fp;
  17. using fmt::detail::max_value;
  18. static_assert(!std::is_copy_constructible<bigint>::value, "");
  19. static_assert(!std::is_copy_assignable<bigint>::value, "");
  20. TEST(bigint_test, construct) {
  21. EXPECT_EQ("", fmt::format("{}", bigint()));
  22. EXPECT_EQ("42", fmt::format("{}", bigint(0x42)));
  23. EXPECT_EQ("123456789abcedf0", fmt::format("{}", bigint(0x123456789abcedf0)));
  24. }
  25. TEST(bigint_test, compare) {
  26. bigint n1(42);
  27. bigint n2(42);
  28. EXPECT_EQ(compare(n1, n2), 0);
  29. n2 <<= 32;
  30. EXPECT_LT(compare(n1, n2), 0);
  31. bigint n3(43);
  32. EXPECT_LT(compare(n1, n3), 0);
  33. EXPECT_GT(compare(n3, n1), 0);
  34. bigint n4(42 * 0x100000001);
  35. EXPECT_LT(compare(n2, n4), 0);
  36. EXPECT_GT(compare(n4, n2), 0);
  37. }
  38. TEST(bigint_test, add_compare) {
  39. EXPECT_LT(
  40. add_compare(bigint(0xffffffff), bigint(0xffffffff), bigint(1) <<= 64), 0);
  41. EXPECT_LT(add_compare(bigint(1) <<= 32, bigint(1), bigint(1) <<= 96), 0);
  42. EXPECT_GT(add_compare(bigint(1) <<= 32, bigint(0), bigint(0xffffffff)), 0);
  43. EXPECT_GT(add_compare(bigint(0), bigint(1) <<= 32, bigint(0xffffffff)), 0);
  44. EXPECT_GT(add_compare(bigint(42), bigint(1), bigint(42)), 0);
  45. EXPECT_GT(add_compare(bigint(0xffffffff), bigint(1), bigint(0xffffffff)), 0);
  46. EXPECT_LT(add_compare(bigint(10), bigint(10), bigint(22)), 0);
  47. EXPECT_LT(add_compare(bigint(0x100000010), bigint(0x100000010),
  48. bigint(0x300000010)),
  49. 0);
  50. EXPECT_GT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
  51. bigint(0x300000000)),
  52. 0);
  53. EXPECT_EQ(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
  54. bigint(0x300000001)),
  55. 0);
  56. EXPECT_LT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
  57. bigint(0x300000002)),
  58. 0);
  59. EXPECT_LT(add_compare(bigint(0x1ffffffff), bigint(0x100000002),
  60. bigint(0x300000003)),
  61. 0);
  62. }
  63. TEST(bigint_test, shift_left) {
  64. bigint n(0x42);
  65. n <<= 0;
  66. EXPECT_EQ("42", fmt::format("{}", n));
  67. n <<= 1;
  68. EXPECT_EQ("84", fmt::format("{}", n));
  69. n <<= 25;
  70. EXPECT_EQ("108000000", fmt::format("{}", n));
  71. }
  72. TEST(bigint_test, multiply) {
  73. bigint n(0x42);
  74. EXPECT_THROW(n *= 0, assertion_failure);
  75. n *= 1;
  76. EXPECT_EQ("42", fmt::format("{}", n));
  77. n *= 2;
  78. EXPECT_EQ("84", fmt::format("{}", n));
  79. n *= 0x12345678;
  80. EXPECT_EQ("962fc95e0", fmt::format("{}", n));
  81. bigint bigmax(max_value<uint32_t>());
  82. bigmax *= max_value<uint32_t>();
  83. EXPECT_EQ("fffffffe00000001", fmt::format("{}", bigmax));
  84. bigmax.assign(max_value<uint64_t>());
  85. bigmax *= max_value<uint64_t>();
  86. EXPECT_EQ("fffffffffffffffe0000000000000001", fmt::format("{}", bigmax));
  87. }
  88. TEST(bigint_test, accumulator) {
  89. fmt::detail::accumulator acc;
  90. EXPECT_EQ(acc.lower, 0);
  91. EXPECT_EQ(acc.upper, 0);
  92. acc.upper = 12;
  93. acc.lower = 34;
  94. EXPECT_EQ(static_cast<uint32_t>(acc), 34);
  95. acc += 56;
  96. EXPECT_EQ(acc.lower, 90);
  97. acc += max_value<uint64_t>();
  98. EXPECT_EQ(acc.upper, 13);
  99. EXPECT_EQ(acc.lower, 89);
  100. acc >>= 32;
  101. EXPECT_EQ(acc.upper, 0);
  102. EXPECT_EQ(acc.lower, 13 * 0x100000000);
  103. }
  104. TEST(bigint_test, square) {
  105. bigint n0(0);
  106. n0.square();
  107. EXPECT_EQ("0", fmt::format("{}", n0));
  108. bigint n1(0x100);
  109. n1.square();
  110. EXPECT_EQ("10000", fmt::format("{}", n1));
  111. bigint n2(0xfffffffff);
  112. n2.square();
  113. EXPECT_EQ("ffffffffe000000001", fmt::format("{}", n2));
  114. bigint n3(max_value<uint64_t>());
  115. n3.square();
  116. EXPECT_EQ("fffffffffffffffe0000000000000001", fmt::format("{}", n3));
  117. bigint n4;
  118. n4.assign_pow10(10);
  119. EXPECT_EQ("2540be400", fmt::format("{}", n4));
  120. }
  121. TEST(bigint_test, divmod_assign_zero_divisor) {
  122. bigint zero(0);
  123. EXPECT_THROW(bigint(0).divmod_assign(zero), assertion_failure);
  124. EXPECT_THROW(bigint(42).divmod_assign(zero), assertion_failure);
  125. }
  126. TEST(bigint_test, divmod_assign_self) {
  127. bigint n(100);
  128. EXPECT_THROW(n.divmod_assign(n), assertion_failure);
  129. }
  130. TEST(bigint_test, divmod_assign_unaligned) {
  131. // (42 << 340) / pow(10, 100):
  132. bigint n1(42);
  133. n1 <<= 340;
  134. bigint n2;
  135. n2.assign_pow10(100);
  136. int result = n1.divmod_assign(n2);
  137. EXPECT_EQ(result, 9406);
  138. EXPECT_EQ("10f8353019583bfc29ffc8f564e1b9f9d819dbb4cf783e4507eca1539220p96",
  139. fmt::format("{}", n1));
  140. }
  141. TEST(bigint_test, divmod_assign) {
  142. // 100 / 10:
  143. bigint n1(100);
  144. int result = n1.divmod_assign(bigint(10));
  145. EXPECT_EQ(result, 10);
  146. EXPECT_EQ("0", fmt::format("{}", n1));
  147. // pow(10, 100) / (42 << 320):
  148. n1.assign_pow10(100);
  149. result = n1.divmod_assign(bigint(42) <<= 320);
  150. EXPECT_EQ(result, 111);
  151. EXPECT_EQ("13ad2594c37ceb0b2784c4ce0bf38ace408e211a7caab24308a82e8f10p96",
  152. fmt::format("{}", n1));
  153. // 42 / 100:
  154. bigint n2(42);
  155. n1.assign_pow10(2);
  156. result = n2.divmod_assign(n1);
  157. EXPECT_EQ(result, 0);
  158. EXPECT_EQ("2a", fmt::format("{}", n2));
  159. }
  160. template <bool is_iec559> void run_double_tests() {
  161. fmt::print("warning: double is not IEC559, skipping FP tests\n");
  162. }
  163. template <> void run_double_tests<true>() {
  164. // Construct from double.
  165. EXPECT_EQ(fp(1.23), fp(0x13ae147ae147aeu, -52));
  166. }
  167. TEST(fp_test, double_tests) {
  168. run_double_tests<std::numeric_limits<double>::is_iec559>();
  169. }
  170. TEST(fp_test, normalize) {
  171. const auto v = fp(0xbeef, 42);
  172. auto normalized = normalize(v);
  173. EXPECT_EQ(0xbeef000000000000, normalized.f);
  174. EXPECT_EQ(-6, normalized.e);
  175. }
  176. TEST(fp_test, multiply) {
  177. auto v = fp(123ULL << 32, 4) * fp(56ULL << 32, 7);
  178. EXPECT_EQ(v.f, 123u * 56u);
  179. EXPECT_EQ(v.e, 4 + 7 + 64);
  180. v = fp(123ULL << 32, 4) * fp(567ULL << 31, 8);
  181. EXPECT_EQ(v.f, (123 * 567 + 1u) / 2);
  182. EXPECT_EQ(v.e, 4 + 8 + 64);
  183. }
  184. TEST(fp_test, get_cached_power) {
  185. using limits = std::numeric_limits<double>;
  186. for (auto exp = limits::min_exponent; exp <= limits::max_exponent; ++exp) {
  187. int dec_exp = 0;
  188. auto fp = fmt::detail::get_cached_power(exp, dec_exp);
  189. bigint exact, cache(fp.f);
  190. if (dec_exp >= 0) {
  191. exact.assign_pow10(dec_exp);
  192. if (fp.e <= 0)
  193. exact <<= -fp.e;
  194. else
  195. cache <<= fp.e;
  196. exact.align(cache);
  197. cache.align(exact);
  198. auto exact_str = fmt::format("{}", exact);
  199. auto cache_str = fmt::format("{}", cache);
  200. EXPECT_EQ(exact_str.size(), cache_str.size());
  201. EXPECT_EQ(exact_str.substr(0, 15), cache_str.substr(0, 15));
  202. int diff = cache_str[15] - exact_str[15];
  203. if (diff == 1)
  204. EXPECT_GT(exact_str[16], '8');
  205. else
  206. EXPECT_EQ(diff, 0);
  207. } else {
  208. cache.assign_pow10(-dec_exp);
  209. cache *= fp.f + 1; // Inexact check.
  210. exact.assign(1);
  211. exact <<= -fp.e;
  212. exact.align(cache);
  213. auto exact_str = fmt::format("{}", exact);
  214. auto cache_str = fmt::format("{}", cache);
  215. EXPECT_EQ(exact_str.size(), cache_str.size());
  216. EXPECT_EQ(exact_str.substr(0, 16), cache_str.substr(0, 16));
  217. }
  218. }
  219. }
  220. TEST(fp_test, dragonbox_max_k) {
  221. using fmt::detail::dragonbox::floor_log10_pow2;
  222. using float_info = fmt::detail::dragonbox::float_info<float>;
  223. EXPECT_EQ(fmt::detail::const_check(float_info::max_k),
  224. float_info::kappa - floor_log10_pow2(float_info::min_exponent -
  225. float_info::significand_bits));
  226. using double_info = fmt::detail::dragonbox::float_info<double>;
  227. EXPECT_EQ(
  228. fmt::detail::const_check(double_info::max_k),
  229. double_info::kappa - floor_log10_pow2(double_info::min_exponent -
  230. double_info::significand_bits));
  231. }
  232. TEST(fp_test, get_round_direction) {
  233. using fmt::detail::get_round_direction;
  234. using fmt::detail::round_direction;
  235. EXPECT_EQ(round_direction::down, get_round_direction(100, 50, 0));
  236. EXPECT_EQ(round_direction::up, get_round_direction(100, 51, 0));
  237. EXPECT_EQ(round_direction::down, get_round_direction(100, 40, 10));
  238. EXPECT_EQ(round_direction::up, get_round_direction(100, 60, 10));
  239. for (size_t i = 41; i < 60; ++i)
  240. EXPECT_EQ(round_direction::unknown, get_round_direction(100, i, 10));
  241. uint64_t max = max_value<uint64_t>();
  242. EXPECT_THROW(get_round_direction(100, 100, 0), assertion_failure);
  243. EXPECT_THROW(get_round_direction(100, 0, 100), assertion_failure);
  244. EXPECT_THROW(get_round_direction(100, 0, 50), assertion_failure);
  245. // Check that remainder + error doesn't overflow.
  246. EXPECT_EQ(round_direction::up, get_round_direction(max, max - 1, 2));
  247. // Check that 2 * (remainder + error) doesn't overflow.
  248. EXPECT_EQ(round_direction::unknown,
  249. get_round_direction(max, max / 2 + 1, max / 2));
  250. // Check that remainder - error doesn't overflow.
  251. EXPECT_EQ(round_direction::unknown, get_round_direction(100, 40, 41));
  252. // Check that 2 * (remainder - error) doesn't overflow.
  253. EXPECT_EQ(round_direction::up, get_round_direction(max, max - 1, 1));
  254. }
  255. TEST(fp_test, fixed_handler) {
  256. struct handler : fmt::detail::gen_digits_handler {
  257. char buffer[10];
  258. handler(int prec = 0) : fmt::detail::gen_digits_handler() {
  259. buf = buffer;
  260. precision = prec;
  261. }
  262. };
  263. handler().on_digit('0', 100, 99, 0, false);
  264. EXPECT_THROW(handler().on_digit('0', 100, 100, 0, false), assertion_failure);
  265. namespace digits = fmt::detail::digits;
  266. EXPECT_EQ(handler(1).on_digit('0', 100, 10, 10, false), digits::error);
  267. // Check that divisor - error doesn't overflow.
  268. EXPECT_EQ(handler(1).on_digit('0', 100, 10, 101, false), digits::error);
  269. // Check that 2 * error doesn't overflow.
  270. uint64_t max = max_value<uint64_t>();
  271. EXPECT_EQ(handler(1).on_digit('0', max, 10, max - 1, false), digits::error);
  272. }
  273. TEST(fp_test, grisu_format_compiles_with_on_ieee_double) {
  274. fmt::memory_buffer buf;
  275. format_float(0.42, -1, fmt::detail::float_specs(), buf);
  276. }
  277. TEST(format_impl_test, format_error_code) {
  278. std::string msg = "error 42", sep = ": ";
  279. {
  280. fmt::memory_buffer buffer;
  281. format_to(fmt::appender(buffer), "garbage");
  282. fmt::detail::format_error_code(buffer, 42, "test");
  283. EXPECT_EQ("test: " + msg, to_string(buffer));
  284. }
  285. {
  286. fmt::memory_buffer buffer;
  287. auto prefix =
  288. std::string(fmt::inline_buffer_size - msg.size() - sep.size() + 1, 'x');
  289. fmt::detail::format_error_code(buffer, 42, prefix);
  290. EXPECT_EQ(msg, to_string(buffer));
  291. }
  292. int codes[] = {42, -1};
  293. for (size_t i = 0, n = sizeof(codes) / sizeof(*codes); i < n; ++i) {
  294. // Test maximum buffer size.
  295. msg = fmt::format("error {}", codes[i]);
  296. fmt::memory_buffer buffer;
  297. auto prefix =
  298. std::string(fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
  299. fmt::detail::format_error_code(buffer, codes[i], prefix);
  300. EXPECT_EQ(prefix + sep + msg, to_string(buffer));
  301. size_t size = fmt::inline_buffer_size;
  302. EXPECT_EQ(size, buffer.size());
  303. buffer.resize(0);
  304. // Test with a message that doesn't fit into the buffer.
  305. prefix += 'x';
  306. fmt::detail::format_error_code(buffer, codes[i], prefix);
  307. EXPECT_EQ(msg, to_string(buffer));
  308. }
  309. }
  310. TEST(format_impl_test, compute_width) {
  311. EXPECT_EQ(4,
  312. fmt::detail::compute_width(
  313. fmt::basic_string_view<fmt::detail::char8_type>(
  314. reinterpret_cast<const fmt::detail::char8_type*>("ёжик"))));
  315. }
  316. // Tests fmt::detail::count_digits for integer type Int.
  317. template <typename Int> void test_count_digits() {
  318. for (Int i = 0; i < 10; ++i) EXPECT_EQ(1u, fmt::detail::count_digits(i));
  319. for (Int i = 1, n = 1, end = max_value<Int>() / 10; n <= end; ++i) {
  320. n *= 10;
  321. EXPECT_EQ(i, fmt::detail::count_digits(n - 1));
  322. EXPECT_EQ(i + 1, fmt::detail::count_digits(n));
  323. }
  324. }
  325. TEST(format_impl_test, count_digits) {
  326. test_count_digits<uint32_t>();
  327. test_count_digits<uint64_t>();
  328. }
  329. TEST(format_impl_test, write_fallback_uintptr) {
  330. std::string s;
  331. fmt::detail::write_ptr<char>(
  332. std::back_inserter(s),
  333. fmt::detail::fallback_uintptr(reinterpret_cast<void*>(0xface)), nullptr);
  334. EXPECT_EQ(s, "0xface");
  335. }
  336. #ifdef _WIN32
  337. # include <windows.h>
  338. #endif
  339. #ifdef _WIN32
  340. TEST(format_impl_test, write_console_signature) {
  341. decltype(WriteConsoleW)* p = fmt::detail::WriteConsoleW;
  342. (void)p;
  343. }
  344. #endif