From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35671) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZeP9I-0000SW-QA for qemu-devel@nongnu.org; Tue, 22 Sep 2015 11:06:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZeP9H-0000tl-P1 for qemu-devel@nongnu.org; Tue, 22 Sep 2015 11:06:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58498) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZeP9H-0000th-Ii for qemu-devel@nongnu.org; Tue, 22 Sep 2015 11:06:27 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 368B2C0AE665 for ; Tue, 22 Sep 2015 15:06:27 +0000 (UTC) From: Paolo Bonzini Date: Tue, 22 Sep 2015 17:05:27 +0200 Message-Id: <1442934371-12567-5-git-send-email-pbonzini@redhat.com> In-Reply-To: <1442934371-12567-1-git-send-email-pbonzini@redhat.com> References: <1442934371-12567-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 04/48] tests: add some qemu_strtosz() tests List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= From: Marc-Andr=C3=A9 Lureau While reading the function I decided to write some tests. Signed-off-by: Marc-Andr=C3=A9 Lureau Message-Id: <1442419377-9309-2-git-send-email-marcandre.lureau@redhat.com= > Signed-off-by: Paolo Bonzini --- tests/test-cutils.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++= ++++++ 1 file changed, 91 insertions(+) diff --git a/tests/test-cutils.c b/tests/test-cutils.c index 0046c61..a3de6ab 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, =3D=3D, ULLONG_MAX); } =20 +static void test_qemu_strtosz_simple(void) +{ + const char *str =3D "12345M"; + char *endptr =3D NULL; + int64_t res; + + res =3D qemu_strtosz(str, &endptr); + g_assert_cmpint(res, =3D=3D, 12345 * M_BYTE); + g_assert(endptr =3D=3D str + 6); + + res =3D qemu_strtosz(str, NULL); + g_assert_cmpint(res, =3D=3D, 12345 * M_BYTE); +} + +static void test_qemu_strtosz_units(void) +{ + const char *none =3D "1"; + const char *b =3D "1B"; + const char *k =3D "1K"; + const char *m =3D "1M"; + const char *g =3D "1G"; + const char *t =3D "1T"; + const char *p =3D "1P"; + const char *e =3D "1E"; + int64_t res; + + /* default is M */ + res =3D qemu_strtosz(none, NULL); + g_assert_cmpint(res, =3D=3D, M_BYTE); + + res =3D qemu_strtosz(b, NULL); + g_assert_cmpint(res, =3D=3D, 1); + + res =3D qemu_strtosz(k, NULL); + g_assert_cmpint(res, =3D=3D, K_BYTE); + + res =3D qemu_strtosz(m, NULL); + g_assert_cmpint(res, =3D=3D, M_BYTE); + + res =3D qemu_strtosz(g, NULL); + g_assert_cmpint(res, =3D=3D, G_BYTE); + + res =3D qemu_strtosz(t, NULL); + g_assert_cmpint(res, =3D=3D, T_BYTE); + + res =3D qemu_strtosz(p, NULL); + g_assert_cmpint(res, =3D=3D, P_BYTE); + + res =3D qemu_strtosz(e, NULL); + g_assert_cmpint(res, =3D=3D, E_BYTE); +} + +static void test_qemu_strtosz_float(void) +{ + const char *str =3D "12.345M"; + int64_t res; + + res =3D qemu_strtosz(str, NULL); + g_assert_cmpint(res, =3D=3D, 12.345 * M_BYTE); +} + +static void test_qemu_strtosz_erange(void) +{ + const char *str =3D "10E"; + int64_t res; + + res =3D qemu_strtosz(str, NULL); + g_assert_cmpint(res, =3D=3D, -ERANGE); +} + +static void test_qemu_strtosz_suffix_unit(void) +{ + const char *str =3D "12345"; + int64_t res; + + res =3D qemu_strtosz_suffix_unit(str, NULL, + QEMU_STRTOSZ_DEFSUFFIX_KB, 1000); + g_assert_cmpint(res, =3D=3D, 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); =20 + 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(); } --=20 2.5.0