1 /*
2  pblhttst.c - hash table test frame
3 
4  Copyright (C) 2002 - 2007   Peter Graf
5 
6    This file is part of PBL - The Program Base Library.
7    PBL is free software.
8 
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13 
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 
23    For more information on the Program Base Library or Peter Graf,
24    please see: http://www.mission-base.com/.
25 
26     $Log: pblhttst.c,v $
27     Revision 1.10  2009/11/26 18:18:43  peter
28     New Eclispse cpp version.
29 
30     Revision 1.9  2009/10/20 21:08:00  peter
31     Added the pblHtCurrentKey function.
32 
33     Revision 1.8  2009/03/08 20:56:50  peter
34     port to gcc (Ubuntu 4.3.2-1ubuntu12) 4.3.2.
35     Exposing the hash set and tree set interfaces.
36 
37     Revision 1.7  2009/02/03 16:40:14  peter
38     PBL vesion 1.04, optimizations,
39     MAC OS X port, port to Microsoft Visual C++ 2008 Express Edition,
40     exposing the array list and the linked list interface
41 
42 
43     Revision 1.2  2002/09/12 20:47:01  peter
44     added the isam file handling to the library
45 
46     Revision 1.1  2002/09/05 13:45:02  peter
47     Initial revision
48 
49 */
50 
51 /*
52  * make sure "strings <exe> | grep Id | sort -u" shows the source file versions
53  */
54 char* pblhttst_c_id = "$Id: pblhttst.c,v 1.10 2009/11/26 18:18:43 peter Exp $";
55 
56 #include <stdio.h>
57 #include <memory.h>
58 
59 #ifndef __APPLE__
60 #include <stdlib.h>
61 #endif
62 
63 #include "pbl.h"
64 
65 /*****************************************************************************/
66 /* #defines                                                                  */
67 /*****************************************************************************/
68 
69 /*****************************************************************************/
70 /* typedefs                                                                  */
71 /*****************************************************************************/
72 
73 /*****************************************************************************/
74 /* globals                                                                   */
75 /*****************************************************************************/
76 
77 /*****************************************************************************/
78 /* functions                                                                 */
79 /*****************************************************************************/
80 
81 /*
82  * test frame for the hash table library
83  *
84  * this test frame calls the hash table library,
85  * it does not have any parameters, it is meant for
86  * debugging the hash table library
87  */
pblHASHTABLE_TestFrame(int argc,char * argv[])88 int pblHASHTABLE_TestFrame( int argc, char * argv[ ] )
89 {
90     pblHashTable_t * ht;
91     int    rc;
92     size_t size;
93 
94     char * data;
95 
96     ht = pblHtCreate();
97     fprintf( stdout, "pblHtCreate() ht = %p\n", ht );
98 
99     rc = pblHtInsert( ht, "123", 4, "123" );
100     fprintf( stdout, "pblHtInsert( ht, 123, 4, 123 ) rc = %d\n", rc );
101 
102     rc = pblHtInsert( ht, "124", 4, "124" );
103     fprintf( stdout, "pblHtInsert( ht, 124, 4, 124 ) rc = %d\n", rc );
104 
105     rc = pblHtInsert( ht, "125", 4, "125" );
106     fprintf( stdout, "pblHtInsert( ht, 125, 4, 125 ) rc = %d\n", rc );
107 
108     rc = pblHtInsert( ht, "123", 4, "123" );
109     fprintf( stdout, "pblHtInsert( ht, 123, 4, 123 ) rc = %d\n", rc );
110 
111     rc = pblHtInsert( ht, "123", 3, "123" );
112     fprintf( stdout, "pblHtInsert( ht, 123, 3, 123 ) rc = %d\n", rc );
113 
114     data = pblHtLookup( ht, "123", 4 );
115     fprintf( stdout, "pblHtLookup( ht, 123, 4 ) data = %s\n",
116              data ? data : "NULL" );
117 
118     data = pblHtLookup( ht, "123", 3 );
119     fprintf( stdout, "pblHtLookup( ht, 123, 3 ) data = %s\n",
120              data ? data : "NULL" );
121 
122     data = pblHtLookup( ht, "124", 4 );
123     fprintf( stdout, "pblHtLookup( ht, 124, 4 ) data = %s\n",
124              data ? data : "NULL" );
125 
126     data = pblHtLookup( ht, "125", 4 );
127     fprintf( stdout, "pblHtLookup( ht, 125, 4 ) data = %s\n",
128              data ? data : "NULL" );
129 
130     data = pblHtLookup( ht, "126", 4 );
131     fprintf( stdout, "pblHtLookup( ht, 126, 4 ) data = %s\n",
132              data ? data : "NULL" );
133 
134 
135     for( data = pblHtFirst( ht ); data; data = pblHtNext( ht ))
136     {
137         data = pblHtCurrent( ht );
138         fprintf( stdout, "pblHtCurrent( ht ) data = %s\n",
139                  data ? data : "NULL" );
140         size = 0;
141         data = pblHtCurrentKey( ht, &size );
142         fprintf( stdout, "pblHtCurrentKey( ht, &size ) data = %s, size %lu\n",
143                  data ? data : "NULL", (unsigned long)size );
144 
145     }
146 
147     rc = pblHtRemove( ht, "125", 4 );
148     fprintf( stdout, "pblHtRemove( ht, 125, 4 ) rc = %d\n", rc );
149 
150     data = pblHtFirst( ht );
151     fprintf( stdout, "pblHtFirst( ht ) data = %s\n", data ? data : "NULL" );
152 
153     rc = pblHtDelete( ht );
154     fprintf( stdout, "pblHtDelete( ht, 125, 4 ) rc = %d\n", rc );
155 
156     while( !pblHtRemove( ht, 0, 0 ));
157 
158     rc = pblHtInsert( ht, "123", 4, "123" );
159     fprintf( stdout, "pblHtInsert( ht, 123, 4, 123 ) rc = %d\n", rc );
160 
161     rc = pblHtInsert( ht, "124", 4, "124" );
162     fprintf( stdout, "pblHtInsert( ht, 124, 4, 124 ) rc = %d\n", rc );
163 
164     rc = pblHtInsert( ht, "125", 4, "125" );
165     fprintf( stdout, "pblHtInsert( ht, 125, 4, 125 ) rc = %d\n", rc );
166 
167     rc = pblHtInsert( ht, "123", 4, "123" );
168     fprintf( stdout, "pblHtInsert( ht, 123, 4, 123 ) rc = %d\n", rc );
169 
170     rc = pblHtInsert( ht, "123", 3, "123" );
171     fprintf( stdout, "pblHtInsert( ht, 123, 3, 123 ) rc = %d\n", rc );
172 
173 
174     for( data = pblHtFirst( ht ); data; data = pblHtNext( ht ))
175     {
176         pblHtRemove( ht, 0, 0 );
177     }
178 
179     rc = pblHtDelete( ht );
180     fprintf( stdout, "pblHtDelete( ht ) rc = %d\n", rc );
181 
182     return( rc );
183 }
184 
185 /*
186  * Eclipse CDT does not like more than one main,
187  * therefore hide all but one main with this -D option
188  */
189 
190 #ifndef CDT_BUILD
191 
192 
main(int argc,char * argv[])193 int main( int argc, char * argv[] )
194 {
195     return( pblHASHTABLE_TestFrame( argc, argv ));
196 }
197 
198 #endif /* CDT_BUILD */
199 
200