[PATCH 10/32] tests: replace free_all() usage with g_auto

marcandre.lureau@redhat.com posted 32 patches 2 years, 8 months ago
Maintainers: Cameron Esfahani <dirty@apple.com>, Roman Bolshakov <r.bolshakov@yadro.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Gerd Hoffmann <kraxel@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, David Hildenbrand <david@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Stefan Berger <stefanb@linux.vnet.ibm.com>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Fam Zheng <fam@euphon.net>, Aarushi Mehta <mehta.aaru20@gmail.com>, Julia Suvorova <jusual@redhat.com>, Stefano Garzarella <sgarzare@redhat.com>, Ronnie Sahlberg <ronniesahlberg@gmail.com>, Peter Lieven <pl@kamp.de>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, "Denis V. Lunev" <den@openvz.org>, Vladimir Sementsov-Ogievskiy <v.sementsov-og@mail.ru>, Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>, Laurent Vivier <laurent@vivier.eu>, "Michael S. Tsirkin" <mst@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Yanan Wang <wangyanan55@huawei.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Greg Kurz <groug@kaod.org>, Christian Schoenebeck <qemu_oss@crudebyte.com>, "Alex Bennée" <alex.bennee@linaro.org>, Peter Maydell <peter.maydell@linaro.org>, Michael Rolnik <mrolnik@gmail.com>, Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>, Alistair Francis <alistair@alistair23.me>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Helge Deller <deller@gmx.de>, Cornelia Huck <cohuck@redhat.com>, Thomas Huth <thuth@redhat.com>, Halil Pasic <pasic@linux.ibm.com>, Christian Borntraeger <borntraeger@linux.ibm.com>, "Cédric Le Goater" <clg@kaod.org>, Daniel Henrique Barboza <danielhb413@gmail.com>, David Gibson <david@gibson.dropbear.id.au>, Huacai Chen <chenhuacai@kernel.org>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Aurelien Jarno <aurelien@aurel32.net>, "Hervé Poussineau" <hpoussin@reactos.org>, Aleksandar Rikalo <aleksandar.rikalo@syrmia.com>, Pavel Pisa <pisa@cmp.felk.cvut.cz>, Vikram Garhwal <fnu.vikram@xilinx.com>, Jason Wang <jasowang@redhat.com>, Subbaraya Sundeep <sundeep.lkml@gmail.com>, Havard Skinnemoen <hskinnemoen@google.com>, Tyrone Ting <kfting@nuvoton.com>, Dmitry Fleytman <dmitry.fleytman@gmail.com>, Chris Wulff <crwulff@gmail.com>, Marek Vasut <marex@denx.de>, BALATON Zoltan <balaton@eik.bme.hu>, Elena Ufimtseva <elena.ufimtseva@oracle.com>, Jagannathan Raman <jag.raman@oracle.com>, John G Johnson <john.g.johnson@oracle.com>, Alexey Kardashevskiy <aik@ozlabs.ru>, Yuval Shaia <yuval.shaia.ml@gmail.com>, Palmer Dabbelt <palmer@dabbelt.com>, Bin Meng <bin.meng@windriver.com>, Yoshinori Sato <ysato@users.sourceforge.jp>, Eric Farman <farman@linux.ibm.com>, Fabien Chouteau <chouteau@adacore.com>, KONRAD Frederic <frederic.konrad@adacore.com>, Artyom Tarasenko <atar4qemu@gmail.com>, Alex Williamson <alex.williamson@redhat.com>, Eric Auger <eric.auger@redhat.com>, Max Filippov <jcmvbkbc@gmail.com>, Peter Xu <peterx@redhat.com>, Markus Armbruster <armbru@redhat.com>, Stefan Weil <sw@weilnetz.de>, Taylor Simpson <tsimpson@quicinc.com>, Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, Zhang Chen <chen.zhang@intel.com>, Li Zhijian <lizhijian@fujitsu.com>, Michael Roth <michael.roth@amd.com>, Alexander Graf <agraf@csgraf.de>, Wenchao Wang <wenchao.wang@intel.com>, Colin Xu <colin.xu@intel.com>, Marcelo Tosatti <mtosatti@redhat.com>, Kamil Rytarowski <kamil@netbsd.org>, Reinoud Zandijk <reinoud@netbsd.org>, Sunil Muthuswamy <sunilmut@microsoft.com>, Stafford Horne <shorne@gmail.com>, Bastian Koppelmann <kbastian@mail.uni-paderborn.de>, Ani Sinha <ani@anisinha.ca>, John Snow <jsnow@redhat.com>, Andrew Jeffery <andrew@aj.id.au>, Joel Stanley <joel@jms.id.au>, Alexander Bulekov <alxndr@bu.edu>, Bandan Das <bsd@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Corey Minyard <minyard@acm.org>, Coiby Xu <Coiby.Xu@gmail.com>, Akihiko Odaki <akihiko.odaki@gmail.com>
There is a newer version of this series
[PATCH 10/32] tests: replace free_all() usage with g_auto
Posted by marcandre.lureau@redhat.com 2 years, 8 months ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Use more idiomatic glib/auto-style code.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 tests/unit/check-qobject.c | 127 ++++++++++++-------------------------
 1 file changed, 40 insertions(+), 87 deletions(-)

diff --git a/tests/unit/check-qobject.c b/tests/unit/check-qobject.c
index c3d50e99949a..7903ebf4cf80 100644
--- a/tests/unit/check-qobject.c
+++ b/tests/unit/check-qobject.c
@@ -74,21 +74,6 @@ static void do_test_equality(bool expected, int _, ...)
 #define check_unequal(...) \
     do_test_equality(false, 0, __VA_ARGS__, &test_equality_end_of_arguments)
 
-static void do_free_all(int _, ...)
-{
-    va_list ap;
-    QObject *obj;
-
-    va_start(ap, _);
-    while ((obj = va_arg(ap, QObject *)) != NULL) {
-        qobject_unref(obj);
-    }
-    va_end(ap);
-}
-
-#define free_all(...) \
-    do_free_all(0, __VA_ARGS__, NULL)
-
 static void qobject_is_equal_null_test(void)
 {
     check_unequal(qnull(), NULL);
@@ -96,15 +81,14 @@ static void qobject_is_equal_null_test(void)
 
 static void qobject_is_equal_num_test(void)
 {
-    QNum *u0, *i0, *d0, *dnan, *um42, *im42, *dm42;
+    g_autoptr(QNum) u0 = qnum_from_uint(0u);
+    g_autoptr(QNum) i0 = qnum_from_int(0);
+    g_autoptr(QNum) d0 = qnum_from_double(0.0);
+    g_autoptr(QNum) dnan = qnum_from_double(NAN);
+    g_autoptr(QNum) um42 = qnum_from_uint((uint64_t)-42);
+    g_autoptr(QNum) im42 = qnum_from_int(-42);
+    g_autoptr(QNum) dm42 = qnum_from_double(-42.0);
 
-    u0 = qnum_from_uint(0u);
-    i0 = qnum_from_int(0);
-    d0 = qnum_from_double(0.0);
-    dnan = qnum_from_double(NAN);
-    um42 = qnum_from_uint((uint64_t)-42);
-    im42 = qnum_from_int(-42);
-    dm42 = qnum_from_double(-42.0);
 
     /* Integers representing a mathematically equal number should
      * compare equal */
@@ -121,60 +105,45 @@ static void qobject_is_equal_num_test(void)
     check_unequal(um42, im42);
     check_unequal(um42, dm42);
     check_unequal(im42, dm42);
-
-    free_all(u0, i0, d0, dnan, um42, im42, dm42);
 }
 
 static void qobject_is_equal_bool_test(void)
 {
-    QBool *btrue_0, *btrue_1, *bfalse_0, *bfalse_1;
-
-    btrue_0 = qbool_from_bool(true);
-    btrue_1 = qbool_from_bool(true);
-    bfalse_0 = qbool_from_bool(false);
-    bfalse_1 = qbool_from_bool(false);
+    g_autoptr(QBool) btrue_0 = qbool_from_bool(true);
+    g_autoptr(QBool) btrue_1 = qbool_from_bool(true);
+    g_autoptr(QBool) bfalse_0 = qbool_from_bool(false);
+    g_autoptr(QBool) bfalse_1 = qbool_from_bool(false);
 
     check_equal(btrue_0, btrue_1);
     check_equal(bfalse_0, bfalse_1);
     check_unequal(btrue_0, bfalse_0);
-
-    free_all(btrue_0, btrue_1, bfalse_0, bfalse_1);
 }
 
 static void qobject_is_equal_string_test(void)
 {
-    QString *str_base, *str_whitespace_0, *str_whitespace_1, *str_whitespace_2;
-    QString *str_whitespace_3, *str_case, *str_built;
-
-    str_base = qstring_from_str("foo");
-    str_whitespace_0 = qstring_from_str(" foo");
-    str_whitespace_1 = qstring_from_str("foo ");
-    str_whitespace_2 = qstring_from_str("foo\b");
-    str_whitespace_3 = qstring_from_str("fooo\b");
-    str_case = qstring_from_str("Foo");
-
+    g_autoptr(QString) str_base = qstring_from_str("foo");
+    g_autoptr(QString) str_whitespace_0 = qstring_from_str(" foo");
+    g_autoptr(QString) str_whitespace_1 = qstring_from_str("foo ");
+    g_autoptr(QString) str_whitespace_2 = qstring_from_str("foo\b");
+    g_autoptr(QString) str_whitespace_3 = qstring_from_str("fooo\b");
+    g_autoptr(QString) str_case = qstring_from_str("Foo");
     /* Should yield "foo" */
-    str_built = qstring_from_substr("buffoon", 3, 6);
+    g_autoptr(QString) 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);
 
     check_equal(str_base, str_built);
-
-    free_all(str_base, str_whitespace_0, str_whitespace_1, str_whitespace_2,
-             str_whitespace_3, str_case, str_built);
 }
 
 static void qobject_is_equal_list_test(void)
 {
-    QList *list_0, *list_1, *list_cloned;
-    QList *list_reordered, *list_longer, *list_shorter;
-
-    list_0 = qlist_new();
-    list_1 = qlist_new();
-    list_reordered = qlist_new();
-    list_longer = qlist_new();
-    list_shorter = qlist_new();
+    g_autoptr(QList) list_0 = qlist_new();
+    g_autoptr(QList) list_1 = qlist_new();
+    g_autoptr(QList) list_reordered = qlist_new();
+    g_autoptr(QList) list_longer = qlist_new();
+    g_autoptr(QList) list_shorter = qlist_new();
+    g_autoptr(QList) list_cloned = NULL;
 
     qlist_append_int(list_0, 1);
     qlist_append_int(list_0, 2);
@@ -205,26 +174,20 @@ static void qobject_is_equal_list_test(void)
      * itself */
     qlist_append(list_0, qnum_from_double(NAN));
     g_assert(qobject_is_equal(QOBJECT(list_0), QOBJECT(list_0)) == false);
-
-    free_all(list_0, list_1, list_cloned, list_reordered, list_longer,
-             list_shorter);
 }
 
 static void qobject_is_equal_dict_test(void)
 {
-    QDict *dict_0, *dict_1, *dict_cloned;
-    QDict *dict_different_key, *dict_different_value, *dict_different_null_key;
-    QDict *dict_longer, *dict_shorter, *dict_nested;
-    QDict *dict_crumpled;
-
-    dict_0 = qdict_new();
-    dict_1 = qdict_new();
-    dict_different_key = qdict_new();
-    dict_different_value = qdict_new();
-    dict_different_null_key = qdict_new();
-    dict_longer = qdict_new();
-    dict_shorter = qdict_new();
-    dict_nested = qdict_new();
+    g_autoptr(QDict) dict_cloned = NULL;
+    g_autoptr(QDict) dict_crumpled = NULL;
+    g_autoptr(QDict) dict_0 = qdict_new();
+    g_autoptr(QDict) dict_1 = qdict_new();
+    g_autoptr(QDict) dict_different_key = qdict_new();
+    g_autoptr(QDict) dict_different_value = qdict_new();
+    g_autoptr(QDict) dict_different_null_key = qdict_new();
+    g_autoptr(QDict) dict_longer = qdict_new();
+    g_autoptr(QDict) dict_shorter = qdict_new();
+    g_autoptr(QDict) dict_nested = qdict_new();
 
     qdict_put_int(dict_0, "f.o", 1);
     qdict_put_int(dict_0, "bar", 2);
@@ -284,31 +247,21 @@ static void qobject_is_equal_dict_test(void)
      * itself */
     qdict_put(dict_0, "NaN", qnum_from_double(NAN));
     g_assert(qobject_is_equal(QOBJECT(dict_0), QOBJECT(dict_0)) == false);
-
-    free_all(dict_0, dict_1, dict_cloned, dict_different_key,
-             dict_different_value, dict_different_null_key, dict_longer,
-             dict_shorter, dict_nested, dict_crumpled);
 }
 
 static void qobject_is_equal_conversion_test(void)
 {
-    QNum *u0, *i0, *d0;
-    QString *s0, *s_empty;
-    QBool *bfalse;
-
-    u0 = qnum_from_uint(0u);
-    i0 = qnum_from_int(0);
-    d0 = qnum_from_double(0.0);
-    s0 = qstring_from_str("0");
-    s_empty = qstring_new();
-    bfalse = qbool_from_bool(false);
+    g_autoptr(QNum) u0 = qnum_from_uint(0u);
+    g_autoptr(QNum) i0 = qnum_from_int(0);
+    g_autoptr(QNum) d0 = qnum_from_double(0.0);
+    g_autoptr(QString) s0 = qstring_from_str("0");
+    g_autoptr(QString) s_empty = qstring_new();
+    g_autoptr(QBool) bfalse = qbool_from_bool(false);
 
     /* No automatic type conversion */
     check_unequal(u0, s0, s_empty, bfalse, qnull(), NULL);
     check_unequal(i0, s0, s_empty, bfalse, qnull(), NULL);
     check_unequal(d0, s0, s_empty, bfalse, qnull(), NULL);
-
-    free_all(u0, i0, d0, s0, s_empty, bfalse);
 }
 
 int main(int argc, char **argv)
-- 
2.35.1.273.ge6ebfd0e8cbb


Re: [PATCH 10/32] tests: replace free_all() usage with g_auto
Posted by Richard Henderson 2 years, 8 months ago
On 3/23/22 08:57, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau<marcandre.lureau@redhat.com>
> 
> Use more idiomatic glib/auto-style code.
> 
> Signed-off-by: Marc-André Lureau<marcandre.lureau@redhat.com>
> ---
>   tests/unit/check-qobject.c | 127 ++++++++++++-------------------------
>   1 file changed, 40 insertions(+), 87 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~