test-unichar.c revision 5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294
5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294Timo Sirainen/* Copyright (c) 2007-2013 Dovecot authors, see the included COPYING file */
86763e4c68313df83606e1060fc1795280590677Timo Sirainen
86763e4c68313df83606e1060fc1795280590677Timo Sirainen#include "test-lib.h"
86763e4c68313df83606e1060fc1795280590677Timo Sirainen#include "str.h"
86763e4c68313df83606e1060fc1795280590677Timo Sirainen#include "unichar.h"
86763e4c68313df83606e1060fc1795280590677Timo Sirainen
86763e4c68313df83606e1060fc1795280590677Timo Sirainenvoid test_unichar(void)
86763e4c68313df83606e1060fc1795280590677Timo Sirainen{
86763e4c68313df83606e1060fc1795280590677Timo Sirainen static const char *overlong_utf8 = "\xf8\x80\x95\x81\xa1";
86763e4c68313df83606e1060fc1795280590677Timo Sirainen unichar_t chr, chr2;
86763e4c68313df83606e1060fc1795280590677Timo Sirainen string_t *str = t_str_new(16);
86763e4c68313df83606e1060fc1795280590677Timo Sirainen
86763e4c68313df83606e1060fc1795280590677Timo Sirainen test_begin("unichars");
86763e4c68313df83606e1060fc1795280590677Timo Sirainen for (chr = 0; chr <= 0x10ffff; chr++) {
86763e4c68313df83606e1060fc1795280590677Timo Sirainen str_truncate(str, 0);
86763e4c68313df83606e1060fc1795280590677Timo Sirainen uni_ucs4_to_utf8_c(chr, str);
86763e4c68313df83606e1060fc1795280590677Timo Sirainen test_assert(uni_utf8_str_is_valid(str_c(str)));
86763e4c68313df83606e1060fc1795280590677Timo Sirainen test_assert(uni_utf8_get_char(str_c(str), &chr2) > 0);
86763e4c68313df83606e1060fc1795280590677Timo Sirainen test_assert(chr2 == chr);
86763e4c68313df83606e1060fc1795280590677Timo Sirainen }
86763e4c68313df83606e1060fc1795280590677Timo Sirainen test_assert(!uni_utf8_str_is_valid(overlong_utf8));
86763e4c68313df83606e1060fc1795280590677Timo Sirainen test_assert(uni_utf8_get_char(overlong_utf8, &chr2) < 0);
86763e4c68313df83606e1060fc1795280590677Timo Sirainen test_end();
86763e4c68313df83606e1060fc1795280590677Timo Sirainen}