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 | |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 14 | #include "qapi/qmp/qnum.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 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 | |
Marc-André Lureau | 36aeb60 | 2017-06-07 20:36:00 +0400 | [diff] [blame] | 32 | QDECREF(qlist); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 33 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 34 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 35 | static void qlist_append_test(void) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 36 | { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 37 | QNum *qi; |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 38 | QList *qlist; |
| 39 | QListEntry *entry; |
| 40 | |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 41 | qi = qnum_from_int(42); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 42 | |
| 43 | qlist = qlist_new(); |
| 44 | qlist_append(qlist, qi); |
| 45 | |
| 46 | entry = QTAILQ_FIRST(&qlist->head); |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 47 | g_assert(entry != NULL); |
| 48 | g_assert(entry->value == QOBJECT(qi)); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 49 | |
Marc-André Lureau | 36aeb60 | 2017-06-07 20:36:00 +0400 | [diff] [blame] | 50 | QDECREF(qlist); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 51 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 52 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 53 | static void qobject_to_qlist_test(void) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 54 | { |
| 55 | QList *qlist; |
| 56 | |
| 57 | qlist = qlist_new(); |
| 58 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 59 | g_assert(qobject_to_qlist(QOBJECT(qlist)) == qlist); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 60 | |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 61 | QDECREF(qlist); |
| 62 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 63 | |
| 64 | static int iter_called; |
| 65 | static const int iter_max = 42; |
| 66 | |
| 67 | static void iter_func(QObject *obj, void *opaque) |
| 68 | { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 69 | QNum *qi; |
| 70 | int64_t val; |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 71 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 72 | g_assert(opaque == NULL); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 73 | |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 74 | qi = qobject_to_qnum(obj); |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 75 | g_assert(qi != NULL); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 76 | |
| 77 | g_assert(qnum_get_try_int(qi, &val)); |
| 78 | g_assert_cmpint(val, >=, 0); |
| 79 | g_assert_cmpint(val, <=, iter_max); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 80 | |
| 81 | iter_called++; |
| 82 | } |
| 83 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 84 | static void qlist_iter_test(void) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 85 | { |
| 86 | int i; |
| 87 | QList *qlist; |
| 88 | |
| 89 | qlist = qlist_new(); |
| 90 | |
| 91 | for (i = 0; i < iter_max; i++) |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 92 | qlist_append_int(qlist, i); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 93 | |
| 94 | iter_called = 0; |
| 95 | qlist_iter(qlist, iter_func, NULL); |
| 96 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 97 | g_assert(iter_called == iter_max); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 98 | |
| 99 | QDECREF(qlist); |
| 100 | } |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 101 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 102 | int main(int argc, char **argv) |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 103 | { |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 104 | g_test_init(&argc, &argv, NULL); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 105 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 106 | g_test_add_func("/public/new", qlist_new_test); |
| 107 | g_test_add_func("/public/append", qlist_append_test); |
| 108 | g_test_add_func("/public/to_qlist", qobject_to_qlist_test); |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 109 | g_test_add_func("/public/iter", qlist_iter_test); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 110 | |
Anthony Liguori | 91479dd | 2012-01-10 13:10:48 -0600 | [diff] [blame] | 111 | return g_test_run(); |
Luiz Capitulino | 3aa3dcf | 2009-10-07 13:41:49 -0300 | [diff] [blame] | 112 | } |