Nilorea Library list api test.
#define LIST_LIMIT 10
#define NB_TEST_ELEM 15
void print_list_info(
LIST *list )
{
n_log(
LOG_NOTICE,
"List: %p, %d max_elements , %d elements", list, list -> nb_max_items, list -> nb_items );
}
int nstrcmp( const void *a, const void *b )
{
if( !s1 || !s1 -> data )
return 1 ;
if( !s2 || !s2 -> data )
return -1 ;
return strcmp( s1 -> data, s2 -> data );
}
int main(void)
{
n_log(
LOG_NOTICE,
"list list: adding %d element in list element (%d) list, empty the list at the end", NB_TEST_ELEM, LIST_LIMIT );
for( int it = 0 ; it < NB_TEST_ELEM ; it ++ )
{
nstrprintf( nstr,
"Nombre aleatoire : %d", rand()%1000 );
if( nstr )
{
int func = rand()%4 ;
switch( func )
{
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
default:
n_log(
LOG_ERR,
"should never happen: no func %d !", func );
break ;
}
nstr = NULL ;
print_list_info( list );
}
}
n_log(
LOG_NOTICE,
"Emptying the list and setting nb_max_item to unlimit" );
list -> nb_max_items = -1 ;
for( int it = 0 ; it < NB_TEST_ELEM ; it ++ )
{
nstrprintf( nstr,
"Nombre aleatoire : %d", rand()%1000 );
if( nstr )
{
int func = 2 + rand()%1 ;
switch( func )
{
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
default:
n_log(
LOG_ERR,
"should never happen: no func %d !", func );
break ;
}
nstr = NULL ;
print_list_info( list );
}
}
{
n_log(
LOG_INFO,
"Listnode: %p item: %s", node, nodestr -> data );
}
exit( 0 );
}
#define __n_assert(__ptr, __ret)
macro to assert things
int list_empty(LIST *list)
Empty a LIST list of pointers.
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper.
int list_unshift(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer at the start of the list.
int list_destroy(LIST **list)
Empty and Free a list container.
int list_unshift_sorted(LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
Add a pointer sorted in the list , starting by the start of the list.
LIST * new_generic_list(int max_items)
Initialiaze a generic list container to max_items pointers.
int list_push_sorted(LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
Add a pointer sorted in the list , starting by the end of the list.
Structure of a generic LIST container.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_INFO
informational
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
#define nstrprintf(__nstr_var,...)
Macro to quickly allocate and sprintf to N_STR *.
A box including a string and his lenght.
List structures and definitions.
N_STR and string function declaration.