1 /* Test the 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_hash.c,v 1.1 2001/02/08 12:51:31 rich Exp $
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.
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;
56 pool pool = new_pool ();
60 /* Create a int -> int hash. */
61 h = new_hash (pool, int, int);
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 */
71 /* This should replace existing values. */
72 if (hash_insert (h, five, twentyfive) == 0) abort ();
73 if (hash_insert (h, six, thirtysix) == 0) abort ();
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 ();
82 h = copy_hash (pool, h);
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 ();
88 /* Get list of keys and values. */
90 values = hash_values (h);
92 printf ("keys = [ %s ]\n",
93 pjoin (pool, pvitostr (pool, keys), ", "));
94 printf ("values = [ %s ]\n",
95 pjoin (pool, pvitostr (pool, values), ", "));