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