1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
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.
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.
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/>.
27 #include "eina_suite.h"
31 eina_foreach_check(__UNUSED__ const Eina_Hash *hash,
34 __UNUSED__ void *fdata)
48 START_TEST(eina_hash_simple)
50 Eina_Hash *hash = NULL;
52 int array[] = { 1, 42, 4, 5, 6 };
54 /* As mempool is already initialized and it use hash, we should have 2 init. */
55 fail_if(eina_init() != 2);
57 hash = eina_hash_string_superfast_new(NULL);
58 fail_if(hash == NULL);
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);
66 test = eina_hash_find(hash, "4");
70 test = eina_hash_find(hash, "42");
74 eina_hash_foreach(hash, eina_foreach_check, NULL);
76 test = eina_hash_modify(hash, "5", &array[4]);
80 test = eina_hash_find(hash, "5");
84 fail_if(eina_hash_population(hash) != 5);
86 fail_if(eina_hash_find(hash, "120") != NULL);
88 fail_if(eina_hash_del(hash, "5", NULL) != EINA_TRUE);
89 fail_if(eina_hash_find(hash, "5") != NULL);
91 fail_if(eina_hash_del(hash, NULL, &array[2]) != EINA_TRUE);
92 fail_if(eina_hash_find(hash, "4") != NULL);
94 fail_if(eina_hash_del(hash, NULL, &array[2]) != EINA_FALSE);
96 fail_if(eina_hash_del(hash, "1", NULL) != EINA_TRUE);
97 fail_if(eina_hash_del(hash, "42", NULL) != EINA_TRUE);
101 /* Same comment as eina_init */
102 fail_if(eina_shutdown() != 1);
106 START_TEST(eina_hash_extended)
108 Eina_Hash *hash = NULL;
111 fail_if(eina_init() != 2);
113 hash = eina_hash_string_djb2_new(NULL);
114 fail_if(hash == NULL);
116 fail_if(eina_hash_direct_add(hash, "42", "42") != EINA_TRUE);
118 for (i = 43; i < 3043; ++i)
120 char *tmp = malloc(10);
122 eina_convert_itoa(i, tmp);
123 fail_if(eina_hash_direct_add(hash, tmp, tmp) != EINA_TRUE);
126 fail_if(eina_hash_find(hash, "42") == NULL);
128 eina_hash_free(hash);
130 fail_if(eina_shutdown() != 1);
134 START_TEST(eina_hash_double_item)
136 Eina_Hash *hash = NULL;
140 fail_if(eina_init() != 2);
142 hash = eina_hash_string_superfast_new(NULL);
143 fail_if(hash == NULL);
145 fail_if(eina_hash_add(hash, "7", &i[0]) != EINA_TRUE);
146 fail_if(eina_hash_add(hash, "7", &i[1]) != EINA_TRUE);
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]);
152 eina_hash_free(hash);
154 fail_if(eina_shutdown() != 1);
158 START_TEST(eina_hash_all_int)
161 int64_t j[] = { 4321312301243122, 6, 7, 128 };
162 int i[] = { 42, 6, 7, 0 };
167 fail_if(eina_init() != 2);
169 hash = eina_hash_int32_new(NULL);
170 fail_if(hash == NULL);
172 for (it = 0; it < 4; ++it)
173 fail_if(eina_hash_add(hash, &i[it], &i[it]) != EINA_TRUE);
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]);
179 test = eina_hash_find(hash, &i[3]);
180 fail_if(test != &i[3]);
182 eina_hash_free(hash);
184 hash = eina_hash_int64_new(NULL);
185 fail_if(hash == NULL);
187 for (it = 0; it < 4; ++it)
188 fail_if(eina_hash_add(hash, &j[it], &j[it]) != EINA_TRUE);
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]);
194 eina_hash_free(hash);
196 fail_if(eina_shutdown() != 1);
200 void eina_test_hash(TCase *tc)
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);