1 /*
2 * Copyright (c) 2019 The Linux Foundation. All rights reserved.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all
7 * copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "qdf_slist.h"
20 #include "qdf_slist_test.h"
21 #include "qdf_trace.h"
22
23 struct qdf_slist_test_item {
24 uint32_t id;
25 struct qdf_slist_node node;
26 };
27
28 #define qdf_slist_node_count 10
29
qdf_slist_test_empty(void)30 static uint32_t qdf_slist_test_empty(void)
31 {
32 struct qdf_slist list;
33 struct qdf_slist_test_item *item;
34
35 /* a new list should ... */
36 qdf_slist_init(&list);
37
38 /* ... be empty */
39 QDF_BUG(qdf_slist_empty(&list));
40
41 /* ... return NULL when pop()'d */
42 QDF_BUG(!qdf_slist_pop(&list, item, node));
43
44 qdf_slist_deinit(&list);
45
46 return 0;
47 }
48
qdf_slist_test_push_pop(void)49 static uint32_t qdf_slist_test_push_pop(void)
50 {
51 struct qdf_slist list;
52 struct qdf_slist_test_item items[qdf_slist_node_count];
53 struct qdf_slist_test_item *item;
54 int i;
55
56 qdf_slist_init(&list);
57
58 /* a list with items should ... */
59 for (i = 0; i < qdf_slist_node_count; i++)
60 qdf_slist_push(&list, &items[i], node);
61
62 /* ... not be empty */
63 QDF_BUG(!qdf_slist_empty(&list));
64
65 /* ... be able to pop() all items previously push()'d */
66 for (i = 0; i < qdf_slist_node_count; i++)
67 QDF_BUG(qdf_slist_pop(&list, item, node));
68
69 /* ... be empty after pop()'ing all items */
70 QDF_BUG(qdf_slist_empty(&list));
71
72 qdf_slist_deinit(&list);
73
74 return 0;
75 }
76
qdf_slist_test_for_each(void)77 static uint32_t qdf_slist_test_for_each(void)
78 {
79 struct qdf_slist list;
80 struct qdf_slist_test_item items[qdf_slist_node_count];
81 struct qdf_slist_test_item *prev;
82 struct qdf_slist_test_item *item;
83 int i;
84
85 qdf_slist_init(&list);
86
87 /* a list with items should ... */
88 for (i = 0; i < qdf_slist_node_count; i++)
89 qdf_slist_push(&list, &items[i], node);
90
91 /* ... be able to iterate over each item */
92 i = 0;
93 qdf_slist_for_each(&list, item, node) {
94 item->id = i++;
95 }
96 QDF_BUG(i == qdf_slist_node_count);
97
98 /* ... be able to remove each item in the same order */
99 i = 0;
100 qdf_slist_for_each_del(&list, prev, item, node) {
101 QDF_BUG(item);
102 QDF_BUG(item->id == i++);
103 QDF_BUG(qdf_slist_remove(&list, prev, node)->id == item->id);
104 }
105 QDF_BUG(i == qdf_slist_node_count);
106
107 /* ... be empty after all items are removed */
108 QDF_BUG(!qdf_slist_pop(&list, item, node));
109 QDF_BUG(qdf_slist_empty(&list));
110
111 qdf_slist_deinit(&list);
112
113 return 0;
114 }
115
qdf_slist_unit_test(void)116 uint32_t qdf_slist_unit_test(void)
117 {
118 uint32_t errors = 0;
119
120 errors += qdf_slist_test_empty();
121 errors += qdf_slist_test_push_pop();
122 errors += qdf_slist_test_for_each();
123
124 return errors;
125 }
126
127