2023-08-31 17:35:12 +02:00
|
|
|
/* Unit tests for array-based list.
|
2018-08-22 11:29:39 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <criterion/criterion.h>
|
2019-06-23 16:26:44 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2020-01-27 12:17:57 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-08-11 12:40:19 -04:00
|
|
|
#include <villas/list.hpp>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2020-01-27 12:17:57 +01:00
|
|
|
using namespace villas;
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
static const 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"};
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
struct data {
|
2023-09-07 13:19:19 +02:00
|
|
|
const char *name;
|
|
|
|
int data;
|
2018-08-22 11:29:39 +02:00
|
|
|
};
|
|
|
|
|
2020-09-11 15:16:53 +02:00
|
|
|
// cppcheck-suppress unknownMacro
|
2018-10-19 14:33:10 +02:00
|
|
|
TestSuite(list, .description = "List datastructure");
|
2018-08-23 13:14:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(list, list_search) {
|
|
|
|
int ret;
|
|
|
|
struct List l;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_init(&l);
|
|
|
|
cr_assert_eq(ret, 0);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Fill list
|
|
|
|
for (unsigned i = 0; i < ARRAY_LEN(nouns); i++)
|
|
|
|
list_push(&l, (void *)nouns[i]);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(list_length(&l), ARRAY_LEN(nouns));
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Declare on stack!
|
|
|
|
char positive[] = "woman";
|
|
|
|
char negative[] = "dinosaurrier";
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
char *found = (char *)list_search(&l, (cmp_cb_t)strcmp, positive);
|
|
|
|
cr_assert_not_null(found);
|
|
|
|
cr_assert_eq(found, nouns[13], "found = %p, nouns[13] = %p", found,
|
|
|
|
nouns[13]);
|
|
|
|
cr_assert_str_eq(found, positive);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
char *not_found = (char *)list_search(&l, (cmp_cb_t)strcmp, negative);
|
|
|
|
cr_assert_null(not_found);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_destroy(&l, nullptr, false);
|
|
|
|
cr_assert_eq(ret, 0);
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct content {
|
2023-09-07 13:19:19 +02:00
|
|
|
int destroyed;
|
2018-08-22 11:29:39 +02:00
|
|
|
};
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
static int dtor(void *ptr) {
|
|
|
|
struct content *elm = (struct content *)ptr;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
elm->destroyed = 1;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return 0;
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(list, destructor) {
|
|
|
|
int ret;
|
|
|
|
struct List l;
|
2018-08-23 13:14:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
struct content elm;
|
|
|
|
elm.destroyed = 0;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_init(&l);
|
|
|
|
cr_assert_eq(ret, 0);
|
2020-09-03 14:48:11 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
list_push(&l, &elm);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(list_length(&l), 1);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_destroy(&l, dtor, false);
|
|
|
|
cr_assert_eq(ret, 0);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(elm.destroyed, 1);
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Test(list, basics) {
|
|
|
|
uintptr_t i;
|
|
|
|
int ret;
|
|
|
|
struct List l;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_init(&l);
|
|
|
|
cr_assert_eq(ret, 0);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
cr_assert_eq(list_length(&l), i);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
list_push(&l, (void *)i);
|
|
|
|
}
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(list_at_safe(&l, 555), nullptr);
|
|
|
|
cr_assert_eq(list_last(&l), (void *)99);
|
|
|
|
cr_assert_eq(list_first(&l), (void *)0);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
for (size_t j = 0, i = 0; j < list_length(&l); j++) {
|
|
|
|
void *k = list_at(&l, j);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cr_assert_eq(k, (void *)i++);
|
|
|
|
}
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
list_sort(
|
|
|
|
&l, (cmp_cb_t)[](const void *a, const void *b)->int {
|
|
|
|
return (intptr_t)b - (intptr_t)a;
|
|
|
|
});
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
for (size_t j = 0, i = 99; j <= 99; j++, i--) {
|
|
|
|
uintptr_t k = (uintptr_t)list_at(&l, j);
|
|
|
|
cr_assert_eq(k, i, "Is %zu, expected %zu", k, i);
|
|
|
|
}
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_contains(&l, (void *)55);
|
|
|
|
cr_assert(ret);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void *before_ptr = list_at(&l, 12);
|
2019-02-24 09:22:20 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_insert(&l, 12, (void *)123);
|
|
|
|
cr_assert_eq(ret, 0);
|
|
|
|
cr_assert_eq(list_at(&l, 12), (void *)123, "Is: %p", list_at(&l, 12));
|
2019-02-24 09:22:20 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_remove(&l, 12);
|
|
|
|
cr_assert_eq(ret, 0);
|
|
|
|
cr_assert_eq(list_at(&l, 12), before_ptr);
|
2019-02-24 09:22:20 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int counts, before_len;
|
2019-02-24 09:22:20 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
before_len = list_length(&l);
|
|
|
|
counts = list_contains(&l, (void *)55);
|
|
|
|
cr_assert_gt(counts, 0);
|
2019-02-24 09:22:20 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
list_remove_all(&l, (void *)55);
|
|
|
|
cr_assert_eq(list_length(&l), (size_t)(before_len - counts));
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_contains(&l, (void *)55);
|
|
|
|
cr_assert(!ret);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_destroy(&l, nullptr, false);
|
|
|
|
cr_assert(!ret);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = list_length(&l);
|
|
|
|
cr_assert_eq(ret, -1, "List not properly destroyed: l.length = %zd",
|
|
|
|
l.length);
|
2018-08-22 11:29:39 +02:00
|
|
|
}
|