Marcel Holtmann | 611f14d | 2014-01-25 11:30:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * BlueZ - Bluetooth protocol stack for Linux |
| 4 | * |
| 5 | * Copyright (C) 2012 Intel Corporation. All rights reserved. |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <glib.h> |
| 29 | |
| 30 | #include "src/shared/util.h" |
| 31 | #include "src/shared/queue.h" |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 32 | #include "src/shared/tester.h" |
Marcel Holtmann | 611f14d | 2014-01-25 11:30:52 -0800 | [diff] [blame] | 33 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 34 | static void test_basic(const void *data) |
Marcel Holtmann | 611f14d | 2014-01-25 11:30:52 -0800 | [diff] [blame] | 35 | { |
| 36 | struct queue *queue; |
| 37 | unsigned int n, i; |
| 38 | |
| 39 | queue = queue_new(); |
| 40 | g_assert(queue != NULL); |
| 41 | |
| 42 | for (n = 0; n < 1024; n++) { |
| 43 | for (i = 1; i < n + 2; i++) |
| 44 | queue_push_tail(queue, UINT_TO_PTR(i)); |
| 45 | |
| 46 | g_assert(queue_length(queue) == n + 1); |
| 47 | |
| 48 | for (i = 1; i < n + 2; i++) { |
| 49 | void *ptr; |
| 50 | |
| 51 | ptr = queue_pop_head(queue); |
| 52 | g_assert(ptr != NULL); |
| 53 | g_assert(i == PTR_TO_UINT(ptr)); |
| 54 | } |
| 55 | |
| 56 | g_assert(queue_isempty(queue) == true); |
| 57 | } |
| 58 | |
| 59 | queue_destroy(queue, NULL); |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 60 | tester_test_passed(); |
Marcel Holtmann | 611f14d | 2014-01-25 11:30:52 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Luiz Augusto von Dentz | 661d845 | 2014-05-22 12:14:18 +0300 | [diff] [blame] | 63 | static void foreach_destroy(void *data, void *user_data) |
| 64 | { |
| 65 | struct queue *queue = user_data; |
| 66 | |
| 67 | queue_destroy(queue, NULL); |
| 68 | } |
| 69 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 70 | static void test_foreach_destroy(const void *data) |
Luiz Augusto von Dentz | 661d845 | 2014-05-22 12:14:18 +0300 | [diff] [blame] | 71 | { |
| 72 | struct queue *queue; |
| 73 | |
| 74 | queue = queue_new(); |
| 75 | g_assert(queue != NULL); |
| 76 | |
| 77 | queue_push_tail(queue, UINT_TO_PTR(1)); |
| 78 | queue_push_tail(queue, UINT_TO_PTR(2)); |
| 79 | |
| 80 | queue_foreach(queue, foreach_destroy, queue); |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 81 | tester_test_passed(); |
Luiz Augusto von Dentz | 661d845 | 2014-05-22 12:14:18 +0300 | [diff] [blame] | 82 | } |
| 83 | |
Luiz Augusto von Dentz | 7dade6c | 2014-12-08 13:44:15 +0200 | [diff] [blame] | 84 | static void foreach_remove(void *data, void *user_data) |
| 85 | { |
| 86 | struct queue *queue = user_data; |
| 87 | |
| 88 | g_assert(queue_remove(queue, data)); |
| 89 | } |
| 90 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 91 | static void test_foreach_remove(const void *data) |
Luiz Augusto von Dentz | 7dade6c | 2014-12-08 13:44:15 +0200 | [diff] [blame] | 92 | { |
| 93 | struct queue *queue; |
| 94 | |
| 95 | queue = queue_new(); |
| 96 | g_assert(queue != NULL); |
| 97 | |
| 98 | queue_push_tail(queue, UINT_TO_PTR(1)); |
| 99 | queue_push_tail(queue, UINT_TO_PTR(2)); |
| 100 | |
| 101 | queue_foreach(queue, foreach_remove, queue); |
| 102 | queue_destroy(queue, NULL); |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 103 | tester_test_passed(); |
Luiz Augusto von Dentz | 7dade6c | 2014-12-08 13:44:15 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Luiz Augusto von Dentz | d03c4da | 2014-05-22 13:07:22 +0300 | [diff] [blame] | 106 | static void foreach_remove_all(void *data, void *user_data) |
| 107 | { |
| 108 | struct queue *queue = user_data; |
| 109 | |
| 110 | queue_remove_all(queue, NULL, NULL, NULL); |
| 111 | } |
| 112 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 113 | static void test_foreach_remove_all(const void *data) |
Luiz Augusto von Dentz | d03c4da | 2014-05-22 13:07:22 +0300 | [diff] [blame] | 114 | { |
| 115 | struct queue *queue; |
| 116 | |
| 117 | queue = queue_new(); |
| 118 | g_assert(queue != NULL); |
| 119 | |
| 120 | queue_push_tail(queue, UINT_TO_PTR(1)); |
| 121 | queue_push_tail(queue, UINT_TO_PTR(2)); |
| 122 | |
| 123 | queue_foreach(queue, foreach_remove_all, queue); |
Luiz Augusto von Dentz | 3053eb7 | 2014-06-27 13:51:46 +0300 | [diff] [blame] | 124 | queue_destroy(queue, NULL); |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 125 | tester_test_passed(); |
Luiz Augusto von Dentz | d03c4da | 2014-05-22 13:07:22 +0300 | [diff] [blame] | 126 | } |
| 127 | |
Luiz Augusto von Dentz | c9528d9 | 2014-12-08 13:51:38 +0200 | [diff] [blame] | 128 | static void foreach_remove_backward(void *data, void *user_data) |
| 129 | { |
| 130 | struct queue *queue = user_data; |
| 131 | |
| 132 | queue_remove(queue, UINT_TO_PTR(2)); |
| 133 | queue_remove(queue, UINT_TO_PTR(1)); |
| 134 | } |
| 135 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 136 | static void test_foreach_remove_backward(const void *data) |
Luiz Augusto von Dentz | c9528d9 | 2014-12-08 13:51:38 +0200 | [diff] [blame] | 137 | { |
| 138 | struct queue *queue; |
| 139 | |
| 140 | queue = queue_new(); |
| 141 | g_assert(queue != NULL); |
| 142 | |
| 143 | queue_push_tail(queue, UINT_TO_PTR(1)); |
| 144 | queue_push_tail(queue, UINT_TO_PTR(2)); |
| 145 | |
| 146 | queue_foreach(queue, foreach_remove_backward, queue); |
| 147 | queue_destroy(queue, NULL); |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 148 | tester_test_passed(); |
Luiz Augusto von Dentz | c9528d9 | 2014-12-08 13:51:38 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Luiz Augusto von Dentz | dbf1226 | 2014-11-17 16:31:52 +0200 | [diff] [blame] | 151 | static struct queue *static_queue; |
| 152 | |
| 153 | static void destroy_remove(void *user_data) |
| 154 | { |
| 155 | queue_remove(static_queue, user_data); |
| 156 | } |
| 157 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 158 | static void test_destroy_remove(const void *data) |
Luiz Augusto von Dentz | dbf1226 | 2014-11-17 16:31:52 +0200 | [diff] [blame] | 159 | { |
| 160 | static_queue = queue_new(); |
| 161 | |
| 162 | g_assert(static_queue != NULL); |
| 163 | |
| 164 | queue_push_tail(static_queue, UINT_TO_PTR(1)); |
| 165 | queue_push_tail(static_queue, UINT_TO_PTR(2)); |
| 166 | |
| 167 | queue_destroy(static_queue, destroy_remove); |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 168 | tester_test_passed(); |
Luiz Augusto von Dentz | dbf1226 | 2014-11-17 16:31:52 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 171 | static void test_push_after(const void *data) |
Arman Uguray | 28e66d9 | 2014-11-26 15:03:10 -0800 | [diff] [blame] | 172 | { |
| 173 | struct queue *queue; |
| 174 | unsigned int len, i; |
| 175 | |
| 176 | queue = queue_new(); |
| 177 | g_assert(queue != NULL); |
| 178 | |
| 179 | /* |
| 180 | * Pre-populate queue. Initial elements are: |
| 181 | * [ NULL, 2, 5 ] |
| 182 | */ |
| 183 | g_assert(queue_push_tail(queue, NULL)); |
| 184 | g_assert(queue_push_tail(queue, UINT_TO_PTR(2))); |
| 185 | g_assert(queue_push_tail(queue, UINT_TO_PTR(5))); |
| 186 | g_assert(queue_length(queue) == 3); |
| 187 | |
| 188 | /* Invalid insertion */ |
| 189 | g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1))); |
| 190 | |
| 191 | /* Valid insertions */ |
| 192 | g_assert(queue_push_after(queue, NULL, UINT_TO_PTR(1))); |
| 193 | g_assert(queue_push_after(queue, UINT_TO_PTR(2), UINT_TO_PTR(3))); |
| 194 | g_assert(queue_push_after(queue, UINT_TO_PTR(3), UINT_TO_PTR(4))); |
| 195 | g_assert(queue_push_after(queue, UINT_TO_PTR(5), UINT_TO_PTR(6))); |
| 196 | |
| 197 | g_assert(queue_peek_head(queue) == NULL); |
| 198 | g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6)); |
| 199 | |
| 200 | /* |
| 201 | * Queue should contain 7 elements: |
| 202 | * [ NULL, 1, 2, 3, 4, 5, 6 ] |
| 203 | */ |
| 204 | len = queue_length(queue); |
| 205 | g_assert(len == 7); |
| 206 | |
| 207 | for (i = 0; i < 7; i++) |
| 208 | g_assert(queue_pop_head(queue) == UINT_TO_PTR(i)); |
| 209 | |
| 210 | /* Test with identical elements */ |
| 211 | g_assert(queue_push_head(queue, UINT_TO_PTR(1))); |
| 212 | g_assert(queue_push_head(queue, UINT_TO_PTR(1))); |
| 213 | g_assert(queue_push_head(queue, UINT_TO_PTR(1))); |
| 214 | g_assert(queue_push_after(queue, UINT_TO_PTR(1), UINT_TO_PTR(0))); |
| 215 | |
| 216 | g_assert(queue_pop_head(queue) == UINT_TO_PTR(1)); |
| 217 | g_assert(queue_pop_head(queue) == UINT_TO_PTR(0)); |
| 218 | g_assert(queue_pop_head(queue) == UINT_TO_PTR(1)); |
| 219 | g_assert(queue_pop_head(queue) == UINT_TO_PTR(1)); |
Luiz Augusto von Dentz | 7f57669 | 2014-12-03 20:54:39 +0200 | [diff] [blame] | 220 | |
| 221 | queue_destroy(queue, NULL); |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 222 | tester_test_passed(); |
Arman Uguray | 28e66d9 | 2014-11-26 15:03:10 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Szymon Janc | 047c182 | 2015-01-07 16:50:09 +0100 | [diff] [blame] | 225 | static bool match_int(const void *a, const void *b) |
| 226 | { |
| 227 | int i = PTR_TO_INT(a); |
| 228 | int j = PTR_TO_INT(b); |
| 229 | |
| 230 | return i == j; |
| 231 | } |
| 232 | |
Szymon Janc | b68c03b | 2015-01-07 17:26:02 +0100 | [diff] [blame] | 233 | static bool match_ptr(const void *a, const void *b) |
| 234 | { |
| 235 | return a == b; |
| 236 | } |
| 237 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 238 | static void test_remove_all(const void *data) |
Szymon Janc | 047c182 | 2015-01-07 16:50:09 +0100 | [diff] [blame] | 239 | { |
| 240 | struct queue *queue; |
| 241 | |
| 242 | queue = queue_new(); |
| 243 | g_assert(queue != NULL); |
| 244 | |
| 245 | g_assert(queue_push_tail(queue, INT_TO_PTR(10))); |
| 246 | |
| 247 | g_assert(queue_remove_all(queue, match_int, INT_TO_PTR(10), NULL) == 1); |
| 248 | g_assert(queue_isempty(queue)); |
| 249 | |
Szymon Janc | b68c03b | 2015-01-07 17:26:02 +0100 | [diff] [blame] | 250 | g_assert(queue_push_tail(queue, NULL)); |
| 251 | g_assert(queue_remove_all(queue, match_ptr, NULL, NULL) == 1); |
| 252 | g_assert(queue_isempty(queue)); |
| 253 | |
| 254 | g_assert(queue_push_tail(queue, UINT_TO_PTR(0))); |
| 255 | g_assert(queue_remove_all(queue, match_int, UINT_TO_PTR(0), NULL) == 1); |
| 256 | g_assert(queue_isempty(queue)); |
| 257 | |
Szymon Janc | 047c182 | 2015-01-07 16:50:09 +0100 | [diff] [blame] | 258 | queue_destroy(queue, NULL); |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 259 | tester_test_passed(); |
Szymon Janc | 047c182 | 2015-01-07 16:50:09 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Marcel Holtmann | 611f14d | 2014-01-25 11:30:52 -0800 | [diff] [blame] | 262 | int main(int argc, char *argv[]) |
| 263 | { |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 264 | tester_init(&argc, &argv); |
Marcel Holtmann | 611f14d | 2014-01-25 11:30:52 -0800 | [diff] [blame] | 265 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 266 | tester_add("/queue/basic", NULL, NULL, test_basic, NULL); |
| 267 | tester_add("/queue/foreach_destroy", NULL, NULL, |
| 268 | test_foreach_destroy, NULL); |
| 269 | tester_add("/queue/foreach_remove", NULL, NULL, |
| 270 | test_foreach_remove, NULL); |
| 271 | tester_add("/queue/foreach_remove_all", NULL, NULL, |
| 272 | test_foreach_remove_all, NULL); |
| 273 | tester_add("/queue/foreach_remove_backward", NULL, NULL, |
| 274 | test_foreach_remove_backward, NULL); |
| 275 | tester_add("/queue/destroy_remove", NULL, NULL, |
| 276 | test_destroy_remove, NULL); |
| 277 | tester_add("/queue/push_after", NULL, NULL, test_push_after, NULL); |
| 278 | tester_add("/queue/remove_all", NULL, NULL, test_remove_all, NULL); |
Marcel Holtmann | 611f14d | 2014-01-25 11:30:52 -0800 | [diff] [blame] | 279 | |
Gowtham Anandha Babu | 3fb9cbb | 2015-05-19 20:31:02 +0530 | [diff] [blame] | 280 | return tester_run(); |
Marcel Holtmann | 611f14d | 2014-01-25 11:30:52 -0800 | [diff] [blame] | 281 | } |