1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/tests/unit/list.c

164 lines
3.7 KiB
C
Raw Permalink Normal View History

2016-10-06 17:56:55 -04:00
/** Unit tests for array-based list
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-10-06 17:56:55 -04:00
*********************************************************************************/
2016-09-27 19:27:55 -04:00
#include <stdint.h>
2016-10-13 22:12:06 -04:00
#include <string.h>
2016-09-27 19:27:55 -04:00
#include <criterion/criterion.h>
2018-03-26 12:50:15 +02:00
#include <villas/utils.h>
#include <villas/list.h>
2016-09-27 19:27:55 -04:00
2016-10-13 22:12:06 -04:00
static char *nouns[] = { "time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact" };
struct data {
char *tag;
int data;
};
Test(list, list_lookup)
{
2017-03-25 21:23:48 +01:00
struct list l = { .state = STATE_DESTROYED };
2016-10-13 22:12:06 -04:00
list_init(&l);
2016-10-13 22:12:06 -04:00
for (int i = 0; i < ARRAY_LEN(nouns); i++) {
struct data *d = malloc(sizeof(struct data));
2016-10-13 22:12:06 -04:00
d->tag = nouns[i];
d->data = i;
2016-10-13 22:12:06 -04:00
list_push(&l, d);
}
2016-10-13 22:12:06 -04:00
struct data *found = list_lookup(&l, "woman");
2016-10-13 22:12:06 -04:00
cr_assert_eq(found->data, 13);
2016-10-13 22:12:06 -04:00
list_destroy(&l, NULL, true);
}
Test(list, list_search)
{
2017-03-25 21:23:48 +01:00
struct list l = { .state = STATE_DESTROYED };
2016-10-13 22:12:06 -04:00
list_init(&l);
2016-10-13 22:12:06 -04:00
/* Fill list */
for (int i = 0; i < ARRAY_LEN(nouns); i++)
list_push(&l, nouns[i]);
2017-03-25 21:23:48 +01:00
cr_assert_eq(list_length(&l), ARRAY_LEN(nouns));
2016-10-13 22:12:06 -04:00
/* Declare on stack! */
char positive[] = "woman";
char negative[] = "dinosaurrier";
2017-03-25 21:23:48 +01:00
char *found = list_search(&l, (cmp_cb_t) strcmp, positive);
2016-10-13 22:12:06 -04:00
cr_assert_not_null(found);
2017-03-25 21:23:48 +01:00
cr_assert_eq(found, nouns[13], "found = %p, nouns[13] = %p", found, nouns[13]);
2016-10-13 22:12:06 -04:00
cr_assert_str_eq(found, positive);
2016-10-13 22:12:06 -04:00
char *not_found = (char *) list_search(&l, (cmp_cb_t) strcmp, negative);
cr_assert_null(not_found);
list_destroy(&l, NULL, false);
}
struct content {
int destroyed;
};
2017-03-07 06:36:40 -04:00
static int dtor(void *ptr)
2016-10-13 22:12:06 -04:00
{
struct content *elm = (struct content *) ptr;
2016-10-13 22:12:06 -04:00
elm->destroyed = 1;
2017-03-07 06:36:40 -04:00
return 0;
2016-10-13 22:12:06 -04:00
}
Test(list, destructor)
{
2017-03-25 21:23:48 +01:00
struct list l = { .state = STATE_DESTROYED };
2016-10-13 22:12:06 -04:00
struct content elm = { .destroyed = 0 };
2016-10-13 22:12:06 -04:00
list_init(&l);
list_push(&l, &elm);
2016-10-13 22:12:06 -04:00
cr_assert_eq(list_length(&l), 1);
2016-10-13 22:12:06 -04:00
list_destroy(&l, dtor, false);
cr_assert_eq(elm.destroyed, 1);
}
2016-09-27 19:27:55 -04:00
static int compare(const void *a, const void *b) {
return b - a;
}
2016-10-13 22:12:06 -04:00
Test(list, basics)
2016-09-27 19:27:55 -04:00
{
intptr_t i;
2017-03-25 21:23:48 +01:00
int ret;
struct list l = { .state = STATE_DESTROYED };
2016-09-27 19:27:55 -04:00
list_init(&l);
2016-09-27 19:27:55 -04:00
for (i = 0; i < 100; i++) {
cr_assert_eq(list_length(&l), i);
2016-09-27 19:27:55 -04:00
list_push(&l, (void *) i);
}
2017-06-17 03:47:10 +02:00
cr_assert_eq(list_at_safe(&l, 555), NULL);
2016-09-27 19:27:55 -04:00
cr_assert_eq(list_last(&l), (void *) 99);
2017-06-17 03:47:10 +02:00
cr_assert_eq(list_first(&l), (void *) 0);
2016-09-27 19:27:55 -04:00
for (size_t j = 0, i = 0; j < list_length(&l); j++) {
void *k = list_at(&l, j);
cr_assert_eq(k, (void *) i++);
}
2016-09-27 19:27:55 -04:00
list_sort(&l, compare); /* Reverse list */
for (size_t j = 0, i = 99; j < list_length(&l); j++) {
void *k = list_at(&l, j);
2017-06-17 03:46:42 +02:00
cr_assert_eq(k, (void *) i, "Is %#zx, expected %p", i, k);
2016-09-27 19:27:55 -04:00
i--;
}
2017-03-25 21:23:48 +01:00
ret = list_contains(&l, (void *) 55);
cr_assert(ret);
2016-09-27 19:27:55 -04:00
list_remove(&l, (void *) 55);
2017-03-25 21:23:48 +01:00
ret = list_contains(&l, (void *) 55);
cr_assert(!ret);
2016-09-27 19:27:55 -04:00
list_destroy(&l, NULL, false);
2017-03-25 21:23:48 +01:00
ret = list_length(&l);
cr_assert_eq(ret, -1, "List not properly destroyed: l.length = %zd", l.length);
2018-03-26 12:50:15 +02:00
}