aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test-cutils.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/test-cutils.c')
-rw-r--r--tests/unit/test-cutils.c52
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();
}