format-impl-test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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::to_string(bigint()), "");
  22. EXPECT_EQ(fmt::to_string(bigint(0x42)), "42");
  23. EXPECT_EQ(fmt::to_string(bigint(0x123456789abcedf0)), "123456789abcedf0");
  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(fmt::to_string(n), "42");
  67. n <<= 1;
  68. EXPECT_EQ(fmt::to_string(n), "84");
  69. n <<= 25;
  70. EXPECT_EQ(fmt::to_string(n), "108000000");
  71. }
  72. TEST(bigint_test, multiply) {
  73. bigint n(0x42);
  74. EXPECT_THROW(n *= 0, assertion_failure);
  75. n *= 1;
  76. EXPECT_EQ(fmt::to_string(n), "42");
  77. n *= 2;
  78. EXPECT_EQ(fmt::to_string(n), "84");
  79. n *= 0x12345678;
  80. EXPECT_EQ(fmt::to_string(n), "962fc95e0");
  81. bigint bigmax(max_value<uint32_t>());
  82. bigmax *= max_value<uint32_t>();
  83. EXPECT_EQ(fmt::to_string(bigmax), "fffffffe00000001");
  84. const auto max64 = max_value<uint64_t>();
  85. bigmax = max64;
  86. bigmax *= max64;
  87. EXPECT_EQ(fmt::to_string(bigmax), "fffffffffffffffe0000000000000001");
  88. const auto max128 = (fmt::detail::uint128_t(max64) << 64) | max64;
  89. bigmax = max128;
  90. bigmax *= max128;
  91. EXPECT_EQ(fmt::to_string(bigmax),
  92. "fffffffffffffffffffffffffffffffe00000000000000000000000000000001");
  93. }
  94. TEST(bigint_test, square) {
  95. bigint n0(0);
  96. n0.square();
  97. EXPECT_EQ(fmt::to_string(n0), "0");
  98. bigint n1(0x100);
  99. n1.square();
  100. EXPECT_EQ(fmt::to_string(n1), "10000");
  101. bigint n2(0xfffffffff);
  102. n2.square();
  103. EXPECT_EQ(fmt::to_string(n2), "ffffffffe000000001");
  104. bigint n3(max_value<uint64_t>());
  105. n3.square();
  106. EXPECT_EQ(fmt::to_string(n3), "fffffffffffffffe0000000000000001");
  107. bigint n4;
  108. n4.assign_pow10(10);
  109. EXPECT_EQ(fmt::to_string(n4), "2540be400");
  110. }
  111. TEST(bigint_test, divmod_assign_zero_divisor) {
  112. bigint zero(0);
  113. EXPECT_THROW(bigint(0).divmod_assign(zero), assertion_failure);
  114. EXPECT_THROW(bigint(42).divmod_assign(zero), assertion_failure);
  115. }
  116. TEST(bigint_test, divmod_assign_self) {
  117. bigint n(100);
  118. EXPECT_THROW(n.divmod_assign(n), assertion_failure);
  119. }
  120. TEST(bigint_test, divmod_assign_unaligned) {
  121. // (42 << 340) / pow(10, 100):
  122. bigint n1(42);
  123. n1 <<= 340;
  124. bigint n2;
  125. n2.assign_pow10(100);
  126. int result = n1.divmod_assign(n2);
  127. EXPECT_EQ(result, 9406);
  128. EXPECT_EQ(fmt::to_string(n1),
  129. "10f8353019583bfc29ffc8f564e1b9f9d819dbb4cf783e4507eca1539220p96");
  130. }
  131. TEST(bigint_test, divmod_assign) {
  132. // 100 / 10:
  133. bigint n1(100);
  134. int result = n1.divmod_assign(bigint(10));
  135. EXPECT_EQ(result, 10);
  136. EXPECT_EQ(fmt::to_string(n1), "0");
  137. // pow(10, 100) / (42 << 320):
  138. n1.assign_pow10(100);
  139. result = n1.divmod_assign(bigint(42) <<= 320);
  140. EXPECT_EQ(result, 111);
  141. EXPECT_EQ(fmt::to_string(n1),
  142. "13ad2594c37ceb0b2784c4ce0bf38ace408e211a7caab24308a82e8f10p96");
  143. // 42 / 100:
  144. bigint n2(42);
  145. n1.assign_pow10(2);
  146. result = n2.divmod_assign(n1);
  147. EXPECT_EQ(result, 0);
  148. EXPECT_EQ(fmt::to_string(n2), "2a");
  149. }
  150. template <bool is_iec559> void run_double_tests() {
  151. fmt::print("warning: double is not IEC559, skipping FP tests\n");
  152. }
  153. template <> void run_double_tests<true>() {
  154. // Construct from double.
  155. EXPECT_EQ(fp(1.23), fp(0x13ae147ae147aeu, -52));
  156. }
  157. TEST(fp_test, double_tests) {
  158. run_double_tests<std::numeric_limits<double>::is_iec559>();
  159. }
  160. TEST(fp_test, normalize) {
  161. const auto v = fp(0xbeef, 42);
  162. auto normalized = normalize(v);
  163. EXPECT_EQ(normalized.f, 0xbeef000000000000);
  164. EXPECT_EQ(normalized.e, -6);
  165. }
  166. TEST(fp_test, multiply) {
  167. auto v = fp(123ULL << 32, 4) * fp(56ULL << 32, 7);
  168. EXPECT_EQ(v.f, 123u * 56u);
  169. EXPECT_EQ(v.e, 4 + 7 + 64);
  170. v = fp(123ULL << 32, 4) * fp(567ULL << 31, 8);
  171. EXPECT_EQ(v.f, (123 * 567 + 1u) / 2);
  172. EXPECT_EQ(v.e, 4 + 8 + 64);
  173. }
  174. TEST(fp_test, dragonbox_max_k) {
  175. using fmt::detail::dragonbox::floor_log10_pow2;
  176. using float_info = fmt::detail::dragonbox::float_info<float>;
  177. EXPECT_EQ(
  178. fmt::detail::const_check(float_info::max_k),
  179. float_info::kappa -
  180. floor_log10_pow2(std::numeric_limits<float>::min_exponent -
  181. fmt::detail::num_significand_bits<float>() - 1));
  182. using double_info = fmt::detail::dragonbox::float_info<double>;
  183. EXPECT_EQ(fmt::detail::const_check(double_info::max_k),
  184. double_info::kappa -
  185. floor_log10_pow2(
  186. std::numeric_limits<double>::min_exponent -
  187. 2 * fmt::detail::num_significand_bits<double>() - 1));
  188. }
  189. TEST(format_impl_test, format_error_code) {
  190. std::string msg = "error 42", sep = ": ";
  191. {
  192. auto buffer = fmt::memory_buffer();
  193. fmt::format_to(fmt::appender(buffer), "garbage");
  194. fmt::detail::format_error_code(buffer, 42, "test");
  195. EXPECT_EQ(to_string(buffer), "test: " + msg);
  196. }
  197. {
  198. auto buffer = fmt::memory_buffer();
  199. auto prefix =
  200. std::string(fmt::inline_buffer_size - msg.size() - sep.size() + 1, 'x');
  201. fmt::detail::format_error_code(buffer, 42, prefix);
  202. EXPECT_EQ(msg, to_string(buffer));
  203. }
  204. int codes[] = {42, -1};
  205. for (size_t i = 0, n = sizeof(codes) / sizeof(*codes); i < n; ++i) {
  206. // Test maximum buffer size.
  207. msg = fmt::format("error {}", codes[i]);
  208. fmt::memory_buffer buffer;
  209. auto prefix =
  210. std::string(fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
  211. fmt::detail::format_error_code(buffer, codes[i], prefix);
  212. EXPECT_EQ(prefix + sep + msg, to_string(buffer));
  213. size_t size = fmt::inline_buffer_size;
  214. EXPECT_EQ(size, buffer.size());
  215. buffer.resize(0);
  216. // Test with a message that doesn't fit into the buffer.
  217. prefix += 'x';
  218. fmt::detail::format_error_code(buffer, codes[i], prefix);
  219. EXPECT_EQ(to_string(buffer), msg);
  220. }
  221. }
  222. // Tests fmt::detail::count_digits for integer type Int.
  223. template <typename Int> void test_count_digits() {
  224. for (Int i = 0; i < 10; ++i) EXPECT_EQ(1u, fmt::detail::count_digits(i));
  225. for (Int i = 1, n = 1, end = max_value<Int>() / 10; n <= end; ++i) {
  226. n *= 10;
  227. EXPECT_EQ(fmt::detail::count_digits(n - 1), i);
  228. EXPECT_EQ(fmt::detail::count_digits(n), i + 1);
  229. }
  230. }
  231. TEST(format_impl_test, count_digits) {
  232. test_count_digits<uint32_t>();
  233. test_count_digits<uint64_t>();
  234. }
  235. TEST(format_impl_test, countl_zero) {
  236. constexpr auto num_bits = fmt::detail::num_bits<uint32_t>();
  237. uint32_t n = 1u;
  238. for (int i = 1; i < num_bits - 1; i++) {
  239. n <<= 1;
  240. EXPECT_EQ(fmt::detail::countl_zero(n - 1), num_bits - i);
  241. EXPECT_EQ(fmt::detail::countl_zero(n), num_bits - i - 1);
  242. }
  243. }
  244. #if FMT_USE_FLOAT128
  245. TEST(format_impl_test, write_float128) {
  246. auto s = std::string();
  247. fmt::detail::write<char>(std::back_inserter(s), __float128(42));
  248. EXPECT_EQ(s, "42");
  249. }
  250. #endif
  251. struct double_double {
  252. double a;
  253. double b;
  254. constexpr explicit double_double(double a_val = 0, double b_val = 0)
  255. : a(a_val), b(b_val) {}
  256. operator double() const { return a + b; }
  257. auto operator-() const -> double_double { return double_double(-a, -b); }
  258. };
  259. auto format_as(double_double d) -> double { return d; }
  260. bool operator>=(const double_double& lhs, const double_double& rhs) {
  261. return lhs.a + lhs.b >= rhs.a + rhs.b;
  262. }
  263. struct slow_float {
  264. float value;
  265. constexpr explicit slow_float(float val = 0) : value(val) {}
  266. operator float() const { return value; }
  267. auto operator-() const -> slow_float { return slow_float(-value); }
  268. };
  269. auto format_as(slow_float f) -> float { return f; }
  270. namespace std {
  271. template <> struct is_floating_point<double_double> : std::true_type {};
  272. template <> struct numeric_limits<double_double> {
  273. // is_iec559 is true for double-double in libstdc++.
  274. static constexpr bool is_iec559 = true;
  275. static constexpr int digits = 106;
  276. static constexpr int digits10 = 33;
  277. };
  278. template <> struct is_floating_point<slow_float> : std::true_type {};
  279. template <> struct numeric_limits<slow_float> : numeric_limits<float> {};
  280. } // namespace std
  281. FMT_BEGIN_NAMESPACE
  282. namespace detail {
  283. template <> struct is_fast_float<slow_float> : std::false_type {};
  284. namespace dragonbox {
  285. template <> struct float_info<slow_float> {
  286. using carrier_uint = uint32_t;
  287. static const int exponent_bits = 8;
  288. };
  289. } // namespace dragonbox
  290. } // namespace detail
  291. FMT_END_NAMESPACE
  292. TEST(format_impl_test, write_double_double) {
  293. auto s = std::string();
  294. fmt::detail::write<char>(std::back_inserter(s), double_double(42), {});
  295. // Specializing is_floating_point is broken in MSVC.
  296. if (!FMT_MSC_VERSION) EXPECT_EQ(s, "42");
  297. }
  298. TEST(format_impl_test, write_dragon_even) {
  299. auto s = std::string();
  300. fmt::detail::write<char>(std::back_inserter(s), slow_float(33554450.0f), {});
  301. // Specializing is_floating_point is broken in MSVC.
  302. if (!FMT_MSC_VERSION) EXPECT_EQ(s, "3.355445e+07");
  303. }
  304. #if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE)
  305. # include <windows.h>
  306. TEST(format_impl_test, write_console_signature) {
  307. decltype(::WriteConsoleW)* p = fmt::detail::WriteConsoleW;
  308. (void)p;
  309. }
  310. #endif
  311. // A public domain branchless UTF-8 decoder by Christopher Wellons:
  312. // https://github.com/skeeto/branchless-utf8
  313. constexpr bool unicode_is_surrogate(uint32_t c) {
  314. return c >= 0xD800U && c <= 0xDFFFU;
  315. }
  316. FMT_CONSTEXPR char* utf8_encode(char* s, uint32_t c) {
  317. if (c >= (1UL << 16)) {
  318. s[0] = static_cast<char>(0xf0 | (c >> 18));
  319. s[1] = static_cast<char>(0x80 | ((c >> 12) & 0x3f));
  320. s[2] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
  321. s[3] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  322. return s + 4;
  323. } else if (c >= (1UL << 11)) {
  324. s[0] = static_cast<char>(0xe0 | (c >> 12));
  325. s[1] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
  326. s[2] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  327. return s + 3;
  328. } else if (c >= (1UL << 7)) {
  329. s[0] = static_cast<char>(0xc0 | (c >> 6));
  330. s[1] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  331. return s + 2;
  332. } else {
  333. s[0] = static_cast<char>(c);
  334. return s + 1;
  335. }
  336. }
  337. // Make sure it can decode every character
  338. TEST(format_impl_test, utf8_decode_decode_all) {
  339. for (uint32_t i = 0; i < 0x10ffff; i++) {
  340. if (!unicode_is_surrogate(i)) {
  341. int e;
  342. uint32_t c;
  343. char buf[8] = {0};
  344. char* end = utf8_encode(buf, i);
  345. const char* res = fmt::detail::utf8_decode(buf, &c, &e);
  346. EXPECT_EQ(end, res);
  347. EXPECT_EQ(c, i);
  348. EXPECT_EQ(e, 0);
  349. }
  350. }
  351. }
  352. // Reject everything outside of U+0000..U+10FFFF
  353. TEST(format_impl_test, utf8_decode_out_of_range) {
  354. for (uint32_t i = 0x110000; i < 0x1fffff; i++) {
  355. int e;
  356. uint32_t c;
  357. char buf[8] = {0};
  358. utf8_encode(buf, i);
  359. const char* end = fmt::detail::utf8_decode(buf, &c, &e);
  360. EXPECT_NE(e, 0);
  361. EXPECT_EQ(end - buf, 4);
  362. }
  363. }
  364. // Does it reject all surrogate halves?
  365. TEST(format_impl_test, utf8_decode_surrogate_halves) {
  366. for (uint32_t i = 0xd800; i <= 0xdfff; i++) {
  367. int e;
  368. uint32_t c;
  369. char buf[8] = {0};
  370. utf8_encode(buf, i);
  371. fmt::detail::utf8_decode(buf, &c, &e);
  372. EXPECT_NE(e, 0);
  373. }
  374. }
  375. // How about non-canonical encodings?
  376. TEST(format_impl_test, utf8_decode_non_canonical_encodings) {
  377. int e;
  378. uint32_t c;
  379. const char* end;
  380. char buf2[8] = {char(0xc0), char(0xA4)};
  381. end = fmt::detail::utf8_decode(buf2, &c, &e);
  382. EXPECT_NE(e, 0); // non-canonical len 2
  383. EXPECT_EQ(end, buf2 + 2); // non-canonical recover 2
  384. char buf3[8] = {char(0xe0), char(0x80), char(0xA4)};
  385. end = fmt::detail::utf8_decode(buf3, &c, &e);
  386. EXPECT_NE(e, 0); // non-canonical len 3
  387. EXPECT_EQ(end, buf3 + 3); // non-canonical recover 3
  388. char buf4[8] = {char(0xf0), char(0x80), char(0x80), char(0xA4)};
  389. end = fmt::detail::utf8_decode(buf4, &c, &e);
  390. EXPECT_NE(e, 0); // non-canonical encoding len 4
  391. EXPECT_EQ(end, buf4 + 4); // non-canonical recover 4
  392. }
  393. // Let's try some bogus byte sequences
  394. TEST(format_impl_test, utf8_decode_bogus_byte_sequences) {
  395. int e;
  396. uint32_t c;
  397. // Invalid first byte
  398. char buf0[4] = {char(0xff)};
  399. auto len = fmt::detail::utf8_decode(buf0, &c, &e) - buf0;
  400. EXPECT_NE(e, 0); // "bogus [ff] 0x%02x U+%04lx", e, (unsigned long)c);
  401. EXPECT_EQ(len, 1); // "bogus [ff] recovery %d", len);
  402. // Invalid first byte
  403. char buf1[4] = {char(0x80)};
  404. len = fmt::detail::utf8_decode(buf1, &c, &e) - buf1;
  405. EXPECT_NE(e, 0); // "bogus [80] 0x%02x U+%04lx", e, (unsigned long)c);
  406. EXPECT_EQ(len, 1); // "bogus [80] recovery %d", len);
  407. // Looks like a two-byte sequence but second byte is wrong
  408. char buf2[4] = {char(0xc0), char(0x0a)};
  409. len = fmt::detail::utf8_decode(buf2, &c, &e) - buf2;
  410. EXPECT_NE(e, 0); // "bogus [c0 0a] 0x%02x U+%04lx", e, (unsigned long)c
  411. EXPECT_EQ(len, 2); // "bogus [c0 0a] recovery %d", len);
  412. }
  413. TEST(format_impl_test, to_utf8) {
  414. auto s = std::string("ёжик");
  415. auto u = fmt::detail::to_utf8<wchar_t>(L"\x0451\x0436\x0438\x043A");
  416. EXPECT_EQ(s, u.str());
  417. EXPECT_EQ(s.size(), u.size());
  418. }