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