Nilorea Library
C utilities for networking, threading, graphics
ex_list.c
1
8#include "nilorea/n_log.h"
9#include "nilorea/n_list.h"
10#include "nilorea/n_str.h"
11
12#define LIST_LIMIT 10
13#define NB_TEST_ELEM 15
14
15void print_list_info( LIST *list )
16{
17 __n_assert( list, return );
18 n_log( LOG_NOTICE, "List: %p, %d max_elements , %d elements", list, list -> nb_max_items, list -> nb_items );
19}
20
21int nstrcmp( const void *a, const void *b )
22{
23 const N_STR *s1 = a;
24 const N_STR *s2 = b;
25
26 if( !s1 || !s1 -> data )
27 return 1 ;
28 if( !s2 || !s2 -> data )
29 return -1 ;
30
31 return strcmp( s1 -> data, s2 -> data );
32}
33
34
35int main(void)
36{
38
39 LIST *list = new_generic_list( LIST_LIMIT );
40
41 __n_assert( list, return FALSE );
42
43 N_STR *nstr = NULL ;
44
45 n_log( LOG_NOTICE, "Testing empty list cleaning" );
46
47 list_destroy( &list );
48
49
50 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 );
51 list = new_generic_list( LIST_LIMIT );
52 for( int it = 0 ; it < NB_TEST_ELEM ; it ++ )
53 {
54 nstrprintf( nstr, "Nombre aleatoire : %d", rand()%1000 );
55 if( nstr )
56 {
57 int func = rand()%4 ;
58 switch( func )
59 {
60 case 0 :
61 n_log( LOG_NOTICE, "list_push" );
62 if( list_push( list, nstr, free_nstr_ptr ) == FALSE )
63 free_nstr( &nstr );
64 break;
65 case 1 :
66 n_log( LOG_NOTICE, "list_unshift" );
67 if( list_unshift( list, nstr, free_nstr_ptr ) == FALSE )
68 free_nstr( &nstr );
69 break;
70 case 2 :
71 n_log( LOG_NOTICE, "list_push_sorted" );
72 if( list_push_sorted( list, nstr, nstrcmp, free_nstr_ptr ) == FALSE )
73 free_nstr( &nstr );
74 break;
75 case 3 :
76 n_log( LOG_NOTICE, "list_unshift" );
77 if( list_unshift_sorted( list, nstr, nstrcmp, free_nstr_ptr ) == FALSE )
78 free_nstr( &nstr );
79 break;
80 default:
81 n_log( LOG_ERR, "should never happen: no func %d !", func );
82 break ;
83 }
84 nstr = NULL ;
85 print_list_info( list );
86 }
87 }
88 n_log( LOG_NOTICE, "Emptying the list and setting nb_max_item to unlimit" );
89 list_empty( list );
90 /* setiing no item limit in list */
91 list -> nb_max_items = -1 ;
92 for( int it = 0 ; it < NB_TEST_ELEM ; it ++ )
93 {
94 nstrprintf( nstr, "Nombre aleatoire : %d", rand()%1000 );
95 if( nstr )
96 {
97 int func = 2 + rand()%1 ;
98 switch( func )
99 {
100 case 0 :
101 n_log( LOG_NOTICE, "list_push" );
102 if( list_push( list, nstr, free_nstr_ptr ) == FALSE )
103 free_nstr( &nstr );
104 break;
105 case 1 :
106 n_log( LOG_NOTICE, "list_unshift" );
107 if( list_unshift( list, nstr, free_nstr_ptr ) == FALSE )
108 free_nstr( &nstr );
109 break;
110 case 2 :
111 n_log( LOG_NOTICE, "list_push_sorted" );
112 if( list_push_sorted( list, nstr, nstrcmp, free_nstr_ptr ) == FALSE )
113 free_nstr( &nstr );
114 break;
115 case 3 :
116 n_log( LOG_NOTICE, "list_unshift sorted" );
117 if( list_unshift_sorted( list, nstr, nstrcmp, free_nstr_ptr ) == FALSE )
118 free_nstr( &nstr );
119 break;
120 default:
121 n_log( LOG_ERR, "should never happen: no func %d !", func );
122 break ;
123 }
124 nstr = NULL ;
125 print_list_info( list );
126 }
127 }
128 list_foreach( node, list )
129 {
130 N_STR *nodestr = (N_STR *)node -> ptr ;
131 n_log( LOG_INFO, "Listnode: %p item: %s", node, nodestr -> data );
132 }
133 list_destroy( &list );
134
135 exit( 0 );
136} /* END_OF_MAIN */
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
int list_empty(LIST *list)
Empty a LIST list of pointers.
Definition: n_list.c:547
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition: n_list.c:244
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper.
Definition: n_list.h:70
int list_unshift(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer at the start of the list.
Definition: n_list.c:342
int list_destroy(LIST **list)
Empty and Free a list container.
Definition: n_list.c:603
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.
Definition: n_list.c:382
LIST * new_generic_list(int max_items)
Initialiaze a generic list container to max_items pointers.
Definition: n_list.c:20
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.
Definition: n_list.c:285
Structure of a generic LIST container.
Definition: n_list.h:45
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition: n_log.h:66
#define LOG_ERR
error conditions
Definition: n_log.h:58
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition: n_log.c:97
#define LOG_NOTICE
normal but significant condition
Definition: n_log.h:62
#define LOG_INFO
informational
Definition: n_log.h:64
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
Definition: n_str.c:55
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition: n_str.h:222
#define nstrprintf(__nstr_var,...)
Macro to quickly allocate and sprintf to N_STR *.
Definition: n_str.h:97
A box including a string and his lenght.
Definition: n_str.h:173
List structures and definitions.
Generic log system.
N_STR and string function declaration.