format-impl-test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. TEST(format_impl_test, compute_width) {
  223. EXPECT_EQ(4,
  224. fmt::detail::compute_width(
  225. fmt::basic_string_view<fmt::detail::char8_type>(
  226. reinterpret_cast<const fmt::detail::char8_type*>("ёжик"))));
  227. }
  228. // Tests fmt::detail::count_digits for integer type Int.
  229. template <typename Int> void test_count_digits() {
  230. for (Int i = 0; i < 10; ++i) EXPECT_EQ(1u, fmt::detail::count_digits(i));
  231. for (Int i = 1, n = 1, end = max_value<Int>() / 10; n <= end; ++i) {
  232. n *= 10;
  233. EXPECT_EQ(fmt::detail::count_digits(n - 1), i);
  234. EXPECT_EQ(fmt::detail::count_digits(n), i + 1);
  235. }
  236. }
  237. TEST(format_impl_test, count_digits) {
  238. test_count_digits<uint32_t>();
  239. test_count_digits<uint64_t>();
  240. }
  241. TEST(format_impl_test, countl_zero) {
  242. constexpr auto num_bits = fmt::detail::num_bits<uint32_t>();
  243. uint32_t n = 1u;
  244. for (int i = 1; i < num_bits - 1; i++) {
  245. n <<= 1;
  246. EXPECT_EQ(fmt::detail::countl_zero(n - 1), num_bits - i);
  247. EXPECT_EQ(fmt::detail::countl_zero(n), num_bits - i - 1);
  248. }
  249. }
  250. #if FMT_USE_FLOAT128
  251. TEST(format_impl_test, write_float128) {
  252. auto s = std::string();
  253. fmt::detail::write<char>(std::back_inserter(s), __float128(42));
  254. EXPECT_EQ(s, "42");
  255. }
  256. #endif
  257. struct double_double {
  258. double a;
  259. double b;
  260. explicit constexpr double_double(double a_val = 0, double b_val = 0)
  261. : a(a_val), b(b_val) {}
  262. operator double() const { return a + b; }
  263. auto operator-() const -> double_double { return double_double(-a, -b); }
  264. };
  265. auto format_as(double_double d) -> double { return d; }
  266. bool operator>=(const double_double& lhs, const double_double& rhs) {
  267. return lhs.a + lhs.b >= rhs.a + rhs.b;
  268. }
  269. struct slow_float {
  270. float value;
  271. explicit constexpr slow_float(float val = 0) : value(val) {}
  272. operator float() const { return value; }
  273. auto operator-() const -> slow_float { return slow_float(-value); }
  274. };
  275. auto format_as(slow_float f) -> float { return f; }
  276. namespace std {
  277. template <> struct is_floating_point<double_double> : std::true_type {};
  278. template <> struct numeric_limits<double_double> {
  279. // is_iec559 is true for double-double in libstdc++.
  280. static constexpr bool is_iec559 = true;
  281. static constexpr int digits = 106;
  282. };
  283. template <> struct is_floating_point<slow_float> : std::true_type {};
  284. template <> struct numeric_limits<slow_float> : numeric_limits<float> {};
  285. } // namespace std
  286. FMT_BEGIN_NAMESPACE
  287. namespace detail {
  288. template <> struct is_fast_float<slow_float> : std::false_type {};
  289. namespace dragonbox {
  290. template <> struct float_info<slow_float> {
  291. using carrier_uint = uint32_t;
  292. static const int exponent_bits = 8;
  293. };
  294. } // namespace dragonbox
  295. } // namespace detail
  296. FMT_END_NAMESPACE
  297. TEST(format_impl_test, write_double_double) {
  298. auto s = std::string();
  299. fmt::detail::write<char>(std::back_inserter(s), double_double(42), {});
  300. // Specializing is_floating_point is broken in MSVC.
  301. if (!FMT_MSC_VERSION) EXPECT_EQ(s, "42");
  302. }
  303. TEST(format_impl_test, write_dragon_even) {
  304. auto s = std::string();
  305. fmt::detail::write<char>(std::back_inserter(s), slow_float(33554450.0f), {});
  306. // Specializing is_floating_point is broken in MSVC.
  307. if (!FMT_MSC_VERSION) EXPECT_EQ(s, "33554450");
  308. }
  309. #ifdef _WIN32
  310. # include <windows.h>
  311. TEST(format_impl_test, write_console_signature) {
  312. decltype(::WriteConsoleW)* p = fmt::detail::WriteConsoleW;
  313. (void)p;
  314. }
  315. #endif
  316. // A public domain branchless UTF-8 decoder by Christopher Wellons:
  317. // https://github.com/skeeto/branchless-utf8
  318. constexpr bool unicode_is_surrogate(uint32_t c) {
  319. return c >= 0xD800U && c <= 0xDFFFU;
  320. }
  321. FMT_CONSTEXPR char* utf8_encode(char* s, uint32_t c) {
  322. if (c >= (1UL << 16)) {
  323. s[0] = static_cast<char>(0xf0 | (c >> 18));
  324. s[1] = static_cast<char>(0x80 | ((c >> 12) & 0x3f));
  325. s[2] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
  326. s[3] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  327. return s + 4;
  328. } else if (c >= (1UL << 11)) {
  329. s[0] = static_cast<char>(0xe0 | (c >> 12));
  330. s[1] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
  331. s[2] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  332. return s + 3;
  333. } else if (c >= (1UL << 7)) {
  334. s[0] = static_cast<char>(0xc0 | (c >> 6));
  335. s[1] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  336. return s + 2;
  337. } else {
  338. s[0] = static_cast<char>(c);
  339. return s + 1;
  340. }
  341. }
  342. // Make sure it can decode every character
  343. TEST(format_impl_test, utf8_decode_decode_all) {
  344. for (uint32_t i = 0; i < 0x10ffff; i++) {
  345. if (!unicode_is_surrogate(i)) {
  346. int e;
  347. uint32_t c;
  348. char buf[8] = {0};
  349. char* end = utf8_encode(buf, i);
  350. const char* res = fmt::detail::utf8_decode(buf, &c, &e);
  351. EXPECT_EQ(end, res);
  352. EXPECT_EQ(c, i);
  353. EXPECT_EQ(e, 0);
  354. }
  355. }
  356. }
  357. // Reject everything outside of U+0000..U+10FFFF
  358. TEST(format_impl_test, utf8_decode_out_of_range) {
  359. for (uint32_t i = 0x110000; i < 0x1fffff; i++) {
  360. int e;
  361. uint32_t c;
  362. char buf[8] = {0};
  363. utf8_encode(buf, i);
  364. const char* end = fmt::detail::utf8_decode(buf, &c, &e);
  365. EXPECT_NE(e, 0);
  366. EXPECT_EQ(end - buf, 4);
  367. }
  368. }
  369. // Does it reject all surrogate halves?
  370. TEST(format_impl_test, utf8_decode_surrogate_halves) {
  371. for (uint32_t i = 0xd800; i <= 0xdfff; i++) {
  372. int e;
  373. uint32_t c;
  374. char buf[8] = {0};
  375. utf8_encode(buf, i);
  376. fmt::detail::utf8_decode(buf, &c, &e);
  377. EXPECT_NE(e, 0);
  378. }
  379. }
  380. // How about non-canonical encodings?
  381. TEST(format_impl_test, utf8_decode_non_canonical_encodings) {
  382. int e;
  383. uint32_t c;
  384. const char* end;
  385. char buf2[8] = {char(0xc0), char(0xA4)};
  386. end = fmt::detail::utf8_decode(buf2, &c, &e);
  387. EXPECT_NE(e, 0); // non-canonical len 2
  388. EXPECT_EQ(end, buf2 + 2); // non-canonical recover 2
  389. char buf3[8] = {char(0xe0), char(0x80), char(0xA4)};
  390. end = fmt::detail::utf8_decode(buf3, &c, &e);
  391. EXPECT_NE(e, 0); // non-canonical len 3
  392. EXPECT_EQ(end, buf3 + 3); // non-canonical recover 3
  393. char buf4[8] = {char(0xf0), char(0x80), char(0x80), char(0xA4)};
  394. end = fmt::detail::utf8_decode(buf4, &c, &e);
  395. EXPECT_NE(e, 0); // non-canonical encoding len 4
  396. EXPECT_EQ(end, buf4 + 4); // non-canonical recover 4
  397. }
  398. // Let's try some bogus byte sequences
  399. TEST(format_impl_test, utf8_decode_bogus_byte_sequences) {
  400. int e;
  401. uint32_t c;
  402. // Invalid first byte
  403. char buf0[4] = {char(0xff)};
  404. auto len = fmt::detail::utf8_decode(buf0, &c, &e) - buf0;
  405. EXPECT_NE(e, 0); // "bogus [ff] 0x%02x U+%04lx", e, (unsigned long)c);
  406. EXPECT_EQ(len, 1); // "bogus [ff] recovery %d", len);
  407. // Invalid first byte
  408. char buf1[4] = {char(0x80)};
  409. len = fmt::detail::utf8_decode(buf1, &c, &e) - buf1;
  410. EXPECT_NE(e, 0); // "bogus [80] 0x%02x U+%04lx", e, (unsigned long)c);
  411. EXPECT_EQ(len, 1); // "bogus [80] recovery %d", len);
  412. // Looks like a two-byte sequence but second byte is wrong
  413. char buf2[4] = {char(0xc0), char(0x0a)};
  414. len = fmt::detail::utf8_decode(buf2, &c, &e) - buf2;
  415. EXPECT_NE(e, 0); // "bogus [c0 0a] 0x%02x U+%04lx", e, (unsigned long)c
  416. EXPECT_EQ(len, 2); // "bogus [c0 0a] recovery %d", len);
  417. }
  418. TEST(format_impl_test, to_utf8) {
  419. auto s = std::string("ёжик");
  420. auto u = fmt::detail::to_utf8<wchar_t>(L"\x0451\x0436\x0438\x043A");
  421. EXPECT_EQ(s, u.str());
  422. EXPECT_EQ(s.size(), u.size());
  423. }