EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / tests / eina_test_hash.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "eina_suite.h"
28 #include "Eina.h"
29
30 static Eina_Bool
31 eina_foreach_check(__UNUSED__ const Eina_Hash *hash,
32                    const void *key,
33                    void *data,
34                    __UNUSED__ void *fdata)
35 {
36    int *j = data;
37    int i;
38
39    if (strlen(key) <= 0)
40       return EINA_TRUE;
41
42    i = atoi(key);
43    fail_if(i != *j);
44
45    return EINA_TRUE;
46 }
47
48 START_TEST(eina_hash_simple)
49 {
50    Eina_Hash *hash = NULL;
51    int *test;
52    int array[] = { 1, 42, 4, 5, 6 };
53
54    /* As mempool is already initialized and it use hash, we should have 2 init. */
55    fail_if(eina_init() != 2);
56
57    hash = eina_hash_string_superfast_new(NULL);
58    fail_if(hash == NULL);
59
60    fail_if(eina_hash_add(hash, "1", &array[0]) != EINA_TRUE);
61    fail_if(eina_hash_add(hash, "42", &array[1]) != EINA_TRUE);
62    fail_if(eina_hash_direct_add(hash, "4", &array[2]) != EINA_TRUE);
63    fail_if(eina_hash_direct_add(hash, "5", &array[3]) != EINA_TRUE);
64    fail_if(eina_hash_add(hash, "", "") != EINA_TRUE);
65
66    test = eina_hash_find(hash, "4");
67    fail_if(!test);
68    fail_if(*test != 4);
69
70    test = eina_hash_find(hash, "42");
71    fail_if(!test);
72    fail_if(*test != 42);
73
74    eina_hash_foreach(hash, eina_foreach_check, NULL);
75
76    test = eina_hash_modify(hash, "5", &array[4]);
77    fail_if(!test);
78    fail_if(*test != 5);
79
80    test = eina_hash_find(hash, "5");
81    fail_if(!test);
82    fail_if(*test != 6);
83
84    fail_if(eina_hash_population(hash) != 5);
85
86    fail_if(eina_hash_find(hash, "120") != NULL);
87
88    fail_if(eina_hash_del(hash, "5", NULL) != EINA_TRUE);
89    fail_if(eina_hash_find(hash, "5") != NULL);
90
91    fail_if(eina_hash_del(hash, NULL, &array[2]) != EINA_TRUE);
92    fail_if(eina_hash_find(hash, "4") != NULL);
93
94    fail_if(eina_hash_del(hash, NULL, &array[2]) != EINA_FALSE);
95
96    fail_if(eina_hash_del(hash, "1", NULL) != EINA_TRUE);
97    fail_if(eina_hash_del(hash, "42", NULL) != EINA_TRUE);
98
99    eina_hash_free(hash);
100
101    /* Same comment as eina_init */
102         fail_if(eina_shutdown() != 1);
103 }
104 END_TEST
105
106 START_TEST(eina_hash_extended)
107 {
108    Eina_Hash *hash = NULL;
109    int i;
110
111         fail_if(eina_init() != 2);
112
113    hash = eina_hash_string_djb2_new(NULL);
114         fail_if(hash == NULL);
115
116         fail_if(eina_hash_direct_add(hash, "42", "42") != EINA_TRUE);
117
118    for (i = 43; i < 3043; ++i)
119      {
120         char *tmp = malloc(10);
121         fail_if(!tmp);
122         eina_convert_itoa(i, tmp);
123         fail_if(eina_hash_direct_add(hash, tmp, tmp) != EINA_TRUE);
124      }
125
126         fail_if(eina_hash_find(hash, "42") == NULL);
127
128         eina_hash_free(hash);
129
130    fail_if(eina_shutdown() != 1);
131 }
132 END_TEST
133
134 START_TEST(eina_hash_double_item)
135 {
136    Eina_Hash *hash = NULL;
137    int i[] = { 7, 7 };
138    int *test;
139
140    fail_if(eina_init() != 2);
141
142    hash = eina_hash_string_superfast_new(NULL);
143    fail_if(hash == NULL);
144
145    fail_if(eina_hash_add(hash, "7", &i[0]) != EINA_TRUE);
146    fail_if(eina_hash_add(hash, "7", &i[1]) != EINA_TRUE);
147
148    fail_if(eina_hash_del(hash, "7", &i[1]) != EINA_TRUE);
149    test = eina_hash_find(hash, "7");
150    fail_if(test != &i[0]);
151
152    eina_hash_free(hash);
153
154       fail_if(eina_shutdown() != 1);
155 }
156 END_TEST
157
158 START_TEST(eina_hash_all_int)
159 {
160    Eina_Hash *hash;
161    int64_t j[] = { 4321312301243122, 6, 7, 128 };
162    int i[] = { 42, 6, 7, 0 };
163    int64_t *test2;
164    int *test;
165    int it;
166
167       fail_if(eina_init() != 2);
168
169    hash = eina_hash_int32_new(NULL);
170       fail_if(hash == NULL);
171
172    for (it = 0; it < 4; ++it)
173       fail_if(eina_hash_add(hash, &i[it], &i[it]) != EINA_TRUE);
174
175       fail_if(eina_hash_del(hash, &i[1], &i[1]) != EINA_TRUE);
176    test = eina_hash_find(hash, &i[2]);
177       fail_if(test != &i[2]);
178
179    test = eina_hash_find(hash, &i[3]);
180       fail_if(test != &i[3]);
181
182       eina_hash_free(hash);
183
184    hash = eina_hash_int64_new(NULL);
185       fail_if(hash == NULL);
186
187    for (it = 0; it < 4; ++it)
188       fail_if(eina_hash_add(hash, &j[it], &j[it]) != EINA_TRUE);
189
190       fail_if(eina_hash_del(hash, &j[1], &j[1]) != EINA_TRUE);
191    test2 = eina_hash_find(hash, &j[0]);
192       fail_if(test2 != &j[0]);
193
194       eina_hash_free(hash);
195
196    fail_if(eina_shutdown() != 1);
197 }
198 END_TEST
199
200 void eina_test_hash(TCase *tc)
201 {
202    tcase_add_test(tc, eina_hash_simple);
203    tcase_add_test(tc, eina_hash_extended);
204    tcase_add_test(tc, eina_hash_double_item);
205    tcase_add_test(tc, eina_hash_all_int);
206 }