Add to git.
[c2lib.git] / test_hash.c
1 /* Test the hash class.
2  * By Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: test_hash.c,v 1.1 2001/02/08 12:51:31 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #include <pool.h>
31 #include <vector.h>
32 #include <pstring.h>
33 #include <hash.h>
34
35 /* You can't take the address of non-lvalues in C (ie. you can't
36  * evaluate expressions like "&2"). Hence the following constant
37  * definitions are necessary.
38  */
39 const int one = 1;
40 const int two = 2;
41 const int three = 3;
42 const int four = 4;
43 const int five = 5;
44 const int six = 6;
45 const int nine = 9;
46 const int sixteen = 16;
47 const int twentyfour = 24;
48 const int twentyfive = 25;
49 const int thirtyfive = 35;
50 const int thirtysix = 36;
51
52 int
53 main ()
54 {
55   hash h;
56   pool pool = new_pool ();
57   int v;
58   vector keys, values;
59
60   /* Create a int -> int hash. */
61   h = new_hash (pool, int, int);
62
63   /* Insert some new values. */
64   if (hash_insert (h, one, one) != 0) abort ();
65   if (hash_insert (h, two, four) != 0) abort ();
66   if (hash_insert (h, three, nine) != 0) abort ();
67   if (hash_insert (h, four, sixteen) != 0) abort ();
68   if (hash_insert (h, five, twentyfour) != 0) abort (); /* sic */
69   if (hash_insert (h, six, thirtyfive) != 0) abort (); /* sic */
70
71   /* This should replace existing values. */
72   if (hash_insert (h, five, twentyfive) == 0) abort ();
73   if (hash_insert (h, six, thirtysix) == 0) abort ();
74
75   /* Retrieve some values. */
76   if (hash_get (h, one, v) == 0 || v != 1) abort ();
77   if (hash_get (h, six, v) == 0 || v != 36) abort ();
78   if (hash_get (h, two, v) == 0 || v != 4) abort ();
79   if (hash_get (h, five, v) == 0 || v != 25) abort ();
80
81   /* Copy the hash. */
82   h = copy_hash (pool, h);
83
84   /* Erase a key and check that it no longer exists. */
85   if (hash_erase (h, one) == 0) abort ();
86   if (hash_get (h, one, v) != 0) abort ();
87
88   /* Get list of keys and values. */
89   keys = hash_keys (h);
90   values = hash_values (h);
91
92   printf ("keys = [ %s ]\n",
93           pjoin (pool, pvitostr (pool, keys), ", "));
94   printf ("values = [ %s ]\n",
95           pjoin (pool, pvitostr (pool, values), ", "));
96
97   delete_pool (pool);
98   exit (0);
99 }