libubox
C utility functions for OpenWrt.
test-list.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "utils.h"

Go to the source code of this file.

Data Structures

struct  item
 

Macros

#define OUT(fmt, ...)
 

Functions

static void init_list (struct list_head *list)
 
static void test_basics ()
 
static void test_while_list_empty ()
 
int main ()
 

Macro Definition Documentation

◆ OUT

#define OUT (   fmt,
  ... 
)
Value:
do { \
fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
} while (0);

Definition at line 13 of file test-list.c.

Function Documentation

◆ init_list()

static void init_list ( struct list_head list)
static

Definition at line 17 of file test-list.c.

18 {
19  const char *vals[] = {
20  "zero", "one", "two", "three", "four", "five", "six",
21  "seven", "eight", "nine", "ten", "eleven", "twelve"
22  };
23 
24  OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
25  OUT("list_add_tail: ");
26  for (size_t i=0; i<ARRAY_SIZE(vals); i++) {
27  struct item *e = malloc(sizeof(struct item));
28  e->name = vals[i];
29  list_add_tail(&e->list, list);
30  fprintf(stdout, "%s ", vals[i]);
31  }
32  fprintf(stdout, "\n");
33  OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
34 }
static void list_add_tail(struct list_head *_new, struct list_head *head)
Definition: list.h:165
static bool list_empty(const struct list_head *head)
Definition: list.h:69
Definition: test-list.c:8
struct list_head list
Definition: test-list.c:10
const char * name
Definition: test-list.c:9
#define ARRAY_SIZE(x)
#define OUT(fmt,...)
Definition: test-list.c:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 112 of file test-list.c.

113 {
114  test_basics();
116  return 0;
117 }
static void test_while_list_empty()
Definition: test-list.c:96
static void test_basics()
Definition: test-list.c:36
Here is the call graph for this function:

◆ test_basics()

static void test_basics ( )
static

Definition at line 36 of file test-list.c.

37 {
38  struct item *tmp;
39  struct item *item;
40  struct item *last;
41  struct item *first;
42  struct list_head test_list = LIST_HEAD_INIT(test_list);
43 
44  init_list(&test_list);
45 
46  first = list_first_entry(&test_list, struct item, list);
47  last = list_last_entry(&test_list, struct item, list);
48  OUT("first=%s last=%s\n", first->name, last->name);
49  OUT("'zero' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
50  OUT("'twelve' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
51 
52  OUT("removing 'twelve' and 'zero'\n");
53  list_del(&first->list);
54  list_del(&last->list);
55  free(first);
56  free(last);
57 
58  first = list_first_entry(&test_list, struct item, list);
59  last = list_last_entry(&test_list, struct item, list);
60 
61  if (!first || !last)
62  return;
63 
64  OUT("first=%s last=%s\n", first->name, last->name);
65  OUT("'one' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
66  OUT("'eleven' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
67 
68  OUT("moving 'one' to the tail\n");
69  list_move_tail(&first->list, &test_list);
70  first = list_first_entry(&test_list, struct item, list);
71  last = list_last_entry(&test_list, struct item, list);
72  OUT("first=%s last=%s\n", first->name, last->name);
73  OUT("'two' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
74  OUT("'one' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
75 
76  OUT("list_for_each_entry: ");
77  list_for_each_entry(item, &test_list, list) {
78  fprintf(stdout, "%s ", item->name);
79  }
80  fprintf(stdout, "\n");
81 
82  OUT("list_for_each_entry_reverse: ");
83  list_for_each_entry_reverse(item, &test_list, list) {
84  fprintf(stdout, "%s ", item->name);
85  }
86  fprintf(stdout, "\n");
87 
88  OUT("delete all entries\n");
89  list_for_each_entry_safe(item, tmp, &test_list, list) {
90  list_del(&item->list);
91  free(item);
92  }
93  OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
94 }
#define list_for_each_entry_reverse(p, h, field)
Definition: list.h:151
static void list_move_tail(struct list_head *entry, struct list_head *head)
Definition: list.h:178
#define LIST_HEAD_INIT(name)
Definition: list.h:58
static bool list_is_first(const struct list_head *list, const struct list_head *head)
Definition: list.h:75
#define list_for_each_entry_safe(p, n, h, field)
Definition: list.h:146
#define list_for_each_entry(p, h, field)
Definition: list.h:132
static void list_del(struct list_head *entry)
Definition: list.h:96
static bool list_is_last(const struct list_head *list, const struct list_head *head)
Definition: list.h:82
#define list_first_entry(ptr, type, field)
Definition: list.h:121
#define list_last_entry(ptr, type, field)
Definition: list.h:122
Definition: list.h:53
static void init_list(struct list_head *list)
Definition: test-list.c:17
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_while_list_empty()

static void test_while_list_empty ( )
static

Definition at line 96 of file test-list.c.

97 {
98  struct item *first;
99  struct list_head test_list = LIST_HEAD_INIT(test_list);
100 
101  init_list(&test_list);
102 
103  OUT("delete all entries\n");
104  while (!list_empty(&test_list)) {
105  first = list_first_entry(&test_list, struct item, list);
106  list_del(&first->list);
107  free(first);
108  }
109  OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
110 }
Here is the call graph for this function:
Here is the caller graph for this function: