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