From 54aa3de72ea2aaa2e903e7e879a4f3dda515a00e Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 12 Nov 2020 19:13:37 -0600 Subject: qapi: Use QAPI_LIST_PREPEND() where possible Anywhere we create a list of just one item or by prepending items (typically because order doesn't matter), we can use QAPI_LIST_PREPEND(). But places where we must keep the list in order by appending remain open-coded until later patches. Note that as a side effect, this also performs a cleanup of two minor issues in qga/commands-posix.c: the old code was performing new = g_malloc0(sizeof(*ret)); which 1) is confusing because you have to verify whether 'new' and 'ret' are variables with the same type, and 2) would conflict with C++ compilation (not an actual problem for this file, but makes copy-and-paste harder). Signed-off-by: Eric Blake Message-Id: <20201113011340.463563-5-eblake@redhat.com> Reviewed-by: Markus Armbruster Acked-by: Stefan Hajnoczi [Straightforward conflicts due to commit a8aa94b5f8 "qga: update schema for guest-get-disks 'dependents' field" and commit a10b453a52 "target/mips: Move mips_cpu_add_definition() from helper.c to cpu.c" resolved. Commit message tweaked.] Signed-off-by: Markus Armbruster --- tests/test-clone-visitor.c | 7 +-- tests/test-qobject-output-visitor.c | 48 ++++++++------- tests/test-visitor-serialization.c | 113 +++++------------------------------- 3 files changed, 38 insertions(+), 130 deletions(-) (limited to 'tests') diff --git a/tests/test-clone-visitor.c b/tests/test-clone-visitor.c index 5e1e8b2..4944b3d 100644 --- a/tests/test-clone-visitor.c +++ b/tests/test-clone-visitor.c @@ -65,16 +65,13 @@ static void test_clone_alternate(void) static void test_clone_list_union(void) { - uint8List *src, *dst; + uint8List *src = NULL, *dst; uint8List *tmp = NULL; int i; /* Build list in reverse */ for (i = 10; i; i--) { - src = g_new0(uint8List, 1); - src->next = tmp; - src->value = i; - tmp = src; + QAPI_LIST_PREPEND(src, i); } dst = QAPI_CLONE(uint8List, src); diff --git a/tests/test-qobject-output-visitor.c b/tests/test-qobject-output-visitor.c index 1c856d9..b20ab8b 100644 --- a/tests/test-qobject-output-visitor.c +++ b/tests/test-qobject-output-visitor.c @@ -223,7 +223,8 @@ static void test_visitor_out_list(TestOutputVisitorData *data, const void *unused) { const char *value_str = "list value"; - TestStructList *p, *head = NULL; + TestStruct *value; + TestStructList *head = NULL; const int max_items = 10; bool value_bool = true; int value_int = 10; @@ -233,14 +234,12 @@ static void test_visitor_out_list(TestOutputVisitorData *data, /* Build the list in reverse order... */ for (i = 0; i < max_items; i++) { - p = g_malloc0(sizeof(*p)); - p->value = g_malloc0(sizeof(*p->value)); - p->value->integer = value_int + (max_items - i - 1); - p->value->boolean = value_bool; - p->value->string = g_strdup(value_str); - - p->next = head; - head = p; + value = g_malloc0(sizeof(*value)); + value->integer = value_int + (max_items - i - 1); + value->boolean = value_bool; + value->string = g_strdup(value_str); + + QAPI_LIST_PREPEND(head, value); } visit_type_TestStructList(data->ov, NULL, &head, &error_abort); @@ -270,26 +269,25 @@ static void test_visitor_out_list(TestOutputVisitorData *data, static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data, const void *unused) { - UserDefTwoList *p, *head = NULL; + UserDefTwo *value; + UserDefTwoList *head = NULL; const char string[] = "foo bar"; int i, max_count = 1024; for (i = 0; i < max_count; i++) { - p = g_malloc0(sizeof(*p)); - p->value = g_malloc0(sizeof(*p->value)); - - p->value->string0 = g_strdup(string); - p->value->dict1 = g_new0(UserDefTwoDict, 1); - p->value->dict1->string1 = g_strdup(string); - p->value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1); - p->value->dict1->dict2->userdef = g_new0(UserDefOne, 1); - p->value->dict1->dict2->userdef->string = g_strdup(string); - p->value->dict1->dict2->userdef->integer = 42; - p->value->dict1->dict2->string = g_strdup(string); - p->value->dict1->has_dict3 = false; - - p->next = head; - head = p; + value = g_malloc0(sizeof(*value)); + + value->string0 = g_strdup(string); + value->dict1 = g_new0(UserDefTwoDict, 1); + value->dict1->string1 = g_strdup(string); + value->dict1->dict2 = g_new0(UserDefTwoDictDict, 1); + value->dict1->dict2->userdef = g_new0(UserDefOne, 1); + value->dict1->dict2->userdef->string = g_strdup(string); + value->dict1->dict2->userdef->integer = 42; + value->dict1->dict2->string = g_strdup(string); + value->dict1->has_dict3 = false; + + QAPI_LIST_PREPEND(head, value); } qapi_free_UserDefTwoList(head); diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-serialization.c index 1c5a8b9..12275e5 100644 --- a/tests/test-visitor-serialization.c +++ b/tests/test-visitor-serialization.c @@ -351,135 +351,51 @@ static void test_primitive_lists(gconstpointer opaque) for (i = 0; i < 32; i++) { switch (pl.type) { case PTYPE_STRING: { - strList *tmp = g_new0(strList, 1); - tmp->value = g_strdup(pt->value.string); - if (pl.value.strings == NULL) { - pl.value.strings = tmp; - } else { - tmp->next = pl.value.strings; - pl.value.strings = tmp; - } + QAPI_LIST_PREPEND(pl.value.strings, g_strdup(pt->value.string)); break; } case PTYPE_INTEGER: { - intList *tmp = g_new0(intList, 1); - tmp->value = pt->value.integer; - if (pl.value.integers == NULL) { - pl.value.integers = tmp; - } else { - tmp->next = pl.value.integers; - pl.value.integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.integers, pt->value.integer); break; } case PTYPE_S8: { - int8List *tmp = g_new0(int8List, 1); - tmp->value = pt->value.s8; - if (pl.value.s8_integers == NULL) { - pl.value.s8_integers = tmp; - } else { - tmp->next = pl.value.s8_integers; - pl.value.s8_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.s8_integers, pt->value.s8); break; } case PTYPE_S16: { - int16List *tmp = g_new0(int16List, 1); - tmp->value = pt->value.s16; - if (pl.value.s16_integers == NULL) { - pl.value.s16_integers = tmp; - } else { - tmp->next = pl.value.s16_integers; - pl.value.s16_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.s16_integers, pt->value.s16); break; } case PTYPE_S32: { - int32List *tmp = g_new0(int32List, 1); - tmp->value = pt->value.s32; - if (pl.value.s32_integers == NULL) { - pl.value.s32_integers = tmp; - } else { - tmp->next = pl.value.s32_integers; - pl.value.s32_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.s32_integers, pt->value.s32); break; } case PTYPE_S64: { - int64List *tmp = g_new0(int64List, 1); - tmp->value = pt->value.s64; - if (pl.value.s64_integers == NULL) { - pl.value.s64_integers = tmp; - } else { - tmp->next = pl.value.s64_integers; - pl.value.s64_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.s64_integers, pt->value.s64); break; } case PTYPE_U8: { - uint8List *tmp = g_new0(uint8List, 1); - tmp->value = pt->value.u8; - if (pl.value.u8_integers == NULL) { - pl.value.u8_integers = tmp; - } else { - tmp->next = pl.value.u8_integers; - pl.value.u8_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.u8_integers, pt->value.u8); break; } case PTYPE_U16: { - uint16List *tmp = g_new0(uint16List, 1); - tmp->value = pt->value.u16; - if (pl.value.u16_integers == NULL) { - pl.value.u16_integers = tmp; - } else { - tmp->next = pl.value.u16_integers; - pl.value.u16_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.u16_integers, pt->value.u16); break; } case PTYPE_U32: { - uint32List *tmp = g_new0(uint32List, 1); - tmp->value = pt->value.u32; - if (pl.value.u32_integers == NULL) { - pl.value.u32_integers = tmp; - } else { - tmp->next = pl.value.u32_integers; - pl.value.u32_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.u32_integers, pt->value.u32); break; } case PTYPE_U64: { - uint64List *tmp = g_new0(uint64List, 1); - tmp->value = pt->value.u64; - if (pl.value.u64_integers == NULL) { - pl.value.u64_integers = tmp; - } else { - tmp->next = pl.value.u64_integers; - pl.value.u64_integers = tmp; - } + QAPI_LIST_PREPEND(pl.value.u64_integers, pt->value.u64); break; } case PTYPE_NUMBER: { - numberList *tmp = g_new0(numberList, 1); - tmp->value = pt->value.number; - if (pl.value.numbers == NULL) { - pl.value.numbers = tmp; - } else { - tmp->next = pl.value.numbers; - pl.value.numbers = tmp; - } + QAPI_LIST_PREPEND(pl.value.numbers, pt->value.number); break; } case PTYPE_BOOLEAN: { - boolList *tmp = g_new0(boolList, 1); - tmp->value = pt->value.boolean; - if (pl.value.booleans == NULL) { - pl.value.booleans = tmp; - } else { - tmp->next = pl.value.booleans; - pl.value.booleans = tmp; - } + QAPI_LIST_PREPEND(pl.value.booleans, pt->value.boolean); break; } default: @@ -704,10 +620,7 @@ static void test_nested_struct_list(gconstpointer opaque) int i = 0; for (i = 0; i < 8; i++) { - tmp = g_new0(UserDefTwoList, 1); - tmp->value = nested_struct_create(); - tmp->next = listp; - listp = tmp; + QAPI_LIST_PREPEND(listp, nested_struct_create()); } ops->serialize(listp, &serialize_data, visit_nested_struct_list, -- cgit v1.1 From 3953f826a3ff09a6b71b0365c05d1d3f9fdf49f2 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:43 +0100 Subject: tests/check-qjson: Don't skip funny QNumber to JSON conversions simple_number() and float_number() convert from JSON to QNumber and back. simple_number() tests "-0", but skips the conversion back to JSON, because it yields "0", not "-0". Works as intended, so better cover it: don't skip, but expect the funny result. float_number() tests "-32.20e-10", but skips the conversion back to JSON, because it yields "-0". This is a known bug in qnum_to_string(), marked FIXME there. Cover the bug: don't skip, but expect the funny result. While there, switch from g_assert() to g_assert_cmpstr() & friends for friendlier test failures. Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-2-armbru@redhat.com> --- tests/check-qjson.c | 55 ++++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) (limited to 'tests') diff --git a/tests/check-qjson.c b/tests/check-qjson.c index 9a02079..2a58529 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -793,37 +793,35 @@ static void utf8_string(void) static void simple_number(void) { - int i; struct { const char *encoded; int64_t decoded; - int skip; + const char *reencoded; } test_cases[] = { { "0", 0 }, { "1234", 1234 }, { "1", 1 }, { "-32", -32 }, - { "-0", 0, .skip = 1 }, - { }, + { "-0", 0, "0" }, + {}, }; + int i; + QNum *qnum; + int64_t val; + QString *str; for (i = 0; test_cases[i].encoded; i++) { - QNum *qnum; - int64_t val; - qnum = qobject_to(QNum, qobject_from_json(test_cases[i].encoded, &error_abort)); g_assert(qnum); g_assert(qnum_get_try_int(qnum, &val)); g_assert_cmpint(val, ==, test_cases[i].decoded); - if (test_cases[i].skip == 0) { - QString *str; - str = qobject_to_json(QOBJECT(qnum)); - g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0); - qobject_unref(str); - } + str = qobject_to_json(QOBJECT(qnum)); + g_assert_cmpstr(qstring_get_str(str), ==, + test_cases[i].reencoded ?: test_cases[i].encoded); + qobject_unref(str); qobject_unref(qnum); } @@ -874,35 +872,32 @@ static void large_number(void) static void float_number(void) { - int i; struct { const char *encoded; double decoded; - int skip; + const char *reencoded; } test_cases[] = { { "32.43", 32.43 }, { "0.222", 0.222 }, { "-32.12313", -32.12313 }, - { "-32.20e-10", -32.20e-10, .skip = 1 }, - { }, + { "-32.20e-10", -32.20e-10, "-0" /* BUG */ }, + {}, }; + int i; + QNum *qnum; + QString *str; for (i = 0; test_cases[i].encoded; i++) { - QObject *obj; - QNum *qnum; - - obj = qobject_from_json(test_cases[i].encoded, &error_abort); - qnum = qobject_to(QNum, obj); + qnum = qobject_to(QNum, + qobject_from_json(test_cases[i].encoded, + &error_abort)); g_assert(qnum); - g_assert(qnum_get_double(qnum) == test_cases[i].decoded); - - if (test_cases[i].skip == 0) { - QString *str; + g_assert_cmpfloat(qnum_get_double(qnum), ==, test_cases[i].decoded); - str = qobject_to_json(obj); - g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0); - qobject_unref(str); - } + str = qobject_to_json(QOBJECT(qnum)); + g_assert_cmpstr(qstring_get_str(str), ==, + test_cases[i].reencoded ?: test_cases[i].encoded); + qobject_unref(str); qobject_unref(qnum); } -- cgit v1.1 From 1a68eb8c186d81a2836bd1e9abd03c0b39f252e9 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:44 +0100 Subject: tests/check-qjson: Examine QNum more thoroughly simple_number() checks only qnum_get_try_int(). Also check qnum_get_try_uint() and qnum_get_double(). float_number() checks only qnum_get_double(). Also check qnum_get_try_int() and qnum_get_try_uint(). Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-3-armbru@redhat.com> --- tests/check-qjson.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/check-qjson.c b/tests/check-qjson.c index 2a58529..6ab6b11 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -807,7 +807,8 @@ static void simple_number(void) }; int i; QNum *qnum; - int64_t val; + int64_t ival; + uint64_t uval; QString *str; for (i = 0; test_cases[i].encoded; i++) { @@ -815,8 +816,16 @@ static void simple_number(void) qobject_from_json(test_cases[i].encoded, &error_abort)); g_assert(qnum); - g_assert(qnum_get_try_int(qnum, &val)); - g_assert_cmpint(val, ==, test_cases[i].decoded); + g_assert(qnum_get_try_int(qnum, &ival)); + g_assert_cmpint(ival, ==, test_cases[i].decoded); + if (test_cases[i].decoded >= 0) { + g_assert(qnum_get_try_uint(qnum, &uval)); + g_assert_cmpuint(uval, ==, (uint64_t)test_cases[i].decoded); + } else { + g_assert(!qnum_get_try_uint(qnum, &uval)); + } + g_assert_cmpfloat(qnum_get_double(qnum), ==, + (double)test_cases[i].decoded); str = qobject_to_json(QOBJECT(qnum)); g_assert_cmpstr(qstring_get_str(str), ==, @@ -885,6 +894,8 @@ static void float_number(void) }; int i; QNum *qnum; + int64_t ival; + uint64_t uval; QString *str; for (i = 0; test_cases[i].encoded; i++) { @@ -893,6 +904,8 @@ static void float_number(void) &error_abort)); g_assert(qnum); g_assert_cmpfloat(qnum_get_double(qnum), ==, test_cases[i].decoded); + g_assert(!qnum_get_try_int(qnum, &ival)); + g_assert(!qnum_get_try_uint(qnum, &uval)); str = qobject_to_json(QOBJECT(qnum)); g_assert_cmpstr(qstring_get_str(str), ==, -- cgit v1.1 From 4aea88335d5c824b7b63b73c12830b5db558d463 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:45 +0100 Subject: tests/check-qjson: Cover number 2^63 Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-4-armbru@redhat.com> --- tests/check-qjson.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/check-qjson.c b/tests/check-qjson.c index 6ab6b11..8cb8a40 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -791,7 +791,7 @@ static void utf8_string(void) } } -static void simple_number(void) +static void int_number(void) { struct { const char *encoded; @@ -836,6 +836,42 @@ static void simple_number(void) } } +static void uint_number(void) +{ + struct { + const char *encoded; + uint64_t decoded; + const char *reencoded; + } test_cases[] = { + { "9223372036854775808", (uint64_t)1 << 63 }, + {}, + }; + int i; + QNum *qnum; + int64_t ival; + uint64_t uval; + QString *str; + + for (i = 0; test_cases[i].encoded; i++) { + qnum = qobject_to(QNum, + qobject_from_json(test_cases[i].encoded, + &error_abort)); + g_assert(qnum); + g_assert(qnum_get_try_uint(qnum, &uval)); + g_assert_cmpuint(uval, ==, test_cases[i].decoded); + g_assert(!qnum_get_try_int(qnum, &ival)); + g_assert_cmpfloat(qnum_get_double(qnum), ==, + (double)test_cases[i].decoded); + + str = qobject_to_json(QOBJECT(qnum)); + g_assert_cmpstr(qstring_get_str(str), ==, + test_cases[i].reencoded ?: test_cases[i].encoded); + qobject_unref(str); + + qobject_unref(qnum); + } +} + static void large_number(void) { const char *maxu64 = "18446744073709551615"; /* 2^64-1 */ @@ -1487,7 +1523,8 @@ int main(int argc, char **argv) g_test_add_func("/literals/string/quotes", string_with_quotes); g_test_add_func("/literals/string/utf8", utf8_string); - g_test_add_func("/literals/number/simple", simple_number); + g_test_add_func("/literals/number/int", int_number); + g_test_add_func("/literals/number/uint", uint_number); g_test_add_func("/literals/number/large", large_number); g_test_add_func("/literals/number/float", float_number); -- cgit v1.1 From 780df5d42befde9293cd667e1e237f26bcf37e94 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:46 +0100 Subject: tests/check-qjson: Replace redundant large_number() Move one of large_number()'s three checks to uint_number(), and the other two to float_number(). Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-5-armbru@redhat.com> --- tests/check-qjson.c | 47 +++-------------------------------------------- 1 file changed, 3 insertions(+), 44 deletions(-) (limited to 'tests') diff --git a/tests/check-qjson.c b/tests/check-qjson.c index 8cb8a40..98515b1 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -844,6 +844,7 @@ static void uint_number(void) const char *reencoded; } test_cases[] = { { "9223372036854775808", (uint64_t)1 << 63 }, + { "18446744073709551615", UINT64_MAX }, {}, }; int i; @@ -872,49 +873,6 @@ static void uint_number(void) } } -static void large_number(void) -{ - const char *maxu64 = "18446744073709551615"; /* 2^64-1 */ - const char *gtu64 = "18446744073709551616"; /* 2^64 */ - const char *lti64 = "-9223372036854775809"; /* -2^63 - 1 */ - QNum *qnum; - QString *str; - uint64_t val; - int64_t ival; - - qnum = qobject_to(QNum, qobject_from_json(maxu64, &error_abort)); - g_assert(qnum); - g_assert_cmpuint(qnum_get_uint(qnum), ==, 18446744073709551615U); - g_assert(!qnum_get_try_int(qnum, &ival)); - - str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, maxu64); - qobject_unref(str); - qobject_unref(qnum); - - qnum = qobject_to(QNum, qobject_from_json(gtu64, &error_abort)); - g_assert(qnum); - g_assert_cmpfloat(qnum_get_double(qnum), ==, 18446744073709552e3); - g_assert(!qnum_get_try_uint(qnum, &val)); - g_assert(!qnum_get_try_int(qnum, &ival)); - - str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, gtu64); - qobject_unref(str); - qobject_unref(qnum); - - qnum = qobject_to(QNum, qobject_from_json(lti64, &error_abort)); - g_assert(qnum); - g_assert_cmpfloat(qnum_get_double(qnum), ==, -92233720368547758e2); - g_assert(!qnum_get_try_uint(qnum, &val)); - g_assert(!qnum_get_try_int(qnum, &ival)); - - str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, "-9223372036854775808"); - qobject_unref(str); - qobject_unref(qnum); -} - static void float_number(void) { struct { @@ -926,6 +884,8 @@ static void float_number(void) { "0.222", 0.222 }, { "-32.12313", -32.12313 }, { "-32.20e-10", -32.20e-10, "-0" /* BUG */ }, + { "18446744073709551616", 0x1p64 }, + { "-9223372036854775809", -0x1p63, "-9223372036854775808" }, {}, }; int i; @@ -1525,7 +1485,6 @@ int main(int argc, char **argv) g_test_add_func("/literals/number/int", int_number); g_test_add_func("/literals/number/uint", uint_number); - g_test_add_func("/literals/number/large", large_number); g_test_add_func("/literals/number/float", float_number); g_test_add_func("/literals/keyword", keyword_literal); -- cgit v1.1 From 1a9076919f5367309ee8d89b91aae5330dec37d6 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:47 +0100 Subject: tests/check-qnum: Cover qnum_to_string() for "unround" argument qnum_to_string() has a FIXME comment about rounding errors due to insufficient precision. Cover it: 2.718281828459045 gets converted to "2.718282". The next commit will fix it. Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-6-armbru@redhat.com> --- tests/check-qnum.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests') diff --git a/tests/check-qnum.c b/tests/check-qnum.c index 4105015..a73809d 100644 --- a/tests/check-qnum.c +++ b/tests/check-qnum.c @@ -150,6 +150,12 @@ static void qnum_to_string_test(void) g_assert_cmpstr(tmp, ==, "0.42"); g_free(tmp); qobject_unref(qn); + + qn = qnum_from_double(2.718281828459045); + tmp = qnum_to_string(qn); + g_assert_cmpstr(tmp, ==, "2.718282"); /* BUG */ + g_free(tmp); + qobject_unref(qn); } int main(int argc, char **argv) -- cgit v1.1 From f917eed3069640f6fa15f07cc5a61ecf4270e6a3 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:48 +0100 Subject: qobject: Fix qnum_to_string() to use sufficient precision We should serialize numbers to JSON so that they deserialize back to the same number. We fail to do so. The culprit is qnum_to_string(): it uses format %f with trailing '0' trimmed. Results in pretty output for "nice" numbers, but is prone to nasty rounding errors. For instance, numbers between 0 and 0.0000005 get flushed to zero. Where exactly the incorrect rounding can bite is tiresome to gauge. Here's my take. * In QMP output, type 'number': - query-blockstats value avg_rd_queue_depth - QMP query-migrate values mbps, cache-miss-rate, encoding-rate, busy-rate, compression-rate. Relatively harmless, I guess. * In tracing QMP input. Harmless. * In qemu-ga output, type 'number': guest-get-users value login-time. Harmless. * In output of HMP qom-get. Harmless. Not affected, because double values don't actually occur there (I think): * QMP output, type 'any': * qom-get value * qom-list, qom-list-properties value default-value * query-cpu-model-comparison, query-cpu-model-baseline, query-cpu-model-expansion value props. * qemu-img --output json output. * "json:" pseudo-filenames generated by bdrv_refresh_filename(). * The rbd block driver's "=keyvalue-pairs" hack. * In -object help on property default values. Aside: use of JSON feels inappropriate here. * Output of HMP qom-get. * Argument conversion to QemuOpts for qdev_device_add() and HMP with qemu_opts_from_qdict() QMP and HMP device_add, virtio-net failover primary creation, xen-usb "usb-host" creation, HMP netdev_add, object_add. * The uses of qobject_input_visitor_new_flat_confused() As far as I can tell, none of the visited types contain double values. * Dumping ImageInfoSpecific with dump_qobject() Fix by formatting with %.17g. 17 decimal digits always suffice for IEEE double. The change to expected test output illustrates the effect: the rounding errors are gone, but some seemingly "nice" numbers now get converted to not so nice strings, e.g. 0.42 to "0.41999999999999998". This is because 0.42 is not representable exactly in double. It's more accurate in this example than strictly necessary, though. If ugly accuracy bothers us, we can we can try using the least number of digits that still converts back to the same double. In this example, "0.42" would do. Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-7-armbru@redhat.com> --- tests/check-qjson.c | 8 ++++---- tests/check-qnum.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/check-qjson.c b/tests/check-qjson.c index 98515b1..ca8fb81 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -882,10 +882,10 @@ static void float_number(void) } test_cases[] = { { "32.43", 32.43 }, { "0.222", 0.222 }, - { "-32.12313", -32.12313 }, - { "-32.20e-10", -32.20e-10, "-0" /* BUG */ }, - { "18446744073709551616", 0x1p64 }, - { "-9223372036854775809", -0x1p63, "-9223372036854775808" }, + { "-32.12313", -32.12313, "-32.123130000000003" }, + { "-32.20e-10", -32.20e-10, "-3.22e-09" }, + { "18446744073709551616", 0x1p64, "1.8446744073709552e+19" }, + { "-9223372036854775809", -0x1p63, "-9.2233720368547758e+18" }, {}, }; int i; diff --git a/tests/check-qnum.c b/tests/check-qnum.c index a73809d..b85fca2 100644 --- a/tests/check-qnum.c +++ b/tests/check-qnum.c @@ -147,13 +147,13 @@ static void qnum_to_string_test(void) qn = qnum_from_double(0.42); tmp = qnum_to_string(qn); - g_assert_cmpstr(tmp, ==, "0.42"); + g_assert_cmpstr(tmp, ==, "0.41999999999999998"); g_free(tmp); qobject_unref(qn); qn = qnum_from_double(2.718281828459045); tmp = qnum_to_string(qn); - g_assert_cmpstr(tmp, ==, "2.718282"); /* BUG */ + g_assert_cmpstr(tmp, ==, "2.7182818284590451"); g_free(tmp); qobject_unref(qn); } -- cgit v1.1 From 7b205a7373c25db2f3680dee5a8c82e038135ec1 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:49 +0100 Subject: test-string-output-visitor: Cover "unround" number This demonstrates rounding error due to insufficient precision: double 3.1415926535897932 gets converted to JSON 3.141593. Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-8-armbru@redhat.com> --- tests/test-string-output-visitor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test-string-output-visitor.c b/tests/test-string-output-visitor.c index 9f65814..cec2084 100644 --- a/tests/test-string-output-visitor.c +++ b/tests/test-string-output-visitor.c @@ -130,13 +130,13 @@ static void test_visitor_out_bool(TestOutputVisitorData *data, static void test_visitor_out_number(TestOutputVisitorData *data, const void *unused) { - double value = 3.14; + double value = 3.1415926535897932; char *str; visit_type_number(data->ov, NULL, &value, &error_abort); str = visitor_get(data); - g_assert_cmpstr(str, ==, "3.140000"); + g_assert_cmpstr(str, ==, "3.141593"); } static void test_visitor_out_string(TestOutputVisitorData *data, -- cgit v1.1 From 54addb01d8c2511ef96b0f0ca6b695d120dd8363 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:50 +0100 Subject: string-output-visitor: Fix to use sufficient precision The string output visitor should serialize numbers so that the string input visitor deserializes them back to the same number. It fails to do so. print_type_number() uses format %f. This is prone to nasty rounding errors. For instance, numbers between 0 and 0.0000005 get flushed to zero. We currently use this visitor only for HMP info migrate, info network, info qtree, and info memdev. No double values occur there as far as I can tell. Fix anyway by formatting with %.17g. 17 decimal digits always suffice for IEEE double. See also recent commit "qobject: Fix qnum_to_string() to use sufficient precision". Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-9-armbru@redhat.com> --- tests/test-string-output-visitor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test-string-output-visitor.c b/tests/test-string-output-visitor.c index cec2084..0dae04b 100644 --- a/tests/test-string-output-visitor.c +++ b/tests/test-string-output-visitor.c @@ -136,7 +136,7 @@ static void test_visitor_out_number(TestOutputVisitorData *data, visit_type_number(data->ov, NULL, &value, &error_abort); str = visitor_get(data); - g_assert_cmpstr(str, ==, "3.141593"); + g_assert_cmpstr(str, ==, "3.1415926535897931"); } static void test_visitor_out_string(TestOutputVisitorData *data, -- cgit v1.1 From 2a02c1398a47e75aa6963baf7dbfa68a54dc2e41 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:51 +0100 Subject: test-visitor-serialization: Drop insufficient precision workaround Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-10-armbru@redhat.com> --- tests/test-visitor-serialization.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) (limited to 'tests') diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-serialization.c index 12275e5..cf19924 100644 --- a/tests/test-visitor-serialization.c +++ b/tests/test-visitor-serialization.c @@ -311,17 +311,7 @@ static void test_primitives(gconstpointer opaque) g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string); g_free((char *)pt_copy->value.string); } else if (pt->type == PTYPE_NUMBER) { - GString *double_expected = g_string_new(""); - GString *double_actual = g_string_new(""); - /* we serialize with %f for our reference visitors, so rather than fuzzy - * floating math to test "equality", just compare the formatted values - */ - g_string_printf(double_expected, "%.6f", pt->value.number); - g_string_printf(double_actual, "%.6f", pt_copy->value.number); - g_assert_cmpstr(double_actual->str, ==, double_expected->str); - - g_string_free(double_expected, true); - g_string_free(double_actual, true); + g_assert_cmpfloat(pt->value.number, ==, pt_copy->value.number); } else if (pt->type == PTYPE_BOOLEAN) { g_assert_cmpint(!!pt->value.max, ==, !!pt->value.max); } else { @@ -703,10 +693,6 @@ static PrimitiveType pt_values[] = { .value.boolean = 0, }, /* number tests (double) */ - /* note: we format these to %.6f before comparing, since that's how - * we serialize them and it doesn't make sense to check precision - * beyond that. - */ { .description = "number_sanity1", .type = PTYPE_NUMBER, @@ -715,7 +701,7 @@ static PrimitiveType pt_values[] = { { .description = "number_sanity2", .type = PTYPE_NUMBER, - .value.number = 3.14159265, + .value.number = 3.141593, }, { .description = "number_min", -- cgit v1.1 From 28f1c1f6e07c4bb4d79bed9474d1425c55e21712 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 Dec 2020 17:14:52 +0100 Subject: test-visitor-serialization: Clean up test_primitives() test_primitives() uses union member intmax_t max to compare the integer members. Unspecified behavior. Has worked fine for many years, though. Clean it up. Signed-off-by: Markus Armbruster Message-Id: <20201210161452.2813491-11-armbru@redhat.com> --- tests/test-visitor-serialization.c | 44 ++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-serialization.c index cf19924..dfe120a 100644 --- a/tests/test-visitor-serialization.c +++ b/tests/test-visitor-serialization.c @@ -55,7 +55,6 @@ typedef struct PrimitiveType { int16_t s16; int32_t s32; int64_t s64; - intmax_t max; } value; enum PrimitiveTypeKind type; const char *description; @@ -307,15 +306,46 @@ static void test_primitives(gconstpointer opaque) &error_abort); g_assert(pt_copy != NULL); - if (pt->type == PTYPE_STRING) { + switch (pt->type) { + case PTYPE_STRING: g_assert_cmpstr(pt->value.string, ==, pt_copy->value.string); g_free((char *)pt_copy->value.string); - } else if (pt->type == PTYPE_NUMBER) { + break; + case PTYPE_BOOLEAN: + g_assert_cmpint(pt->value.boolean, ==, pt->value.boolean); + break; + case PTYPE_NUMBER: g_assert_cmpfloat(pt->value.number, ==, pt_copy->value.number); - } else if (pt->type == PTYPE_BOOLEAN) { - g_assert_cmpint(!!pt->value.max, ==, !!pt->value.max); - } else { - g_assert_cmpint(pt->value.max, ==, pt_copy->value.max); + break; + case PTYPE_INTEGER: + g_assert_cmpint(pt->value.integer, ==, pt_copy->value.integer); + break; + case PTYPE_U8: + g_assert_cmpuint(pt->value.u8, ==, pt_copy->value.u8); + break; + case PTYPE_U16: + g_assert_cmpuint(pt->value.u16, ==, pt_copy->value.u16); + break; + case PTYPE_U32: + g_assert_cmpuint(pt->value.u32, ==, pt_copy->value.u32); + break; + case PTYPE_U64: + g_assert_cmpuint(pt->value.u64, ==, pt_copy->value.u64); + break; + case PTYPE_S8: + g_assert_cmpint(pt->value.s8, ==, pt_copy->value.s8); + break; + case PTYPE_S16: + g_assert_cmpint(pt->value.s16, ==, pt_copy->value.s16); + break; + case PTYPE_S32: + g_assert_cmpint(pt->value.s32, ==, pt_copy->value.s32); + break; + case PTYPE_S64: + g_assert_cmpint(pt->value.s64, ==, pt_copy->value.s64); + break; + case PTYPE_EOL: + g_assert_not_reached(); } ops->cleanup(serialize_data); -- cgit v1.1 From 6589f4599151201a61d6b1be8450adb63ae81017 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 11 Dec 2020 18:11:35 +0100 Subject: qobject: Make qobject_to_json_pretty() take a pretty argument Signed-off-by: Markus Armbruster Message-Id: <20201211171152.146877-4-armbru@redhat.com> --- tests/qtest/libqtest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c index e49f3a1..213fa4f 100644 --- a/tests/qtest/libqtest.c +++ b/tests/qtest/libqtest.c @@ -1197,7 +1197,7 @@ void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...) g_assert(response); if (!qdict_haskey(response, "return")) { - QString *s = qobject_to_json_pretty(QOBJECT(response)); + QString *s = qobject_to_json_pretty(QOBJECT(response), true); g_test_message("%s", qstring_get_str(s)); qobject_unref(s); } -- cgit v1.1 From eab3a4678b07267c39e7290a6e9e7690b1d2a521 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 11 Dec 2020 18:11:37 +0100 Subject: qobject: Change qobject_to_json()'s value to GString qobject_to_json() and qobject_to_json_pretty() build a GString, then covert it to QString. Just one of the callers actually needs a QString: qemu_rbd_parse_filename(). A few others need a string they can modify: qmp_send_response(), qga's send_response(), to_json_str(), and qmp_fd_vsend_fds(). The remainder just need a string. Change qobject_to_json() and qobject_to_json_pretty() to return the GString. qemu_rbd_parse_filename() now has to convert to QString. All others save a QString temporary. to_json_str() actually becomes a bit simpler, because GString provides more convenient modification functions. Signed-off-by: Markus Armbruster Message-Id: <20201211171152.146877-6-armbru@redhat.com> --- tests/check-qjson.c | 56 ++++++++++++++++++-------------------- tests/qtest/libqtest.c | 20 ++++++-------- tests/test-visitor-serialization.c | 6 ++-- 3 files changed, 39 insertions(+), 43 deletions(-) (limited to 'tests') diff --git a/tests/check-qjson.c b/tests/check-qjson.c index ca8fb81..337add0 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -35,17 +35,15 @@ static QString *from_json_str(const char *jstr, bool single, Error **errp) static char *to_json_str(QString *str) { - QString *json = qobject_to_json(QOBJECT(str)); - char *jstr; + GString *json = qobject_to_json(QOBJECT(str)); if (!json) { return NULL; } /* peel off double quotes */ - jstr = g_strndup(qstring_get_str(json) + 1, - qstring_get_length(json) - 2); - qobject_unref(json); - return jstr; + g_string_truncate(json, json->len - 1); + g_string_erase(json, 0, 1); + return g_string_free(json, false); } static void escaped_string(void) @@ -809,7 +807,7 @@ static void int_number(void) QNum *qnum; int64_t ival; uint64_t uval; - QString *str; + GString *str; for (i = 0; test_cases[i].encoded; i++) { qnum = qobject_to(QNum, @@ -828,9 +826,9 @@ static void int_number(void) (double)test_cases[i].decoded); str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, + g_assert_cmpstr(str->str, ==, test_cases[i].reencoded ?: test_cases[i].encoded); - qobject_unref(str); + g_string_free(str, true); qobject_unref(qnum); } @@ -851,7 +849,7 @@ static void uint_number(void) QNum *qnum; int64_t ival; uint64_t uval; - QString *str; + GString *str; for (i = 0; test_cases[i].encoded; i++) { qnum = qobject_to(QNum, @@ -865,9 +863,9 @@ static void uint_number(void) (double)test_cases[i].decoded); str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, + g_assert_cmpstr(str->str, ==, test_cases[i].reencoded ?: test_cases[i].encoded); - qobject_unref(str); + g_string_free(str, true); qobject_unref(qnum); } @@ -892,7 +890,7 @@ static void float_number(void) QNum *qnum; int64_t ival; uint64_t uval; - QString *str; + GString *str; for (i = 0; test_cases[i].encoded; i++) { qnum = qobject_to(QNum, @@ -904,9 +902,9 @@ static void float_number(void) g_assert(!qnum_get_try_uint(qnum, &uval)); str = qobject_to_json(QOBJECT(qnum)); - g_assert_cmpstr(qstring_get_str(str), ==, + g_assert_cmpstr(str->str, ==, test_cases[i].reencoded ?: test_cases[i].encoded); - qobject_unref(str); + g_string_free(str, true); qobject_unref(qnum); } @@ -917,7 +915,7 @@ static void keyword_literal(void) QObject *obj; QBool *qbool; QNull *null; - QString *str; + GString *str; obj = qobject_from_json("true", &error_abort); qbool = qobject_to(QBool, obj); @@ -925,8 +923,8 @@ static void keyword_literal(void) g_assert(qbool_get_bool(qbool) == true); str = qobject_to_json(obj); - g_assert(strcmp(qstring_get_str(str), "true") == 0); - qobject_unref(str); + g_assert_cmpstr(str->str, ==, "true"); + g_string_free(str, true); qobject_unref(qbool); @@ -936,8 +934,8 @@ static void keyword_literal(void) g_assert(qbool_get_bool(qbool) == false); str = qobject_to_json(obj); - g_assert(strcmp(qstring_get_str(str), "false") == 0); - qobject_unref(str); + g_assert_cmpstr(str->str, ==, "false"); + g_string_free(str, true); qobject_unref(qbool); @@ -1087,7 +1085,7 @@ static void simple_dict(void) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; - QString *str; + GString *str; obj = qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); @@ -1095,10 +1093,10 @@ static void simple_dict(void) str = qobject_to_json(obj); qobject_unref(obj); - obj = qobject_from_json(qstring_get_str(str), &error_abort); + obj = qobject_from_json(str->str, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); qobject_unref(obj); - qobject_unref(str); + g_string_free(str, true); } } @@ -1196,7 +1194,7 @@ static void simple_list(void) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; - QString *str; + GString *str; obj = qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); @@ -1204,10 +1202,10 @@ static void simple_list(void) str = qobject_to_json(obj); qobject_unref(obj); - obj = qobject_from_json(qstring_get_str(str), &error_abort); + obj = qobject_from_json(str->str, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); qobject_unref(obj); - qobject_unref(str); + g_string_free(str, true); } } @@ -1258,7 +1256,7 @@ static void simple_whitespace(void) for (i = 0; test_cases[i].encoded; i++) { QObject *obj; - QString *str; + GString *str; obj = qobject_from_json(test_cases[i].encoded, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); @@ -1266,11 +1264,11 @@ static void simple_whitespace(void) str = qobject_to_json(obj); qobject_unref(obj); - obj = qobject_from_json(qstring_get_str(str), &error_abort); + obj = qobject_from_json(str->str, &error_abort); g_assert(qlit_equal_qobject(&test_cases[i].decoded, obj)); qobject_unref(obj); - qobject_unref(str); + g_string_free(str, true); } } diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c index 213fa4f..8e93b0a 100644 --- a/tests/qtest/libqtest.c +++ b/tests/qtest/libqtest.c @@ -652,27 +652,25 @@ void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num, /* No need to send anything for an empty QObject. */ if (qobj) { int log = getenv("QTEST_LOG") != NULL; - QString *qstr = qobject_to_json(qobj); - const char *str; + GString *str = qobject_to_json(qobj); /* * BUG: QMP doesn't react to input until it sees a newline, an * object, or an array. Work-around: give it a newline. */ - qstring_append_chr(qstr, '\n'); - str = qstring_get_str(qstr); + g_string_append_c(str, '\n'); if (log) { - fprintf(stderr, "%s", str); + fprintf(stderr, "%s", str->str); } /* Send QMP request */ if (fds && fds_num > 0) { - socket_send_fds(fd, fds, fds_num, str, qstring_get_length(qstr)); + socket_send_fds(fd, fds, fds_num, str->str, str->len); } else { - socket_send(fd, str, qstring_get_length(qstr)); + socket_send(fd, str->str, str->len); } - qobject_unref(qstr); + g_string_free(str, true); qobject_unref(qobj); } } @@ -1197,9 +1195,9 @@ void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...) g_assert(response); if (!qdict_haskey(response, "return")) { - QString *s = qobject_to_json_pretty(QOBJECT(response), true); - g_test_message("%s", qstring_get_str(s)); - qobject_unref(s); + GString *s = qobject_to_json_pretty(QOBJECT(response), true); + g_test_message("%s", s->str); + g_string_free(s, true); } g_assert(qdict_haskey(response, "return")); qobject_unref(response); diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-serialization.c index dfe120a..4629958 100644 --- a/tests/test-visitor-serialization.c +++ b/tests/test-visitor-serialization.c @@ -957,15 +957,15 @@ static void qmp_deserialize(void **native_out, void *datap, VisitorFunc visit, Error **errp) { QmpSerializeData *d = datap; - QString *output_json; + GString *output_json; QObject *obj_orig, *obj; visit_complete(d->qov, &d->obj); obj_orig = d->obj; output_json = qobject_to_json(obj_orig); - obj = qobject_from_json(qstring_get_str(output_json), &error_abort); + obj = qobject_from_json(output_json->str, &error_abort); - qobject_unref(output_json); + g_string_free(output_json, true); d->qiv = qobject_input_visitor_new(obj); qobject_unref(obj_orig); qobject_unref(obj); -- cgit v1.1 From b3119b08143a036e8089fb442f9d42c4920ba22c Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 11 Dec 2020 18:11:45 +0100 Subject: qobject: Drop qstring_get_try_str() No users left outside tests/, and the ones in tests/ can just as well use qstring_get_str(). Do that, and drop the function. Signed-off-by: Markus Armbruster Message-Id: <20201211171152.146877-14-armbru@redhat.com> --- tests/check-qjson.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/check-qjson.c b/tests/check-qjson.c index 337add0..c845f91 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -89,7 +89,7 @@ static void escaped_string(void) for (j = 0; j < 2; j++) { if (test_cases[i].utf8_out) { cstr = from_json_str(test_cases[i].json_in, j, &error_abort); - g_assert_cmpstr(qstring_get_try_str(cstr), + g_assert_cmpstr(qstring_get_str(cstr), ==, test_cases[i].utf8_out); if (!test_cases[i].skip) { jstr = to_json_str(cstr); @@ -751,7 +751,7 @@ static void utf8_string(void) /* Parse @json_in, expect @utf8_out */ if (utf8_out) { str = from_json_str(json_in, j, &error_abort); - g_assert_cmpstr(qstring_get_try_str(str), ==, utf8_out); + g_assert_cmpstr(qstring_get_str(str), ==, utf8_out); qobject_unref(str); } else { str = from_json_str(json_in, j, NULL); @@ -782,7 +782,7 @@ static void utf8_string(void) /* Parse @json_out right back, unless it has replacements */ if (!strstr(json_out, "\\uFFFD")) { str = from_json_str(json_out, j, &error_abort); - g_assert_cmpstr(qstring_get_try_str(str), ==, utf8_in); + g_assert_cmpstr(qstring_get_str(str), ==, utf8_in); qobject_unref(str); } } @@ -1021,9 +1021,8 @@ static void interpolation_valid(void) /* string */ - qstr = qobject_to(QString, - qobject_from_jsonf_nofail("%s", value_s)); - g_assert_cmpstr(qstring_get_try_str(qstr), ==, value_s); + qstr = qobject_to(QString, qobject_from_jsonf_nofail("%s", value_s)); + g_assert_cmpstr(qstring_get_str(qstr), ==, value_s); qobject_unref(qstr); /* object */ -- cgit v1.1 From 4ac76ba414ecb94f086d73621775d8b38b6f0a43 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 11 Dec 2020 18:11:52 +0100 Subject: qobject: Make QString immutable The functions to modify a QString's string are all unused now. Drop them, and make the string immutable. Saves 16 bytes per QString on my system. Signed-off-by: Markus Armbruster Message-Id: <20201211171152.146877-21-armbru@redhat.com> --- tests/check-qobject.c | 3 +-- tests/check-qstring.c | 16 ---------------- 2 files changed, 1 insertion(+), 18 deletions(-) (limited to 'tests') diff --git a/tests/check-qobject.c b/tests/check-qobject.c index 6b6deae..c1713d1 100644 --- a/tests/check-qobject.c +++ b/tests/check-qobject.c @@ -155,8 +155,7 @@ static void qobject_is_equal_string_test(void) str_case = qstring_from_str("Foo"); /* Should yield "foo" */ - str_built = qstring_from_substr("form", 0, 2); - qstring_append_chr(str_built, 'o'); + str_built = qstring_from_substr("buffoon", 3, 6); check_unequal(str_base, str_whitespace_0, str_whitespace_1, str_whitespace_2, str_whitespace_3, str_case); diff --git a/tests/check-qstring.c b/tests/check-qstring.c index 2d07992..4bf9772 100644 --- a/tests/check-qstring.c +++ b/tests/check-qstring.c @@ -47,21 +47,6 @@ static void qstring_get_str_test(void) qobject_unref(qstring); } -static void qstring_append_chr_test(void) -{ - int i; - QString *qstring; - const char *str = "qstring append char unit-test"; - - qstring = qstring_new(); - - for (i = 0; str[i]; i++) - qstring_append_chr(qstring, str[i]); - - g_assert(strcmp(str, qstring_get_str(qstring)) == 0); - qobject_unref(qstring); -} - static void qstring_from_substr_test(void) { QString *qs; @@ -90,7 +75,6 @@ int main(int argc, char **argv) g_test_add_func("/public/from_str", qstring_from_str_test); g_test_add_func("/public/get_str", qstring_get_str_test); - g_test_add_func("/public/append_chr", qstring_append_chr_test); g_test_add_func("/public/from_substr", qstring_from_substr_test); g_test_add_func("/public/to_qstring", qobject_to_qstring_test); -- cgit v1.1