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

Go to the source code of this file.

Data Structures

struct  node
 

Macros

#define OUT(fmt, ...)
 

Functions

static void test_basics ()
 
int main ()
 

Macro Definition Documentation

◆ OUT

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

Definition at line 9 of file test-avl.c.

Function Documentation

◆ main()

int main ( )

Definition at line 83 of file test-avl.c.

84 {
85  test_basics();
86  return 0;
87 }
static void test_basics()
Definition: test-avl.c:17

◆ test_basics()

static void test_basics ( )
static

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

18 {
19  size_t i;
20  struct avl_tree t;
21  struct node *temp;
22  struct node *elem;
23  struct node *last;
24  struct node *first;
25  const char *vals[] = {
26  "zero", "one", "two", "three", "four", "five", "six",
27  "seven", "eight", "nine", "ten", "eleven", "twelve"
28  };
29 
30  avl_init(&t, avl_strcmp, false, NULL);
31 
32  OUT("insert: ");
33  for (i=0; i<ARRAY_SIZE(vals); i++) {
34  struct node *n = malloc(sizeof(struct node));
35  n->avl.key = vals[i];
36 
37  int r = avl_insert(&t, &n->avl);
38  fprintf(stdout, "%d=%s ", r, (char *)n->avl.key);
39  }
40  fprintf(stdout, "\n");
41 
42  OUT("insert duplicate: ");
43  for (i=0; i<ARRAY_SIZE(vals); i++) {
44  struct node *n = malloc(sizeof(struct node));
45  n->avl.key = vals[i];
46 
47  int r = avl_insert(&t, &n->avl);
48  fprintf(stdout, "%d=%s ", r, (char *)n->avl.key);
49 
50  if (r)
51  free(n);
52  }
53  fprintf(stdout, "\n");
54 
55  first = avl_first_element(&t, first, avl);
56  last = avl_last_element(&t, last, avl);
57  OUT("first=%s last=%s\n", (char*)first->avl.key, (char*)last->avl.key);
58 
59  OUT("for each element: ");
60  avl_for_each_element(&t, elem, avl) {
61  fprintf(stdout, "%s ", (char*)elem->avl.key);
62  }
63  fprintf(stdout, "\n");
64 
65  OUT("delete 'one' element\n");
66  elem = avl_find_element(&t, "one", elem, avl);
67  avl_delete(&t, &elem->avl);
68  free(elem);
69 
70  OUT("for each element reverse: ");
72  fprintf(stdout, "%s ", (char*)elem->avl.key);
73  }
74  fprintf(stdout, "\n");
75 
76  OUT("delete all elements\n");
77  avl_for_each_element_safe(&t, elem, avl, temp) {
78  avl_delete(&t, &elem->avl);
79  free(elem);
80  }
81 }
int avl_strcmp(const void *k1, const void *k2, void *ptr)
Definition: avl-cmp.c:26
int avl_insert(struct avl_tree *tree, struct avl_node *new)
Definition: avl.c:220
void avl_init(struct avl_tree *tree, avl_tree_comp comp, bool allow_dups, void *ptr)
Definition: avl.c:92
void avl_delete(struct avl_tree *tree, struct avl_node *node)
Definition: avl.c:307
#define avl_first_element(tree, element, node_member)
Definition: avl.h:280
#define avl_find_element(tree, key, element, node_element)
Definition: avl.h:240
#define avl_for_each_element_reverse(tree, element, node_member)
Definition: avl.h:382
#define avl_for_each_element(tree, element, node_member)
Definition: avl.h:366
#define avl_last_element(tree, element, node_member)
Definition: avl.h:292
#define avl_for_each_element_safe(tree, element, node_member, ptr)
Definition: avl.h:503
const void * key
Definition: avl.h:84
Definition: avl.h:110
Definition: test-avl.c:13
struct avl_node avl
Definition: test-avl.c:14
#define OUT(fmt,...)
Definition: test-avl.c:9
#define ARRAY_SIZE(x)
Here is the call graph for this function: