1 /* EINA - EFL data type library
2 * Copyright (C) 2010 Brett Nash
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.
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.
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/>.
28 #include "eina_suite.h"
31 #ifdef EINA_SAFETY_CHECKS
38 /* tests should not output on success, just uncomment this for debugging */
42 _eina_test_safety_print_cb(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args __UNUSED__)
44 struct log_ctx *ctx = data;
48 va_copy(cp_args, args);
49 str = va_arg(cp_args, const char *);
52 ck_assert_int_eq(level, EINA_LOG_LEVEL_ERR);
53 ck_assert_str_eq(fmt, "%s");
54 ck_assert_str_eq(ctx->msg, str);
55 ck_assert_str_eq(ctx->fnc, fnc);
59 eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
68 static const Eina_Unicode STR1[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'n', 0};
69 static const Eina_Unicode STR2[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'f', 'f', 0};
70 static const Eina_Unicode STR3[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'n', 0};
71 static const Eina_Unicode STR4[] = {'A', 0};
72 static const Eina_Unicode EMPTYSTR[] = {0};
74 START_TEST(eina_unicode_strcmp_test)
79 fail_if(eina_unicode_strcmp(STR1,STR2) == 0);
80 fail_if(eina_unicode_strcmp(STR1,STR2) < 1);
83 fail_if(eina_unicode_strcmp(STR1, STR3) != 0);
86 fail_if(eina_unicode_strcmp(STR1, STR4) == 0);
87 fail_if(eina_unicode_strcmp(STR1, STR4) > 1);
90 fail_if(eina_unicode_strcmp(STR1, EMPTYSTR) < 1);
93 fail_if(eina_unicode_strcmp(STR1, STR1) != 0);
94 fail_if(eina_unicode_strcmp(STR2, STR2) != 0);
95 fail_if(eina_unicode_strcmp(STR3, STR3) != 0);
96 fail_if(eina_unicode_strcmp(STR4, STR4) != 0);
97 fail_if(eina_unicode_strcmp(EMPTYSTR, EMPTYSTR) != 0);
103 START_TEST(eina_unicode_strcpy_test)
105 Eina_Unicode buf[10] = { 0 };
110 rv = eina_unicode_strcpy(buf,STR1);
112 fail_if(eina_unicode_strcmp(buf,STR1) != 0);
114 rv = eina_unicode_strcpy(buf,STR2);
116 fail_if(eina_unicode_strcmp(buf,STR2) != 0);
118 /* Now a shorter string */
119 rv = eina_unicode_strcpy(buf,STR2);
121 fail_if(eina_unicode_strcmp(buf,STR2) != 0);
123 /* Really short string */
124 rv = eina_unicode_strcpy(buf,STR4);
126 fail_if(eina_unicode_strcmp(buf,STR4) != 0);
127 fail_if(buf[2] != 'n'); /* check old buf is there */
130 rv = eina_unicode_strcpy(buf,EMPTYSTR);
132 fail_if(buf[0] != 0);
133 fail_if(buf[1] != '7');
139 START_TEST(eina_unicode_strncpy_test)
141 Eina_Unicode buf[10] = { 0 };
146 rv = eina_unicode_strncpy(buf,STR1,9);
148 fail_if(eina_unicode_strcmp(buf,STR1) != 0);
151 rv = eina_unicode_strncpy(buf,STR1,1);
153 fail_if(buf[1] != '7');
154 fail_if(buf[0] != STR1[0]);
157 rv = eina_unicode_strncpy(buf, STR4, 10);
159 fail_if(eina_unicode_strcmp(buf,STR4) != 0);
160 fail_if(buf[9] != 0);
163 rv = eina_unicode_strncpy(buf, STR1, 0);
164 fail_if(buf[0] != '7');
166 #ifdef EINA_SAFETY_CHECKS
170 #define TEST_MAGIC_SAFETY(fn, _msg) \
175 eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
180 fprintf(stderr, "you should have a safety check failure below:\n");
182 TEST_MAGIC_SAFETY("eina_unicode_strncpy",
183 "safety check failed: source == NULL");
184 rv = eina_unicode_strncpy(buf, NULL, 0);
185 fail_if(buf[0] != '7');
186 fail_unless(ctx.did);
188 /* Hopefully won't segfault */
190 fprintf(stderr, "you should have a safety check failure below:\n");
192 TEST_MAGIC_SAFETY("eina_unicode_strncpy",
193 "safety check failed: dest == NULL");
194 rv = eina_unicode_strncpy(NULL, STR1, 0);
196 fail_unless(ctx.did);
198 eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
199 #undef TEST_MAGIC_SAFETY
209 START_TEST(eina_ustr_strlen_test)
214 fail_if(eina_unicode_strlen(STR1) != 8);
215 fail_if(eina_unicode_strlen(STR2) != 9);
216 fail_if(eina_unicode_strlen(STR3) != 8);
217 fail_if(eina_unicode_strlen(STR4) != 1);
218 fail_if(eina_unicode_strlen(EMPTYSTR) != 0);
220 #ifdef EINA_SAFETY_CHECKS
224 #define TEST_MAGIC_SAFETY(fn, _msg) \
229 eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
232 fprintf(stderr, "you should have a safety check failure below:\n");
234 TEST_MAGIC_SAFETY("eina_unicode_strlen",
235 "safety check failed: ustr == NULL");
236 fail_if(eina_unicode_strlen(NULL));
237 fail_unless(ctx.did);
239 eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
240 #undef TEST_MAGIC_SAFETY
248 START_TEST(eina_unicode_strnlen_test)
252 /* Strlen style tests*/
253 fail_if(eina_unicode_strnlen(STR1,10) != 8);
254 fail_if(eina_unicode_strnlen(STR2,10) != 9);
255 fail_if(eina_unicode_strnlen(STR3,10) != 8);
256 fail_if(eina_unicode_strnlen(STR4,10) != 1);
257 fail_if(eina_unicode_strnlen(EMPTYSTR,10) != 0);
259 /* Too short tests */
260 fail_if(eina_unicode_strnlen(STR1,3) != 3);
261 fail_if(eina_unicode_strnlen(STR2,3) != 3);
262 fail_if(eina_unicode_strnlen(STR3,3) != 3);
263 fail_if(eina_unicode_strnlen(EMPTYSTR,1) != 0);
265 #ifdef EINA_SAFETY_CHECKS
269 #define TEST_MAGIC_SAFETY(fn, _msg) \
274 eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
277 fprintf(stderr, "you should have a safety check failure below:\n");
279 TEST_MAGIC_SAFETY("eina_unicode_strnlen",
280 "safety check failed: ustr == NULL");
281 fail_if(eina_unicode_strnlen(NULL,0) != 0);
282 fail_unless(ctx.did);
284 eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
285 #undef TEST_MAGIC_SAFETY
293 START_TEST(eina_unicode_strdup_test)
299 buf = eina_unicode_strdup(STR1);
301 fail_if(eina_unicode_strlen(buf) != eina_unicode_strlen(STR1));
302 fail_if(eina_unicode_strcmp(buf, STR1));
305 buf = eina_unicode_strdup(EMPTYSTR);
307 fail_if(buf[0] != 0);
313 START_TEST(eina_unicode_strstr_test)
316 Eina_Unicode on[] = { 'O', 'n', 0 };
320 buf = eina_unicode_strstr(STR1,on);
322 fail_if(buf != STR1 + 6);
323 fail_if(eina_unicode_strcmp(buf,on) != 0);
325 buf = eina_unicode_strstr(STR2,on);
328 buf = eina_unicode_strstr(EMPTYSTR, on);
331 buf = eina_unicode_strstr(STR1, EMPTYSTR);
333 fail_if(buf != STR1);
339 START_TEST(eina_unicode_utf8)
345 /* Valid utf-8 cases */
346 /* First possible sequence of a certain length */
348 fail_if((eina_unicode_utf8_get_next("\x00", &ind) != 0x00) ||
351 fail_if((eina_unicode_utf8_get_next("\x01", &ind) != 0x01) ||
354 fail_if((eina_unicode_utf8_get_next("\xC2\x80", &ind) != 0x80) ||
357 fail_if((eina_unicode_utf8_get_next("\xE0\xA0\x80", &ind) != 0x800) ||
360 fail_if((eina_unicode_utf8_get_next("\xF0\x90\x80\x80", &ind) != 0x10000) ||
363 fail_if((eina_unicode_utf8_get_next("\xF8\x88\x80\x80\x80", &ind) != 0x200000) || (ind != 5));
365 fail_if((eina_unicode_utf8_get_next("\xFC\x84\x80\x80\x80\x80", &ind) != 0x4000000) || (ind != 6));
367 /* Last possible sequence of a certain length */
369 fail_if((eina_unicode_utf8_get_next("\x7F", &ind) != 0x7F) ||
372 fail_if((eina_unicode_utf8_get_next("\xDF\xBF", &ind) != 0x7FF) ||
375 fail_if((eina_unicode_utf8_get_next("\xEF\xBF\xBF", &ind) != 0xFFFF) ||
378 fail_if((eina_unicode_utf8_get_next("\xF7\xBF\xBF\xBF", &ind) != 0x1FFFFF) ||
381 fail_if((eina_unicode_utf8_get_next("\xFB\xBF\xBF\xBF\xBF", &ind) != 0x3FFFFFF) || (ind != 5));
383 fail_if((eina_unicode_utf8_get_next("\xFD\xBF\xBF\xBF\xBF\xBF", &ind) != 0x7FFFFFFF) || (ind != 6));
385 /* Other boundary conditions */
387 fail_if((eina_unicode_utf8_get_next("\xED\x9F\xBF", &ind) != 0xD7FF) ||
390 fail_if((eina_unicode_utf8_get_next("\xEE\x80\x80", &ind) != 0xE000) ||
393 fail_if((eina_unicode_utf8_get_next("\xEF\xBF\xBD", &ind) != 0xFFFD) ||
396 fail_if((eina_unicode_utf8_get_next("\xF4\x8F\xBF\xBF", &ind) != 0x10FFFF) ||
399 fail_if((eina_unicode_utf8_get_next("\xF4\x90\x80\x80", &ind) != 0x110000) ||
403 /* Standalone continuation bytes */
405 fail_if((eina_unicode_utf8_get_next("\x80", &ind) != 0xDC80) ||
408 fail_if((eina_unicode_utf8_get_next("\xBF", &ind) != 0xDCBF) ||
411 fail_if((eina_unicode_utf8_get_next("\x80\xBF", &ind) != 0xDC80) ||
414 fail_if((eina_unicode_utf8_get_next("\xBF\x80", &ind) != 0xDCBF) ||
416 /* All possible continuation bytes */
417 for (ch = 0x80 ; ch <= 0xBF ; ch++)
419 char buf[] = {ch, 0};
421 fail_if((eina_unicode_utf8_get_next(buf, &ind) != (0xDC00 | ch)) ||
425 /* Isolated starting sequences */
426 #define _FIRST_SEQUENCES(start, end) \
430 char *buf = alloca(((end - start + 1) * 2) + 1); \
431 for (i = 0, ch = start ; ch <= end ; i++, ch++) \
434 buf[(i * 2) + 1] = ' '; \
437 for (i = 0, ch = start ; ch <= end ; ch++) \
439 fail_if((eina_unicode_utf8_get_next(buf, &ind) != (0xDC00 | ch)) || \
441 fail_if((eina_unicode_utf8_get_next(buf, &ind) != 0x20) || \
446 /* all first bytes of 2-byte sequences separated by spaces. */
447 _FIRST_SEQUENCES(0xC0, 0xDF);
448 /* all first bytes of 3-byte sequences separated by spaces. */
449 _FIRST_SEQUENCES(0xE0, 0xEF);
450 /* all first bytes of 4-byte sequences separated by spaces. */
451 _FIRST_SEQUENCES(0xF0, 0xF7);
452 /* all first bytes of 5-byte sequences separated by spaces. */
453 _FIRST_SEQUENCES(0xF8, 0xFB);
454 /* all first bytes of 6-byte sequences separated by spaces. */
455 _FIRST_SEQUENCES(0xFC, 0xFD);
457 /* Incomplete sequences first means the first utf8 char, len means
458 * the correct length */
459 #define _INCOMPLETE_SEQUENCES(first, conti, len) \
463 char *buf = alloca(len + 1); \
466 for ( ; i < len ; i++) \
469 for (j = 1 ; j < i ; j++) \
476 (eina_unicode_utf8_get_next(buf, &ind) != (0xDC00 | first))); \
477 while ((val = eina_unicode_utf8_get_next(buf, &ind))) \
479 fail_if(val != (0xDC00 | conti)); \
486 /* Sequences with missing continuation */
487 _INCOMPLETE_SEQUENCES(0xC0, 0x81, 2);
488 _INCOMPLETE_SEQUENCES(0xDF, 0xBF, 2);
489 _INCOMPLETE_SEQUENCES(0xE0, 0x81, 3);
490 _INCOMPLETE_SEQUENCES(0xEF, 0xBF, 3);
491 _INCOMPLETE_SEQUENCES(0xF0, 0x81, 4);
492 _INCOMPLETE_SEQUENCES(0xF7, 0xBF, 4);
493 _INCOMPLETE_SEQUENCES(0xF8, 0x81, 5);
494 _INCOMPLETE_SEQUENCES(0xFB, 0xBF, 5);
495 _INCOMPLETE_SEQUENCES(0xFC, 0x81, 6);
496 _INCOMPLETE_SEQUENCES(0xFD, 0xBF, 6);
498 /* Impossible bytes */
500 fail_if((eina_unicode_utf8_get_next("\xFE", &ind) != 0xDCFE) ||
503 fail_if((eina_unicode_utf8_get_next("\xFF", &ind) != 0xDCFF) ||
506 /* Overlong sequences */
508 fail_if((eina_unicode_utf8_get_next("\xC0\xAF", &ind) != 0xDCC0) ||
511 fail_if((eina_unicode_utf8_get_next("\xE0\x80\xAF", &ind) != 0xDCE0) ||
514 fail_if((eina_unicode_utf8_get_next("\xF0\x80\x80\xAF", &ind) != 0xDCF0) ||
517 fail_if((eina_unicode_utf8_get_next("\xF8\x80\x80\x80\xAF", &ind) != 0xDCF8) ||
520 fail_if((eina_unicode_utf8_get_next("\xFC\x80\x80\x80\x80\xAF", &ind) != 0xDCFC) ||
523 /* Maximum overlong sequences */
525 fail_if((eina_unicode_utf8_get_next("\xC1\xBF", &ind) != 0xDCC1) ||
528 fail_if((eina_unicode_utf8_get_next("\xE0\x9F\xBF", &ind) != 0xDCE0) ||
531 fail_if((eina_unicode_utf8_get_next("\xF0\x8F\xBF\xBF", &ind) != 0xDCF0) ||
534 fail_if((eina_unicode_utf8_get_next("\xF8\x87\xBF\xBF\xBF", &ind) != 0xDCF8) ||
537 fail_if((eina_unicode_utf8_get_next("\xFC\x83\xBF\xBF\xBF\xBF", &ind) != 0xDCFC) ||
539 /* Add some more error cases here */
541 /* Just to cover prev/len. General utf-8 parsing was covered above */
542 fail_if(eina_unicode_utf8_get_len("\xF4\x90\x80\x80\xF4\x8F\xBF\xBF") != 2);
544 fail_if((eina_unicode_utf8_get_prev("\xED\x9F\xBF", &ind) != 0xD7FF) ||
547 fail_if((eina_unicode_utf8_get_prev("\xED\x9F\xBF", &ind) != 0x00) ||
554 START_TEST(eina_unicode_utf8_conversion)
556 Eina_Unicode uni_in[] = {0x5D0, 0xFDF6, 0xDC80, 0x1F459, 0x3FFFFFF,
557 0x7FFFFFFF, 'a', 'b', 'c', 0};
558 Eina_Unicode *uni_out;
559 char c_in[] = "\xD7\x90""\xEF\xB7\xB6""\x80""\xF0\x9F\x91\x99"
560 "\xFB\xBF\xBF\xBF\xBF""\xFD\xBF\xBF\xBF\xBF\xBF""abc";
566 uni_out = eina_unicode_utf8_to_unicode(c_in, &len);
567 fail_if((len != 9) || eina_unicode_strcmp(uni_in, uni_out));
570 c_out = eina_unicode_unicode_to_utf8(uni_in, &len);
571 fail_if((len != 24) || strcmp(c_in, c_out));
579 eina_test_ustr(TCase *tc)
581 printf("ustr test\n");
582 tcase_add_test(tc,eina_unicode_strcmp_test);
583 tcase_add_test(tc,eina_unicode_strcpy_test);
584 tcase_add_test(tc,eina_unicode_strncpy_test);
585 tcase_add_test(tc,eina_ustr_strlen_test);
586 tcase_add_test(tc,eina_unicode_strnlen_test);
587 tcase_add_test(tc,eina_unicode_strdup_test);
588 tcase_add_test(tc,eina_unicode_strstr_test);
589 tcase_add_test(tc,eina_unicode_utf8);
590 tcase_add_test(tc,eina_unicode_utf8_conversion);