aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/tests/eina_test_binbuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/tests/eina_test_binbuf.c')
-rw-r--r--libraries/eina/src/tests/eina_test_binbuf.c235
1 files changed, 235 insertions, 0 deletions
diff --git a/libraries/eina/src/tests/eina_test_binbuf.c b/libraries/eina/src/tests/eina_test_binbuf.c
new file mode 100644
index 0000000..713e078
--- /dev/null
+++ b/libraries/eina/src/tests/eina_test_binbuf.c
@@ -0,0 +1,235 @@
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
27START_TEST(binbuf_simple)
28{
29 Eina_Binbuf *buf;
30 char *txt;
31 const 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}
67END_TEST
68
69START_TEST(binbuf_remove)
70{
71 Eina_Binbuf *buf;
72 const 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}
99END_TEST
100
101START_TEST(binbuf_insert)
102{
103#if 0
104 Eina_Binbuf *buf;
105
106 eina_init();
107
108 buf = eina_binbuf_new();
109 fail_if(!buf);
110
111 eina_binbuf_insert(buf, "abc", 10);
112 fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
113 fail_if(strcmp(eina_binbuf_string_get(buf), "abc"));
114
115 eina_binbuf_insert(buf, "123", 0);
116 fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
117 fail_if(strcmp(eina_binbuf_string_get(buf), "123abc"));
118
119 eina_binbuf_insert(buf, "xyz", eina_binbuf_length_get(buf));
120 fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
121 fail_if(strcmp(eina_binbuf_string_get(buf), "123abcxyz"));
122
123 eina_binbuf_insert(buf, "xyz", 1);
124 fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
125 fail_if(strcmp(eina_binbuf_string_get(buf), "1xyz23abcxyz"));
126
127 eina_binbuf_insert_n(buf, "ABCDEF", 2, 1);
128 fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
129 fail_if(strcmp(eina_binbuf_string_get(buf), "1ABxyz23abcxyz"));
130
131 eina_binbuf_insert_n(buf, "EINA", 2, 3);
132 fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
133 fail_if(strcmp(eina_binbuf_string_get(buf), "1ABEIxyz23abcxyz"));
134
135 eina_binbuf_insert_escaped(buf, "678", 3);
136 fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf));
137 fail_if(strncmp(eina_binbuf_string_get(buf) + 3, "678", 3));
138
139 eina_binbuf_insert_escaped(buf, "089 '\\", 9);
140 fail_if(strlen(eina_binbuf_string_get(
141 buf)) != eina_binbuf_length_get(buf));
142 fail_if(strncmp(eina_binbuf_string_get(buf) + 9,
143 "089\\ \\'\\\\",
144 strlen("089\\ \\'\\\\")));
145 eina_binbuf_reset(buf);
146
147 eina_binbuf_free(buf);
148
149 eina_shutdown();
150#endif
151}
152END_TEST
153
154START_TEST(binbuf_realloc)
155{
156 Eina_Binbuf *buf;
157 char pattern[1024 * 16];
158 unsigned int i;
159 size_t sz;
160
161 for (i = 0; i < sizeof(pattern) - 1; i++)
162 {
163 if (i % 27 == 26)
164 pattern[i] = '\0';
165 else
166 pattern[i] = 'a' + (i % 27);
167 }
168 pattern[i] = '\0';
169
170 eina_init();
171
172 buf = eina_binbuf_new();
173 fail_if(!buf);
174
175 sz = 0;
176
177 eina_binbuf_append_length(buf, pattern, 1);
178 fail_if(eina_binbuf_length_get(buf) != sz + 1);
179 fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 1));
180 sz += 1;
181
182 eina_binbuf_append_length(buf, pattern, 32);
183 fail_if(eina_binbuf_length_get(buf) != sz + 32);
184 fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 32));
185 sz += 32;
186
187 eina_binbuf_append_length(buf, pattern, 64);
188 fail_if(eina_binbuf_length_get(buf) != sz + 64);
189 fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 64));
190 sz += 64;
191
192 eina_binbuf_append_length(buf, pattern, 128);
193 fail_if(eina_binbuf_length_get(buf) != sz + 128);
194 fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 128));
195 sz += 128;
196
197 eina_binbuf_append_length(buf, pattern, 4096);
198 fail_if(eina_binbuf_length_get(buf) != sz + 4096);
199 fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 4096));
200 sz += 4096;
201
202 eina_binbuf_append_length(buf, pattern, sizeof(pattern) - 1);
203 fail_if(eina_binbuf_length_get(buf) != sz + sizeof(pattern) - 1);
204 fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, sizeof(pattern) -
205 1));
206 sz += sizeof(pattern) - 1;
207
208
209 eina_binbuf_remove(buf, 1024, 1024 + 1234);
210 fail_if(eina_binbuf_length_get(buf) != sz - 1234);
211 sz -= 1234;
212
213 eina_binbuf_remove(buf, 0, 0 + 8192);
214 fail_if(eina_binbuf_length_get(buf) != sz - 8192);
215 sz -= 8192;
216
217 eina_binbuf_remove(buf, 0, 0 + 32);
218 fail_if(eina_binbuf_length_get(buf) != sz - 32);
219 sz -= 32;
220
221
222 eina_binbuf_free(buf);
223
224 eina_shutdown();
225}
226END_TEST
227
228void
229eina_test_binbuf(TCase *tc)
230{
231 tcase_add_test(tc, binbuf_simple);
232 tcase_add_test(tc, binbuf_remove);
233 tcase_add_test(tc, binbuf_insert);
234 tcase_add_test(tc, binbuf_realloc);
235}