@@ -2450,6 +2450,22 @@ static void test_qemu_strtosz_metric(void)
g_assert(endptr == str + 7);
}
+static void test_freq_to_str(void)
+{
+ g_assert_cmpstr(freq_to_str(999), ==, "999 Hz");
+ g_assert_cmpstr(freq_to_str(1000), ==, "1 kHz");
+ g_assert_cmpstr(freq_to_str(1010), ==, "1.01 kHz");
+}
+
+static void test_size_to_str(void)
+{
+ g_assert_cmpstr(size_to_str(0), ==, "0 B");
+ g_assert_cmpstr(size_to_str(1), ==, "1 B");
+ g_assert_cmpstr(size_to_str(1016), ==, "0.992 kiB");
+ g_assert_cmpstr(size_to_str(1024), ==, "1 kiB");
+ g_assert_cmpstr(size_to_str(512ull << 20), ==, "512 MiB");
+}
+
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
@@ -2729,5 +2745,9 @@ int main(int argc, char **argv)
g_test_add_func("/cutils/strtosz/metric",
test_qemu_strtosz_metric);
+ g_test_add_func("/cutils/size_to_str",
+ test_size_to_str);
+ g_test_add_func("/cutils/freq_to_str",
+ test_freq_to_str);
return g_test_run();
}
@@ -875,12 +875,12 @@ int parse_debug_env(const char *name, int max, int initial)
/*
* Return human readable string for size @val.
* @val can be anything that uint64_t allows (no more than "16 EiB").
- * Use IEC binary units like KiB, MiB, and so forth.
+ * Use IEC binary units like kiB, MiB, and so forth.
* Caller is responsible for passing it to g_free().
*/
char *size_to_str(uint64_t val)
{
- static const char *suffixes[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
+ static const char *suffixes[] = { "", "ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
uint64_t div;
int i;
@@ -899,7 +899,7 @@ char *size_to_str(uint64_t val)
char *freq_to_str(uint64_t freq_hz)
{
- static const char *const suffixes[] = { "", "K", "M", "G", "T", "P", "E" };
+ static const char *const suffixes[] = { "", "k", "M", "G", "T", "P", "E" };
double freq = freq_hz;
size_t idx = 0;
The correct abbreviations use a lowercase k, so adjust freq_to_str and size_to_str accordingly and add tests. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> --- tests/unit/test-cutils.c | 20 ++++++++++++++++++++ util/cutils.c | 6 +++--- 2 files changed, 23 insertions(+), 3 deletions(-)