Added zebra_strmap_it(erator).
[idzebra-moved-to-github.git] / util / test_strmap.c
1 /* $Id: test_strmap.c,v 1.2 2007-12-03 09:12:38 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 v = 1;
37         void *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", &v, sizeof v);
43         data_buf = zebra_strmap_lookup(sm, "a", 0, &data_len);
44         YAZ_CHECK(data_buf && data_len == sizeof v 
45                   && v == *((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
51         v = 1;
52         zebra_strmap_add(sm, "a", &v, sizeof v);
53
54         v = 2;
55         zebra_strmap_add(sm, "b", &v, sizeof v);
56
57         v = 3;
58         zebra_strmap_add(sm, "c", &v, sizeof v);
59
60         {
61             zebra_strmap_it it = zebra_strmap_it_create(sm);
62             const char *name;
63             int no = 0;
64             while ((name = zebra_strmap_it_next(it, &data_buf, &data_len)))
65             {
66                 YAZ_CHECK(!strcmp(name, "a") || !strcmp(name, "b") ||
67                           !strcmp(name, "c"));
68                 no++;
69             }
70             YAZ_CHECK_EQ(no, 3);
71             zebra_strmap_it_destroy(it);
72         }
73         zebra_strmap_destroy(sm);
74     }
75 }
76
77 static void test2(int no_iter)
78 {
79     zebra_strmap_t sm = zebra_strmap_create();
80     {
81         int i;
82         srand(12);
83         for (i = 0; i < no_iter; i++)
84         {
85             char str[8];
86             int j;
87             int v = i;
88
89             for (j = 0; j < sizeof(str)-1; j++)
90                 str[j] = rand() & 255;
91             str[j] = '\0';
92
93             zebra_strmap_add(sm, str, &v, sizeof v);
94         }
95     }
96     {
97         int i;
98         srand(12);
99         for (i = 0; i < no_iter; i++)
100         {
101             char str[8];
102             int j;
103             int v = i;
104             void *data_buf;
105             size_t data_len;
106
107             for (j = 0; j < sizeof(str)-1; j++)
108                 str[j] = rand() & 255;
109             str[j] = '\0';
110
111             j = 0;
112             while ((data_buf = zebra_strmap_lookup(sm, str, j, &data_len)))
113             {
114                 if (data_len == sizeof v && v == *((int*) data_buf))
115                     break;
116                 j++;
117             }
118             YAZ_CHECK(data_buf && data_len == sizeof v
119                       && v == *((int*) data_buf));
120         }
121     }
122     zebra_strmap_destroy(sm);
123 }
124
125 int main (int argc, char **argv)
126 {
127     YAZ_CHECK_INIT(argc, argv);
128     YAZ_CHECK_LOG();
129     test1();
130     test2(50000);
131     YAZ_CHECK_TERM;
132 }
133 /*
134  * Local variables:
135  * c-basic-offset: 4
136  * indent-tabs-mode: nil
137  * End:
138  * vim: shiftwidth=4 tabstop=8 expandtab
139  */
140