EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / tests / eina_test_binbuf.c
1 /* EINA - EFL data type library
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library;
15  * if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include <stdio.h>
23
24 #include "eina_suite.h"
25 #include "Eina.h"
26
27 START_TEST(binbuf_simple)
28 {
29    Eina_Binbuf *buf;
30    unsigned char *txt;
31    const unsigned char cbuf[] = "Null in the middle \0 and more text afterwards and \0 anotehr null just there and another one \0 here.";
32    size_t size = sizeof(cbuf) - 1; /* We don't care about the real NULL */
33
34
35    eina_init();
36
37    buf = eina_binbuf_new();
38    fail_if(!buf);
39
40    eina_binbuf_append_length(buf, cbuf, size);
41    fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size));
42    fail_if(size != eina_binbuf_length_get(buf));
43
44    eina_binbuf_append_length(buf, cbuf, size);
45    fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size));
46    fail_if(memcmp(eina_binbuf_string_get(buf) + size, cbuf, size));
47    fail_if(2 * size != eina_binbuf_length_get(buf));
48
49    txt = eina_binbuf_string_steal(buf);
50    fail_if(memcmp(txt, cbuf, size));
51    fail_if(memcmp(txt + size, cbuf, size));
52    free(txt);
53    fail_if(eina_binbuf_length_get(buf) != 0);
54
55    eina_binbuf_append_length(buf, cbuf, size);
56    fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size));
57    fail_if(size != eina_binbuf_length_get(buf));
58
59    eina_binbuf_reset(buf);
60    fail_if(eina_binbuf_length_get(buf) != 0);
61
62    eina_binbuf_free(buf);
63
64    eina_shutdown();
65 #undef TEXT
66 }
67 END_TEST
68
69 START_TEST(binbuf_remove)
70 {
71    Eina_Binbuf *buf;
72    const unsigned char cbuf[] = "12\0 456 78\0 abcthis is some more random junk here!";
73    size_t size = sizeof(cbuf) - 1; /* We don't care about the real NULL */
74
75    eina_init();
76
77    buf = eina_binbuf_new();
78    fail_if(!buf);
79
80    eina_binbuf_append_length(buf, cbuf, size);
81    fail_if(size != eina_binbuf_length_get(buf));
82    eina_binbuf_remove(buf, 0, 4);
83    fail_if(size - 4 != eina_binbuf_length_get(buf));
84    eina_binbuf_remove(buf, 8, 1000);
85    fail_if(8 != eina_binbuf_length_get(buf));
86    eina_binbuf_remove(buf, 7, eina_binbuf_length_get(buf));
87    fail_if(7 != eina_binbuf_length_get(buf));
88    eina_binbuf_remove(buf, 2, 4);
89    fail_if(5 != eina_binbuf_length_get(buf));
90    eina_binbuf_remove(buf, 4, 1);
91    fail_if(5 != eina_binbuf_length_get(buf));
92    eina_binbuf_remove(buf, 0, eina_binbuf_length_get(buf));
93    fail_if(0 != eina_binbuf_length_get(buf));
94
95    eina_binbuf_free(buf);
96
97    eina_shutdown();
98 }
99 END_TEST
100
101 START_TEST(binbuf_manage_simple)
102 {
103    Eina_Binbuf *buf;
104    const char *_cbuf = "12\0 456 78\0 abcthis is some more random junk here!";
105    const unsigned char *cbuf = (const unsigned char *) _cbuf;
106    size_t size = sizeof(cbuf) - 1; /* We don't care about the real NULL */
107    unsigned char *alloc_buf = malloc(size);
108    memcpy(alloc_buf, cbuf, size);
109
110    eina_init();
111
112    buf = eina_binbuf_manage_new_length(alloc_buf, size);
113    fail_if(!buf);
114
115    fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size));
116    fail_if(size != eina_binbuf_length_get(buf));
117    eina_binbuf_append_length(buf, cbuf, size);
118    fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size));
119    fail_if(memcmp(eina_binbuf_string_get(buf) + size, cbuf, size));
120    fail_if(2 * size != eina_binbuf_length_get(buf));
121
122    eina_binbuf_free(buf);
123
124    eina_shutdown();
125 }
126 END_TEST
127
128 START_TEST(binbuf_insert)
129 {
130 #if 0
131    Eina_Binbuf *buf;
132
133    eina_init();
134
135    buf = eina_binbuf_new();
136    fail_if(!buf);
137
138    eina_binbuf_insert(buf, "abc", 10);
139    fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
140    fail_if(strcmp(eina_binbuf_string_get(buf), "abc"));
141
142    eina_binbuf_insert(buf, "123", 0);
143    fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
144    fail_if(strcmp(eina_binbuf_string_get(buf), "123abc"));
145
146    eina_binbuf_insert(buf, "xyz", eina_binbuf_length_get(buf));
147    fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
148    fail_if(strcmp(eina_binbuf_string_get(buf), "123abcxyz"));
149
150    eina_binbuf_insert(buf, "xyz", 1);
151    fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
152    fail_if(strcmp(eina_binbuf_string_get(buf), "1xyz23abcxyz"));
153
154    eina_binbuf_insert_n(buf, "ABCDEF", 2, 1);
155    fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
156    fail_if(strcmp(eina_binbuf_string_get(buf), "1ABxyz23abcxyz"));
157
158    eina_binbuf_insert_n(buf, "EINA", 2, 3);
159    fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
160    fail_if(strcmp(eina_binbuf_string_get(buf), "1ABEIxyz23abcxyz"));
161
162    eina_binbuf_insert_escaped(buf, "678", 3);
163    fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
164    fail_if(strncmp(eina_binbuf_string_get(buf) + 3, "678", 3));
165
166    eina_binbuf_insert_escaped(buf, "089 '\\", 9);
167                    fail_if(strlen(eina_binbuf_string_get(
168                      buf)) != eina_binbuf_length_get(buf));
169                    fail_if(strncmp(eina_binbuf_string_get(buf) + 9,
170                    "089\\ \\'\\\\",
171                    strlen("089\\ \\'\\\\")));
172    eina_binbuf_reset(buf);
173
174    eina_binbuf_free(buf);
175
176    eina_shutdown();
177 #endif
178 }
179 END_TEST
180
181 START_TEST(binbuf_realloc)
182 {
183    Eina_Binbuf *buf;
184    unsigned char pattern[1024 * 16];
185    unsigned int i;
186    size_t sz;
187
188    for (i = 0; i < sizeof(pattern) - 1; i++)
189      {
190         if (i % 27 == 26)
191            pattern[i] = '\0';
192         else
193            pattern[i] = 'a' + (i % 27);
194      }
195    pattern[i] = '\0';
196
197    eina_init();
198
199    buf = eina_binbuf_new();
200    fail_if(!buf);
201
202    sz = 0;
203
204    eina_binbuf_append_length(buf, pattern, 1);
205    fail_if(eina_binbuf_length_get(buf) != sz + 1);
206    fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 1));
207    sz += 1;
208
209    eina_binbuf_append_length(buf, pattern, 32);
210    fail_if(eina_binbuf_length_get(buf) != sz + 32);
211    fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 32));
212    sz += 32;
213
214    eina_binbuf_append_length(buf, pattern, 64);
215    fail_if(eina_binbuf_length_get(buf) != sz + 64);
216    fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 64));
217    sz += 64;
218
219    eina_binbuf_append_length(buf, pattern, 128);
220    fail_if(eina_binbuf_length_get(buf) != sz + 128);
221    fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 128));
222    sz += 128;
223
224    eina_binbuf_append_length(buf, pattern, 4096);
225    fail_if(eina_binbuf_length_get(buf) != sz + 4096);
226    fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 4096));
227    sz += 4096;
228
229    eina_binbuf_append_length(buf, pattern, sizeof(pattern) - 1);
230    fail_if(eina_binbuf_length_get(buf) != sz + sizeof(pattern) - 1);
231    fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, sizeof(pattern) -
232                   1));
233    sz += sizeof(pattern) - 1;
234
235
236    eina_binbuf_remove(buf, 1024, 1024 + 1234);
237    fail_if(eina_binbuf_length_get(buf) != sz - 1234);
238    sz -= 1234;
239
240    eina_binbuf_remove(buf, 0, 0 + 8192);
241    fail_if(eina_binbuf_length_get(buf) != sz - 8192);
242    sz -= 8192;
243
244    eina_binbuf_remove(buf, 0, 0 + 32);
245    fail_if(eina_binbuf_length_get(buf) != sz - 32);
246    sz -= 32;
247
248
249    eina_binbuf_free(buf);
250
251    eina_shutdown();
252 }
253 END_TEST
254
255 void
256 eina_test_binbuf(TCase *tc)
257 {
258    tcase_add_test(tc, binbuf_simple);
259    tcase_add_test(tc, binbuf_remove);
260    tcase_add_test(tc, binbuf_insert);
261    tcase_add_test(tc, binbuf_realloc);
262    tcase_add_test(tc, binbuf_manage_simple);
263 }