IDZEBRA  2.2.7
test_strmap.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) 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 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
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  {
32  YAZ_CHECK(sm);
34  }
35  {
36  int v = 1;
37  void *data_buf;
38  size_t data_len;
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  {
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);
72  }
74  }
75 }
76 
77 static void test2(int no_iter)
78 {
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 failed = 0;
98  int i;
99  srand(12);
100  for (i = 0; i < no_iter; i++)
101  {
102  char str[8];
103  int j;
104  int v = i;
105  void *data_buf;
106  size_t data_len;
107 
108  for (j = 0; j < sizeof(str)-1; j++)
109  str[j] = rand() & 255;
110  str[j] = '\0';
111 
112  j = 0;
113  while ((data_buf = zebra_strmap_lookup(sm, str, j, &data_len)))
114  {
115  if (data_len == sizeof v && v == *((int*) data_buf))
116  break;
117  j++;
118  }
119  if (!(data_buf && data_len == sizeof v
120  && v == *((int*) data_buf)))
121  failed++;
122  }
123  if (failed)
124  YAZ_CHECK_EQ(failed, 0);
125  }
127 }
128 
129 int main (int argc, char **argv)
130 {
131  YAZ_CHECK_INIT(argc, argv);
132  YAZ_CHECK_LOG();
133  test1();
134  test2(50000);
135  YAZ_CHECK_TERM;
136 }
137 /*
138  * Local variables:
139  * c-basic-offset: 4
140  * c-file-style: "Stroustrup"
141  * indent-tabs-mode: nil
142  * End:
143  * vim: shiftwidth=4 tabstop=8 expandtab
144  */
145 
static void test2(int no_iter)
Definition: test_strmap.c:77
int main(int argc, char **argv)
Definition: test_strmap.c:129
static void test1(void)
Definition: test_strmap.c:28
int zebra_strmap_remove(zebra_strmap_t st, const char *name)
Definition: strmap.c:118
void zebra_strmap_destroy(zebra_strmap_t st)
Definition: strmap.c:61
void * zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no, size_t *data_len)
Definition: strmap.c:99
const char * zebra_strmap_it_next(zebra_strmap_it it, void **data_buf, size_t *data_len)
Definition: strmap.c:162
zebra_strmap_t zebra_strmap_create(void)
Definition: strmap.c:45
void zebra_strmap_it_destroy(zebra_strmap_it it)
Definition: strmap.c:157
void zebra_strmap_add(zebra_strmap_t st, const char *name, void *data_buf, size_t data_len)
Definition: strmap.c:80
zebra_strmap_it zebra_strmap_it_create(zebra_strmap_t st)
Definition: strmap.c:148