]> git.proxmox.com Git - ovs.git/blame - tests/test-list.c
ofp-prop: Add generic functions for working with 16- and 32-bit properties.
[ovs.git] / tests / test-list.c
CommitLineData
a14bc59f 1/*
eadd1644 2 * Copyright (c) 2008, 2009, 2010, 2011, 2014 Nicira, Inc.
a14bc59f
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
064af421
BP
17/* A non-exhaustive test for some of the functions and macros declared in
18 * list.h. */
19
20#include <config.h>
3f636c7e 21#undef NDEBUG
064af421 22#include "list.h"
3f636c7e 23#include <assert.h>
064af421 24#include <string.h>
eadd1644 25#include "ovstest.h"
064af421 26
064af421
BP
27/* Sample list element. */
28struct element {
29 int value;
ca6ba700 30 struct ovs_list node;
064af421
BP
31};
32
33/* Puts the 'n' values in 'values' into 'elements', and then puts those
34 * elements in order into 'list'. */
35static void
ca6ba700 36make_list(struct ovs_list *list, struct element elements[],
d295e8e9 37 int values[], size_t n)
064af421
BP
38{
39 size_t i;
d295e8e9 40
064af421
BP
41 list_init(list);
42 for (i = 0; i < n; i++) {
43 elements[i].value = i;
44 list_push_back(list, &elements[i].node);
45 values[i] = i;
46 }
47}
48
49/* Verifies that 'list' contains exactly the 'n' values in 'values', in the
50 * specified order. */
51static void
ca6ba700 52check_list(struct ovs_list *list, const int values[], size_t n)
064af421
BP
53{
54 struct element *e;
55 size_t i;
d295e8e9 56
064af421 57 i = 0;
4e8e4213 58 LIST_FOR_EACH (e, node, list) {
064af421
BP
59 assert(i < n);
60 assert(e->value == values[i]);
61 i++;
62 }
63 assert(&e->node == list);
64 assert(i == n);
65
66 i = 0;
4e8e4213 67 LIST_FOR_EACH_REVERSE (e, node, list) {
064af421
BP
68 assert(i < n);
69 assert(e->value == values[n - i - 1]);
70 i++;
71 }
72 assert(&e->node == list);
73 assert(i == n);
74
75 assert(list_is_empty(list) == !n);
dc1539ae
BP
76 assert(list_is_singleton(list) == (n == 1));
77 assert(list_is_short(list) == (n < 2));
064af421
BP
78 assert(list_size(list) == n);
79}
80
81#if 0
82/* Prints the values in 'list', plus 'name' as a title. */
83static void
ca6ba700 84print_list(const char *name, struct ovs_list *list)
064af421
BP
85{
86 struct element *e;
d295e8e9 87
064af421 88 printf("%s:", name);
4e8e4213 89 LIST_FOR_EACH (e, node, list) {
064af421
BP
90 printf(" %d", e->value);
91 }
92 printf("\n");
93}
94#endif
95
96/* Tests basic list construction. */
97static void
d295e8e9 98test_list_construction(void)
064af421
BP
99{
100 enum { MAX_ELEMS = 100 };
101 size_t n;
102
103 for (n = 0; n <= MAX_ELEMS; n++) {
104 struct element elements[MAX_ELEMS];
105 int values[MAX_ELEMS];
ca6ba700 106 struct ovs_list list;
d295e8e9 107
064af421
BP
108 make_list(&list, elements, values, n);
109 check_list(&list, values, n);
110 }
111}
112
113/* Tests that LIST_FOR_EACH_SAFE properly allows for deletion of the current
114 * element of a list. */
115static void
d295e8e9 116test_list_for_each_safe(void)
064af421
BP
117{
118 enum { MAX_ELEMS = 10 };
119 size_t n;
120 unsigned long int pattern;
121
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];
ca6ba700 126 struct ovs_list list;
064af421
BP
127 struct element *e, *next;
128 size_t values_idx, n_remaining;
129 int i;
d295e8e9 130
064af421
BP
131 make_list(&list, elements, values, n);
132
133 i = 0;
134 values_idx = 0;
135 n_remaining = n;
4e8e4213 136 LIST_FOR_EACH_SAFE (e, next, node, &list) {
064af421
BP
137 assert(i < n);
138 if (pattern & (1ul << i)) {
139 list_remove(&e->node);
140 n_remaining--;
141 memmove(&values[values_idx], &values[values_idx + 1],
142 sizeof *values * (n_remaining - values_idx));
143 } else {
144 values_idx++;
145 }
146 check_list(&list, values, n_remaining);
147 i++;
148 }
149 assert(i == n);
150 assert(&e->node == &list);
151
152 for (i = 0; i < n; i++) {
153 if (pattern & (1ul << i)) {
154 n_remaining++;
155 }
156 }
157 assert(n == n_remaining);
158 }
159 }
160}
161
5f03c983
JR
162/* Tests that LIST_FOR_EACH_POP removes the elements of a list. */
163static void
164test_list_for_each_pop(void)
165{
166 enum { MAX_ELEMS = 10 };
167 size_t n;
168
169 for (n = 0; n <= MAX_ELEMS; n++) {
170 struct element elements[MAX_ELEMS];
171 int values[MAX_ELEMS];
172 struct ovs_list list;
173 struct element *e;
174 size_t n_remaining;
175
176 make_list(&list, elements, values, n);
177
178 n_remaining = n;
179 LIST_FOR_EACH_POP (e, node, &list) {
180 n_remaining--;
181 memmove(values, values + 1, sizeof *values * n_remaining);
182 check_list(&list, values, n_remaining);
183 }
184 }
185}
186
064af421 187static void
d295e8e9 188run_test(void (*function)(void))
064af421
BP
189{
190 function();
191 printf(".");
192}
193
eadd1644
AZ
194static void
195test_list_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
196{
197 run_test(test_list_construction);
198 run_test(test_list_for_each_safe);
5f03c983 199 run_test(test_list_for_each_pop);
064af421 200 printf("\n");
064af421
BP
201}
202
eadd1644 203OVSTEST_REGISTER("test-list", test_list_main);