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