]> git.sur5r.net Git - openldap/blob - libraries/libavl/testavl.c
All implementations of lutil_lockf (aka ldap_lockf) block until
[openldap] / libraries / libavl / testavl.c
1 /* testavl.c - Test Tim Howes AVL code */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <ac/string.h>
9 #include <sys/types.h>
10
11 #include "avl.h"
12
13 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
14 static void myprint LDAP_P(( Avlnode *root ));
15
16 int
17 main( int argc, char **argv )
18 {
19         Avlnode *tree = NULLAVL;
20         char    command[ 10 ];
21         char    name[ 80 ];
22         char    *p;
23
24         printf( "> " );
25         while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
26                 switch( *command ) {
27                 case 'n':       /* new tree */
28                         ( void ) avl_free( tree, (AVL_FREE) free );
29                         tree = NULLAVL;
30                         break;
31                 case 'p':       /* print */
32                         ( void ) myprint( tree );
33                         break;
34                 case 't':       /* traverse with first, next */
35                         printf( "***\n" );
36                         for ( p = (char * ) avl_getfirst( tree );
37                             p != NULL; p = (char *) avl_getnext( /* tree, p */ ) )
38                                 printf( "%s\n", p );
39                         printf( "***\n" );
40                         break;
41                 case 'f':       /* find */
42                         printf( "data? " );
43                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
44                                 exit( 0 );
45                         name[ strlen( name ) - 1 ] = '\0';
46                         if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
47                             == NULL )
48                                 printf( "Not found.\n\n" );
49                         else
50                                 printf( "%s\n\n", p );
51                         break;
52                 case 'i':       /* insert */
53                         printf( "data? " );
54                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
55                                 exit( 0 );
56                         name[ strlen( name ) - 1 ] = '\0';
57                         if ( avl_insert( &tree, strdup( name ), (AVL_CMP) strcmp, 
58                             avl_dup_error ) != 0 )
59                                 printf( "\nNot inserted!\n" );
60                         break;
61                 case 'd':       /* delete */
62                         printf( "data? " );
63                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
64                                 exit( 0 );
65                         name[ strlen( name ) - 1 ] = '\0';
66                         if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
67                                 printf( "\nNot found!\n" );
68                         break;
69                 case 'q':       /* quit */
70                         exit( 0 );
71                         break;
72                 case '\n':
73                         break;
74                 default:
75                         printf("Commands: insert, delete, print, new, quit\n");
76                 }
77
78                 printf( "> " );
79         }
80
81         return( 0 );
82 }
83
84 static void ravl_print( Avlnode *root, int depth )
85 {
86         int     i;
87
88         if ( root == 0 )
89                 return;
90
91         ravl_print( root->avl_right, depth+1 );
92
93         for ( i = 0; i < depth; i++ )
94                 printf( "   " );
95         printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
96
97         ravl_print( root->avl_left, depth+1 );
98 }
99
100 static void myprint( Avlnode *root )
101 {
102         printf( "********\n" );
103
104         if ( root == 0 )
105                 printf( "\tNULL\n" );
106         else
107                 ravl_print( root, 0 );
108
109         printf( "********\n" );
110 }