Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 1 | /* |
| 2 | * QList unit-tests. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
| 8 | * |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 11 | */ |
Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 12 | #include "qemu/osdep.h" |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 13 | |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 14 | #include "qapi/qmp/qint.h" |
| 15 | #include "qapi/qmp/qlist.h" |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Public Interface test-cases |
| 19 | * |
| 20 | * (with some violations to access 'private' data) |
| 21 | */ |
| 22 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 23 | static void qlist_new_test(void) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 24 | { |
| 25 | QList *qlist; |
| 26 | |
| 27 | qlist = qlist_new(); |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 28 | g_assert(qlist != NULL); |
| 29 | g_assert(qlist->base.refcnt == 1); |
| 30 | g_assert(qobject_type(QOBJECT(qlist)) == QTYPE_QLIST); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 31 | |
| 32 | // destroy doesn't exist yet |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 33 | g_free(qlist); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 34 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 35 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 36 | static void qlist_append_test(void) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 37 | { |
| 38 | QInt *qi; |
| 39 | QList *qlist; |
| 40 | QListEntry *entry; |
| 41 | |
| 42 | qi = qint_from_int(42); |
| 43 | |
| 44 | qlist = qlist_new(); |
| 45 | qlist_append(qlist, qi); |
| 46 | |
| 47 | entry = QTAILQ_FIRST(&qlist->head); |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 48 | g_assert(entry != NULL); |
| 49 | g_assert(entry->value == QOBJECT(qi)); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 50 | |
| 51 | // destroy doesn't exist yet |
| 52 | QDECREF(qi); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 53 | g_free(entry); |
| 54 | g_free(qlist); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 55 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 56 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 57 | static void qobject_to_qlist_test(void) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 58 | { |
| 59 | QList *qlist; |
| 60 | |
| 61 | qlist = qlist_new(); |
| 62 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 63 | g_assert(qobject_to_qlist(QOBJECT(qlist)) == qlist); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 64 | |
| 65 | // destroy doesn't exist yet |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 66 | g_free(qlist); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 67 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 68 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 69 | static void qlist_destroy_test(void) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 70 | { |
| 71 | int i; |
| 72 | QList *qlist; |
| 73 | |
| 74 | qlist = qlist_new(); |
| 75 | |
| 76 | for (i = 0; i < 42; i++) |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 77 | qlist_append_int(qlist, i); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 78 | |
| 79 | QDECREF(qlist); |
| 80 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 81 | |
| 82 | static int iter_called; |
| 83 | static const int iter_max = 42; |
| 84 | |
| 85 | static void iter_func(QObject *obj, void *opaque) |
| 86 | { |
| 87 | QInt *qi; |
| 88 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 89 | g_assert(opaque == NULL); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 90 | |
| 91 | qi = qobject_to_qint(obj); |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 92 | g_assert(qi != NULL); |
| 93 | g_assert((qint_get_int(qi) >= 0) && (qint_get_int(qi) <= iter_max)); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 94 | |
| 95 | iter_called++; |
| 96 | } |
| 97 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 98 | static void qlist_iter_test(void) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 99 | { |
| 100 | int i; |
| 101 | QList *qlist; |
| 102 | |
| 103 | qlist = qlist_new(); |
| 104 | |
| 105 | for (i = 0; i < iter_max; i++) |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 106 | qlist_append_int(qlist, i); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 107 | |
| 108 | iter_called = 0; |
| 109 | qlist_iter(qlist, iter_func, NULL); |
| 110 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 111 | g_assert(iter_called == iter_max); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 112 | |
| 113 | QDECREF(qlist); |
| 114 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 115 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 116 | int main(int argc, char **argv) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 117 | { |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 118 | g_test_init(&argc, &argv, NULL); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 119 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 120 | g_test_add_func("/public/new", qlist_new_test); |
| 121 | g_test_add_func("/public/append", qlist_append_test); |
| 122 | g_test_add_func("/public/to_qlist", qobject_to_qlist_test); |
| 123 | g_test_add_func("/public/destroy", qlist_destroy_test); |
| 124 | g_test_add_func("/public/iter", qlist_iter_test); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 125 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 126 | return g_test_run(); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 127 | } |