2009-10-08 00:41:49 +08:00
|
|
|
/*
|
|
|
|
* QList unit-tests.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Luiz Capitulino <lcapitulino@redhat.com>
|
|
|
|
*
|
2010-05-13 03:34:42 +08:00
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
2009-10-08 00:41:49 +08:00
|
|
|
*/
|
2016-02-09 02:08:51 +08:00
|
|
|
#include "qemu/osdep.h"
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2017-06-08 00:35:58 +08:00
|
|
|
#include "qapi/qmp/qnum.h"
|
2012-12-18 01:19:43 +08:00
|
|
|
#include "qapi/qmp/qlist.h"
|
2009-10-08 00:41:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Public Interface test-cases
|
|
|
|
*
|
|
|
|
* (with some violations to access 'private' data)
|
|
|
|
*/
|
|
|
|
|
2012-01-11 03:10:48 +08:00
|
|
|
static void qlist_new_test(void)
|
2009-10-08 00:41:49 +08:00
|
|
|
{
|
|
|
|
QList *qlist;
|
|
|
|
|
|
|
|
qlist = qlist_new();
|
2012-01-11 03:10:48 +08:00
|
|
|
g_assert(qlist != NULL);
|
|
|
|
g_assert(qlist->base.refcnt == 1);
|
|
|
|
g_assert(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST);
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2018-04-19 23:01:43 +08:00
|
|
|
qobject_unref(qlist);
|
2009-10-08 00:41:49 +08:00
|
|
|
}
|
|
|
|
|
2012-01-11 03:10:48 +08:00
|
|
|
static void qlist_append_test(void)
|
2009-10-08 00:41:49 +08:00
|
|
|
{
|
2017-06-08 00:35:58 +08:00
|
|
|
QNum *qi;
|
2009-10-08 00:41:49 +08:00
|
|
|
QList *qlist;
|
|
|
|
QListEntry *entry;
|
|
|
|
|
2017-06-08 00:35:58 +08:00
|
|
|
qi = qnum_from_int(42);
|
2009-10-08 00:41:49 +08:00
|
|
|
|
|
|
|
qlist = qlist_new();
|
|
|
|
qlist_append(qlist, qi);
|
|
|
|
|
|
|
|
entry = QTAILQ_FIRST(&qlist->head);
|
2012-01-11 03:10:48 +08:00
|
|
|
g_assert(entry != NULL);
|
|
|
|
g_assert(entry->value == QOBJECT(qi));
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2018-04-19 23:01:43 +08:00
|
|
|
qobject_unref(qlist);
|
2009-10-08 00:41:49 +08:00
|
|
|
}
|
|
|
|
|
2012-01-11 03:10:48 +08:00
|
|
|
static void qobject_to_qlist_test(void)
|
2009-10-08 00:41:49 +08:00
|
|
|
{
|
|
|
|
QList *qlist;
|
|
|
|
|
|
|
|
qlist = qlist_new();
|
|
|
|
|
2018-02-24 23:40:29 +08:00
|
|
|
g_assert(qobject_to(QList, QOBJECT(qlist)) == qlist);
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2018-04-19 23:01:43 +08:00
|
|
|
qobject_unref(qlist);
|
2009-10-08 00:41:49 +08:00
|
|
|
}
|
|
|
|
|
2012-01-11 03:10:48 +08:00
|
|
|
static void qlist_iter_test(void)
|
2009-10-08 00:41:49 +08:00
|
|
|
{
|
2020-04-15 16:30:46 +08:00
|
|
|
const int iter_max = 42;
|
2009-10-08 00:41:49 +08:00
|
|
|
int i;
|
|
|
|
QList *qlist;
|
2020-04-15 16:30:46 +08:00
|
|
|
QListEntry *entry;
|
|
|
|
QNum *qi;
|
|
|
|
int64_t val;
|
2009-10-08 00:41:49 +08:00
|
|
|
|
|
|
|
qlist = qlist_new();
|
|
|
|
|
|
|
|
for (i = 0; i < iter_max; i++)
|
2017-04-28 05:58:17 +08:00
|
|
|
qlist_append_int(qlist, i);
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2020-04-15 16:30:46 +08:00
|
|
|
i = 0;
|
|
|
|
QLIST_FOREACH_ENTRY(qlist, entry) {
|
|
|
|
qi = qobject_to(QNum, qlist_entry_obj(entry));
|
|
|
|
g_assert(qi != NULL);
|
|
|
|
|
|
|
|
g_assert(qnum_get_try_int(qi, &val));
|
|
|
|
g_assert_cmpint(val, ==, i);
|
|
|
|
i++;
|
|
|
|
}
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2020-04-15 16:30:46 +08:00
|
|
|
g_assert(i == iter_max);
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2018-04-19 23:01:43 +08:00
|
|
|
qobject_unref(qlist);
|
2009-10-08 00:41:49 +08:00
|
|
|
}
|
|
|
|
|
2012-01-11 03:10:48 +08:00
|
|
|
int main(int argc, char **argv)
|
2009-10-08 00:41:49 +08:00
|
|
|
{
|
2012-01-11 03:10:48 +08:00
|
|
|
g_test_init(&argc, &argv, NULL);
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2012-01-11 03:10:48 +08:00
|
|
|
g_test_add_func("/public/new", qlist_new_test);
|
|
|
|
g_test_add_func("/public/append", qlist_append_test);
|
|
|
|
g_test_add_func("/public/to_qlist", qobject_to_qlist_test);
|
|
|
|
g_test_add_func("/public/iter", qlist_iter_test);
|
2009-10-08 00:41:49 +08:00
|
|
|
|
2012-01-11 03:10:48 +08:00
|
|
|
return g_test_run();
|
2009-10-08 00:41:49 +08:00
|
|
|
}
|