2009-10-07 16:41:49 +00:00
|
|
|
/*
|
|
|
|
* QList unit-tests.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Luiz Capitulino <lcapitulino@redhat.com>
|
|
|
|
*
|
2010-05-12 19:34:42 +00: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-07 16:41:49 +00:00
|
|
|
*/
|
2012-01-10 19:10:48 +00:00
|
|
|
#include <glib.h>
|
2009-10-07 16:41:49 +00:00
|
|
|
|
|
|
|
#include "qint.h"
|
|
|
|
#include "qlist.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Public Interface test-cases
|
|
|
|
*
|
|
|
|
* (with some violations to access 'private' data)
|
|
|
|
*/
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
static void qlist_new_test(void)
|
2009-10-07 16:41:49 +00:00
|
|
|
{
|
|
|
|
QList *qlist;
|
|
|
|
|
|
|
|
qlist = qlist_new();
|
2012-01-10 19:10:48 +00:00
|
|
|
g_assert(qlist != NULL);
|
|
|
|
g_assert(qlist->base.refcnt == 1);
|
|
|
|
g_assert(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST);
|
2009-10-07 16:41:49 +00:00
|
|
|
|
|
|
|
// destroy doesn't exist yet
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(qlist);
|
2009-10-07 16:41:49 +00:00
|
|
|
}
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
static void qlist_append_test(void)
|
2009-10-07 16:41:49 +00:00
|
|
|
{
|
|
|
|
QInt *qi;
|
|
|
|
QList *qlist;
|
|
|
|
QListEntry *entry;
|
|
|
|
|
|
|
|
qi = qint_from_int(42);
|
|
|
|
|
|
|
|
qlist = qlist_new();
|
|
|
|
qlist_append(qlist, qi);
|
|
|
|
|
|
|
|
entry = QTAILQ_FIRST(&qlist->head);
|
2012-01-10 19:10:48 +00:00
|
|
|
g_assert(entry != NULL);
|
|
|
|
g_assert(entry->value == QOBJECT(qi));
|
2009-10-07 16:41:49 +00:00
|
|
|
|
|
|
|
// destroy doesn't exist yet
|
|
|
|
QDECREF(qi);
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(entry);
|
|
|
|
g_free(qlist);
|
2009-10-07 16:41:49 +00:00
|
|
|
}
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
static void qobject_to_qlist_test(void)
|
2009-10-07 16:41:49 +00:00
|
|
|
{
|
|
|
|
QList *qlist;
|
|
|
|
|
|
|
|
qlist = qlist_new();
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
g_assert(qobject_to_qlist(QOBJECT(qlist)) == qlist);
|
2009-10-07 16:41:49 +00:00
|
|
|
|
|
|
|
// destroy doesn't exist yet
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(qlist);
|
2009-10-07 16:41:49 +00:00
|
|
|
}
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
static void qlist_destroy_test(void)
|
2009-10-07 16:41:49 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
QList *qlist;
|
|
|
|
|
|
|
|
qlist = qlist_new();
|
|
|
|
|
|
|
|
for (i = 0; i < 42; i++)
|
|
|
|
qlist_append(qlist, qint_from_int(i));
|
|
|
|
|
|
|
|
QDECREF(qlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iter_called;
|
|
|
|
static const int iter_max = 42;
|
|
|
|
|
|
|
|
static void iter_func(QObject *obj, void *opaque)
|
|
|
|
{
|
|
|
|
QInt *qi;
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
g_assert(opaque == NULL);
|
2009-10-07 16:41:49 +00:00
|
|
|
|
|
|
|
qi = qobject_to_qint(obj);
|
2012-01-10 19:10:48 +00:00
|
|
|
g_assert(qi != NULL);
|
|
|
|
g_assert((qint_get_int(qi) >= 0) && (qint_get_int(qi) <= iter_max));
|
2009-10-07 16:41:49 +00:00
|
|
|
|
|
|
|
iter_called++;
|
|
|
|
}
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
static void qlist_iter_test(void)
|
2009-10-07 16:41:49 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
QList *qlist;
|
|
|
|
|
|
|
|
qlist = qlist_new();
|
|
|
|
|
|
|
|
for (i = 0; i < iter_max; i++)
|
|
|
|
qlist_append(qlist, qint_from_int(i));
|
|
|
|
|
|
|
|
iter_called = 0;
|
|
|
|
qlist_iter(qlist, iter_func, NULL);
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
g_assert(iter_called == iter_max);
|
2009-10-07 16:41:49 +00:00
|
|
|
|
|
|
|
QDECREF(qlist);
|
|
|
|
}
|
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
int main(int argc, char **argv)
|
2009-10-07 16:41:49 +00:00
|
|
|
{
|
2012-01-10 19:10:48 +00:00
|
|
|
g_test_init(&argc, &argv, NULL);
|
2009-10-07 16:41:49 +00:00
|
|
|
|
2012-01-10 19:10:48 +00: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/destroy", qlist_destroy_test);
|
|
|
|
g_test_add_func("/public/iter", qlist_iter_test);
|
2009-10-07 16:41:49 +00:00
|
|
|
|
2012-01-10 19:10:48 +00:00
|
|
|
return g_test_run();
|
2009-10-07 16:41:49 +00:00
|
|
|
}
|