1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #15342 from miri64/gnrc_pkt/enh/tests

gnrc_pkt: port to list.h
This commit is contained in:
Martine Lenders 2020-11-03 17:49:40 +01:00 committed by GitHub
commit 1070e287f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 9 deletions

View File

@ -27,7 +27,7 @@
#include "kernel_types.h"
#include "net/gnrc/nettype.h"
#include "utlist.h"
#include "list.h"
#ifdef __cplusplus
extern "C" {
@ -135,9 +135,10 @@ typedef struct gnrc_pktsnip {
static inline gnrc_pktsnip_t *gnrc_pkt_prev_snip(gnrc_pktsnip_t *pkt,
gnrc_pktsnip_t *snip)
{
gnrc_pktsnip_t *prev;
LL_SEARCH_SCALAR(pkt, prev, next, snip);
return prev;
while ((pkt != NULL) && (pkt->next != snip)) {
pkt = pkt->next;
}
return pkt;
}
/**
@ -170,7 +171,16 @@ static inline size_t gnrc_pkt_len(const gnrc_pktsnip_t *pkt)
static inline gnrc_pktsnip_t *gnrc_pkt_append(gnrc_pktsnip_t *pkt,
gnrc_pktsnip_t *snip)
{
LL_APPEND(pkt, snip);
/* find last snip in pkt */
gnrc_pktsnip_t *last = gnrc_pkt_prev_snip(pkt, NULL);
if (last != NULL) {
last->next = snip;
}
else {
/* last == NULL means snip */
pkt = snip;
}
return pkt;
}
@ -185,8 +195,8 @@ static inline gnrc_pktsnip_t *gnrc_pkt_append(gnrc_pktsnip_t *pkt,
static inline gnrc_pktsnip_t *gnrc_pkt_prepend(gnrc_pktsnip_t *pkt,
gnrc_pktsnip_t *snip)
{
LL_PREPEND(pkt, snip);
return pkt;
snip->next = pkt;
return snip;
}
/**
@ -200,8 +210,10 @@ static inline gnrc_pktsnip_t *gnrc_pkt_prepend(gnrc_pktsnip_t *pkt,
static inline gnrc_pktsnip_t *gnrc_pkt_delete(gnrc_pktsnip_t *pkt,
gnrc_pktsnip_t *snip)
{
LL_DELETE(pkt, snip);
return pkt;
list_node_t list = { .next = (list_node_t *)pkt };
list_remove(&list, (list_node_t *)snip);
return (gnrc_pktsnip_t *)list.next;
}

View File

@ -35,6 +35,38 @@
#define _INIT_ELEM_STATIC_TYPE(_type, _next) \
{ .users = 1, .next = (_next), .data = NULL, .size = 0, .type = (_type) }
static void test_pkt_prev_snip__NULL_NULL(void)
{
TEST_ASSERT_NULL(gnrc_pkt_prev_snip(NULL, NULL));
}
static void test_pkt_prev_snip__pkt_NULL(void)
{
gnrc_pktsnip_t pkt = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t *res;
res = gnrc_pkt_prev_snip(&pkt, NULL);
TEST_ASSERT((&pkt) == res);
}
static void test_pkt_prev_snip__NULL_snip(void)
{
gnrc_pktsnip_t snip = _INIT_ELEM(SIZE_MAX, NULL, NULL);
TEST_ASSERT_NULL(gnrc_pkt_prev_snip(NULL, &snip));
}
static void test_pkt_prev_snip__pkt_snip(void)
{
gnrc_pktsnip_t snip1 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t snip2 = _INIT_ELEM(SIZE_MAX, NULL, &snip1);
gnrc_pktsnip_t pkt = _INIT_ELEM(SIZE_MAX, NULL, &snip2);
gnrc_pktsnip_t *res;
res = gnrc_pkt_prev_snip(&pkt, &snip1);
TEST_ASSERT((&snip2) == res);
}
static void test_pkt_len__NULL(void)
{
TEST_ASSERT_EQUAL_INT(0, gnrc_pkt_len(NULL));
@ -93,6 +125,80 @@ static void test_pkt_len__3_elem(void)
TEST_ASSERT_EQUAL_INT(sizeof(TEST_STRING8), gnrc_pkt_len(&snip1));
}
static void test_pkt_append(void)
{
gnrc_pktsnip_t snip1 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t snip2 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t pkt = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t *res = &pkt;
res = gnrc_pkt_append(res, &snip1);
TEST_ASSERT((&pkt) == res);
TEST_ASSERT((&snip1) == res->next);
TEST_ASSERT_NULL(res->next->next);
res = gnrc_pkt_append(res, &snip2);
TEST_ASSERT((&pkt) == res);
TEST_ASSERT((&snip1) == res->next);
TEST_ASSERT((&snip2) == res->next->next);
}
static void test_pkt_prepend(void)
{
gnrc_pktsnip_t snip1 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t snip2 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t pkt = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t *res = &pkt;
res = gnrc_pkt_prepend(res, &snip1);
TEST_ASSERT((&snip1) == res);
TEST_ASSERT((&pkt) == res->next);
TEST_ASSERT_NULL(res->next->next);
res = gnrc_pkt_prepend(res, &snip2);
TEST_ASSERT((&snip2) == res);
TEST_ASSERT((&snip1) == res->next);
TEST_ASSERT((&pkt) == res->next->next);
}
static void test_pkt_delete__NULL(void)
{
gnrc_pktsnip_t snip1 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t snip2 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t pkt = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t *res = &pkt;
res = gnrc_pkt_prepend(res, &snip1);
res = gnrc_pkt_prepend(res, &snip2);
TEST_ASSERT((&snip2) == res);
TEST_ASSERT((&snip1) == res->next);
TEST_ASSERT((&pkt) == res->next->next);
res = gnrc_pkt_delete(res, NULL);
/* pkt did not change */
TEST_ASSERT((&snip2) == res);
TEST_ASSERT((&snip1) == res->next);
TEST_ASSERT((&pkt) == res->next->next);
}
static void test_pkt_delete(void)
{
gnrc_pktsnip_t snip1 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t snip2 = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t pkt = _INIT_ELEM(SIZE_MAX, NULL, NULL);
gnrc_pktsnip_t *res = &pkt;
res = gnrc_pkt_prepend(res, &snip1);
res = gnrc_pkt_prepend(res, &snip2);
TEST_ASSERT((&snip2) == res);
TEST_ASSERT((&snip1) == res->next);
TEST_ASSERT((&pkt) == res->next->next);
res = gnrc_pkt_delete(res, &snip1);
TEST_ASSERT((&snip2) == res);
TEST_ASSERT((&pkt) == res->next);
res = gnrc_pkt_delete(res, &snip2);
TEST_ASSERT((&pkt) == res);
res = gnrc_pkt_delete(res, &pkt);
TEST_ASSERT_NULL(res);
}
static void test_pkt_len_upto__NULL(void)
{
TEST_ASSERT_EQUAL_INT(0, gnrc_pkt_len_upto(NULL, GNRC_NETTYPE_TEST));
@ -198,6 +304,10 @@ static void test_pkt_equals_iolist(void)
Test *tests_pkt_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_pkt_prev_snip__NULL_NULL),
new_TestFixture(test_pkt_prev_snip__pkt_NULL),
new_TestFixture(test_pkt_prev_snip__NULL_snip),
new_TestFixture(test_pkt_prev_snip__pkt_snip),
new_TestFixture(test_pkt_len__NULL),
new_TestFixture(test_pkt_len__1_elem__size_MAX),
new_TestFixture(test_pkt_len__1_elem__size_0),
@ -205,6 +315,10 @@ Test *tests_pkt_tests(void)
new_TestFixture(test_pkt_len__2_elem),
new_TestFixture(test_pkt_len__2_elem__overflow),
new_TestFixture(test_pkt_len__3_elem),
new_TestFixture(test_pkt_append),
new_TestFixture(test_pkt_prepend),
new_TestFixture(test_pkt_delete__NULL),
new_TestFixture(test_pkt_delete),
new_TestFixture(test_pkt_len_upto__NULL),
new_TestFixture(test_pkt_len_upto__not_in_list),
new_TestFixture(test_pkt_len_upto__in_list),