Kannel: Open Source WAP and SMS gateway  svn-r5335
test_dict.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * test_dict.c - test Dict objects
59  *
60  * Lars Wirzenius
61  * Stipe Tolj
62  */
63 
64 #include "gwlib/gwlib.h"
65 
66 #define HUGE_SIZE 200000
67 
68 int main(void)
69 {
70  Dict *dict1, *dict2;
71  Octstr *key;
72  unsigned long i;
73  List *keys;
74 
75  gwlib_init();
76 
77  debug("",0,"Dict populate phase.");
80  for (i = 1; i <= HUGE_SIZE; i++) {
81  Octstr *okey, *oval;
82  uuid_t id1, id2;
83  char key[UUID_STR_LEN + 1];
84  char val[UUID_STR_LEN + 1];
85  uuid_generate(id1);
86  uuid_generate(id2);
87  uuid_unparse(id1, key);
88  uuid_unparse(id2, val);
89  okey = octstr_create(key);
90  oval = octstr_create(val);
91  dict_put(dict1, okey, oval);
92  dict_put(dict2, oval, okey);
93  }
94 
95  if (dict_key_count(dict1) == HUGE_SIZE)
96  info(0, "ok, got %d entries in dict1.", HUGE_SIZE);
97  else
98  error(0, "key count is %ld, should be %d in dict1.", dict_key_count(dict1), HUGE_SIZE);
99  if (dict_key_count(dict2) == HUGE_SIZE)
100  info(0, "ok, got %d entries in dict2.", HUGE_SIZE);
101  else
102  error(0, "key count is %ld, should be %d in dict2.", dict_key_count(dict2), HUGE_SIZE);
103 
104  debug("",0,"Dict lookup phase.");
105  keys = dict_keys(dict1);
106  while ((key = gwlist_extract_first(keys)) != NULL) {
107  Octstr *oval1, *oval2;
108  if ((oval1 = dict_get(dict1, key)) != NULL) {
109  if ((oval2 = dict_get(dict2, oval1)) != NULL) {
110  if (octstr_compare(oval2, key) != 0) {
111  error(0, "Dict cross-key check inconsistent:");
112  error(0, "dict1: key <%s>, value <%s>", octstr_get_cstr(key), octstr_get_cstr(oval1));
113  error(0, "dict2: key <%s>, value <%s>", octstr_get_cstr(oval1), octstr_get_cstr(oval2));
114  }
115  } else {
116  error(0, "dict2 key %s has NULL value.", octstr_get_cstr(key));
117  }
118  } else {
119  error(0, "dict1 key %s has NULL value.", octstr_get_cstr(key));
120  }
121  octstr_destroy(key);
122  }
123  gwlist_destroy(keys, NULL);
124 
125  dict_destroy(dict1);
126  dict_destroy(dict2);
127 
128  gwlib_shutdown();
129  return 0;
130 }
Dict * dict_create(long size_hint, void(*destroy_value)(void *))
Definition: dict.c:192
void error(int err, const char *fmt,...)
Definition: log.c:648
void info(int err, const char *fmt,...)
Definition: log.c:672
void dict_put(Dict *dict, Octstr *key, void *value)
Definition: dict.c:240
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
void uuid_generate(uuid_t out)
Definition: gw_uuid.c:393
#define HUGE_SIZE
Definition: test_dict.c:66
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void * gwlist_extract_first(List *list)
Definition: list.c:305
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286
Definition: dict.c:116
long dict_key_count(Dict *dict)
Definition: dict.c:335
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
void octstr_destroy_item(void *os)
Definition: octstr.c:336
#define UUID_STR_LEN
Definition: gw_uuid.h:19
void dict_destroy(Dict *dict)
Definition: dict.c:215
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
void gwlib_shutdown(void)
Definition: gwlib.c:94
List * dict_keys(Dict *dict)
Definition: dict.c:347
unsigned char uuid_t[16]
Definition: gw_uuid.h:32
void gwlib_init(void)
Definition: gwlib.c:78
int main(void)
Definition: test_dict.c:68
Definition: list.c:102
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.