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