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