2 * Copyright (c) 2008, 2009, 2010, 2011 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
27 /* Sample list element. */
33 /* Puts the 'n' values in 'values' into 'elements', and then puts those
34 * elements in order into 'list'. */
36 make_list(struct list *list, struct element elements[],
37 int values[], size_t n)
42 for (i = 0; i < n; i++) {
43 elements[i].value = i;
44 list_push_back(list, &elements[i].node);
49 /* Verifies that 'list' contains exactly the 'n' values in 'values', in the
52 check_list(struct list *list, const int values[], size_t n)
58 LIST_FOR_EACH (e, node, list) {
60 assert(e->value == values[i]);
63 assert(&e->node == list);
67 LIST_FOR_EACH_REVERSE (e, node, list) {
69 assert(e->value == values[n - i - 1]);
72 assert(&e->node == list);
75 assert(list_is_empty(list) == !n);
76 assert(list_is_singleton(list) == (n == 1));
77 assert(list_is_short(list) == (n < 2));
78 assert(list_size(list) == n);
82 /* Prints the values in 'list', plus 'name' as a title. */
84 print_list(const char *name, struct list *list)
89 LIST_FOR_EACH (e, node, list) {
90 printf(" %d", e->value);
96 /* Tests basic list construction. */
98 test_list_construction(void)
100 enum { MAX_ELEMS = 100 };
103 for (n = 0; n <= MAX_ELEMS; n++) {
104 struct element elements[MAX_ELEMS];
105 int values[MAX_ELEMS];
108 make_list(&list, elements, values, n);
109 check_list(&list, values, n);
113 /* Tests that LIST_FOR_EACH_SAFE properly allows for deletion of the current
114 * element of a list. */
116 test_list_for_each_safe(void)
118 enum { MAX_ELEMS = 10 };
120 unsigned long int pattern;
122 for (n = 0; n <= MAX_ELEMS; n++) {
123 for (pattern = 0; pattern < 1ul << n; pattern++) {
124 struct element elements[MAX_ELEMS];
125 int values[MAX_ELEMS];
127 struct element *e, *next;
128 size_t values_idx, n_remaining;
131 make_list(&list, elements, values, n);
136 LIST_FOR_EACH_SAFE (e, next, node, &list) {
138 if (pattern & (1ul << i)) {
139 list_remove(&e->node);
141 memmove(&values[values_idx], &values[values_idx + 1],
142 sizeof *values * (n_remaining - values_idx));
146 check_list(&list, values, n_remaining);
150 assert(&e->node == &list);
152 for (i = 0; i < n; i++) {
153 if (pattern & (1ul << i)) {
157 assert(n == n_remaining);
163 run_test(void (*function)(void))
172 run_test(test_list_construction);
173 run_test(test_list_for_each_safe);