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