fixed typos and added some basic tests for the llist.h
This commit is contained in:
parent
3ce5d3cedd
commit
2aaad10bbd
4 changed files with 149 additions and 24 deletions
|
|
@ -88,6 +88,12 @@ typedef llist * LList;
|
|||
typedef const llist * const_LList;
|
||||
typedef llist ** LList_ref;
|
||||
|
||||
/**
|
||||
* Macro to instantiate a local llist.
|
||||
* @param name of the llist node
|
||||
*/
|
||||
#define LLIST_AUTO(name) llist name = {&name,&name}
|
||||
|
||||
|
||||
/**
|
||||
* cast back from a member of a structure to a pointer of the structure
|
||||
|
|
@ -110,7 +116,7 @@ typedef llist ** LList_ref;
|
|||
*/
|
||||
#define LLIST_FOREACH(list, node) \
|
||||
if (!list); else \
|
||||
for (LList node = llist_get_head (list); \
|
||||
for (LList node = llist_head (list); \
|
||||
! llist_is_end (node, list); \
|
||||
llist_forward (&node))
|
||||
|
||||
|
|
@ -121,7 +127,7 @@ typedef llist ** LList_ref;
|
|||
*/
|
||||
#define LLIST_FOREACH_REV(list, node) \
|
||||
if (!list); else \
|
||||
for (LList node = llist_get_tail (list); \
|
||||
for (LList node = llist_tail (list); \
|
||||
! llist_is_end (node, list); \
|
||||
llist_backward (&node))
|
||||
|
||||
|
|
@ -133,9 +139,9 @@ typedef llist ** LList_ref;
|
|||
*/
|
||||
#define LLIST_WHILE_HEAD(list, head) \
|
||||
if (!list); else \
|
||||
for (LList head = llist_get_head (list); \
|
||||
for (LList head = llist_head (list); \
|
||||
!llist_is_empty (list); \
|
||||
head = llist_get_head (list))
|
||||
head = llist_head (list))
|
||||
|
||||
/**
|
||||
* Consume a list from tail.
|
||||
|
|
@ -145,22 +151,9 @@ typedef llist ** LList_ref;
|
|||
*/
|
||||
#define LLIST_WHILE_TAIL(list, tail) \
|
||||
if (!list); else \
|
||||
for (LList tail = llist_get_tail (list); \
|
||||
for (LList tail = llist_tail (list); \
|
||||
!llist_is_empty (list); \
|
||||
tail = llist_get_tail (list))
|
||||
|
||||
|
||||
/**
|
||||
* Macro to instantiate a local llist.
|
||||
* @param name becomes a LList handle
|
||||
* the underlying instance is hidden as name_llist_, this list is statically initialized
|
||||
*/
|
||||
#define LLIST_AUTO(name) \
|
||||
llist name##_llist_ = LLIST_STATIC_INITIALIZER(name##_llist_);\
|
||||
LList name = &name##_llist_
|
||||
#define LLIST_STATIC_INITIALIZER(name) {&name,&name}
|
||||
|
||||
|
||||
tail = llist_tail (list))
|
||||
|
||||
/**
|
||||
* Initialize a new llist.
|
||||
|
|
@ -474,10 +467,10 @@ LLIST_FUNC (void llist_backward (LList_ref self),
|
|||
LLIST_FUNC (LList llist_nth (LList self, int n),
|
||||
if (n>0)
|
||||
while (n--)
|
||||
self = llist_get_next (self);
|
||||
self = llist_next (self);
|
||||
else
|
||||
while (n++)
|
||||
self = llist_get_prev (self);
|
||||
self = llist_prev (self);
|
||||
return self;
|
||||
);
|
||||
|
||||
|
|
@ -491,14 +484,14 @@ LLIST_FUNC (LList llist_get_nth_stop (LList self, int n, const_LList stop),
|
|||
if (n>0)
|
||||
while (n--)
|
||||
{
|
||||
self = llist_get_next (self);
|
||||
self = llist_next (self);
|
||||
if (self == stop)
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
while (n++)
|
||||
{
|
||||
self = llist_get_prev (self);
|
||||
self = llist_prev (self);
|
||||
if (self == stop)
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
55
tests/15list.tests
Normal file
55
tests/15list.tests
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
TESTING "Linked Lists" ./test-llist
|
||||
|
||||
TEST "init nodes" basic <<END
|
||||
out: 1
|
||||
out: 1
|
||||
return: 0
|
||||
END
|
||||
|
||||
TEST "insert nodes" nodeinsert <<END
|
||||
out: 0
|
||||
out: 0
|
||||
out: 1
|
||||
out: 0
|
||||
out: 1
|
||||
out: 1
|
||||
out: 1
|
||||
out: 1
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
|
||||
echo TODO; cat <<END
|
||||
#define LLIST_FOREACH(list, node) \
|
||||
#define LLIST_FOREACH_REV(list, node) \
|
||||
#define LLIST_WHILE_HEAD(list, head) \
|
||||
#define LLIST_WHILE_TAIL(list, tail) \
|
||||
LLIST_FUNC (int llist_is_head (const_LList self, const_LList head),
|
||||
LLIST_FUNC (int llist_is_tail (const_LList self, const_LList tail),
|
||||
LLIST_FUNC (int llist_is_end (const_LList self, const_LList end),
|
||||
LLIST_FUNC (int llist_is_member (const_LList self, const_LList member),
|
||||
LLIST_FUNC (int llist_is_before_after (const_LList self, const_LList before, const_LList after),
|
||||
LLIST_FUNC (unsigned llist_count (const_LList self),
|
||||
LLIST_FUNC (void llist_unlink_fast_ (LList self),
|
||||
LLIST_FUNC (LList llist_unlink (LList self),
|
||||
LLIST_FUNC (LList llist_relocate (LList self),
|
||||
LLIST_FUNC (LList llist_insertlist_next (LList self, LList next),
|
||||
LLIST_FUNC (LList llist_insertlist_prev (LList self, LList prev),
|
||||
PLANNED LLIST_FUNC (LList llist_insertafter_range (LList self, LList start, LList end),
|
||||
PLANNED LLIST_FUNC (LList llist_inserbefore_range (LList self, LList start, LList end),
|
||||
LLIST_FUNC (LList llist_advance (LList self),
|
||||
LLIST_FUNC (LList llist_retreat (LList self),
|
||||
LLIST_FUNC (void llist_forward (LList_ref self),
|
||||
LLIST_FUNC (void llist_backward (LList_ref self),
|
||||
LLIST_FUNC (LList llist_nth (LList self, int n),
|
||||
LLIST_FUNC (LList llist_get_nth_stop (LList self, int n, const_LList stop),
|
||||
#define llist_insert_head(list, element) llist_insert_next (list, element)
|
||||
#define llist_insert_tail(list, element) llist_insert_prev (list, element)
|
||||
#define llist_head llist_next
|
||||
#define llist_tail llist_prev
|
||||
END
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
tests_srcdir = $(top_srcdir)/tests
|
||||
|
||||
check_PROGRAMS += test-error test-time test-locking
|
||||
check_PROGRAMS += test-error test-time test-locking test-llist
|
||||
|
||||
test_error_SOURCES = $(tests_srcdir)/error/errortest.c
|
||||
test_error_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
|
|
@ -35,4 +35,8 @@ test_locking_SOURCES = \
|
|||
test_locking_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
test_locking_LDADD = $(builddir)/libcin3.a -lnobugmt -lpthread -ldl -lm
|
||||
|
||||
test_llist_SOURCES = $(tests_srcdir)/library/test-llist.c
|
||||
test_llist_CPPFLAGS = $(AM_CPPFLAGS) -std=gnu99 -Wall -Werror -I$(top_srcdir)/src/
|
||||
test_llist_LDADD = $(builddir)/libcin3.a -lnobugmt -lpthread -ldl -lm
|
||||
|
||||
TESTS = $(tests_srcdir)/test.sh
|
||||
|
|
|
|||
73
tests/library/test-llist.c
Normal file
73
tests/library/test-llist.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
test-llist.c - test the linked lis lib
|
||||
|
||||
Copyright (C) CinelerraCV
|
||||
2007, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
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 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
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.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
//#include <stdio.h>
|
||||
//#include <string.h>
|
||||
|
||||
#include "lib/llist.h"
|
||||
#include "lib/framerate.h"
|
||||
|
||||
|
||||
CINELERRA_ERROR_DEFINE(TEST, "test error");
|
||||
|
||||
int
|
||||
main (int argc, char** argv)
|
||||
{
|
||||
NOBUG_INIT;
|
||||
|
||||
if (argc == 1)
|
||||
return 0;
|
||||
|
||||
if (!strcmp(argv[1], "basic"))
|
||||
{
|
||||
LLIST_AUTO (node1);
|
||||
|
||||
llist node2;
|
||||
llist_init (&node2);
|
||||
|
||||
printf ("%d\n", llist_is_empty (&node1));
|
||||
printf ("%d\n", llist_is_empty (&node2));
|
||||
}
|
||||
else if (!strcmp(argv[1], "nodeinsert"))
|
||||
{
|
||||
LLIST_AUTO (list);
|
||||
LLIST_AUTO (node1);
|
||||
LLIST_AUTO (node2);
|
||||
LLIST_AUTO (node3);
|
||||
|
||||
llist_insert_next (&list, &node1);
|
||||
printf ("%d\n", llist_is_empty (&list));
|
||||
printf ("%d\n", llist_is_empty (&node1));
|
||||
printf ("%d\n", llist_is_single (&node1));
|
||||
llist_insert_next (&node1, &node2);
|
||||
printf ("%d\n", llist_is_single (&node1));
|
||||
llist_insert_prev (&node1, &node3);
|
||||
printf ("%d\n", llist_next (&list) == &node3);
|
||||
printf ("%d\n", llist_next (&node3) == &node1);
|
||||
printf ("%d\n", llist_next (&node1) == &node2);
|
||||
printf ("%d\n", llist_prev (&list) == &node2);
|
||||
|
||||
}
|
||||
else
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue