1 /* EINA - EFL data type library
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.
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.
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/>.
24 #include "eina_suite.h"
27 START_TEST(binbuf_simple)
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 */
37 buf = eina_binbuf_new();
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));
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));
49 txt = eina_binbuf_string_steal(buf);
50 fail_if(memcmp(txt, cbuf, size));
51 fail_if(memcmp(txt + size, cbuf, size));
53 fail_if(eina_binbuf_length_get(buf) != 0);
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));
59 eina_binbuf_reset(buf);
60 fail_if(eina_binbuf_length_get(buf) != 0);
62 eina_binbuf_free(buf);
69 START_TEST(binbuf_remove)
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 */
77 buf = eina_binbuf_new();
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));
95 eina_binbuf_free(buf);
101 START_TEST(binbuf_manage_simple)
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);
112 buf = eina_binbuf_manage_new_length(alloc_buf, size);
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));
122 eina_binbuf_free(buf);
128 START_TEST(binbuf_insert)
135 buf = eina_binbuf_new();
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"));
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"));
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"));
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"));
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"));
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"));
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));
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,
171 strlen("089\\ \\'\\\\")));
172 eina_binbuf_reset(buf);
174 eina_binbuf_free(buf);
181 START_TEST(binbuf_realloc)
184 unsigned char pattern[1024 * 16];
188 for (i = 0; i < sizeof(pattern) - 1; i++)
193 pattern[i] = 'a' + (i % 27);
199 buf = eina_binbuf_new();
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));
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));
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));
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));
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));
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) -
233 sz += sizeof(pattern) - 1;
236 eina_binbuf_remove(buf, 1024, 1024 + 1234);
237 fail_if(eina_binbuf_length_get(buf) != sz - 1234);
240 eina_binbuf_remove(buf, 0, 0 + 8192);
241 fail_if(eina_binbuf_length_get(buf) != sz - 8192);
244 eina_binbuf_remove(buf, 0, 0 + 32);
245 fail_if(eina_binbuf_length_get(buf) != sz - 32);
249 eina_binbuf_free(buf);
256 eina_test_binbuf(TCase *tc)
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);