1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #include "avl.h" 6 #include "avl-cmp.h" 7 #include "utils.h" 8 9 #define OUT(fmt, ...) do { \ 10 fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \ 11 } while (0); 12 13 struct node { 14 struct avl_node avl; 15 }; 16 17 static void test_basics() 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: "); 71 avl_for_each_element_reverse(&t, elem, avl) { 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 } 82 83 int main() 84 { 85 test_basics(); 86 return 0; 87 } 88
This page was automatically generated by LXR 0.3.1. • OpenWrt