Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
test_dlinklists.c File Reference

Doubly-Linked List Tests. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/dlinkedlists.h"
Include dependency graph for test_dlinklists.c:

Go to the source code of this file.

Data Structures

struct  test_container::entries
 
struct  test1
 
struct  test_container
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void destroy_test_container (struct test_container *x)
 
static void dll_tests (void)
 
static int load_module (void)
 
static struct test_containermake_cont (void)
 
static struct test1make_test1 (char *name)
 
static void print_list (struct test_container *x, char *expect)
 
static void print_list_backwards (struct test_container *x, char *expect)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Test Doubly-Linked Lists" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Doubly-Linked List Tests.

Author
Steve Murphy <[email protected]> 

This module will run some DLL tests at load time

Definition in file test_dlinklists.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 364 of file test_dlinklists.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 364 of file test_dlinklists.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 364 of file test_dlinklists.c.

◆ destroy_test_container()

static void destroy_test_container ( struct test_container x)
static

Definition at line 101 of file test_dlinklists.c.

References AST_DLLIST_REMOVE_CURRENT, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END, ast_free, test_container::entries, test1::list, and t1.

Referenced by dll_tests().

102 {
103  /* remove all the test1's */
104  struct test1 *t1;
107  ast_free(t1);
108  }
110  ast_free(x);
111 }
#define AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: dlinkedlists.h:887
#define AST_DLLIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: dlinkedlists.h:752
struct test1::@508 list
struct test_container::entries entries
#define ast_free(a)
Definition: astmm.h:182
#define AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END
Closes a safe loop traversal block.
Definition: dlinkedlists.h:920
static uint16_t t1
Definition: res_pktccops.c:157

◆ dll_tests()

static void dll_tests ( void  )
static

Definition at line 172 of file test_dlinklists.c.

References a, ast_debug, AST_DLLIST_EMPTY, AST_DLLIST_FIRST, AST_DLLIST_INSERT_AFTER, AST_DLLIST_INSERT_BEFORE_CURRENT, AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS, AST_DLLIST_INSERT_HEAD, AST_DLLIST_INSERT_TAIL, AST_DLLIST_LAST, AST_DLLIST_MOVE_CURRENT, AST_DLLIST_MOVE_CURRENT_BACKWARDS, AST_DLLIST_NEXT, AST_DLLIST_PREV, AST_DLLIST_REMOVE, AST_DLLIST_REMOVE_CURRENT, AST_DLLIST_REMOVE_HEAD, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN, AST_DLLIST_TRAVERSE_SAFE_BEGIN, AST_DLLIST_TRAVERSE_SAFE_END, ast_free, ast_log, b, c, d, destroy_test_container(), test_container::entries, test1::list, LOG_ERROR, make_cont(), make_test1(), print_list(), and print_list_backwards().

Referenced by load_module().

173 {
174  struct test_container *tc;
175  struct test1 *a;
176  struct test1 *b;
177  struct test1 *c;
178  struct test1 *d;
179  struct test1 *e;
180 
181  ast_debug(1,"Test AST_DLLIST_INSERT_HEAD, AST_DLLIST_TRAVERSE, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END\n");
182  tc = make_cont();
183  a = make_test1("A");
184  b = make_test1("B");
185  c = make_test1("C");
186  d = make_test1("D");
191  print_list(tc, "A <=> B <=> C <=> D");
192 
194 
195  tc = make_cont();
196 
197  if (AST_DLLIST_EMPTY(&tc->entries))
198  ast_debug(1,"Test AST_DLLIST_EMPTY....OK\n");
199  else
200  ast_log(LOG_ERROR,"Test AST_DLLIST_EMPTY....PROBLEM!!\n");
201 
202 
203  a = make_test1("A");
204  b = make_test1("B");
205  c = make_test1("C");
206  d = make_test1("D");
207 
208  ast_debug(1,"Test AST_DLLIST_INSERT_TAIL\n");
213  print_list(tc, "A <=> B <=> C <=> D");
214 
215  if (AST_DLLIST_FIRST(&tc->entries) == a)
216  ast_debug(1,"Test AST_DLLIST_FIRST....OK\n");
217  else
218  ast_log(LOG_ERROR,"Test AST_DLLIST_FIRST....PROBLEM\n");
219 
220  if (AST_DLLIST_LAST(&tc->entries) == d)
221  ast_debug(1,"Test AST_DLLIST_LAST....OK\n");
222  else
223  ast_log(LOG_ERROR,"Test AST_DLLIST_LAST....PROBLEM\n");
224 
225  if (AST_DLLIST_NEXT(a,list) == b)
226  ast_debug(1,"Test AST_DLLIST_NEXT....OK\n");
227  else
228  ast_log(LOG_ERROR,"Test AST_DLLIST_NEXT....PROBLEM\n");
229 
230  if (AST_DLLIST_PREV(d,list) == c)
231  ast_debug(1,"Test AST_DLLIST_PREV....OK\n");
232  else
233  ast_log(LOG_ERROR,"Test AST_DLLIST_PREV....PROBLEM\n");
234 
236 
237  tc = make_cont();
238 
239  a = make_test1("A");
240  b = make_test1("B");
241  c = make_test1("C");
242  d = make_test1("D");
243 
244  ast_debug(1,"Test AST_DLLIST_INSERT_AFTER, AST_DLLIST_TRAVERSE_BACKWARDS\n");
246  AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
247  AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
248  AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
249  print_list_backwards(tc, "D <=> C <=> B <=> A");
250 
251  ast_debug(1,"Test AST_DLLIST_REMOVE_HEAD\n");
253  print_list_backwards(tc, "D <=> C <=> B");
254  ast_debug(1,"Test AST_DLLIST_REMOVE_HEAD\n");
256  print_list_backwards(tc, "D <=> C");
257  ast_debug(1,"Test AST_DLLIST_REMOVE_HEAD\n");
259  print_list_backwards(tc, "D");
261 
262  if (AST_DLLIST_EMPTY(&tc->entries))
263  ast_debug(1,"Test AST_DLLIST_REMOVE_HEAD....OK\n");
264  else
265  ast_log(LOG_ERROR,"Test AST_DLLIST_REMOVE_HEAD....PROBLEM!!\n");
266 
268  AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
269  AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
270  AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
271 
272  ast_debug(1,"Test AST_DLLIST_REMOVE\n");
273  AST_DLLIST_REMOVE(&tc->entries, c, list);
274  print_list(tc, "A <=> B <=> D");
275  AST_DLLIST_REMOVE(&tc->entries, a, list);
276  print_list(tc, "B <=> D");
277  AST_DLLIST_REMOVE(&tc->entries, d, list);
278  print_list(tc, "B");
279  AST_DLLIST_REMOVE(&tc->entries, b, list);
280 
281  if (AST_DLLIST_EMPTY(&tc->entries))
282  ast_debug(1,"Test AST_DLLIST_REMOVE....OK\n");
283  else
284  ast_log(LOG_ERROR,"Test AST_DLLIST_REMOVE....PROBLEM!!\n");
285 
287  AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
288  AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
289  AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
290 
293  }
295  if (AST_DLLIST_EMPTY(&tc->entries))
296  ast_debug(1,"Test AST_DLLIST_REMOVE_CURRENT... OK\n");
297  else
298  ast_log(LOG_ERROR,"Test AST_DLLIST_REMOVE_CURRENT... PROBLEM\n");
299 
300  ast_debug(1,"Test AST_DLLIST_MOVE_CURRENT, AST_DLLIST_INSERT_BEFORE_CURRENT\n");
302  AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
303  AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
305  if (e == a) {
306  AST_DLLIST_INSERT_BEFORE_CURRENT(d, list); /* D A B C */
307  }
308 
309  if (e == b) {
310  AST_DLLIST_MOVE_CURRENT(&tc->entries, list); /* D A C B */
311  }
312 
313  }
315  print_list(tc, "D <=> A <=> C <=> B");
316 
318 
319  tc = make_cont();
320 
321  a = make_test1("A");
322  b = make_test1("B");
323  c = make_test1("C");
324  d = make_test1("D");
325 
326  ast_debug(1,"Test: AST_DLLIST_MOVE_CURRENT_BACKWARDS and AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS\n");
328  AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
329  AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
331  if (e == c && AST_DLLIST_FIRST(&tc->entries) != c) {
333  print_list(tc, "C <=> A <=> B");
334  }
335 
336  if (e == b) {
337  AST_DLLIST_REMOVE_CURRENT(list); /* C A */
338  ast_free(b);
339  print_list(tc, "C <=> A");
340  }
341  if (e == a) {
343  print_list(tc, "C <=> A <=> D");
344  }
345 
346  }
348  print_list(tc, "C <=> A <=> D");
349 
351 }
#define AST_DLLIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: dlinkedlists.h:848
static void print_list_backwards(struct test_container *x, char *expect)
#define AST_DLLIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
static struct test_val d
#define AST_DLLIST_MOVE_CURRENT_BACKWARDS(newhead, field)
Move the current list entry to another list at the head.
Definition: dlinkedlists.h:800
#define AST_DLLIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
#define AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: dlinkedlists.h:887
#define AST_DLLIST_INSERT_AFTER(head, listelm, elm, field)
Inserts a list entry after a given entry.
static void print_list(struct test_container *x, char *expect)
#define AST_DLLIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: dlinkedlists.h:752
static struct test_val c
struct test1::@508 list
static void destroy_test_container(struct test_container *x)
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define AST_DLLIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
#define ast_log
Definition: astobj2.c:42
#define AST_DLLIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: dlinkedlists.h:445
#define AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS(elm, field)
Inserts a list entry after the current entry during a backwards traversal. Since this is a backwards ...
Definition: dlinkedlists.h:903
static struct test1 * make_test1(char *name)
struct test_container::entries entries
#define AST_DLLIST_INSERT_BEFORE_CURRENT(elm, field)
Inserts a list node before the current node during a traversal.
Definition: dlinkedlists.h:695
#define LOG_ERROR
Definition: logger.h:285
#define ast_free(a)
Definition: astmm.h:182
#define AST_DLLIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: dlinkedlists.h:468
#define AST_DLLIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: dlinkedlists.h:912
#define AST_DLLIST_PREV(elm, field)
Returns the previous entry in the list before the given entry.
Definition: dlinkedlists.h:456
static struct test_container * make_cont(void)
#define AST_DLLIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
static struct test_val b
#define AST_DLLIST_FIRST(head)
Returns the first entry contained in a list.
Definition: dlinkedlists.h:421
#define AST_DLLIST_MOVE_CURRENT(newhead, field)
Move the current list entry to another list at the tail.
Definition: dlinkedlists.h:781
#define AST_DLLIST_LAST(head)
Returns the last entry contained in a list.
Definition: dlinkedlists.h:430
static struct test_val a

◆ load_module()

static int load_module ( void  )
static

Definition at line 358 of file test_dlinklists.c.

References AST_MODULE_LOAD_SUCCESS, and dll_tests().

359 {
360  dll_tests();
362 }
static void dll_tests(void)

◆ make_cont()

static struct test_container* make_cont ( void  )
static

Definition at line 88 of file test_dlinklists.c.

References ast_calloc.

Referenced by dll_tests().

89 {
90  struct test_container *t = ast_calloc(sizeof(struct test_container),1);
91  return t;
92 }
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204

◆ make_test1()

static struct test1* make_test1 ( char *  name)
static

Definition at line 94 of file test_dlinklists.c.

References ast_calloc, test1::name, and t1.

Referenced by dll_tests().

95 {
96  struct test1 *t1 = ast_calloc(sizeof(struct test1),1);
97  strcpy(t1->name, name);
98  return t1;
99 }
char name[10]
static const char name[]
Definition: cdr_mysql.c:74
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static uint16_t t1
Definition: res_pktccops.c:157

◆ print_list()

static void print_list ( struct test_container x,
char *  expect 
)
static

Definition at line 60 of file test_dlinklists.c.

References ast_debug, AST_DLLIST_LAST, AST_DLLIST_TRAVERSE, buff, test_container::entries, test1::list, test1::name, and t1.

Referenced by dll_tests().

61 {
62  struct test1 *t1;
63  char buff[1000];
64  buff[0] = 0;
65  AST_DLLIST_TRAVERSE(&x->entries, t1, list) {
66  strcat(buff,t1->name);
67  if (t1 != AST_DLLIST_LAST(&x->entries))
68  strcat(buff," <=> ");
69  }
70 
71  ast_debug(1,"Got: %s [expect %s]\n", buff, expect);
72 }
struct test1::@508 list
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define AST_DLLIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: dlinkedlists.h:575
char name[10]
struct test_container::entries entries
static unsigned char * buff
Definition: chan_unistim.c:259
#define AST_DLLIST_LAST(head)
Returns the last entry contained in a list.
Definition: dlinkedlists.h:430
static uint16_t t1
Definition: res_pktccops.c:157

◆ print_list_backwards()

static void print_list_backwards ( struct test_container x,
char *  expect 
)
static

Definition at line 74 of file test_dlinklists.c.

References ast_debug, AST_DLLIST_FIRST, AST_DLLIST_TRAVERSE_BACKWARDS, buff, test_container::entries, test1::list, test1::name, and t1.

Referenced by dll_tests().

75 {
76  struct test1 *t1;
77  char buff[1000];
78  buff[0] = 0;
80  strcat(buff,t1->name);
81  if (t1 != AST_DLLIST_FIRST(&x->entries))
82  strcat(buff," <=> ");
83  }
84 
85  ast_debug(1,"Got: %s [expect %s]\n", buff, expect);
86 }
#define AST_DLLIST_TRAVERSE_BACKWARDS(head, var, field)
Loops over (traverses) the entries in a list in reverse order, starting at the end.
Definition: dlinkedlists.h:617
struct test1::@508 list
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
char name[10]
struct test_container::entries entries
static unsigned char * buff
Definition: chan_unistim.c:259
#define AST_DLLIST_FIRST(head)
Returns the first entry contained in a list.
Definition: dlinkedlists.h:421
static uint16_t t1
Definition: res_pktccops.c:157

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 353 of file test_dlinklists.c.

354 {
355  return 0;
356 }

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Test Doubly-Linked Lists" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 364 of file test_dlinklists.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 364 of file test_dlinklists.c.