diff mbox series

[v17,06/14] tests: Add test for QAPI builtin type time

Message ID 20191122074826.1373-7-tao3.xu@intel.com (mailing list archive)
State New, archived
Headers show
Series Build ACPI Heterogeneous Memory Attribute Table (HMAT) | expand

Commit Message

Tao Xu Nov. 22, 2019, 7:48 a.m. UTC
Add tests for time input such as zero, around limit of precision,
signed upper limit, actual upper limit, beyond limits, time suffixes,
and etc.

Signed-off-by: Tao Xu <tao3.xu@intel.com>
---

No changes in v17.

Changes in v16:
    - Update the test cases

Changes in v14:
    - Drop time unit picosecond (Eric)
---
 tests/test-keyval.c                | 89 ++++++++++++++++++++++++++++++
 tests/test-qobject-input-visitor.c | 29 ++++++++++
 2 files changed, 118 insertions(+)

Comments

Markus Armbruster Nov. 25, 2019, 9:08 a.m. UTC | #1
Tao Xu <tao3.xu@intel.com> writes:

> Add tests for time input such as zero, around limit of precision,
> signed upper limit, actual upper limit, beyond limits, time suffixes,
> and etc.
>
> Signed-off-by: Tao Xu <tao3.xu@intel.com>
> ---
>
> No changes in v17.
>
> Changes in v16:
>     - Update the test cases
>
> Changes in v14:
>     - Drop time unit picosecond (Eric)
> ---
>  tests/test-keyval.c                | 89 ++++++++++++++++++++++++++++++
>  tests/test-qobject-input-visitor.c | 29 ++++++++++
>  2 files changed, 118 insertions(+)
>
> diff --git a/tests/test-keyval.c b/tests/test-keyval.c
> index fad941fcb8..39443f7e0c 100644
> --- a/tests/test-keyval.c
> +++ b/tests/test-keyval.c
> @@ -457,6 +457,94 @@ static void test_keyval_visit_size(void)
>      visit_free(v);
>  }
>  
> +static void test_keyval_visit_time(void)
> +{
> +    Error *err = NULL;
> +    Visitor *v;
> +    QDict *qdict;
> +    uint64_t time;
> +
> +    /* Lower limit zero */
> +    qdict = keyval_parse("time1=0", NULL, &error_abort);
> +    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
> +    qobject_unref(qdict);
> +    visit_start_struct(v, NULL, NULL, 0, &error_abort);
> +    visit_type_time(v, "time1", &time, &error_abort);
> +    g_assert_cmpuint(time, ==, 0);
> +    visit_check_struct(v, &error_abort);
> +    visit_end_struct(v, NULL);
> +    visit_free(v);
> +
> +    /* Around limit of precision: UINT64_MAX - 1, UINT64_MAX */
> +    qdict = keyval_parse("time1=18446744073709551614,"
> +                         "time2=18446744073709551615",
> +                         NULL, &error_abort);
> +    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
> +    qobject_unref(qdict);
> +    visit_start_struct(v, NULL, NULL, 0, &error_abort);
> +    visit_type_time(v, "time1", &time, &error_abort);
> +    g_assert_cmphex(time, ==, 0xfffffffffffffffe);
> +    visit_type_time(v, "time2", &time, &error_abort);
> +    g_assert_cmphex(time, ==, 0xffffffffffffffff);
> +    visit_check_struct(v, &error_abort);
> +    visit_end_struct(v, NULL);
> +    visit_free(v);
> +
> +    /* Beyond limits */
> +    qdict = keyval_parse("time1=-1,"
> +                         "time2=18446744073709551616", /* 2^64 */
> +                         NULL, &error_abort);
> +    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
> +    qobject_unref(qdict);
> +    visit_start_struct(v, NULL, NULL, 0, &error_abort);
> +    visit_type_time(v, "time1", &time, &err);
> +    error_free_or_abort(&err);
> +    visit_type_time(v, "time2", &time, &err);
> +    error_free_or_abort(&err);
> +    visit_end_struct(v, NULL);
> +    visit_free(v);
> +
> +    /* Suffixes */
> +    qdict = keyval_parse("time1=2ns,time2=3.4us,time3=5ms,time4=600s",
> +                         NULL, &error_abort);
> +    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
> +    qobject_unref(qdict);
> +    visit_start_struct(v, NULL, NULL, 0, &error_abort);
> +    visit_type_time(v, "time1", &time, &error_abort);
> +    g_assert_cmpuint(time, ==, 2);
> +    visit_type_time(v, "time2", &time, &error_abort);
> +    g_assert_cmpuint(time, ==, 3400);
> +    visit_type_time(v, "time3", &time, &error_abort);
> +    g_assert_cmphex(time, ==, 5 * 1000 * 1000);
> +    visit_type_time(v, "time4", &time, &error_abort);
> +    g_assert_cmphex(time, ==, 600 * 1000000000LL);
> +    visit_check_struct(v, &error_abort);
> +    visit_end_struct(v, NULL);
> +    visit_free(v);
> +
> +    /* Beyond limit with suffix */
> +    qdict = keyval_parse("time1=1844674407370955s", NULL, &error_abort);

We want to test a value just above the limit, like
test_keyval_visit_size() does.  The limit is 2^64-1 ns =
1844674.407370955s.

Please use "1844675s".

> +    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
> +    qobject_unref(qdict);
> +    visit_start_struct(v, NULL, NULL, 0, &error_abort);
> +    visit_type_time(v, "time1", &time, &err);
> +    error_free_or_abort(&err);
> +    visit_end_struct(v, NULL);
> +    visit_free(v);
> +
> +    /* Trailing crap */
> +    qdict = keyval_parse("time1=89ks,time2=ns", NULL, &error_abort);

"time1=89ks" tests empty suffix followed by crap "ks".  Matches
test_keyval_visit_size()'s "sz1=16E", except that test case got messed
up in commit 5e00984aef7 (I'll post a fix).

"time2=ns" doesn't test *trailing* crap, i.e. characters following valid
input.  There is no valid input.  Please use something like
"time2=123use".

> +    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
> +    qobject_unref(qdict);
> +    visit_start_struct(v, NULL, NULL, 0, &error_abort);
> +    visit_type_time(v, "time1", &time, &err);
> +    error_free_or_abort(&err);
> +    visit_type_time(v, "time2", &time, &err);;
> +    error_free_or_abort(&err);
> +    visit_end_struct(v, NULL);
> +    visit_free(v);
> +}
> +
>  static void test_keyval_visit_dict(void)
>  {
>      Error *err = NULL;
> @@ -645,6 +733,7 @@ int main(int argc, char *argv[])
>      g_test_add_func("/keyval/visit/bool", test_keyval_visit_bool);
>      g_test_add_func("/keyval/visit/number", test_keyval_visit_number);
>      g_test_add_func("/keyval/visit/size", test_keyval_visit_size);
> +    g_test_add_func("/keyval/visit/time", test_keyval_visit_time);
>      g_test_add_func("/keyval/visit/dict", test_keyval_visit_dict);
>      g_test_add_func("/keyval/visit/list", test_keyval_visit_list);
>      g_test_add_func("/keyval/visit/optional", test_keyval_visit_optional);
> diff --git a/tests/test-qobject-input-visitor.c b/tests/test-qobject-input-visitor.c
> index 6bacabf063..55138042b8 100644
> --- a/tests/test-qobject-input-visitor.c
> +++ b/tests/test-qobject-input-visitor.c
> @@ -366,6 +366,31 @@ static void test_visitor_in_size_str_fail(TestInputVisitorData *data,
>      error_free_or_abort(&err);
>  }
>  
> +static void test_visitor_in_time_str_keyval(TestInputVisitorData *data,
> +                                            const void *unused)
> +{
> +    uint64_t res, value = 265 * 1000 * 1000;
> +    Visitor *v;
> +
> +    v = visitor_input_test_init_full(data, true, "\"265ms\"");
> +
> +    visit_type_time(v, NULL, &res, &error_abort);
> +    g_assert_cmpfloat(res, ==, value);
> +}
> +
> +static void test_visitor_in_time_str_fail(TestInputVisitorData *data,
> +                                          const void *unused)
> +{
> +    uint64_t res = 0;
> +    Visitor *v;
> +    Error *err = NULL;
> +
> +    v = visitor_input_test_init(data, "\"265ms\"");
> +
> +    visit_type_time(v, NULL, &res, &err);
> +    error_free_or_abort(&err);
> +}
> +
>  static void test_visitor_in_string(TestInputVisitorData *data,
>                                     const void *unused)
>  {
> @@ -1311,6 +1336,10 @@ int main(int argc, char **argv)
>                             NULL, test_visitor_in_size_str_keyval);
>      input_visitor_test_add("/visitor/input/size_str_fail",
>                             NULL, test_visitor_in_size_str_fail);
> +    input_visitor_test_add("/visitor/input/time_str_keyval",
> +                           NULL, test_visitor_in_time_str_keyval);
> +    input_visitor_test_add("/visitor/input/time_str_fail",
> +                           NULL, test_visitor_in_time_str_fail);
>      input_visitor_test_add("/visitor/input/string",
>                             NULL, test_visitor_in_string);
>      input_visitor_test_add("/visitor/input/enum",
diff mbox series

Patch

diff --git a/tests/test-keyval.c b/tests/test-keyval.c
index fad941fcb8..39443f7e0c 100644
--- a/tests/test-keyval.c
+++ b/tests/test-keyval.c
@@ -457,6 +457,94 @@  static void test_keyval_visit_size(void)
     visit_free(v);
 }
 
+static void test_keyval_visit_time(void)
+{
+    Error *err = NULL;
+    Visitor *v;
+    QDict *qdict;
+    uint64_t time;
+
+    /* Lower limit zero */
+    qdict = keyval_parse("time1=0", NULL, &error_abort);
+    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+    qobject_unref(qdict);
+    visit_start_struct(v, NULL, NULL, 0, &error_abort);
+    visit_type_time(v, "time1", &time, &error_abort);
+    g_assert_cmpuint(time, ==, 0);
+    visit_check_struct(v, &error_abort);
+    visit_end_struct(v, NULL);
+    visit_free(v);
+
+    /* Around limit of precision: UINT64_MAX - 1, UINT64_MAX */
+    qdict = keyval_parse("time1=18446744073709551614,"
+                         "time2=18446744073709551615",
+                         NULL, &error_abort);
+    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+    qobject_unref(qdict);
+    visit_start_struct(v, NULL, NULL, 0, &error_abort);
+    visit_type_time(v, "time1", &time, &error_abort);
+    g_assert_cmphex(time, ==, 0xfffffffffffffffe);
+    visit_type_time(v, "time2", &time, &error_abort);
+    g_assert_cmphex(time, ==, 0xffffffffffffffff);
+    visit_check_struct(v, &error_abort);
+    visit_end_struct(v, NULL);
+    visit_free(v);
+
+    /* Beyond limits */
+    qdict = keyval_parse("time1=-1,"
+                         "time2=18446744073709551616", /* 2^64 */
+                         NULL, &error_abort);
+    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+    qobject_unref(qdict);
+    visit_start_struct(v, NULL, NULL, 0, &error_abort);
+    visit_type_time(v, "time1", &time, &err);
+    error_free_or_abort(&err);
+    visit_type_time(v, "time2", &time, &err);
+    error_free_or_abort(&err);
+    visit_end_struct(v, NULL);
+    visit_free(v);
+
+    /* Suffixes */
+    qdict = keyval_parse("time1=2ns,time2=3.4us,time3=5ms,time4=600s",
+                         NULL, &error_abort);
+    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+    qobject_unref(qdict);
+    visit_start_struct(v, NULL, NULL, 0, &error_abort);
+    visit_type_time(v, "time1", &time, &error_abort);
+    g_assert_cmpuint(time, ==, 2);
+    visit_type_time(v, "time2", &time, &error_abort);
+    g_assert_cmpuint(time, ==, 3400);
+    visit_type_time(v, "time3", &time, &error_abort);
+    g_assert_cmphex(time, ==, 5 * 1000 * 1000);
+    visit_type_time(v, "time4", &time, &error_abort);
+    g_assert_cmphex(time, ==, 600 * 1000000000LL);
+    visit_check_struct(v, &error_abort);
+    visit_end_struct(v, NULL);
+    visit_free(v);
+
+    /* Beyond limit with suffix */
+    qdict = keyval_parse("time1=1844674407370955s", NULL, &error_abort);
+    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+    qobject_unref(qdict);
+    visit_start_struct(v, NULL, NULL, 0, &error_abort);
+    visit_type_time(v, "time1", &time, &err);
+    error_free_or_abort(&err);
+    visit_end_struct(v, NULL);
+    visit_free(v);
+
+    /* Trailing crap */
+    qdict = keyval_parse("time1=89ks,time2=ns", NULL, &error_abort);
+    v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
+    qobject_unref(qdict);
+    visit_start_struct(v, NULL, NULL, 0, &error_abort);
+    visit_type_time(v, "time1", &time, &err);
+    error_free_or_abort(&err);
+    visit_type_time(v, "time2", &time, &err);;
+    error_free_or_abort(&err);
+    visit_end_struct(v, NULL);
+    visit_free(v);
+}
+
 static void test_keyval_visit_dict(void)
 {
     Error *err = NULL;
@@ -645,6 +733,7 @@  int main(int argc, char *argv[])
     g_test_add_func("/keyval/visit/bool", test_keyval_visit_bool);
     g_test_add_func("/keyval/visit/number", test_keyval_visit_number);
     g_test_add_func("/keyval/visit/size", test_keyval_visit_size);
+    g_test_add_func("/keyval/visit/time", test_keyval_visit_time);
     g_test_add_func("/keyval/visit/dict", test_keyval_visit_dict);
     g_test_add_func("/keyval/visit/list", test_keyval_visit_list);
     g_test_add_func("/keyval/visit/optional", test_keyval_visit_optional);
diff --git a/tests/test-qobject-input-visitor.c b/tests/test-qobject-input-visitor.c
index 6bacabf063..55138042b8 100644
--- a/tests/test-qobject-input-visitor.c
+++ b/tests/test-qobject-input-visitor.c
@@ -366,6 +366,31 @@  static void test_visitor_in_size_str_fail(TestInputVisitorData *data,
     error_free_or_abort(&err);
 }
 
+static void test_visitor_in_time_str_keyval(TestInputVisitorData *data,
+                                            const void *unused)
+{
+    uint64_t res, value = 265 * 1000 * 1000;
+    Visitor *v;
+
+    v = visitor_input_test_init_full(data, true, "\"265ms\"");
+
+    visit_type_time(v, NULL, &res, &error_abort);
+    g_assert_cmpfloat(res, ==, value);
+}
+
+static void test_visitor_in_time_str_fail(TestInputVisitorData *data,
+                                          const void *unused)
+{
+    uint64_t res = 0;
+    Visitor *v;
+    Error *err = NULL;
+
+    v = visitor_input_test_init(data, "\"265ms\"");
+
+    visit_type_time(v, NULL, &res, &err);
+    error_free_or_abort(&err);
+}
+
 static void test_visitor_in_string(TestInputVisitorData *data,
                                    const void *unused)
 {
@@ -1311,6 +1336,10 @@  int main(int argc, char **argv)
                            NULL, test_visitor_in_size_str_keyval);
     input_visitor_test_add("/visitor/input/size_str_fail",
                            NULL, test_visitor_in_size_str_fail);
+    input_visitor_test_add("/visitor/input/time_str_keyval",
+                           NULL, test_visitor_in_time_str_keyval);
+    input_visitor_test_add("/visitor/input/time_str_fail",
+                           NULL, test_visitor_in_time_str_fail);
     input_visitor_test_add("/visitor/input/string",
                            NULL, test_visitor_in_string);
     input_visitor_test_add("/visitor/input/enum",