Added simple string map (hash) utility.
[idzebra-moved-to-github.git] / util / test_strmap.c
1 /* $Id: test_strmap.c,v 1.1 2007-12-02 11:30:28 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <zebra_strmap.h>
24 #include <yaz/test.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 static void test1(void)
29 {
30     {
31         zebra_strmap_t sm = zebra_strmap_create();
32         YAZ_CHECK(sm);
33         zebra_strmap_destroy(sm);
34     }
35     {
36         int v1 = 1;
37         int *data_buf;
38         size_t data_len;
39         zebra_strmap_t sm = zebra_strmap_create();
40         YAZ_CHECK(!zebra_strmap_lookup(sm, "a", 0, 0));
41         
42         zebra_strmap_add(sm, "a", &v1, sizeof v1);
43         data_buf = zebra_strmap_lookup(sm, "a", 0, &data_len);
44         YAZ_CHECK(data_buf && data_len == sizeof v1 
45                   && v1 == *((int*) data_buf));
46
47         zebra_strmap_remove(sm, "a");
48         data_buf = zebra_strmap_lookup(sm, "a", 0, &data_len);
49         YAZ_CHECK(data_buf == 0);
50         zebra_strmap_destroy(sm);
51     }
52 }
53
54 static void test2(int no_iter)
55 {
56     zebra_strmap_t sm = zebra_strmap_create();
57     {
58         int i;
59         srand(12);
60         for (i = 0; i < no_iter; i++)
61         {
62             char str[8];
63             int j;
64             int v = i;
65
66             for (j = 0; j < sizeof(str)-1; j++)
67                 str[j] = rand() & 255;
68             str[j] = '\0';
69
70             zebra_strmap_add(sm, str, &v, sizeof v);
71         }
72     }
73     {
74         int i;
75         srand(12);
76         for (i = 0; i < no_iter; i++)
77         {
78             char str[8];
79             int j;
80             int v = i;
81             void *data_buf;
82             size_t data_len;
83
84             for (j = 0; j < sizeof(str)-1; j++)
85                 str[j] = rand() & 255;
86             str[j] = '\0';
87
88             j = 0;
89             while ((data_buf = zebra_strmap_lookup(sm, str, j, &data_len)))
90             {
91                 if (data_len == sizeof v && v == *((int*) data_buf))
92                     break;
93                 j++;
94             }
95             YAZ_CHECK(data_buf && data_len == sizeof v
96                       && v == *((int*) data_buf));
97         }
98     }
99     zebra_strmap_destroy(sm);
100 }
101
102 int main (int argc, char **argv)
103 {
104     YAZ_CHECK_INIT(argc, argv);
105     YAZ_CHECK_LOG();
106     test1();
107     test2(50000);
108     YAZ_CHECK_TERM;
109 }
110 /*
111  * Local variables:
112  * c-basic-offset: 4
113  * indent-tabs-mode: nil
114  * End:
115  * vim: shiftwidth=4 tabstop=8 expandtab
116  */
117