utils: add fmt formatter for pretty printers

add fmt formatter for `utils::pretty_printed_data_size` and
`utils::pretty_printed_throughput`.

this is a part of a series to migrating from `operator<<(ostream&, ..)`
based formatting to fmtlib based formatting. the goal here is to enable
fmtlib to print `utils::pretty_printed_data_size` and
`utils::pretty_printed_throughput` without the help of `operator<<`.

please note, despite that it's more popular to use the IEC prefixes
when presenting the size of storage, i.e., MiB for 1024**2 bytes instead
of MB for 1000**2 bytes, we are still using the SI binary prefixes as
the default binary prefix, in order to preserve the existing behavior.

also, we use the singular form of "byte" when formating "1". this is
more correct.

the tests are updated accordingly.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
This commit is contained in:
Kefu Chai
2023-07-17 13:28:08 +08:00
parent 567b453689
commit fc6b84ec1f
3 changed files with 147 additions and 16 deletions

View File

@@ -8,6 +8,7 @@
#define BOOST_TEST_MODULE utils
#include <iterator>
#include <sstream>
#include <boost/test/unit_test.hpp>
#include "utils/pretty_printers.hh"
@@ -18,6 +19,7 @@ BOOST_AUTO_TEST_CASE(test_print_data_size) {
std::string_view formatted;
} sizes[] = {
{0ULL, "0 bytes"},
{1ULL, "1 byte"},
{42ULL, "42 bytes"},
{10'000ULL, "10kB"},
{10'000'000ULL, "10MB"},
@@ -31,6 +33,10 @@ BOOST_AUTO_TEST_CASE(test_print_data_size) {
out << utils::pretty_printed_data_size{n};
auto actual = out.str();
BOOST_CHECK_EQUAL(actual, expected);
std::string s;
fmt::format_to(std::back_inserter(s), "{}", utils::pretty_printed_data_size{n});
BOOST_CHECK_EQUAL(s, expected);
}
}
@@ -52,5 +58,9 @@ BOOST_AUTO_TEST_CASE(test_print_throughput) {
out << utils::pretty_printed_throughput{n, std::chrono::duration<float>(seconds)};
auto actual = out.str();
BOOST_CHECK_EQUAL(actual, expected);
std::string s;
fmt::format_to(std::back_inserter(s), "{}", utils::pretty_printed_throughput{n, std::chrono::duration<float>(seconds)});
BOOST_CHECK_EQUAL(s, expected);
}
}