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 <stdio.h>
24 
25 #include <Eina.h>
26 
27 #include "eina_suite.h"
28 
EFL_START_TEST(eina_array_simple)29 EFL_START_TEST(eina_array_simple)
30 {
31    Eina_Array *ea;
32    char *tmp;
33    Eina_Array_Iterator it;
34    unsigned int i;
35 
36    ea = eina_array_new(11);
37         fail_if(!ea);
38 
39    for (i = 0; i < 201; ++i)
40      {
41         tmp = malloc(sizeof(char) * 10);
42         fail_if(!tmp);
43         eina_convert_itoa(i, tmp);
44 
45         eina_array_push(ea, tmp);
46      }
47 
48    fail_if(eina_array_data_get(ea, 10) == NULL);
49    fail_if(atoi(eina_array_data_get(ea, 10)) != 10);
50    tmp = eina_array_pop(ea);
51    fail_if(tmp == NULL);
52    fail_if(atoi(tmp) != 200);
53    free(tmp);
54 
55    EINA_ARRAY_ITER_NEXT(ea, i, tmp, it)
56      {
57 	fail_if((unsigned int)atoi(tmp) != i);
58 	free(tmp);
59      }
60 
61    fail_if(i != 200);
62 
63    eina_array_clean(ea);
64    eina_array_flush(ea);
65    eina_array_free(ea);
66 
67 }
68 EFL_END_TEST
69 
EFL_START_TEST(eina_array_static)70 EFL_START_TEST(eina_array_static)
71 {
72    Eina_Array sea;
73    char *tmp;
74    Eina_Array_Iterator it;
75    unsigned int i;
76 
77    eina_array_step_set(&sea, sizeof(sea), 10);
78 
79    for (i = 0; i < 200; ++i)
80      {
81         tmp = malloc(sizeof(char) * 10);
82         fail_if(!tmp);
83         eina_convert_itoa(i, tmp);
84 
85         eina_array_push(&sea, tmp);
86      }
87 
88    fail_if(eina_array_data_get(&sea, 10) == NULL);
89    fail_if(atoi(eina_array_data_get(&sea, 10)) != 10);
90 
91    EINA_ARRAY_ITER_NEXT(&sea, i, tmp, it)
92      {
93 	fail_if((unsigned int)atoi(tmp) != i);
94         free(tmp);
95      }
96 
97    fail_if(i != 200);
98 
99    eina_array_clean(&sea);
100    eina_array_flush(&sea);
101 
102 }
103 EFL_END_TEST
104 
105 Eina_Bool
keep_int(void * data,void * gdata)106 keep_int(void *data, void *gdata)
107 {
108    int *tmp = data;
109 
110    fail_if(gdata);
111    fail_if(!tmp);
112 
113    if (*tmp == 0)
114       return EINA_FALSE;
115 
116    return EINA_TRUE;
117 }
118 
EFL_START_TEST(eina_array_remove_stuff)119 EFL_START_TEST(eina_array_remove_stuff)
120 {
121    Eina_Array *ea;
122    int *tmp;
123    Eina_Array_Iterator it;
124    unsigned int i;
125 
126    ea = eina_array_new(64);
127         fail_if(!ea);
128 
129    for (i = 0; i < 1000; ++i)
130      {
131         tmp = malloc(sizeof(int));
132         fail_if(!tmp);
133         *tmp = i;
134 
135         eina_array_push(ea, tmp);
136      }
137 
138    // Remove the first 10 items
139    for (i = 0; i < 10; ++i)
140      {
141         tmp = eina_array_data_get(ea, i);
142         fail_if(!tmp);
143         *tmp = 0;
144      }
145    fail_if(eina_array_remove(ea, keep_int, NULL) != EINA_TRUE);
146 
147    fail_if(eina_array_count(ea) != 990);
148    EINA_ARRAY_ITER_NEXT(ea, i, tmp, it)
149      fail_if(*tmp == 0);
150 
151    // Remove the last items
152    for (i = 980; i < 990; ++i)
153      {
154         tmp = eina_array_data_get(ea, i);
155         fail_if(!tmp);
156         *tmp = 0;
157      }
158         eina_array_remove(ea, keep_int, NULL);
159 
160    // Remove all items
161    fail_if(eina_array_count(ea) != 980);
162    EINA_ARRAY_ITER_NEXT(ea, i, tmp, it)
163      {
164         fail_if(*tmp == 0);
165         *tmp = 0;
166      }
167 
168    eina_array_remove(ea, keep_int, NULL);
169 
170    fail_if(eina_array_count(ea) != 0);
171 
172    eina_array_free(ea);
173 
174 }
175 EFL_END_TEST
176 
EFL_START_TEST(eina_array_find_test)177 EFL_START_TEST(eina_array_find_test)
178 {
179    Eina_Array sea;
180    uintptr_t i;
181    unsigned int out = 0;
182 
183    fail_if(eina_array_find(NULL, (void *)1, NULL) != EINA_FALSE);
184 
185    eina_array_step_set(&sea, sizeof(sea), 5);
186 
187    for (i = 1 ; i < 10 ; i++)
188      eina_array_push(&sea, (void *)i);
189 
190    fail_if(eina_array_find(&sea, (void *)15, NULL) != EINA_FALSE);
191 
192    fail_if(eina_array_find(&sea, (void *)5, NULL) != EINA_TRUE);
193    fail_if(eina_array_find(&sea, (void *)6, &out) != EINA_TRUE);
194    fail_if(out != 5);
195 
196    eina_array_data_set(&sea, 7, (void *)99);
197    fail_if(eina_array_find(&sea, (void *)99, &out) != EINA_TRUE);
198    fail_if(out != 7);
199 
200    eina_array_flush(&sea);
201 
202  }
203 
204 EFL_END_TEST
205 void
eina_test_array(TCase * tc)206 eina_test_array(TCase *tc)
207 {
208    tcase_add_test(tc, eina_array_simple);
209    tcase_add_test(tc, eina_array_static);
210    tcase_add_test(tc, eina_array_remove_stuff);
211    tcase_add_test(tc, eina_array_find_test);
212 }
213