diff options
author | Luiz Capitulino <lcapitulino@redhat.com> | 2009-10-07 13:41:49 -0300 |
---|---|---|
committer | Anthony Liguori <aliguori@us.ibm.com> | 2009-10-08 21:17:18 -0500 |
commit | 3aa3dcfff66ca70d5983d3122f24724793046d66 (patch) | |
tree | fd7540cb33afe07b33e69eab2008e95dca78160b /check-qlist.c | |
parent | a6fd08eb625ae3c6a878f527270a9d384d1b04d2 (diff) | |
download | qemu-3aa3dcfff66ca70d5983d3122f24724793046d66.zip qemu-3aa3dcfff66ca70d5983d3122f24724793046d66.tar.gz qemu-3aa3dcfff66ca70d5983d3122f24724793046d66.tar.bz2 |
Introduce QList unit-tests
This suite contains tests to assure that QList API works as expected.
To execute it you should have check installed and build QEMU with
check support enabled (--enable-check-utests) and then run:
$ ./check-qlist
Patchworks-ID: 35333
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Diffstat (limited to 'check-qlist.c')
-rw-r--r-- | check-qlist.c | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/check-qlist.c b/check-qlist.c new file mode 100644 index 0000000..0117ef3 --- /dev/null +++ b/check-qlist.c @@ -0,0 +1,153 @@ +/* + * QList unit-tests. + * + * Copyright (C) 2009 Red Hat Inc. + * + * Authors: + * Luiz Capitulino <lcapitulino@redhat.com> + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ +#include <check.h> + +#include "qint.h" +#include "qlist.h" + +/* + * Public Interface test-cases + * + * (with some violations to access 'private' data) + */ + +START_TEST(qlist_new_test) +{ + QList *qlist; + + qlist = qlist_new(); + fail_unless(qlist != NULL); + fail_unless(qlist->base.refcnt == 1); + fail_unless(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST); + + // destroy doesn't exist yet + qemu_free(qlist); +} +END_TEST + +START_TEST(qlist_append_test) +{ + QInt *qi; + QList *qlist; + QListEntry *entry; + + qi = qint_from_int(42); + + qlist = qlist_new(); + qlist_append(qlist, qi); + + entry = QTAILQ_FIRST(&qlist->head); + fail_unless(entry != NULL); + fail_unless(entry->value == QOBJECT(qi)); + + // destroy doesn't exist yet + QDECREF(qi); + qemu_free(entry); + qemu_free(qlist); +} +END_TEST + +START_TEST(qobject_to_qlist_test) +{ + QList *qlist; + + qlist = qlist_new(); + + fail_unless(qobject_to_qlist(QOBJECT(qlist)) == qlist); + + // destroy doesn't exist yet + qemu_free(qlist); +} +END_TEST + +START_TEST(qlist_destroy_test) +{ + int i; + QList *qlist; + + qlist = qlist_new(); + + for (i = 0; i < 42; i++) + qlist_append(qlist, qint_from_int(i)); + + QDECREF(qlist); +} +END_TEST + +static int iter_called; +static const int iter_max = 42; + +static void iter_func(QObject *obj, void *opaque) +{ + QInt *qi; + + fail_unless(opaque == NULL); + + qi = qobject_to_qint(obj); + fail_unless(qi != NULL); + fail_unless((qint_get_int(qi) >= 0) && (qint_get_int(qi) <= iter_max)); + + iter_called++; +} + +START_TEST(qlist_iter_test) +{ + 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); + + fail_unless(iter_called == iter_max); + + QDECREF(qlist); +} +END_TEST + +static Suite *QList_suite(void) +{ + Suite *s; + TCase *qlist_public_tcase; + + s = suite_create("QList suite"); + + qlist_public_tcase = tcase_create("Public Interface"); + suite_add_tcase(s, qlist_public_tcase); + tcase_add_test(qlist_public_tcase, qlist_new_test); + tcase_add_test(qlist_public_tcase, qlist_append_test); + tcase_add_test(qlist_public_tcase, qobject_to_qlist_test); + tcase_add_test(qlist_public_tcase, qlist_destroy_test); + tcase_add_test(qlist_public_tcase, qlist_iter_test); + + return s; +} + +int main(void) +{ + int nf; + Suite *s; + SRunner *sr; + + s = QList_suite(); + sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + nf = srunner_ntests_failed(sr); + srunner_free(sr); + + return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} |