1 /* Test the shash (string -> something hash) class.
2 * By Richard W.M. Jones <rich@annexia.org>
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.
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.
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.
18 * $Id: test_shash.c,v 1.2 2002/08/23 09:44:08 rich Exp $
35 const int red = 1, orange = 2, yellow = 3, green = 4;
41 pool pool = new_pool (), pool2, tmp;
45 /* Create a string -> string hash. */
46 h = new_shash (pool, int);
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 ();
57 /* This should replace existing values. */
58 if (shash_insert (h, "apple", red) == 0) abort ();
59 if (shash_insert (h, "peach", yellow) == 0) abort ();
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 ();
67 h = copy_shash (pool, h);
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 ();
73 /* Get list of keys and values. */
74 keys = shash_keys (h);
75 values = shash_values (h);
77 printf ("keys = [ %s ]\n", pjoin (pool, keys, ", "));
78 printf ("values = [ %s ]\n", pjoin (pool, pvitostr (pool, values), ", "));
80 /* Regression test: get list of keys into another temporary pool. */
82 keys = shash_keys_in_pool (h, tmp);
83 printf ("keys = [ %s ]\n", pjoin (pool, keys, ", "));
86 /* Copy the shash into another pool, delete the old pool, check all
87 * keys and values have been copied across.
90 h = copy_shash (pool2, h);
93 keys = shash_keys (h);
94 values = shash_values (h);
96 printf ("keys = [ %s ]\n", pjoin (pool2, keys, ", "));
97 printf ("values = [ %s ]\n", pjoin (pool2, pvitostr (pool2, values), ", "));