Add to git.
[c2lib.git] / test_sash.c
1 /* Test the sash (string 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_sash.c,v 1.1 2001/02/08 12:51:32 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 int
36 main ()
37 {
38   sash h;
39   pool pool = new_pool (), pool2;
40   const char *v;
41   vector keys, values;
42
43   /* Create a string -> string hash. */
44   h = new_sash (pool);
45
46   /* Insert some new values. */
47   if (sash_insert (h, "tomato", "red") != 0) abort ();
48   if (sash_insert (h, "orange", "orange") != 0) abort ();
49   if (sash_insert (h, "lemon", "yellow") != 0) abort ();
50   if (sash_insert (h, "lime", "green") != 0) abort ();
51   if (sash_insert (h, "peach", "orange") != 0) abort ();
52   if (sash_insert (h, "apple", "green") != 0) abort ();
53   if (sash_insert (h, "raspberry", "red") != 0) abort ();
54
55   /* This should replace existing values. */
56   if (sash_insert (h, "apple", "red") == 0) abort ();
57   if (sash_insert (h, "peach", "yellow") == 0) abort ();
58
59   /* Retrieve some values. */
60   if (sash_get (h, "apple", v) == 0 || strcmp (v, "red") != 0) abort ();
61   if (sash_get (h, "tomato", v) == 0 || strcmp (v, "red") != 0) abort ();
62   if (sash_get (h, "orange", v) == 0 || strcmp (v, "orange") != 0) abort ();
63
64   /* Copy the sash. */
65   h = copy_sash (pool, h);
66
67   /* Erase a key and check that it no longer exists. */
68   if (sash_erase (h, "apple") == 0) abort ();
69   if (sash_get (h, "apple", v) != 0) abort ();
70
71   /* Get list of keys and values. */
72   keys = sash_keys (h);
73   values = sash_values (h);
74
75   printf ("keys = [ %s ]\n", pjoin (pool, keys, ", "));
76   printf ("values = [ %s ]\n", pjoin (pool, values, ", "));
77
78   /* Copy the sash into another pool, delete the old pool, check all
79    * keys and values have been copied across.
80    */
81   pool2 = new_pool ();
82   h = copy_sash (pool2, h);
83   delete_pool (pool);
84
85   keys = sash_keys (h);
86   values = sash_values (h);
87
88   printf ("keys = [ %s ]\n", pjoin (pool2, keys, ", "));
89   printf ("values = [ %s ]\n", pjoin (pool2, values, ", "));
90
91   delete_pool (pool2);
92   exit (0);
93 }