format-impl-test.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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, get_cached_power) {
  175. using limits = std::numeric_limits<double>;
  176. for (auto exp = limits::min_exponent; exp <= limits::max_exponent; ++exp) {
  177. int dec_exp = 0;
  178. auto power = fmt::detail::get_cached_power(exp, dec_exp);
  179. bigint exact, cache(power.f);
  180. if (dec_exp >= 0) {
  181. exact.assign_pow10(dec_exp);
  182. if (power.e <= 0)
  183. exact <<= -power.e;
  184. else
  185. cache <<= power.e;
  186. exact.align(cache);
  187. cache.align(exact);
  188. auto exact_str = fmt::to_string(exact);
  189. auto cache_str = fmt::to_string(cache);
  190. EXPECT_EQ(exact_str.size(), cache_str.size());
  191. EXPECT_EQ(exact_str.substr(0, 15), cache_str.substr(0, 15));
  192. int diff = cache_str[15] - exact_str[15];
  193. if (diff == 1)
  194. EXPECT_GT(exact_str[16], '8');
  195. else
  196. EXPECT_EQ(diff, 0);
  197. } else {
  198. cache.assign_pow10(-dec_exp);
  199. cache *= power.f + 1; // Inexact check.
  200. exact = 1;
  201. exact <<= -power.e;
  202. exact.align(cache);
  203. auto exact_str = fmt::to_string(exact);
  204. auto cache_str = fmt::to_string(cache);
  205. EXPECT_EQ(exact_str.size(), cache_str.size());
  206. EXPECT_EQ(exact_str.substr(0, 16), cache_str.substr(0, 16));
  207. }
  208. }
  209. }
  210. TEST(fp_test, dragonbox_max_k) {
  211. using fmt::detail::dragonbox::floor_log10_pow2;
  212. using float_info = fmt::detail::dragonbox::float_info<float>;
  213. EXPECT_EQ(
  214. fmt::detail::const_check(float_info::max_k),
  215. float_info::kappa -
  216. floor_log10_pow2(std::numeric_limits<float>::min_exponent -
  217. fmt::detail::num_significand_bits<float>() - 1));
  218. using double_info = fmt::detail::dragonbox::float_info<double>;
  219. EXPECT_EQ(
  220. fmt::detail::const_check(double_info::max_k),
  221. double_info::kappa -
  222. floor_log10_pow2(std::numeric_limits<double>::min_exponent -
  223. fmt::detail::num_significand_bits<double>() - 1));
  224. }
  225. TEST(fp_test, get_round_direction) {
  226. using fmt::detail::get_round_direction;
  227. using fmt::detail::round_direction;
  228. EXPECT_EQ(get_round_direction(100, 50, 0), round_direction::down);
  229. EXPECT_EQ(get_round_direction(100, 51, 0), round_direction::up);
  230. EXPECT_EQ(get_round_direction(100, 40, 10), round_direction::down);
  231. EXPECT_EQ(get_round_direction(100, 60, 10), round_direction::up);
  232. for (size_t i = 41; i < 60; ++i)
  233. EXPECT_EQ(get_round_direction(100, i, 10), round_direction::unknown);
  234. uint64_t max = max_value<uint64_t>();
  235. EXPECT_THROW(get_round_direction(100, 100, 0), assertion_failure);
  236. EXPECT_THROW(get_round_direction(100, 0, 100), assertion_failure);
  237. EXPECT_THROW(get_round_direction(100, 0, 50), assertion_failure);
  238. // Check that remainder + error doesn't overflow.
  239. EXPECT_EQ(get_round_direction(max, max - 1, 2), round_direction::up);
  240. // Check that 2 * (remainder + error) doesn't overflow.
  241. EXPECT_EQ(get_round_direction(max, max / 2 + 1, max / 2),
  242. round_direction::unknown);
  243. // Check that remainder - error doesn't overflow.
  244. EXPECT_EQ(get_round_direction(100, 40, 41), round_direction::unknown);
  245. // Check that 2 * (remainder - error) doesn't overflow.
  246. EXPECT_EQ(get_round_direction(max, max - 1, 1), round_direction::up);
  247. }
  248. TEST(fp_test, fixed_handler) {
  249. struct handler : fmt::detail::gen_digits_handler {
  250. char buffer[10];
  251. handler(int prec = 0) : fmt::detail::gen_digits_handler() {
  252. buf = buffer;
  253. precision = prec;
  254. }
  255. };
  256. handler().on_digit('0', 100, 99, 0, false);
  257. EXPECT_THROW(handler().on_digit('0', 100, 100, 0, false), assertion_failure);
  258. namespace digits = fmt::detail::digits;
  259. EXPECT_EQ(handler(1).on_digit('0', 100, 10, 10, false), digits::error);
  260. // Check that divisor - error doesn't overflow.
  261. EXPECT_EQ(handler(1).on_digit('0', 100, 10, 101, false), digits::error);
  262. // Check that 2 * error doesn't overflow.
  263. uint64_t max = max_value<uint64_t>();
  264. EXPECT_EQ(handler(1).on_digit('0', max, 10, max - 1, false), digits::error);
  265. }
  266. TEST(fp_test, grisu_format_compiles_with_on_ieee_double) {
  267. auto buf = fmt::memory_buffer();
  268. format_float(0.42, -1, fmt::detail::float_specs(), buf);
  269. }
  270. TEST(format_impl_test, format_error_code) {
  271. std::string msg = "error 42", sep = ": ";
  272. {
  273. auto buffer = fmt::memory_buffer();
  274. format_to(fmt::appender(buffer), "garbage");
  275. fmt::detail::format_error_code(buffer, 42, "test");
  276. EXPECT_EQ(to_string(buffer), "test: " + msg);
  277. }
  278. {
  279. auto buffer = fmt::memory_buffer();
  280. auto prefix =
  281. std::string(fmt::inline_buffer_size - msg.size() - sep.size() + 1, 'x');
  282. fmt::detail::format_error_code(buffer, 42, prefix);
  283. EXPECT_EQ(msg, to_string(buffer));
  284. }
  285. int codes[] = {42, -1};
  286. for (size_t i = 0, n = sizeof(codes) / sizeof(*codes); i < n; ++i) {
  287. // Test maximum buffer size.
  288. msg = fmt::format("error {}", codes[i]);
  289. fmt::memory_buffer buffer;
  290. auto prefix =
  291. std::string(fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
  292. fmt::detail::format_error_code(buffer, codes[i], prefix);
  293. EXPECT_EQ(prefix + sep + msg, to_string(buffer));
  294. size_t size = fmt::inline_buffer_size;
  295. EXPECT_EQ(size, buffer.size());
  296. buffer.resize(0);
  297. // Test with a message that doesn't fit into the buffer.
  298. prefix += 'x';
  299. fmt::detail::format_error_code(buffer, codes[i], prefix);
  300. EXPECT_EQ(to_string(buffer), msg);
  301. }
  302. }
  303. TEST(format_impl_test, compute_width) {
  304. EXPECT_EQ(4,
  305. fmt::detail::compute_width(
  306. fmt::basic_string_view<fmt::detail::char8_type>(
  307. reinterpret_cast<const fmt::detail::char8_type*>("ёжик"))));
  308. }
  309. // Tests fmt::detail::count_digits for integer type Int.
  310. template <typename Int> void test_count_digits() {
  311. for (Int i = 0; i < 10; ++i) EXPECT_EQ(1u, fmt::detail::count_digits(i));
  312. for (Int i = 1, n = 1, end = max_value<Int>() / 10; n <= end; ++i) {
  313. n *= 10;
  314. EXPECT_EQ(fmt::detail::count_digits(n - 1), i);
  315. EXPECT_EQ(fmt::detail::count_digits(n), i + 1);
  316. }
  317. }
  318. TEST(format_impl_test, count_digits) {
  319. test_count_digits<uint32_t>();
  320. test_count_digits<uint64_t>();
  321. }
  322. #if FMT_USE_FLOAT128
  323. TEST(format_impl_test, write_float128) {
  324. auto s = std::string();
  325. fmt::detail::write<char>(std::back_inserter(s), __float128(42));
  326. EXPECT_EQ(s, "42");
  327. }
  328. #endif
  329. struct double_double {
  330. double a;
  331. double b;
  332. explicit constexpr double_double(double a_val = 0, double b_val = 0)
  333. : a(a_val), b(b_val) {}
  334. operator double() const { return a + b; }
  335. auto operator-() const -> double_double { return double_double(-a, -b); }
  336. };
  337. bool operator>=(const double_double& lhs, const double_double& rhs) {
  338. return lhs.a + lhs.b >= rhs.a + rhs.b;
  339. }
  340. struct slow_float {
  341. float value;
  342. explicit constexpr slow_float(float val = 0) : value(val) {}
  343. operator float() const { return value; }
  344. auto operator-() const -> slow_float { return slow_float(-value); }
  345. };
  346. namespace std {
  347. template <> struct is_floating_point<double_double> : std::true_type {};
  348. template <> struct numeric_limits<double_double> {
  349. // is_iec559 is true for double-double in libstdc++.
  350. static constexpr bool is_iec559 = true;
  351. static constexpr int digits = 106;
  352. };
  353. template <> struct is_floating_point<slow_float> : std::true_type {};
  354. template <> struct numeric_limits<slow_float> : numeric_limits<float> {};
  355. } // namespace std
  356. FMT_BEGIN_NAMESPACE
  357. namespace detail {
  358. template <> struct is_fast_float<slow_float> : std::false_type {};
  359. namespace dragonbox {
  360. template <> struct float_info<slow_float> {
  361. using carrier_uint = uint32_t;
  362. static const int exponent_bits = 8;
  363. };
  364. } // namespace dragonbox
  365. } // namespace detail
  366. FMT_END_NAMESPACE
  367. TEST(format_impl_test, write_double_double) {
  368. auto s = std::string();
  369. fmt::detail::write<char>(std::back_inserter(s), double_double(42), {});
  370. // Specializing is_floating_point is broken in MSVC.
  371. if (!FMT_MSC_VERSION) EXPECT_EQ(s, "42");
  372. }
  373. TEST(format_impl_test, write_dragon_even) {
  374. auto s = std::string();
  375. fmt::detail::write<char>(std::back_inserter(s), slow_float(33554450.0f), {});
  376. // Specializing is_floating_point is broken in MSVC.
  377. if (!FMT_MSC_VERSION) EXPECT_EQ(s, "33554450");
  378. }
  379. #ifdef _WIN32
  380. # include <windows.h>
  381. TEST(format_impl_test, write_console_signature) {
  382. decltype(::WriteConsoleW)* p = fmt::detail::WriteConsoleW;
  383. (void)p;
  384. }
  385. #endif
  386. // A public domain branchless UTF-8 decoder by Christopher Wellons:
  387. // https://github.com/skeeto/branchless-utf8
  388. constexpr bool unicode_is_surrogate(uint32_t c) {
  389. return c >= 0xD800U && c <= 0xDFFFU;
  390. }
  391. FMT_CONSTEXPR char* utf8_encode(char* s, uint32_t c) {
  392. if (c >= (1UL << 16)) {
  393. s[0] = static_cast<char>(0xf0 | (c >> 18));
  394. s[1] = static_cast<char>(0x80 | ((c >> 12) & 0x3f));
  395. s[2] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
  396. s[3] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  397. return s + 4;
  398. } else if (c >= (1UL << 11)) {
  399. s[0] = static_cast<char>(0xe0 | (c >> 12));
  400. s[1] = static_cast<char>(0x80 | ((c >> 6) & 0x3f));
  401. s[2] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  402. return s + 3;
  403. } else if (c >= (1UL << 7)) {
  404. s[0] = static_cast<char>(0xc0 | (c >> 6));
  405. s[1] = static_cast<char>(0x80 | ((c >> 0) & 0x3f));
  406. return s + 2;
  407. } else {
  408. s[0] = static_cast<char>(c);
  409. return s + 1;
  410. }
  411. }
  412. // Make sure it can decode every character
  413. TEST(format_impl_test, utf8_decode_decode_all) {
  414. for (uint32_t i = 0; i < 0x10ffff; i++) {
  415. if (!unicode_is_surrogate(i)) {
  416. int e;
  417. uint32_t c;
  418. char buf[8] = {0};
  419. char* end = utf8_encode(buf, i);
  420. const char* res = fmt::detail::utf8_decode(buf, &c, &e);
  421. EXPECT_EQ(end, res);
  422. EXPECT_EQ(c, i);
  423. EXPECT_EQ(e, 0);
  424. }
  425. }
  426. }
  427. // Reject everything outside of U+0000..U+10FFFF
  428. TEST(format_impl_test, utf8_decode_out_of_range) {
  429. for (uint32_t i = 0x110000; i < 0x1fffff; i++) {
  430. int e;
  431. uint32_t c;
  432. char buf[8] = {0};
  433. utf8_encode(buf, i);
  434. const char* end = fmt::detail::utf8_decode(buf, &c, &e);
  435. EXPECT_NE(e, 0);
  436. EXPECT_EQ(end - buf, 4);
  437. }
  438. }
  439. // Does it reject all surrogate halves?
  440. TEST(format_impl_test, utf8_decode_surrogate_halves) {
  441. for (uint32_t i = 0xd800; i <= 0xdfff; i++) {
  442. int e;
  443. uint32_t c;
  444. char buf[8] = {0};
  445. utf8_encode(buf, i);
  446. fmt::detail::utf8_decode(buf, &c, &e);
  447. EXPECT_NE(e, 0);
  448. }
  449. }
  450. // How about non-canonical encodings?
  451. TEST(format_impl_test, utf8_decode_non_canonical_encodings) {
  452. int e;
  453. uint32_t c;
  454. const char* end;
  455. char buf2[8] = {char(0xc0), char(0xA4)};
  456. end = fmt::detail::utf8_decode(buf2, &c, &e);
  457. EXPECT_NE(e, 0); // non-canonical len 2
  458. EXPECT_EQ(end, buf2 + 2); // non-canonical recover 2
  459. char buf3[8] = {char(0xe0), char(0x80), char(0xA4)};
  460. end = fmt::detail::utf8_decode(buf3, &c, &e);
  461. EXPECT_NE(e, 0); // non-canonical len 3
  462. EXPECT_EQ(end, buf3 + 3); // non-canonical recover 3
  463. char buf4[8] = {char(0xf0), char(0x80), char(0x80), char(0xA4)};
  464. end = fmt::detail::utf8_decode(buf4, &c, &e);
  465. EXPECT_NE(e, 0); // non-canonical encoding len 4
  466. EXPECT_EQ(end, buf4 + 4); // non-canonical recover 4
  467. }
  468. // Let's try some bogus byte sequences
  469. TEST(format_impl_test, utf8_decode_bogus_byte_sequences) {
  470. int e;
  471. uint32_t c;
  472. // Invalid first byte
  473. char buf0[4] = {char(0xff)};
  474. auto len = fmt::detail::utf8_decode(buf0, &c, &e) - buf0;
  475. EXPECT_NE(e, 0); // "bogus [ff] 0x%02x U+%04lx", e, (unsigned long)c);
  476. EXPECT_EQ(len, 1); // "bogus [ff] recovery %d", len);
  477. // Invalid first byte
  478. char buf1[4] = {char(0x80)};
  479. len = fmt::detail::utf8_decode(buf1, &c, &e) - buf1;
  480. EXPECT_NE(e, 0); // "bogus [80] 0x%02x U+%04lx", e, (unsigned long)c);
  481. EXPECT_EQ(len, 1); // "bogus [80] recovery %d", len);
  482. // Looks like a two-byte sequence but second byte is wrong
  483. char buf2[4] = {char(0xc0), char(0x0a)};
  484. len = fmt::detail::utf8_decode(buf2, &c, &e) - buf2;
  485. EXPECT_NE(e, 0); // "bogus [c0 0a] 0x%02x U+%04lx", e, (unsigned long)c
  486. EXPECT_EQ(len, 2); // "bogus [c0 0a] recovery %d", len);
  487. }