test-unichar.c revision 9281d385594c4f158958977f1ead1d2270ef1a9e
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"
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz#include "buffer.h"
86763e4c68313df83606e1060fc1795280590677Timo Sirainen#include "unichar.h"
86763e4c68313df83606e1060fc1795280590677Timo Sirainen
86763e4c68313df83606e1060fc1795280590677Timo Sirainenvoid test_unichar(void)
86763e4c68313df83606e1060fc1795280590677Timo Sirainen{
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz static const char overlong_utf8[] = "\xf8\x80\x95\x81\xa1";
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz static const char collate_in[] = "\xc3\xbc \xc2\xb3";
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz static const char collate_exp[] = "U\xcc\x88 3";
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz buffer_t *collate_out;
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 }
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz collate_out = buffer_create_dynamic(default_pool, 32);
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz uni_utf8_to_decomposed_titlecase(collate_in, sizeof(collate_in),
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz collate_out);
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz test_assert(!strcmp(collate_out->data, collate_exp));
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz buffer_free(&collate_out);
9281d385594c4f158958977f1ead1d2270ef1a9eFlorian Zeitz
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}