{ 0, 0 },
};
-const PrettySuffix kPrettyBytesSuffixes[] = {
+const PrettySuffix kPrettyBytesMetricSuffixes[] = {
+ { "TB", 1e12L },
+ { "GB", 1e9L },
+ { "MB", 1e6L },
+ { "kB", 1e3L },
+ { "B ", 0L },
+ { 0, 0 },
+};
+
+const PrettySuffix kPrettyBytesBinarySuffixes[] = {
{ "TB", int64_t(1) << 40 },
{ "GB", int64_t(1) << 30 },
{ "MB", int64_t(1) << 20 },
{ 0, 0 },
};
-const PrettySuffix kPrettyBytesMetricSuffixes[] = {
- { "TB", 1e12L },
- { "GB", 1e9L },
- { "MB", 1e6L },
- { "kB", 1e3L },
- { "B ", 0L },
+const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = {
+ { "TiB", int64_t(1) << 40 },
+ { "GiB", int64_t(1) << 30 },
+ { "MiB", int64_t(1) << 20 },
+ { "KiB", int64_t(1) << 10 },
+ { "B ", 0L },
{ 0, 0 },
};
{ 0, 0 },
};
+const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = {
+ { "Ti", int64_t(1) << 40 },
+ { "Gi", int64_t(1) << 30 },
+ { "Mi", int64_t(1) << 20 },
+ { "Ki", int64_t(1) << 10 },
+ { " ", 0 },
+ { 0, 0 },
+};
+
const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = {
kPrettyTimeSuffixes,
- kPrettyBytesSuffixes,
kPrettyBytesMetricSuffixes,
+ kPrettyBytesBinarySuffixes,
+ kPrettyBytesBinaryIECSuffixes,
kPrettyUnitsMetricSuffixes,
kPrettyUnitsBinarySuffixes,
+ kPrettyUnitsBinaryIECSuffixes,
};
} // namespace
*
* Current types are:
* PRETTY_TIME - s, ms, us, ns, etc.
- * PRETTY_BYTES - kb, MB, GB, etc (goes up by 2^10 = 1024 each time)
- * PRETTY_BYTES_METRIC - kb, MB, GB, etc (goes up by 10^3 = 1000 each time)
+ * PRETTY_BYTES_METRIC - kB, MB, GB, etc (goes up by 10^3 = 1000 each time)
+ * PRETTY_BYTES - kB, MB, GB, etc (goes up by 2^10 = 1024 each time)
+ * PRETTY_BYTES_IEC - KiB, MiB, GiB, etc
* PRETTY_UNITS_METRIC - k, M, G, etc (goes up by 10^3 = 1000 each time)
* PRETTY_UNITS_BINARY - k, M, G, etc (goes up by 2^10 = 1024 each time)
+ * PRETTY_UNITS_BINARY_IEC - Ki, Mi, Gi, etc
*
* @author Mark Rabkin <mrabkin@fb.com>
*/
enum PrettyType {
PRETTY_TIME,
- PRETTY_BYTES,
+
PRETTY_BYTES_METRIC,
+ PRETTY_BYTES_BINARY,
+ PRETTY_BYTES = PRETTY_BYTES_BINARY,
+ PRETTY_BYTES_BINARY_IEC,
+ PRETTY_BYTES_IEC = PRETTY_BYTES_BINARY_IEC,
+
PRETTY_UNITS_METRIC,
PRETTY_UNITS_BINARY,
+ PRETTY_UNITS_BINARY_IEC,
PRETTY_NUM_TYPES
};
EXPECT_EQ(string("1 GB"), prettyPrint(pow2(30), PRETTY_BYTES));
EXPECT_EQ(string("1 TB"), prettyPrint(pow2(40), PRETTY_BYTES));
+ EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("833 KiB"), prettyPrint(853.e3, PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("813.5 MiB"), prettyPrint(853.e6, PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("7.944 GiB"), prettyPrint(8.53e9, PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("794.4 GiB"), prettyPrint(853.e9, PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("775.8 TiB"), prettyPrint(853.e12, PRETTY_BYTES_IEC));
+
+ EXPECT_EQ(string("0 B "), prettyPrint(0, PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("1 B "), prettyPrint(pow2(0), PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("1 KiB"), prettyPrint(pow2(10), PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("1 MiB"), prettyPrint(pow2(20), PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("1 GiB"), prettyPrint(pow2(30), PRETTY_BYTES_IEC));
+ EXPECT_EQ(string("1 TiB"), prettyPrint(pow2(40), PRETTY_BYTES_IEC));
+
// check bytes metric printing
EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES_METRIC));
EXPECT_EQ(string("853 kB"), prettyPrint(853.e3, PRETTY_BYTES_METRIC));
EXPECT_EQ(string("1024 G"),
prettyPrint(pow2(40) - 1, PRETTY_UNITS_BINARY));
+ EXPECT_EQ(string("0 "), prettyPrint(0, PRETTY_UNITS_BINARY_IEC));
+ EXPECT_EQ(string("1 "), prettyPrint(pow2(0), PRETTY_UNITS_BINARY_IEC));
+ EXPECT_EQ(string("1 Ki"), prettyPrint(pow2(10), PRETTY_UNITS_BINARY_IEC));
+ EXPECT_EQ(string("1 Mi"), prettyPrint(pow2(20), PRETTY_UNITS_BINARY_IEC));
+ EXPECT_EQ(string("1 Gi"), prettyPrint(pow2(30), PRETTY_UNITS_BINARY_IEC));
+ EXPECT_EQ(string("1 Ti"), prettyPrint(pow2(40), PRETTY_UNITS_BINARY_IEC));
+
+ EXPECT_EQ(string("1023 "),
+ prettyPrint(pow2(10) - 1, PRETTY_UNITS_BINARY_IEC));
+ EXPECT_EQ(string("1024 Ki"),
+ prettyPrint(pow2(20) - 1, PRETTY_UNITS_BINARY_IEC));
+ EXPECT_EQ(string("1024 Mi"),
+ prettyPrint(pow2(30) - 1, PRETTY_UNITS_BINARY_IEC));
+ EXPECT_EQ(string("1024 Gi"),
+ prettyPrint(pow2(40) - 1, PRETTY_UNITS_BINARY_IEC));
+
// check that negative values work
EXPECT_EQ(string("-85.3 s "), prettyPrint(-85.3, PRETTY_TIME));
EXPECT_EQ(string("-85.3 ms"), prettyPrint(-85.3e-3, PRETTY_TIME));