]>
git.proxmox.com Git - ovs.git/blob - tests/test-list.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* A non-exhaustive test for some of the functions and macros declared in
18 * openvswitch/list.h. */
22 #include "openvswitch/list.h"
28 /* Sample list element. */
34 /* Puts the 'n' values in 'values' into 'elements', and then puts those
35 * elements in order into 'list'. */
37 make_list(struct ovs_list
*list
, struct element elements
[],
38 int values
[], size_t n
)
43 for (i
= 0; i
< n
; i
++) {
44 elements
[i
].value
= i
;
45 ovs_list_push_back(list
, &elements
[i
].node
);
50 /* Verifies that 'list' contains exactly the 'n' values in 'values', in the
53 check_list(struct ovs_list
*list
, const int values
[], size_t n
)
59 LIST_FOR_EACH (e
, node
, list
) {
61 assert(e
->value
== values
[i
]);
64 assert(&e
->node
== list
);
68 LIST_FOR_EACH_REVERSE (e
, node
, list
) {
70 assert(e
->value
== values
[n
- i
- 1]);
73 assert(&e
->node
== list
);
76 assert(ovs_list_is_empty(list
) == !n
);
77 assert(ovs_list_is_singleton(list
) == (n
== 1));
78 assert(ovs_list_is_short(list
) == (n
< 2));
79 assert(ovs_list_size(list
) == n
);
83 /* Prints the values in 'list', plus 'name' as a title. */
85 print_list(const char *name
, struct ovs_list
*list
)
90 LIST_FOR_EACH (e
, node
, list
) {
91 printf(" %d", e
->value
);
97 /* Tests basic list construction. */
99 test_list_construction(void)
101 enum { MAX_ELEMS
= 100 };
104 for (n
= 0; n
<= MAX_ELEMS
; n
++) {
105 struct element elements
[MAX_ELEMS
];
106 int values
[MAX_ELEMS
];
107 struct ovs_list list
;
109 make_list(&list
, elements
, values
, n
);
110 check_list(&list
, values
, n
);
114 /* Tests that LIST_FOR_EACH_SAFE properly allows for deletion of the current
115 * element of a list. */
117 test_list_for_each_safe(void)
119 enum { MAX_ELEMS
= 10 };
121 unsigned long int pattern
;
123 for (n
= 0; n
<= MAX_ELEMS
; n
++) {
124 for (pattern
= 0; pattern
< 1ul << n
; pattern
++) {
125 struct element elements
[MAX_ELEMS
];
126 int values
[MAX_ELEMS
];
127 struct ovs_list list
;
128 struct element
*e
, *next
;
129 size_t values_idx
, n_remaining
;
132 make_list(&list
, elements
, values
, n
);
137 LIST_FOR_EACH_SAFE (e
, next
, node
, &list
) {
139 if (pattern
& (1ul << i
)) {
140 ovs_list_remove(&e
->node
);
142 memmove(&values
[values_idx
], &values
[values_idx
+ 1],
143 sizeof *values
* (n_remaining
- values_idx
));
147 check_list(&list
, values
, n_remaining
);
151 assert(&e
->node
== &list
);
153 for (i
= 0; i
< n
; i
++) {
154 if (pattern
& (1ul << i
)) {
158 assert(n
== n_remaining
);
163 /* Tests that LIST_FOR_EACH_POP removes the elements of a list. */
165 test_list_for_each_pop(void)
167 enum { MAX_ELEMS
= 10 };
170 for (n
= 0; n
<= MAX_ELEMS
; n
++) {
171 struct element elements
[MAX_ELEMS
];
172 int values
[MAX_ELEMS
];
173 struct ovs_list list
;
177 make_list(&list
, elements
, values
, n
);
180 LIST_FOR_EACH_POP (e
, node
, &list
) {
182 memmove(values
, values
+ 1, sizeof *values
* n_remaining
);
183 check_list(&list
, values
, n_remaining
);
188 /* Tests the transplant of one list into another */
190 test_list_push_back_all(void)
192 struct ovs_list list_a
, list_b
;
193 struct element a
, b
, c
, d
;
200 ovs_list_init(&list_a
);
201 ovs_list_init(&list_b
);
203 ovs_list_insert(&list_a
, &a
.node
);
204 ovs_list_insert(&list_a
, &b
.node
);
205 ovs_list_insert(&list_b
, &c
.node
);
206 ovs_list_insert(&list_b
, &d
.node
);
208 /* Check test preconditions */
209 assert(2 == ovs_list_size(&list_a
));
210 assert(2 == ovs_list_size(&list_b
));
212 /* Perform transplant */
213 ovs_list_push_back_all(&list_a
, &list_b
);
215 /* Check expected result */
216 assert(4 == ovs_list_size(&list_a
));
217 assert(0 == ovs_list_size(&list_b
));
219 struct element
*node
;
221 LIST_FOR_EACH(node
, node
, &list_a
) {
222 assert(n
== node
->value
);
229 run_test(void (*function
)(void))
236 test_list_main(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
238 run_test(test_list_construction
);
239 run_test(test_list_for_each_safe
);
240 run_test(test_list_for_each_pop
);
241 run_test(test_list_push_back_all
);
245 OVSTEST_REGISTER("test-list", test_list_main
);