Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_list.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "nilorea/n_log.h"
28#include "nilorea/n_list.h"
29#include "nilorea/n_str.h"
30
31#define LIST_LIMIT 10
32#define NB_TEST_ELEM 15
33
34void print_list_info(LIST* list) {
35 __n_assert(list, return);
36 n_log(LOG_NOTICE, "List: %p, %d max_elements , %d elements", list, list->nb_max_items, list->nb_items);
37}
38
39int nstrcmp(const void* a, const void* b) {
40 const N_STR* s1 = a;
41 const N_STR* s2 = b;
42
43 if (!s1 || !s1->data)
44 return 1;
45 if (!s2 || !s2->data)
46 return -1;
47
48 return strcmp(s1->data, s2->data);
49}
50
51int main(void) {
53
55
56 __n_assert(list, return FALSE);
57
58 N_STR* nstr = NULL;
59
60 n_log(LOG_NOTICE, "Testing empty list cleaning");
61
62 list_destroy(&list);
63
64 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);
66 for (int it = 0; it < NB_TEST_ELEM; it++) {
67 nstrprintf(nstr, "Nombre aleatoire : %d", rand() % 1000);
68 if (nstr) {
69 int func = rand() % 4;
70 switch (func) {
71 case 0:
72 n_log(LOG_NOTICE, "list_push");
73 if (list_push(list, nstr, free_nstr_ptr) == FALSE)
74 free_nstr(&nstr);
75 break;
76 case 1:
77 n_log(LOG_NOTICE, "list_unshift");
78 if (list_unshift(list, nstr, free_nstr_ptr) == FALSE)
79 free_nstr(&nstr);
80 break;
81 case 2:
82 n_log(LOG_NOTICE, "list_push_sorted");
83 if (list_push_sorted(list, nstr, nstrcmp, free_nstr_ptr) == FALSE)
84 free_nstr(&nstr);
85 break;
86 case 3:
87 n_log(LOG_NOTICE, "list_unshift");
88 if (list_unshift_sorted(list, nstr, nstrcmp, free_nstr_ptr) == FALSE)
89 free_nstr(&nstr);
90 break;
91 default:
92 n_log(LOG_ERR, "should never happen: no func %d !", func);
93 break;
94 }
95 nstr = NULL;
96 print_list_info(list);
97 }
98 }
99 n_log(LOG_NOTICE, "Emptying the list and setting nb_max_item to unlimit");
100 list_empty(list);
101 /* setiing no item limit in list */
103 for (int it = 0; it < NB_TEST_ELEM; it++) {
104 nstrprintf(nstr, "Nombre aleatoire : %d", rand() % 1000);
105 if (nstr) {
106 int func = rand() % 4;
107 switch (func) {
108 case 0:
109 n_log(LOG_NOTICE, "list_push");
110 if (list_push(list, nstr, free_nstr_ptr) == FALSE)
111 free_nstr(&nstr);
112 break;
113 case 1:
114 n_log(LOG_NOTICE, "list_unshift");
115 if (list_unshift(list, nstr, free_nstr_ptr) == FALSE)
116 free_nstr(&nstr);
117 break;
118 case 2:
119 n_log(LOG_NOTICE, "list_push_sorted");
120 if (list_push_sorted(list, nstr, nstrcmp, free_nstr_ptr) == FALSE)
121 free_nstr(&nstr);
122 break;
123 case 3:
124 n_log(LOG_NOTICE, "list_unshift sorted");
125 if (list_unshift_sorted(list, nstr, nstrcmp, free_nstr_ptr) == FALSE)
126 free_nstr(&nstr);
127 break;
128 default:
129 n_log(LOG_ERR, "should never happen: no func %d !", func);
130 break;
131 }
132 nstr = NULL;
133 print_list_info(list);
134 }
135 }
136 list_foreach(node, list) {
137 N_STR* nodestr = (N_STR*)node->ptr;
138 n_log(LOG_INFO, "Listnode: %p item: %s", node, nodestr->data);
139 }
140
141 /* test list_pop and list_shift */
142 N_STR* popped = list_pop(list, N_STR);
143 if (popped) {
144 n_log(LOG_INFO, "list_pop: %s", _nstr(popped));
145 free_nstr_ptr(popped);
146 }
147 N_STR* shifted = list_shift(list, N_STR);
148 if (shifted) {
149 n_log(LOG_INFO, "list_shift: %s", _nstr(shifted));
150 free_nstr_ptr(shifted);
151 }
152
153 /* test list_search */
154 if (list->nb_items > 0) {
155 LIST_NODE* first = list->start;
156 LIST_NODE* found = list_search(list, first->ptr);
157 if (found) {
158 n_log(LOG_INFO, "list_search found node: %p", found);
159 }
160 }
161
162 /* test remove_list_node */
163 if (list->nb_items > 0) {
164 LIST_NODE* target = list->start;
165 void* removed_ptr = remove_list_node(list, target, void);
166 if (removed_ptr) {
167 n_log(LOG_INFO, "remove_list_node: removed %p", removed_ptr);
168 free_nstr_ptr(removed_ptr);
169 }
170 }
171
172 /* test new_list_node and list_node_push / list_node_pop */
173 nstrprintf(nstr, "Manual node test");
174 if (nstr) {
175 LIST_NODE* manual_node = new_list_node(nstr, free_nstr_ptr);
176 if (manual_node) {
177 list_node_push(list, manual_node);
178 n_log(LOG_INFO, "list_node_push: pushed manual node");
179 }
180 LIST_NODE* popped_node = list_node_pop(list);
181 if (popped_node) {
182 N_STR* popped_nstr = (N_STR*)popped_node->ptr;
183 n_log(LOG_INFO, "list_node_pop: %s", _nstr(popped_nstr));
184 if (popped_node->destroy_func) {
185 popped_node->destroy_func(popped_node->ptr);
186 }
187 Free(popped_node);
188 }
189 nstr = NULL;
190 }
191
192 /* test list_node_unshift / list_node_shift */
193 nstrprintf(nstr, "Unshift node test");
194 if (nstr) {
195 LIST_NODE* manual_node2 = new_list_node(nstr, free_nstr_ptr);
196 if (manual_node2) {
197 list_node_unshift(list, manual_node2);
198 n_log(LOG_INFO, "list_node_unshift: unshifted manual node");
199 }
200 LIST_NODE* shifted_node = list_node_shift(list);
201 if (shifted_node) {
202 N_STR* shifted_nstr = (N_STR*)shifted_node->ptr;
203 n_log(LOG_INFO, "list_node_shift: %s", _nstr(shifted_nstr));
204 if (shifted_node->destroy_func) {
205 shifted_node->destroy_func(shifted_node->ptr);
206 }
207 Free(shifted_node);
208 }
209 nstr = NULL;
210 }
211
212 list_destroy(&list);
213
214 exit(0);
215} /* END_OF_MAIN */
int main(void)
#define NB_TEST_ELEM
Definition ex_list.c:32
#define LIST_LIMIT
Definition ex_list.c:31
void print_list_info(LIST *list)
Definition ex_list.c:34
int nstrcmp(const void *a, const void *b)
Definition ex_list.c:39
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
void * ptr
void pointer to store
Definition n_list.h:45
size_t nb_max_items
Maximum number of items in the list.
Definition n_list.h:62
LIST_NODE * start
pointer to the start of the list
Definition n_list.h:65
size_t nb_items
number of item currently in the list
Definition n_list.h:60
void(* destroy_func)(void *ptr)
pointer to destructor function if any, else NULL
Definition n_list.h:48
#define UNLIMITED_LIST_ITEMS
flag to pass to new_generic_list for an unlimited number of item in the list.
Definition n_list.h:72
#define list_shift(__LIST_, __TYPE_)
Shift macro helper for void pointer casting.
Definition n_list.h:95
#define list_pop(__LIST_, __TYPE_)
Pop macro helper for void pointer casting.
Definition n_list.h:93
int list_empty(LIST *list)
Empty a LIST list of pointers.
Definition n_list.c:500
LIST_NODE * list_search(LIST *list, const void *ptr)
search ptr in list
Definition n_list.c:468
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:227
int list_node_unshift(LIST *list, LIST_NODE *node)
Add a pointer at the start of the list.
Definition n_list.c:199
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
Definition n_list.h:88
#define remove_list_node(__LIST_, __NODE_, __TYPE_)
Remove macro helper for void pointer casting.
Definition n_list.h:97
int list_unshift(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer at the start of the list.
Definition n_list.c:316
LIST_NODE * list_node_shift(LIST *list)
Get a LIST_NODE pointer from the start of the list.
Definition n_list.c:169
LIST_NODE * new_list_node(void *ptr, void(*destructor)(void *ptr))
Allocate a new node to link in a list.
Definition n_list.c:56
int list_destroy(LIST **list)
Empty and Free a list container.
Definition n_list.c:547
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:348
LIST_NODE * list_node_pop(LIST *list)
Get a LIST_NODE pointer from the end of the list.
Definition n_list.c:142
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Definition n_list.c:36
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:260
int list_node_push(LIST *list, LIST_NODE *node)
Add a filled node to the end of the list.
Definition n_list.c:116
Structure of a generic LIST container.
Definition n_list.h:58
Structure of a generic list node.
Definition n_list.h:43
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_INFO
informational
Definition n_log.h:81
char * data
the string
Definition n_str.h:62
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
Definition n_str.c:69
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:115
A box including a string and his lenght.
Definition n_str.h:60
List structures and definitions.
Generic log system.
N_STR and string function declaration.