diff options
author | Marc-André Lureau <marcandre.lureau@redhat.com> | 2015-09-16 18:02:57 +0200 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2015-09-25 12:04:41 +0200 |
commit | fe8545386726a2b1f8af409bcd5ea3d33218af54 (patch) | |
tree | bdec8aa3fb0d932d9f738d4c5a4c008c88f8961c /tests | |
parent | 4677bb40f809394bef5fa07329dea855c0371697 (diff) | |
download | qemu-fe8545386726a2b1f8af409bcd5ea3d33218af54.tar.gz qemu-fe8545386726a2b1f8af409bcd5ea3d33218af54.tar.bz2 qemu-fe8545386726a2b1f8af409bcd5ea3d33218af54.zip |
tests: add some qemu_strtosz() tests
While reading the function I decided to write some tests.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1442419377-9309-2-git-send-email-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-cutils.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 0046c61fe9..a3de6ab870 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1352,6 +1352,86 @@ static void test_qemu_strtoull_full_max(void) g_assert_cmpint(res, ==, ULLONG_MAX); } +static void test_qemu_strtosz_simple(void) +{ + const char *str = "12345M"; + char *endptr = NULL; + int64_t res; + + res = qemu_strtosz(str, &endptr); + g_assert_cmpint(res, ==, 12345 * M_BYTE); + g_assert(endptr == str + 6); + + res = qemu_strtosz(str, NULL); + g_assert_cmpint(res, ==, 12345 * M_BYTE); +} + +static void test_qemu_strtosz_units(void) +{ + const char *none = "1"; + const char *b = "1B"; + const char *k = "1K"; + const char *m = "1M"; + const char *g = "1G"; + const char *t = "1T"; + const char *p = "1P"; + const char *e = "1E"; + int64_t res; + + /* default is M */ + res = qemu_strtosz(none, NULL); + g_assert_cmpint(res, ==, M_BYTE); + + res = qemu_strtosz(b, NULL); + g_assert_cmpint(res, ==, 1); + + res = qemu_strtosz(k, NULL); + g_assert_cmpint(res, ==, K_BYTE); + + res = qemu_strtosz(m, NULL); + g_assert_cmpint(res, ==, M_BYTE); + + res = qemu_strtosz(g, NULL); + g_assert_cmpint(res, ==, G_BYTE); + + res = qemu_strtosz(t, NULL); + g_assert_cmpint(res, ==, T_BYTE); + + res = qemu_strtosz(p, NULL); + g_assert_cmpint(res, ==, P_BYTE); + + res = qemu_strtosz(e, NULL); + g_assert_cmpint(res, ==, E_BYTE); +} + +static void test_qemu_strtosz_float(void) +{ + const char *str = "12.345M"; + int64_t res; + + res = qemu_strtosz(str, NULL); + g_assert_cmpint(res, ==, 12.345 * M_BYTE); +} + +static void test_qemu_strtosz_erange(void) +{ + const char *str = "10E"; + int64_t res; + + res = qemu_strtosz(str, NULL); + g_assert_cmpint(res, ==, -ERANGE); +} + +static void test_qemu_strtosz_suffix_unit(void) +{ + const char *str = "12345"; + int64_t res; + + res = qemu_strtosz_suffix_unit(str, NULL, + QEMU_STRTOSZ_DEFSUFFIX_KB, 1000); + g_assert_cmpint(res, ==, 12345000); +} + int main(int argc, char **argv) { g_test_init(&argc, &argv, NULL); @@ -1502,5 +1582,16 @@ int main(int argc, char **argv) g_test_add_func("/cutils/qemu_strtoull_full/max", test_qemu_strtoull_full_max); + g_test_add_func("/cutils/strtosz/simple", + test_qemu_strtosz_simple); + g_test_add_func("/cutils/strtosz/units", + test_qemu_strtosz_units); + g_test_add_func("/cutils/strtosz/float", + test_qemu_strtosz_float); + g_test_add_func("/cutils/strtosz/erange", + test_qemu_strtosz_erange); + g_test_add_func("/cutils/strtosz/suffix-unit", + test_qemu_strtosz_suffix_unit); + return g_test_run(); } |