aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/tests/eina_test_array.c
blob: 0b054eaf3828ed879a7de3e673640764f98082a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* EINA - EFL data type library
 * Copyright (C) 2008 Cedric Bail
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>

#include "eina_suite.h"
#include "Eina.h"

START_TEST(eina_array_simple)
{
   Eina_Array *ea;
   char *tmp;
   Eina_Array_Iterator it;
   unsigned int i;

   eina_init();

   ea = eina_array_new(11);
        fail_if(!ea);

   for (i = 0; i < 201; ++i)
     {
        tmp = malloc(sizeof(char) * 10);
        fail_if(!tmp);
        eina_convert_itoa(i, tmp);

        eina_array_push(ea, tmp);
     }

   fail_if(eina_array_data_get(ea, 10) == NULL);
   fail_if(atoi(eina_array_data_get(ea, 10)) != 10);
   tmp = eina_array_pop(ea);
   fail_if(tmp == NULL);
   fail_if(atoi(tmp) != 200);
   free(tmp);

   EINA_ARRAY_ITER_NEXT(ea, i, tmp, it)
     {
	fail_if((unsigned int)atoi(tmp) != i);
	free(tmp);
     }

   fail_if(i != 200);

   eina_array_clean(ea);
   eina_array_flush(ea);
   eina_array_free(ea);

   eina_shutdown();
}
END_TEST

START_TEST(eina_array_static)
{
   Eina_Array sea;
   char *tmp;
   Eina_Array_Iterator it;
   unsigned int i;

   eina_init();

   eina_array_step_set(&sea, sizeof(sea), 10);

   for (i = 0; i < 200; ++i)
     {
        tmp = malloc(sizeof(char) * 10);
        fail_if(!tmp);
        eina_convert_itoa(i, tmp);

        eina_array_push(&sea, tmp);
     }

   fail_if(eina_array_data_get(&sea, 10) == NULL);
   fail_if(atoi(eina_array_data_get(&sea, 10)) != 10);

   EINA_ARRAY_ITER_NEXT(&sea, i, tmp, it)
     {
	fail_if((unsigned int)atoi(tmp) != i);
        free(tmp);
     }

   fail_if(i != 200);

   eina_array_clean(&sea);
   eina_array_flush(&sea);

   eina_shutdown();
}
END_TEST

Eina_Bool
keep_int(void *data, void *gdata)
{
   int *tmp = data;

   fail_if(gdata);
   fail_if(!tmp);

   if (*tmp == 0)
      return EINA_FALSE;

   return EINA_TRUE;
}

START_TEST(eina_array_remove_stuff)
{
   Eina_Array *ea;
   int *tmp;
   Eina_Array_Iterator it;
   unsigned int i;

   eina_init();

   ea = eina_array_new(64);
        fail_if(!ea);

   for (i = 0; i < 1000; ++i)
     {
        tmp = malloc(sizeof(int));
        fail_if(!tmp);
        *tmp = i;

        eina_array_push(ea, tmp);
     }

   // Remove the first 10 items
   for (i = 0; i < 10; ++i)
     {
        tmp = eina_array_data_get(ea, i);
        fail_if(!tmp);
        *tmp = 0;
     }
   fail_if(eina_array_remove(ea, keep_int, NULL) != EINA_TRUE);

   fail_if(eina_array_count_get(ea) != 990);
   EINA_ARRAY_ITER_NEXT(ea, i, tmp, it)
     fail_if(*tmp == 0);

   // Remove the last items
   for (i = 980; i < 990; ++i)
     {
        tmp = eina_array_data_get(ea, i);
        fail_if(!tmp);
        *tmp = 0;
     }
        eina_array_remove(ea, keep_int, NULL);

   // Remove all items
   fail_if(eina_array_count_get(ea) != 980);
   EINA_ARRAY_ITER_NEXT(ea, i, tmp, it)
     {
        fail_if(*tmp == 0);
        *tmp = 0;
     }

   eina_array_remove(ea, keep_int, NULL);

   fail_if(eina_array_count_get(ea) != 0);

   eina_array_free(ea);

   eina_shutdown();
}
END_TEST

void
eina_test_array(TCase *tc)
{
   tcase_add_test(tc, eina_array_simple);
   tcase_add_test(tc, eina_array_static);
   tcase_add_test(tc, eina_array_remove_stuff);
}