From: Tao Xu <tao3.xu@intel.com>
To: imammedo@redhat.com, eblake@redhat.com, ehabkost@redhat.com
Cc: jingqi.liu@intel.com, tao3.xu@intel.com, fan.du@intel.com,
qemu-devel@nongnu.org, jonathan.cameron@huawei.com
Subject: [PATCH v13 02/12] tests/cutils: Add test for qemu_strtotime_ps()
Date: Sun, 20 Oct 2019 19:11:15 +0800 [thread overview]
Message-ID: <20191020111125.27659-3-tao3.xu@intel.com> (raw)
In-Reply-To: <20191020111125.27659-1-tao3.xu@intel.com>
Test the input of basic, time suffixes, float, invaild, trailing and
overflow.
Signed-off-by: Tao Xu <tao3.xu@intel.com>
---
No changes in v13.
---
tests/test-cutils.c | 199 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 199 insertions(+)
diff --git a/tests/test-cutils.c b/tests/test-cutils.c
index 1aa8351520..19c967d3d5 100644
--- a/tests/test-cutils.c
+++ b/tests/test-cutils.c
@@ -2179,6 +2179,193 @@ static void test_qemu_strtosz_metric(void)
g_assert(endptr == str + 6);
}
+static void test_qemu_strtotime_ps_simple(void)
+{
+ const char *str;
+ const char *endptr;
+ int err;
+ uint64_t res = 0xbaadf00d;
+
+ str = "0";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+ g_assert(endptr == str + 1);
+
+ str = "56789";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 56789);
+ g_assert(endptr == str + 5);
+
+ err = qemu_strtotime_ps(str, NULL, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 56789);
+
+ /* Note: precision is 53 bits since we're parsing with strtod() */
+
+ str = "9007199254740991"; /* 2^53-1 */
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0x1fffffffffffff);
+ g_assert(endptr == str + 16);
+
+ str = "9007199254740992"; /* 2^53 */
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0x20000000000000);
+ g_assert(endptr == str + 16);
+
+ str = "9007199254740993"; /* 2^53+1 */
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */
+ g_assert(endptr == str + 16);
+
+ str = "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0xfffffffffffff800);
+ g_assert(endptr == str + 20);
+
+ str = "18446744073709550591"; /* 0xfffffffffffffbff */
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0xfffffffffffff800); /* rounded to 53 bits */
+ g_assert(endptr == str + 20);
+
+ /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to
+ * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */
+}
+
+static void test_qemu_strtotime_ps_units(void)
+{
+ const char *ps = "1ps";
+ const char *ns = "1ns";
+ const char *us = "1us";
+ const char *ms = "1ms";
+ const char *s = "1s";
+ int err;
+ const char *endptr;
+ uint64_t res = 0xbaadf00d;
+
+ err = qemu_strtotime_ps(ps, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 1);
+ g_assert(endptr == ps + 3);
+
+ err = qemu_strtotime_ps(ns, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 1000);
+ g_assert(endptr == ns + 3);
+
+ err = qemu_strtotime_ps(us, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 1000000);
+ g_assert(endptr == us + 3);
+
+ err = qemu_strtotime_ps(ms, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 1000000000LL);
+ g_assert(endptr == ms + 3);
+
+ err = qemu_strtotime_ps(s, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 1000000000000ULL);
+ g_assert(endptr == s + 2);
+}
+
+static void test_qemu_strtotime_ps_float(void)
+{
+ const char *str = "56.789ns";
+ int err;
+ const char *endptr;
+ uint64_t res = 0xbaadf00d;
+
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 56.789 * 1000);
+ g_assert(endptr == str + 8);
+}
+
+static void test_qemu_strtotime_ps_invalid(void)
+{
+ const char *str;
+ const char *endptr;
+ int err;
+ uint64_t res = 0xbaadf00d;
+
+ str = "";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == str);
+
+ str = " \t ";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == str);
+
+ str = "crap";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == str);
+
+ str = "inf";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == str);
+
+ str = "NaN";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtotime_ps_trailing(void)
+{
+ const char *str;
+ int err;
+ uint64_t res = 0xbaadf00d;
+
+ str = "123xxx";
+
+ err = qemu_strtotime_ps(str, NULL, &res);
+ g_assert_cmpint(err, ==, -EINVAL);
+}
+
+static void test_qemu_strtotime_ps_erange(void)
+{
+ const char *str;
+ const char *endptr;
+ int err;
+ uint64_t res = 0xbaadf00d;
+
+ str = "-1";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert(endptr == str + 2);
+
+ str = "18446744073709550592"; /* 0xfffffffffffffc00 */
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert(endptr == str + 20);
+
+ str = "18446744073709551615"; /* 2^64-1 */
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert(endptr == str + 20);
+
+ str = "18446744073709551616"; /* 2^64 */
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert(endptr == str + 20);
+
+ str = "200000000000000s";
+ err = qemu_strtotime_ps(str, &endptr, &res);
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert(endptr == str + 16);
+}
+
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
@@ -2456,5 +2643,17 @@ int main(int argc, char **argv)
g_test_add_func("/cutils/strtosz/metric",
test_qemu_strtosz_metric);
+ g_test_add_func("/cutils/strtotime/simple",
+ test_qemu_strtotime_ps_simple);
+ g_test_add_func("/cutils/strtotime/units",
+ test_qemu_strtotime_ps_units);
+ g_test_add_func("/cutils/strtotime/float",
+ test_qemu_strtotime_ps_float);
+ g_test_add_func("/cutils/strtotime/invalid",
+ test_qemu_strtotime_ps_invalid);
+ g_test_add_func("/cutils/strtotime/trailing",
+ test_qemu_strtotime_ps_trailing);
+ g_test_add_func("/cutils/strtotime/erange",
+ test_qemu_strtotime_ps_erange);
return g_test_run();
}
--
2.20.1
next prev parent reply other threads:[~2019-10-20 11:20 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-20 11:11 [PATCH v13 00/12] Build ACPI Heterogeneous Memory Attribute Table (HMAT) Tao Xu
2019-10-20 11:11 ` [PATCH v13 01/12] util/cutils: Add qemu_strtotime_ps() Tao Xu
2019-10-23 1:08 ` Eduardo Habkost
2019-10-23 6:07 ` Tao Xu
2019-10-23 1:13 ` Eric Blake
2019-10-23 1:50 ` Tao Xu
2019-10-24 9:54 ` Daniel P. Berrangé
2019-10-24 13:20 ` Eduardo Habkost
2019-10-25 1:22 ` Tao Xu
2019-10-20 11:11 ` Tao Xu [this message]
2019-10-20 11:11 ` [PATCH v13 03/12] qapi: Add builtin type time Tao Xu
2019-10-20 11:11 ` [PATCH v13 04/12] tests: Add test for QAPI " Tao Xu
2019-10-20 11:11 ` [PATCH v13 05/12] numa: Extend CLI to provide initiator information for numa nodes Tao Xu
2019-10-21 12:29 ` Igor Mammedov
2019-10-22 1:01 ` Tao Xu
2019-10-20 11:11 ` [PATCH v13 06/12] numa: Extend CLI to provide memory latency and bandwidth information Tao Xu
2019-10-22 7:08 ` Igor Mammedov
2019-10-22 8:22 ` Tao Xu
2019-10-23 15:28 ` Igor Mammedov
2019-10-25 6:33 ` Tao Xu
2019-10-25 13:27 ` Igor Mammedov
2019-10-25 19:44 ` Markus Armbruster
2019-10-25 20:51 ` Eduardo Habkost
2019-10-28 2:05 ` Tao Xu
2019-10-28 5:46 ` Markus Armbruster
2019-10-28 7:25 ` Tao Xu
2019-10-20 11:11 ` [PATCH v13 07/12] numa: Calculate hmat latency and bandwidth entry list Tao Xu
2019-10-20 11:11 ` [PATCH v13 08/12] numa: Extend CLI to provide memory side cache information Tao Xu
2019-10-20 11:11 ` [PATCH v13 09/12] hmat acpi: Build Memory Proximity Domain Attributes Structure(s) Tao Xu
2019-10-20 11:11 ` [PATCH v13 10/12] hmat acpi: Build System Locality Latency and Bandwidth Information Structure(s) Tao Xu
2019-10-20 11:11 ` [PATCH v13 11/12] hmat acpi: Build Memory Side Cache " Tao Xu
2019-10-20 11:11 ` [PATCH v13 12/12] tests/bios-tables-test: add test cases for ACPI HMAT Tao Xu
2019-10-20 11:43 ` [PATCH v13 00/12] Build ACPI Heterogeneous Memory Attribute Table (HMAT) no-reply
2019-10-20 12:13 ` no-reply
2019-10-20 12:57 ` no-reply
2019-10-20 13:19 ` no-reply
2019-10-22 11:22 ` Markus Armbruster
2019-10-23 1:46 ` Tao Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191020111125.27659-3-tao3.xu@intel.com \
--to=tao3.xu@intel.com \
--cc=eblake@redhat.com \
--cc=ehabkost@redhat.com \
--cc=fan.du@intel.com \
--cc=imammedo@redhat.com \
--cc=jingqi.liu@intel.com \
--cc=jonathan.cameron@huawei.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).