aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/tests/eina_test_ustr.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/tests/eina_test_ustr.c')
-rw-r--r--libraries/eina/src/tests/eina_test_ustr.c483
1 files changed, 483 insertions, 0 deletions
diff --git a/libraries/eina/src/tests/eina_test_ustr.c b/libraries/eina/src/tests/eina_test_ustr.c
new file mode 100644
index 0000000..eaeba9d
--- /dev/null
+++ b/libraries/eina/src/tests/eina_test_ustr.c
@@ -0,0 +1,483 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2010 Brett Nash
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 <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <time.h>
27
28#include "eina_suite.h"
29#include "Eina.h"
30
31static const Eina_Unicode STR1[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'n', 0};
32static const Eina_Unicode STR2[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'f', 'f', 0};
33static const Eina_Unicode STR3[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'n', 0};
34static const Eina_Unicode STR4[] = {'A', 0};
35static const Eina_Unicode EMPTYSTR[] = {0};
36
37START_TEST(eina_unicode_strcmp_test)
38{
39 eina_init();
40
41 /* 1 & 2 */
42 fail_if(eina_unicode_strcmp(STR1,STR2) == 0);
43 fail_if(eina_unicode_strcmp(STR1,STR2) < 1);
44
45 /* 1 & 3 */
46 fail_if(eina_unicode_strcmp(STR1, STR3) != 0);
47
48 /* 1 & 4 */
49 fail_if(eina_unicode_strcmp(STR1, STR4) == 0);
50 fail_if(eina_unicode_strcmp(STR1, STR4) > 1);
51
52 /* 1 & empty */
53 fail_if(eina_unicode_strcmp(STR1, EMPTYSTR) < 1);
54
55 /* Self tests */
56 fail_if(eina_unicode_strcmp(STR1, STR1) != 0);
57 fail_if(eina_unicode_strcmp(STR2, STR2) != 0);
58 fail_if(eina_unicode_strcmp(STR3, STR3) != 0);
59 fail_if(eina_unicode_strcmp(STR4, STR4) != 0);
60 fail_if(eina_unicode_strcmp(EMPTYSTR, EMPTYSTR) != 0);
61
62 eina_shutdown();
63}
64END_TEST
65
66START_TEST(eina_unicode_strcpy_test)
67{
68 Eina_Unicode buf[10] = { 0 };
69 Eina_Unicode *rv;
70
71 eina_init();
72
73 rv = eina_unicode_strcpy(buf,STR1);
74 fail_if(rv != buf);
75 fail_if(eina_unicode_strcmp(buf,STR1) != 0);
76
77 rv = eina_unicode_strcpy(buf,STR2);
78 fail_if(rv != buf);
79 fail_if(eina_unicode_strcmp(buf,STR2) != 0);
80
81 /* Now a shorter string */
82 rv = eina_unicode_strcpy(buf,STR2);
83 fail_if(rv != buf);
84 fail_if(eina_unicode_strcmp(buf,STR2) != 0);
85
86 /* Really short string */
87 rv = eina_unicode_strcpy(buf,STR4);
88 fail_if(rv != buf);
89 fail_if(eina_unicode_strcmp(buf,STR4) != 0);
90 fail_if(buf[2] != 'n'); /* check old buf is there */
91
92 buf[1] = '7';
93 rv = eina_unicode_strcpy(buf,EMPTYSTR);
94 fail_if(rv != buf);
95 fail_if(buf[0] != 0);
96 fail_if(buf[1] != '7');
97
98 eina_shutdown();
99}
100END_TEST
101
102START_TEST(eina_unicode_strncpy_test)
103{
104 Eina_Unicode buf[10] = { 0 };
105 Eina_Unicode *rv;
106
107 eina_init();
108
109 rv = eina_unicode_strncpy(buf,STR1,9);
110 fail_if(rv != buf);
111 fail_if(eina_unicode_strcmp(buf,STR1) != 0);
112
113 buf[1] = '7';
114 rv = eina_unicode_strncpy(buf,STR1,1);
115 fail_if(rv != buf);
116 fail_if(buf[1] != '7');
117 fail_if(buf[0] != STR1[0]);
118
119 buf[9] = '7';
120 rv = eina_unicode_strncpy(buf, STR4, 10);
121 fail_if(rv != buf);
122 fail_if(eina_unicode_strcmp(buf,STR4) != 0);
123 fail_if(buf[9] != 0);
124
125 buf[0] = '7';
126 rv = eina_unicode_strncpy(buf, STR1, 0);
127 fail_if(buf[0] != '7');
128
129 /* may segfault */
130 buf[0] = '7';
131 rv = eina_unicode_strncpy(buf, NULL, 0);
132 fail_if(buf[0] != '7');
133
134 /* Hopefully won't segfault */
135 rv = eina_unicode_strncpy(NULL, STR1, 0);
136 fail_if(rv != NULL);
137
138 eina_shutdown();
139}
140END_TEST
141
142
143
144START_TEST(eina_ustr_strlen_test)
145{
146
147 eina_init();
148
149 fail_if(eina_unicode_strlen(STR1) != 8);
150 fail_if(eina_unicode_strlen(STR2) != 9);
151 fail_if(eina_unicode_strlen(STR3) != 8);
152 fail_if(eina_unicode_strlen(STR4) != 1);
153 fail_if(eina_unicode_strlen(EMPTYSTR) != 0);
154 /* Eina unicode doesn't take NULL */
155 // fail_if(eina_unicode_strlen(NULL));
156
157 eina_shutdown();
158}
159END_TEST
160
161START_TEST(eina_unicode_strnlen_test)
162{
163 eina_init();
164
165 /* Strlen style tests*/
166 fail_if(eina_unicode_strnlen(STR1,10) != 8);
167 fail_if(eina_unicode_strnlen(STR2,10) != 9);
168 fail_if(eina_unicode_strnlen(STR3,10) != 8);
169 fail_if(eina_unicode_strnlen(STR4,10) != 1);
170 fail_if(eina_unicode_strnlen(EMPTYSTR,10) != 0);
171
172 /* Too short tests */
173 fail_if(eina_unicode_strnlen(STR1,3) != 3);
174 fail_if(eina_unicode_strnlen(STR2,3) != 3);
175 fail_if(eina_unicode_strnlen(STR3,3) != 3);
176 fail_if(eina_unicode_strnlen(EMPTYSTR,1) != 0);
177 fail_if(eina_unicode_strnlen(NULL,0) != 0);
178
179 eina_shutdown();
180}
181END_TEST
182
183START_TEST(eina_unicode_strdup_test)
184{
185 Eina_Unicode *buf;
186
187 eina_init();
188
189 buf = eina_unicode_strdup(STR1);
190 fail_if(!buf);
191 fail_if(eina_unicode_strlen(buf) != eina_unicode_strlen(STR1));
192 fail_if(eina_unicode_strcmp(buf, STR1));
193 free(buf);
194
195 buf = eina_unicode_strdup(EMPTYSTR);
196 fail_if(!buf);
197 fail_if(buf[0] != 0);
198
199 eina_shutdown();
200}
201END_TEST
202
203START_TEST(eina_unicode_strstr_test)
204{
205 Eina_Unicode *buf;
206 Eina_Unicode on[] = { 'O', 'n', 0 };
207
208 eina_init();
209
210 buf = eina_unicode_strstr(STR1,on);
211 fail_if(!buf);
212 fail_if(buf != STR1 + 6);
213 fail_if(eina_unicode_strcmp(buf,on) != 0);
214
215 buf = eina_unicode_strstr(STR2,on);
216 fail_if(buf);
217
218 buf = eina_unicode_strstr(EMPTYSTR, on);
219 fail_if(buf);
220
221 buf = eina_unicode_strstr(STR1, EMPTYSTR);
222 fail_if(!buf);
223 fail_if(buf != STR1);
224
225 eina_shutdown();
226}
227END_TEST
228
229START_TEST(eina_unicode_utf8)
230{
231 int ind;
232 unsigned char ch;
233 eina_init();
234
235 /* Valid utf-8 cases */
236 /* First possible sequence of a certain length */
237 ind = 0;
238 fail_if((eina_unicode_utf8_get_next("\x00", &ind) != 0x00) ||
239 (ind != 0));
240 ind = 0;
241 fail_if((eina_unicode_utf8_get_next("\x01", &ind) != 0x01) ||
242 (ind != 1));
243 ind = 0;
244 fail_if((eina_unicode_utf8_get_next("\xC2\x80", &ind) != 0x80) ||
245 (ind != 2));
246 ind = 0;
247 fail_if((eina_unicode_utf8_get_next("\xE0\xA0\x80", &ind) != 0x800) ||
248 (ind != 3));
249 ind = 0;
250 fail_if((eina_unicode_utf8_get_next("\xF0\x90\x80\x80", &ind) != 0x10000) ||
251 (ind != 4));
252 ind = 0;
253 fail_if((eina_unicode_utf8_get_next("\xF8\x88\x80\x80\x80", &ind) != 0x200000) || (ind != 5));
254 ind = 0;
255 fail_if((eina_unicode_utf8_get_next("\xFC\x84\x80\x80\x80\x80", &ind) != 0x4000000) || (ind != 6));
256
257 /* Last possible sequence of a certain length */
258 ind = 0;
259 fail_if((eina_unicode_utf8_get_next("\x7F", &ind) != 0x7F) ||
260 (ind != 1));
261 ind = 0;
262 fail_if((eina_unicode_utf8_get_next("\xDF\xBF", &ind) != 0x7FF) ||
263 (ind != 2));
264 ind = 0;
265 fail_if((eina_unicode_utf8_get_next("\xEF\xBF\xBF", &ind) != 0xFFFF) ||
266 (ind != 3));
267 ind = 0;
268 fail_if((eina_unicode_utf8_get_next("\xF7\xBF\xBF\xBF", &ind) != 0x1FFFFF) ||
269 (ind != 4));
270 ind = 0;
271 fail_if((eina_unicode_utf8_get_next("\xFB\xBF\xBF\xBF\xBF", &ind) != 0x3FFFFFF) || (ind != 5));
272 ind = 0;
273 fail_if((eina_unicode_utf8_get_next("\xFD\xBF\xBF\xBF\xBF\xBF", &ind) != 0x7FFFFFFF) || (ind != 6));
274
275 /* Other boundary conditions */
276 ind = 0;
277 fail_if((eina_unicode_utf8_get_next("\xED\x9F\xBF", &ind) != 0xD7FF) ||
278 (ind != 3));
279 ind = 0;
280 fail_if((eina_unicode_utf8_get_next("\xEE\x80\x80", &ind) != 0xE000) ||
281 (ind != 3));
282 ind = 0;
283 fail_if((eina_unicode_utf8_get_next("\xEF\xBF\xBD", &ind) != 0xFFFD) ||
284 (ind != 3));
285 ind = 0;
286 fail_if((eina_unicode_utf8_get_next("\xF4\x8F\xBF\xBF", &ind) != 0x10FFFF) ||
287 (ind != 4));
288 ind = 0;
289 fail_if((eina_unicode_utf8_get_next("\xF4\x90\x80\x80", &ind) != 0x110000) ||
290 (ind != 4));
291
292 /* Error cases */
293 /* Standalone continuation bytes */
294 ind = 0;
295 fail_if((eina_unicode_utf8_get_next("\x80", &ind) != 0xDC80) ||
296 (ind != 1));
297 ind = 0;
298 fail_if((eina_unicode_utf8_get_next("\xBF", &ind) != 0xDCBF) ||
299 (ind != 1));
300 ind = 0;
301 fail_if((eina_unicode_utf8_get_next("\x80\xBF", &ind) != 0xDC80) ||
302 (ind != 1));
303 ind = 0;
304 fail_if((eina_unicode_utf8_get_next("\xBF\x80", &ind) != 0xDCBF) ||
305 (ind != 1));
306 /* All possible continuation bytes */
307 for (ch = 0x80 ; ch <= 0xBF ; ch++)
308 {
309 char buf[] = {ch, 0};
310 ind = 0;
311 fail_if((eina_unicode_utf8_get_next(buf, &ind) != (0xDC00 | ch)) ||
312 (ind != 1));
313 }
314
315 /* Isolated starting sequences */
316#define _FIRST_SEQUENCES(start, end) \
317 do \
318 { \
319 int i; \
320 char *buf = alloca(((end - start + 1) * 2) + 1); \
321 for (i = 0, ch = start ; ch <= end ; i++, ch++) \
322 { \
323 buf[i * 2] = ch; \
324 buf[(i * 2) + 1] = ' '; \
325 } \
326 ind = 0; \
327 for (i = 0, ch = start ; ch <= end ; ch++) \
328 { \
329 fail_if((eina_unicode_utf8_get_next(buf, &ind) != (0xDC00 | ch)) || \
330 (ind != ++i)); \
331 fail_if((eina_unicode_utf8_get_next(buf, &ind) != 0x20) || \
332 (ind != ++i)); \
333 } \
334 } \
335 while (0)
336 /* all first bytes of 2-byte sequences separated by spaces. */
337 _FIRST_SEQUENCES(0xC0, 0xDF);
338 /* all first bytes of 3-byte sequences separated by spaces. */
339 _FIRST_SEQUENCES(0xE0, 0xEF);
340 /* all first bytes of 4-byte sequences separated by spaces. */
341 _FIRST_SEQUENCES(0xF0, 0xF7);
342 /* all first bytes of 5-byte sequences separated by spaces. */
343 _FIRST_SEQUENCES(0xF8, 0xFB);
344 /* all first bytes of 6-byte sequences separated by spaces. */
345 _FIRST_SEQUENCES(0xFC, 0xFD);
346
347 /* Incomplete sequences first means the first utf8 char, len means
348 * the correct length */
349#define _INCOMPLETE_SEQUENCES(first, conti, len) \
350 do \
351 { \
352 int i, j; \
353 char *buf = alloca(len + 1); \
354 i = 0; \
355 buf[i++] = first; \
356 for ( ; i < len ; i++) \
357 { \
358 Eina_Unicode val; \
359 for (j = 1 ; j < i ; j++) \
360 { \
361 buf[j] = conti; \
362 } \
363 buf[j] = 0; \
364 ind = 0; \
365 fail_if( \
366 (eina_unicode_utf8_get_next(buf, &ind) != (0xDC00 | first))); \
367 while ((val = eina_unicode_utf8_get_next(buf, &ind))) \
368 { \
369 fail_if(val != (0xDC00 | conti)); \
370 } \
371 fail_if(ind != i); \
372 } \
373 } \
374 while (0)
375
376 /* Sequences with missing continuation */
377 _INCOMPLETE_SEQUENCES(0xC0, 0x81, 2);
378 _INCOMPLETE_SEQUENCES(0xDF, 0xBF, 2);
379 _INCOMPLETE_SEQUENCES(0xE0, 0x81, 3);
380 _INCOMPLETE_SEQUENCES(0xEF, 0xBF, 3);
381 _INCOMPLETE_SEQUENCES(0xF0, 0x81, 4);
382 _INCOMPLETE_SEQUENCES(0xF7, 0xBF, 4);
383 _INCOMPLETE_SEQUENCES(0xF8, 0x81, 5);
384 _INCOMPLETE_SEQUENCES(0xFB, 0xBF, 5);
385 _INCOMPLETE_SEQUENCES(0xFC, 0x81, 6);
386 _INCOMPLETE_SEQUENCES(0xFD, 0xBF, 6);
387
388 /* Impossible bytes */
389 ind = 0;
390 fail_if((eina_unicode_utf8_get_next("\xFE", &ind) != 0xDCFE) ||
391 (ind != 1));
392 ind = 0;
393 fail_if((eina_unicode_utf8_get_next("\xFF", &ind) != 0xDCFF) ||
394 (ind != 1));
395
396 /* Overlong sequences */
397 ind = 0;
398 fail_if((eina_unicode_utf8_get_next("\xC0\xAF", &ind) != 0xDCC0) ||
399 (ind != 1));
400 ind = 0;
401 fail_if((eina_unicode_utf8_get_next("\xE0\x80\xAF", &ind) != 0xDCE0) ||
402 (ind != 1));
403 ind = 0;
404 fail_if((eina_unicode_utf8_get_next("\xF0\x80\x80\xAF", &ind) != 0xDCF0) ||
405 (ind != 1));
406 ind = 0;
407 fail_if((eina_unicode_utf8_get_next("\xF8\x80\x80\x80\xAF", &ind) != 0xDCF8) ||
408 (ind != 1));
409 ind = 0;
410 fail_if((eina_unicode_utf8_get_next("\xFC\x80\x80\x80\x80\xAF", &ind) != 0xDCFC) ||
411 (ind != 1));
412
413 /* Maximum overlong sequences */
414 ind = 0;
415 fail_if((eina_unicode_utf8_get_next("\xC1\xBF", &ind) != 0xDCC1) ||
416 (ind != 1));
417 ind = 0;
418 fail_if((eina_unicode_utf8_get_next("\xE0\x9F\xBF", &ind) != 0xDCE0) ||
419 (ind != 1));
420 ind = 0;
421 fail_if((eina_unicode_utf8_get_next("\xF0\x8F\xBF\xBF", &ind) != 0xDCF0) ||
422 (ind != 1));
423 ind = 0;
424 fail_if((eina_unicode_utf8_get_next("\xF8\x87\xBF\xBF\xBF", &ind) != 0xDCF8) ||
425 (ind != 1));
426 ind = 0;
427 fail_if((eina_unicode_utf8_get_next("\xFC\x83\xBF\xBF\xBF\xBF", &ind) != 0xDCFC) ||
428 (ind != 1));
429 /* Add some more error cases here */
430
431 /* Just to cover prev/len. General utf-8 parsing was covered above */
432 fail_if(eina_unicode_utf8_get_len("\xF4\x90\x80\x80\xF4\x8F\xBF\xBF") != 2);
433 ind = 0;
434 fail_if((eina_unicode_utf8_get_prev("\xED\x9F\xBF", &ind) != 0xD7FF) ||
435 (ind != 0));
436 ind = 3;
437 fail_if((eina_unicode_utf8_get_prev("\xED\x9F\xBF", &ind) != 0x00) ||
438 (ind != 0));
439
440 eina_shutdown();
441}
442END_TEST
443
444START_TEST(eina_unicode_utf8_conversion)
445{
446 Eina_Unicode uni_in[] = {0x5D0, 0xFDF6, 0xDC80, 0x1F459, 0x3FFFFFF,
447 0x7FFFFFFF, 'a', 'b', 'c', 0};
448 Eina_Unicode *uni_out;
449 char c_in[] = "\xD7\x90""\xEF\xB7\xB6""\x80""\xF0\x9F\x91\x99"
450 "\xFB\xBF\xBF\xBF\xBF""\xFD\xBF\xBF\xBF\xBF\xBF""abc";
451 char *c_out;
452 int len;
453
454 eina_init();
455
456 uni_out = eina_unicode_utf8_to_unicode(c_in, &len);
457 fail_if((len != 9) || eina_unicode_strcmp(uni_in, uni_out));
458 free(uni_out);
459
460 c_out = eina_unicode_unicode_to_utf8(uni_in, &len);
461 fail_if((len != 24) || strcmp(c_in, c_out));
462 free(c_out);
463
464 eina_shutdown();
465}
466END_TEST
467
468void
469eina_test_ustr(TCase *tc)
470{
471 printf("ustr test\n");
472 tcase_add_test(tc,eina_unicode_strcmp_test);
473 tcase_add_test(tc,eina_unicode_strcpy_test);
474 tcase_add_test(tc,eina_unicode_strncpy_test);
475 tcase_add_test(tc,eina_ustr_strlen_test);
476 tcase_add_test(tc,eina_unicode_strnlen_test);
477 tcase_add_test(tc,eina_unicode_strdup_test);
478 tcase_add_test(tc,eina_unicode_strstr_test);
479 tcase_add_test(tc,eina_unicode_utf8);
480 tcase_add_test(tc,eina_unicode_utf8_conversion);
481
482}
483