2009-06-15 15:11:30 -07:00
|
|
|
/*
|
2014-04-01 00:47:01 -07:00
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011, 2014 Nicira, Inc.
|
2009-06-15 15:11:30 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
/* A non-exhaustive test for some of the functions and macros declared in
|
|
|
|
* list.h. */
|
|
|
|
|
|
|
|
#include <config.h>
|
2014-10-29 11:34:40 -07:00
|
|
|
#undef NDEBUG
|
2009-07-08 13:19:16 -07:00
|
|
|
#include "list.h"
|
2014-10-29 11:34:40 -07:00
|
|
|
#include <assert.h>
|
2009-07-08 13:19:16 -07:00
|
|
|
#include <string.h>
|
2014-04-01 00:47:01 -07:00
|
|
|
#include "ovstest.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
/* Sample list element. */
|
|
|
|
struct element {
|
|
|
|
int value;
|
|
|
|
struct list node;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Puts the 'n' values in 'values' into 'elements', and then puts those
|
|
|
|
* elements in order into 'list'. */
|
|
|
|
static void
|
|
|
|
make_list(struct list *list, struct element elements[],
|
2010-08-30 00:24:53 -07:00
|
|
|
int values[], size_t n)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
size_t i;
|
2010-08-30 00:24:53 -07:00
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
list_init(list);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
elements[i].value = i;
|
|
|
|
list_push_back(list, &elements[i].node);
|
|
|
|
values[i] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verifies that 'list' contains exactly the 'n' values in 'values', in the
|
|
|
|
* specified order. */
|
|
|
|
static void
|
2010-08-30 00:24:53 -07:00
|
|
|
check_list(struct list *list, const int values[], size_t n)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
struct element *e;
|
|
|
|
size_t i;
|
2010-08-30 00:24:53 -07:00
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
i = 0;
|
2010-09-17 10:33:10 -07:00
|
|
|
LIST_FOR_EACH (e, node, list) {
|
2009-07-08 13:19:16 -07:00
|
|
|
assert(i < n);
|
|
|
|
assert(e->value == values[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
assert(&e->node == list);
|
|
|
|
assert(i == n);
|
|
|
|
|
|
|
|
i = 0;
|
2010-09-17 10:33:10 -07:00
|
|
|
LIST_FOR_EACH_REVERSE (e, node, list) {
|
2009-07-08 13:19:16 -07:00
|
|
|
assert(i < n);
|
|
|
|
assert(e->value == values[n - i - 1]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
assert(&e->node == list);
|
|
|
|
assert(i == n);
|
|
|
|
|
|
|
|
assert(list_is_empty(list) == !n);
|
2011-03-24 09:40:07 -07:00
|
|
|
assert(list_is_singleton(list) == (n == 1));
|
|
|
|
assert(list_is_short(list) == (n < 2));
|
2009-07-08 13:19:16 -07:00
|
|
|
assert(list_size(list) == n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Prints the values in 'list', plus 'name' as a title. */
|
|
|
|
static void
|
2010-08-30 00:24:53 -07:00
|
|
|
print_list(const char *name, struct list *list)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
struct element *e;
|
2010-08-30 00:24:53 -07:00
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
printf("%s:", name);
|
2010-09-17 10:33:10 -07:00
|
|
|
LIST_FOR_EACH (e, node, list) {
|
2009-07-08 13:19:16 -07:00
|
|
|
printf(" %d", e->value);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Tests basic list construction. */
|
|
|
|
static void
|
2010-08-30 00:24:53 -07:00
|
|
|
test_list_construction(void)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
enum { MAX_ELEMS = 100 };
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
for (n = 0; n <= MAX_ELEMS; n++) {
|
|
|
|
struct element elements[MAX_ELEMS];
|
|
|
|
int values[MAX_ELEMS];
|
|
|
|
struct list list;
|
2010-08-30 00:24:53 -07:00
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
make_list(&list, elements, values, n);
|
|
|
|
check_list(&list, values, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tests that LIST_FOR_EACH_SAFE properly allows for deletion of the current
|
|
|
|
* element of a list. */
|
|
|
|
static void
|
2010-08-30 00:24:53 -07:00
|
|
|
test_list_for_each_safe(void)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
enum { MAX_ELEMS = 10 };
|
|
|
|
size_t n;
|
|
|
|
unsigned long int pattern;
|
|
|
|
|
|
|
|
for (n = 0; n <= MAX_ELEMS; n++) {
|
|
|
|
for (pattern = 0; pattern < 1ul << n; pattern++) {
|
|
|
|
struct element elements[MAX_ELEMS];
|
|
|
|
int values[MAX_ELEMS];
|
|
|
|
struct list list;
|
|
|
|
struct element *e, *next;
|
|
|
|
size_t values_idx, n_remaining;
|
|
|
|
int i;
|
2010-08-30 00:24:53 -07:00
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
make_list(&list, elements, values, n);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
values_idx = 0;
|
|
|
|
n_remaining = n;
|
2010-09-17 10:33:10 -07:00
|
|
|
LIST_FOR_EACH_SAFE (e, next, node, &list) {
|
2009-07-08 13:19:16 -07:00
|
|
|
assert(i < n);
|
|
|
|
if (pattern & (1ul << i)) {
|
|
|
|
list_remove(&e->node);
|
|
|
|
n_remaining--;
|
|
|
|
memmove(&values[values_idx], &values[values_idx + 1],
|
|
|
|
sizeof *values * (n_remaining - values_idx));
|
|
|
|
} else {
|
|
|
|
values_idx++;
|
|
|
|
}
|
|
|
|
check_list(&list, values, n_remaining);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
assert(i == n);
|
|
|
|
assert(&e->node == &list);
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (pattern & (1ul << i)) {
|
|
|
|
n_remaining++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(n == n_remaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-30 00:24:53 -07:00
|
|
|
run_test(void (*function)(void))
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
function();
|
|
|
|
printf(".");
|
|
|
|
}
|
|
|
|
|
2014-04-01 00:47:01 -07:00
|
|
|
static void
|
|
|
|
test_list_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
|
2009-07-08 13:19:16 -07:00
|
|
|
{
|
|
|
|
run_test(test_list_construction);
|
|
|
|
run_test(test_list_for_each_safe);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2014-04-01 00:47:01 -07:00
|
|
|
OVSTEST_REGISTER("test-list", test_list_main);
|