diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2022-05-25 15:38:48 +0200 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2022-06-14 16:50:30 +0200 |
commit | cfb344892209783a600e80053dba1cfeee4bd16a (patch) | |
tree | d6ef46f71c8b219d10b1f757da873c63fd674932 /tests/unit/test-cutils.c | |
parent | 467ef823d83ed7ba68cc92e1a23938726b8c4e9d (diff) |
cutils: add functions for IEC and SI prefixes
Extract the knowledge of IEC and SI prefixes out of size_to_str and
freq_to_str, so that it can be reused when printing statistics.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tests/unit/test-cutils.c')
-rw-r--r-- | tests/unit/test-cutils.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/unit/test-cutils.c b/tests/unit/test-cutils.c index 98671f1ac3..f5b780f012 100644 --- a/tests/unit/test-cutils.c +++ b/tests/unit/test-cutils.c @@ -2450,6 +2450,50 @@ 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"); +} + +static void test_iec_binary_prefix(void) +{ + g_assert_cmpstr(iec_binary_prefix(0), ==, ""); + g_assert_cmpstr(iec_binary_prefix(10), ==, "Ki"); + g_assert_cmpstr(iec_binary_prefix(20), ==, "Mi"); + g_assert_cmpstr(iec_binary_prefix(30), ==, "Gi"); + g_assert_cmpstr(iec_binary_prefix(40), ==, "Ti"); + g_assert_cmpstr(iec_binary_prefix(50), ==, "Pi"); + g_assert_cmpstr(iec_binary_prefix(60), ==, "Ei"); +} + +static void test_si_prefix(void) +{ + g_assert_cmpstr(si_prefix(-18), ==, "a"); + g_assert_cmpstr(si_prefix(-15), ==, "f"); + g_assert_cmpstr(si_prefix(-12), ==, "p"); + g_assert_cmpstr(si_prefix(-9), ==, "n"); + g_assert_cmpstr(si_prefix(-6), ==, "u"); + g_assert_cmpstr(si_prefix(-3), ==, "m"); + g_assert_cmpstr(si_prefix(0), ==, ""); + g_assert_cmpstr(si_prefix(3), ==, "K"); + g_assert_cmpstr(si_prefix(6), ==, "M"); + g_assert_cmpstr(si_prefix(9), ==, "G"); + g_assert_cmpstr(si_prefix(12), ==, "T"); + g_assert_cmpstr(si_prefix(15), ==, "P"); + g_assert_cmpstr(si_prefix(18), ==, "E"); +} + int main(int argc, char **argv) { g_test_init(&argc, &argv, NULL); @@ -2729,5 +2773,13 @@ 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); + g_test_add_func("/cutils/iec_binary_prefix", + test_iec_binary_prefix); + g_test_add_func("/cutils/si_prefix", + test_si_prefix); return g_test_run(); } |