Add to git.
[c2lib.git] / test_shash.c
1 /* Test the shash (string -> something 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_shash.c,v 1.2 2002/08/23 09:44:08 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 const int red = 1, orange = 2, yellow = 3, green = 4;
36
37 int
38 main ()
39 {
40   shash h;
41   pool pool = new_pool (), pool2, tmp;
42   int v;
43   vector keys, values;
44
45   /* Create a string -> string hash. */
46   h = new_shash (pool, int);
47
48   /* Insert some new values. */
49   if (shash_insert (h, "tomato", red) != 0) abort ();
50   if (shash_insert (h, "orange", orange) != 0) abort ();
51   if (shash_insert (h, "lemon", yellow) != 0) abort ();
52   if (shash_insert (h, "lime", green) != 0) abort ();
53   if (shash_insert (h, "peach", orange) != 0) abort ();
54   if (shash_insert (h, "apple", green) != 0) abort ();
55   if (shash_insert (h, "raspberry", red) != 0) abort ();
56
57   /* This should replace existing values. */
58   if (shash_insert (h, "apple", red) == 0) abort ();
59   if (shash_insert (h, "peach", yellow) == 0) abort ();
60
61   /* Retrieve some values. */
62   if (shash_get (h, "apple", v) == 0 || v != red) abort ();
63   if (shash_get (h, "tomato", v) == 0 || v != red) abort ();
64   if (shash_get (h, "orange", v) == 0 || v != orange) abort ();
65
66   /* Copy the shash. */
67   h = copy_shash (pool, h);
68
69   /* Erase a key and check that it no longer exists. */
70   if (shash_erase (h, "apple") == 0) abort ();
71   if (shash_get (h, "apple", v) != 0) abort ();
72
73   /* Get list of keys and values. */
74   keys = shash_keys (h);
75   values = shash_values (h);
76
77   printf ("keys = [ %s ]\n", pjoin (pool, keys, ", "));
78   printf ("values = [ %s ]\n", pjoin (pool, pvitostr (pool, values), ", "));
79
80   /* Regression test: get list of keys into another temporary pool. */
81   tmp = new_pool ();
82   keys = shash_keys_in_pool (h, tmp);
83   printf ("keys = [ %s ]\n", pjoin (pool, keys, ", "));
84   delete_pool (tmp);
85
86   /* Copy the shash into another pool, delete the old pool, check all
87    * keys and values have been copied across.
88    */
89   pool2 = new_pool ();
90   h = copy_shash (pool2, h);
91   delete_pool (pool);
92
93   keys = shash_keys (h);
94   values = shash_values (h);
95
96   printf ("keys = [ %s ]\n", pjoin (pool2, keys, ", "));
97   printf ("values = [ %s ]\n", pjoin (pool2, pvitostr (pool2, values), ", "));
98
99   delete_pool (pool2);
100   exit (0);
101 }