Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_list.c

Nilorea Library list api test.

Nilorea Library list api test

Author
Castagnier Mickael
Version
1.0
Date
26/05/2015
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "nilorea/n_log.h"
#include "nilorea/n_list.h"
#include "nilorea/n_str.h"
#define LIST_LIMIT 10
#define NB_TEST_ELEM 15
void print_list_info(LIST* list) {
__n_assert(list, return);
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) {
const N_STR* s1 = a;
const N_STR* s2 = b;
if (!s1 || !s1->data)
return 1;
if (!s2 || !s2->data)
return -1;
return strcmp(s1->data, s2->data);
}
int main(void) {
__n_assert(list, return FALSE);
N_STR* nstr = NULL;
n_log(LOG_NOTICE, "Testing empty list cleaning");
list_destroy(&list);
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:
n_log(LOG_NOTICE, "list_push");
if (list_push(list, nstr, free_nstr_ptr) == FALSE)
free_nstr(&nstr);
break;
case 1:
n_log(LOG_NOTICE, "list_unshift");
if (list_unshift(list, nstr, free_nstr_ptr) == FALSE)
free_nstr(&nstr);
break;
case 2:
n_log(LOG_NOTICE, "list_push_sorted");
if (list_push_sorted(list, nstr, nstrcmp, free_nstr_ptr) == FALSE)
free_nstr(&nstr);
break;
case 3:
n_log(LOG_NOTICE, "list_unshift");
if (list_unshift_sorted(list, nstr, nstrcmp, free_nstr_ptr) == FALSE)
free_nstr(&nstr);
break;
default:
n_log(LOG_ERR, "should never happen: no func %d !", func);
break;
}
nstr = NULL;
}
}
n_log(LOG_NOTICE, "Emptying the list and setting nb_max_item to unlimit");
list_empty(list);
/* setiing no item limit in list */
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:
n_log(LOG_NOTICE, "list_push");
if (list_push(list, nstr, free_nstr_ptr) == FALSE)
free_nstr(&nstr);
break;
case 1:
n_log(LOG_NOTICE, "list_unshift");
if (list_unshift(list, nstr, free_nstr_ptr) == FALSE)
free_nstr(&nstr);
break;
case 2:
n_log(LOG_NOTICE, "list_push_sorted");
if (list_push_sorted(list, nstr, nstrcmp, free_nstr_ptr) == FALSE)
free_nstr(&nstr);
break;
case 3:
n_log(LOG_NOTICE, "list_unshift sorted");
if (list_unshift_sorted(list, nstr, nstrcmp, free_nstr_ptr) == FALSE)
free_nstr(&nstr);
break;
default:
n_log(LOG_ERR, "should never happen: no func %d !", func);
break;
}
nstr = NULL;
}
}
list_foreach(node, list) {
N_STR* nodestr = (N_STR*)node->ptr;
n_log(LOG_INFO, "Listnode: %p item: %s", node, nodestr->data);
}
/* test list_pop and list_shift */
N_STR* popped = list_pop(list, N_STR);
if (popped) {
n_log(LOG_INFO, "list_pop: %s", _nstr(popped));
free_nstr_ptr(popped);
}
N_STR* shifted = list_shift(list, N_STR);
if (shifted) {
n_log(LOG_INFO, "list_shift: %s", _nstr(shifted));
free_nstr_ptr(shifted);
}
/* test list_search */
if (list->nb_items > 0) {
LIST_NODE* first = list->start;
LIST_NODE* found = list_search(list, first->ptr);
if (found) {
n_log(LOG_INFO, "list_search found node: %p", found);
}
}
/* test remove_list_node */
if (list->nb_items > 0) {
LIST_NODE* target = list->start;
void* removed_ptr = remove_list_node(list, target, void);
if (removed_ptr) {
n_log(LOG_INFO, "remove_list_node: removed %p", removed_ptr);
free_nstr_ptr(removed_ptr);
}
}
/* test new_list_node and list_node_push / list_node_pop */
nstrprintf(nstr, "Manual node test");
if (nstr) {
LIST_NODE* manual_node = new_list_node(nstr, free_nstr_ptr);
if (manual_node) {
list_node_push(list, manual_node);
n_log(LOG_INFO, "list_node_push: pushed manual node");
}
LIST_NODE* popped_node = list_node_pop(list);
if (popped_node) {
N_STR* popped_nstr = (N_STR*)popped_node->ptr;
n_log(LOG_INFO, "list_node_pop: %s", _nstr(popped_nstr));
if (popped_node->destroy_func) {
popped_node->destroy_func(popped_node->ptr);
}
Free(popped_node);
}
nstr = NULL;
}
/* test list_node_unshift / list_node_shift */
nstrprintf(nstr, "Unshift node test");
if (nstr) {
LIST_NODE* manual_node2 = new_list_node(nstr, free_nstr_ptr);
if (manual_node2) {
list_node_unshift(list, manual_node2);
n_log(LOG_INFO, "list_node_unshift: unshifted manual node");
}
LIST_NODE* shifted_node = list_node_shift(list);
if (shifted_node) {
N_STR* shifted_nstr = (N_STR*)shifted_node->ptr;
n_log(LOG_INFO, "list_node_shift: %s", _nstr(shifted_nstr));
if (shifted_node->destroy_func) {
shifted_node->destroy_func(shifted_node->ptr);
}
Free(shifted_node);
}
nstr = NULL;
}
list_destroy(&list);
exit(0);
} /* 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.