Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 1 | /* |
| 2 | * QDict unit-tests. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 8 | * |
| 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 | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 11 | */ |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 12 | |
Markus Armbruster | 47e6b29 | 2018-02-01 12:18:38 +0100 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 14 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | 992159c | 2018-09-26 14:23:09 +0200 | [diff] [blame] | 15 | #include "qapi/qmp/qnum.h" |
| 16 | #include "qapi/qmp/qstring.h" |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Public Interface test-cases |
| 20 | * |
| 21 | * (with some violations to access 'private' data) |
| 22 | */ |
| 23 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 24 | static void qdict_new_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 25 | { |
| 26 | QDict *qdict; |
| 27 | |
| 28 | qdict = qdict_new(); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 29 | g_assert(qdict != NULL); |
| 30 | g_assert(qdict_size(qdict) == 0); |
| 31 | g_assert(qdict->base.refcnt == 1); |
| 32 | g_assert(qobject_type(QOBJECT(qdict)) == QTYPE_QDICT); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 33 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 34 | qobject_unref(qdict); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 35 | } |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 36 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 37 | static void qdict_put_obj_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 38 | { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 39 | QNum *qn; |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 40 | QDict *qdict; |
| 41 | QDictEntry *ent; |
| 42 | const int num = 42; |
| 43 | |
| 44 | qdict = qdict_new(); |
| 45 | |
| 46 | // key "" will have tdb hash 12345 |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 47 | qdict_put_int(qdict, "", num); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 48 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 49 | g_assert(qdict_size(qdict) == 1); |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 50 | ent = QLIST_FIRST(&qdict->table[12345 % QDICT_BUCKET_MAX]); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 51 | qn = qobject_to(QNum, ent->value); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 52 | g_assert_cmpint(qnum_get_int(qn), ==, num); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 53 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 54 | qobject_unref(qdict); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 55 | } |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 56 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 57 | static void qdict_destroy_simple_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 58 | { |
| 59 | QDict *qdict; |
| 60 | |
| 61 | qdict = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 62 | qdict_put_int(qdict, "num", 0); |
| 63 | qdict_put_str(qdict, "str", "foo"); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 64 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 65 | qobject_unref(qdict); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 66 | } |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 67 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 68 | static void qdict_get_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 69 | { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 70 | QNum *qn; |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 71 | QObject *obj; |
| 72 | const int value = -42; |
| 73 | const char *key = "test"; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 74 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 75 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 76 | qdict_put_int(tests_dict, key, value); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 77 | |
| 78 | obj = qdict_get(tests_dict, key); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 79 | g_assert(obj != NULL); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 80 | |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 81 | qn = qobject_to(QNum, obj); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 82 | g_assert_cmpint(qnum_get_int(qn), ==, value); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 83 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 84 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void qdict_get_int_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 88 | { |
| 89 | int ret; |
| 90 | const int value = 100; |
| 91 | const char *key = "int"; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 92 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 93 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 94 | qdict_put_int(tests_dict, key, value); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 95 | |
| 96 | ret = qdict_get_int(tests_dict, key); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 97 | g_assert(ret == value); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 98 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 99 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void qdict_get_try_int_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 103 | { |
| 104 | int ret; |
| 105 | const int value = 100; |
| 106 | const char *key = "int"; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 107 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 108 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 109 | qdict_put_int(tests_dict, key, value); |
Marc-André Lureau | 269c20b | 2017-06-07 20:36:33 +0400 | [diff] [blame] | 110 | qdict_put_str(tests_dict, "string", "test"); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 111 | |
| 112 | ret = qdict_get_try_int(tests_dict, key, 0); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 113 | g_assert(ret == value); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 114 | |
Marc-André Lureau | 269c20b | 2017-06-07 20:36:33 +0400 | [diff] [blame] | 115 | ret = qdict_get_try_int(tests_dict, "missing", -42); |
| 116 | g_assert_cmpuint(ret, ==, -42); |
| 117 | |
| 118 | ret = qdict_get_try_int(tests_dict, "string", -42); |
| 119 | g_assert_cmpuint(ret, ==, -42); |
| 120 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 121 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void qdict_get_str_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 125 | { |
| 126 | const char *p; |
| 127 | const char *key = "key"; |
| 128 | const char *str = "string"; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 129 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 130 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 131 | qdict_put_str(tests_dict, key, str); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 132 | |
| 133 | p = qdict_get_str(tests_dict, key); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 134 | g_assert(p != NULL); |
| 135 | g_assert(strcmp(p, str) == 0); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 136 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 137 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void qdict_get_try_str_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 141 | { |
| 142 | const char *p; |
| 143 | const char *key = "key"; |
| 144 | const char *str = "string"; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 145 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 146 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 147 | qdict_put_str(tests_dict, key, str); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 148 | |
| 149 | p = qdict_get_try_str(tests_dict, key); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 150 | g_assert(p != NULL); |
| 151 | g_assert(strcmp(p, str) == 0); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 152 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 153 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void qdict_haskey_not_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 157 | { |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 158 | QDict *tests_dict = qdict_new(); |
| 159 | g_assert(qdict_haskey(tests_dict, "test") == 0); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 160 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 161 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void qdict_haskey_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 165 | { |
| 166 | const char *key = "test"; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 167 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 168 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 169 | qdict_put_int(tests_dict, key, 0); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 170 | g_assert(qdict_haskey(tests_dict, key) == 1); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 171 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 172 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void qdict_del_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 176 | { |
| 177 | const char *key = "key test"; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 178 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 179 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 180 | qdict_put_str(tests_dict, key, "foo"); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 181 | g_assert(qdict_size(tests_dict) == 1); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 182 | |
| 183 | qdict_del(tests_dict, key); |
| 184 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 185 | g_assert(qdict_size(tests_dict) == 0); |
| 186 | g_assert(qdict_haskey(tests_dict, key) == 0); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 187 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 188 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void qobject_to_qdict_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 192 | { |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 193 | QDict *tests_dict = qdict_new(); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 194 | g_assert(qobject_to(QDict, QOBJECT(tests_dict)) == tests_dict); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 195 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 196 | qobject_unref(tests_dict); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static void qdict_iterapi_test(void) |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 200 | { |
| 201 | int count; |
| 202 | const QDictEntry *ent; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 203 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 204 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 205 | g_assert(qdict_first(tests_dict) == NULL); |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 206 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 207 | qdict_put_int(tests_dict, "key1", 1); |
| 208 | qdict_put_int(tests_dict, "key2", 2); |
| 209 | qdict_put_int(tests_dict, "key3", 3); |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 210 | |
| 211 | count = 0; |
| 212 | for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){ |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 213 | g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1); |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 214 | count++; |
| 215 | } |
| 216 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 217 | g_assert(count == qdict_size(tests_dict)); |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 218 | |
| 219 | /* Do it again to test restarting */ |
| 220 | count = 0; |
| 221 | for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){ |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 222 | g_assert(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1); |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 223 | count++; |
| 224 | } |
| 225 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 226 | g_assert(count == qdict_size(tests_dict)); |
| 227 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 228 | qobject_unref(tests_dict); |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 229 | } |
Luiz Capitulino | d02c6bd | 2010-06-07 15:29:58 -0300 | [diff] [blame] | 230 | |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 231 | /* |
| 232 | * Errors test-cases |
| 233 | */ |
| 234 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 235 | static void qdict_put_exists_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 236 | { |
| 237 | int value; |
| 238 | const char *key = "exists"; |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 239 | QDict *tests_dict = qdict_new(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 240 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 241 | qdict_put_int(tests_dict, key, 1); |
| 242 | qdict_put_int(tests_dict, key, 2); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 243 | |
| 244 | value = qdict_get_int(tests_dict, key); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 245 | g_assert(value == 2); |
Luiz Capitulino | 29ec315 | 2009-12-14 18:53:20 -0200 | [diff] [blame] | 246 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 247 | g_assert(qdict_size(tests_dict) == 1); |
| 248 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 249 | qobject_unref(tests_dict); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 250 | } |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 251 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 252 | static void qdict_get_not_exists_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 253 | { |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 254 | QDict *tests_dict = qdict_new(); |
| 255 | g_assert(qdict_get(tests_dict, "foo") == NULL); |
| 256 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 257 | qobject_unref(tests_dict); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 258 | } |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * Stress test-case |
| 262 | * |
| 263 | * This is a lot big for a unit-test, but there is no other place |
| 264 | * to have it. |
| 265 | */ |
| 266 | |
| 267 | static void remove_dots(char *string) |
| 268 | { |
| 269 | char *p = strchr(string, ':'); |
| 270 | if (p) |
| 271 | *p = '\0'; |
| 272 | } |
| 273 | |
| 274 | static QString *read_line(FILE *file, char *key) |
| 275 | { |
| 276 | char value[128]; |
| 277 | |
Stefan Weil | 7464f05 | 2011-01-21 22:50:30 +0100 | [diff] [blame] | 278 | if (fscanf(file, "%127s%127s", key, value) == EOF) { |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 279 | return NULL; |
Stefan Weil | 7464f05 | 2011-01-21 22:50:30 +0100 | [diff] [blame] | 280 | } |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 281 | remove_dots(key); |
| 282 | return qstring_from_str(value); |
| 283 | } |
| 284 | |
| 285 | #define reset_file(file) fseek(file, 0L, SEEK_SET) |
| 286 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 287 | static void qdict_stress_test(void) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 288 | { |
| 289 | size_t lines; |
| 290 | char key[128]; |
| 291 | FILE *test_file; |
| 292 | QDict *qdict; |
| 293 | QString *value; |
Philippe Mathieu-Daudé | fe07b62 | 2018-12-18 12:37:17 +0100 | [diff] [blame] | 294 | const char *test_file_path = "tests/data/qobject/qdict.txt"; |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 295 | |
| 296 | test_file = fopen(test_file_path, "r"); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 297 | g_assert(test_file != NULL); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 298 | |
| 299 | // Create the dict |
| 300 | qdict = qdict_new(); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 301 | g_assert(qdict != NULL); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 302 | |
| 303 | // Add everything from the test file |
| 304 | for (lines = 0;; lines++) { |
| 305 | value = read_line(test_file, key); |
| 306 | if (!value) |
| 307 | break; |
| 308 | |
| 309 | qdict_put(qdict, key, value); |
| 310 | } |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 311 | g_assert(qdict_size(qdict) == lines); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 312 | |
| 313 | // Check if everything is really in there |
| 314 | reset_file(test_file); |
| 315 | for (;;) { |
| 316 | const char *str1, *str2; |
| 317 | |
| 318 | value = read_line(test_file, key); |
| 319 | if (!value) |
| 320 | break; |
| 321 | |
| 322 | str1 = qstring_get_str(value); |
| 323 | |
| 324 | str2 = qdict_get_str(qdict, key); |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 325 | g_assert(str2 != NULL); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 326 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 327 | g_assert(strcmp(str1, str2) == 0); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 328 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 329 | qobject_unref(value); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Delete everything |
| 333 | reset_file(test_file); |
| 334 | for (;;) { |
| 335 | value = read_line(test_file, key); |
| 336 | if (!value) |
| 337 | break; |
| 338 | |
| 339 | qdict_del(qdict, key); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 340 | qobject_unref(value); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 341 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 342 | g_assert(qdict_haskey(qdict, key) == 0); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 343 | } |
| 344 | fclose(test_file); |
| 345 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 346 | g_assert(qdict_size(qdict) == 0); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 347 | qobject_unref(qdict); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 348 | } |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 349 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 350 | int main(int argc, char **argv) |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 351 | { |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 352 | g_test_init(&argc, &argv, NULL); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 353 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 354 | g_test_add_func("/public/new", qdict_new_test); |
| 355 | g_test_add_func("/public/put_obj", qdict_put_obj_test); |
| 356 | g_test_add_func("/public/destroy_simple", qdict_destroy_simple_test); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 357 | |
| 358 | /* Continue, but now with fixtures */ |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 359 | g_test_add_func("/public/get", qdict_get_test); |
| 360 | g_test_add_func("/public/get_int", qdict_get_int_test); |
| 361 | g_test_add_func("/public/get_try_int", qdict_get_try_int_test); |
| 362 | g_test_add_func("/public/get_str", qdict_get_str_test); |
| 363 | g_test_add_func("/public/get_try_str", qdict_get_try_str_test); |
| 364 | g_test_add_func("/public/haskey_not", qdict_haskey_not_test); |
| 365 | g_test_add_func("/public/haskey", qdict_haskey_test); |
| 366 | g_test_add_func("/public/del", qdict_del_test); |
| 367 | g_test_add_func("/public/to_qdict", qobject_to_qdict_test); |
| 368 | g_test_add_func("/public/iterapi", qdict_iterapi_test); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 369 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 370 | g_test_add_func("/errors/put_exists", qdict_put_exists_test); |
| 371 | g_test_add_func("/errors/get_not_exists", qdict_get_not_exists_test); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 372 | |
| 373 | /* The Big one */ |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 374 | if (g_test_slow()) { |
| 375 | g_test_add_func("/stress/test", qdict_stress_test); |
| 376 | } |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 377 | |
Anthony Liguori | ac531cb | 2012-01-10 13:10:44 -0600 | [diff] [blame] | 378 | return g_test_run(); |
Luiz Capitulino | 7b8c51a | 2009-08-28 15:27:32 -0300 | [diff] [blame] | 379 | } |