test-unichar.c revision 814bf67459ad405a157af0b8940602024d7fadfe
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2015 Dovecot authors, see the included COPYING file */
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen static const char input[] = "\xC3\xA4\xC3\xA4\0a";
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_strlen_n(input, 1) == 0);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_strlen_n(input, 2) == 1);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_strlen_n(input, 3) == 1);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_strlen_n(input, 4) == 2);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainenstatic void test_unichar_uni_utf8_partial_strlen_n(void)
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen static const char input[] = "\xC3\xA4\xC3\xA4\0a";
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_partial_strlen_n(input, 1, &pos) == 0 && pos == 0);
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen test_assert(uni_utf8_partial_strlen_n(input, 2, &pos) == 1 && pos == 2);
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen test_assert(uni_utf8_partial_strlen_n(input, 3, &pos) == 1 && pos == 2);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_partial_strlen_n(input, 4, &pos) == 2 && pos == 4);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_partial_strlen_n(input, 5, &pos) == 3 && pos == 5);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_partial_strlen_n(input, 6, &pos) == 4 && pos == 6);
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen static const char overlong_utf8[] = "\xf8\x80\x95\x81\xa1";
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen static const char collate_in[] = "\xc3\xbc \xc2\xb3";
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen static const char collate_exp[] = "U\xcc\x88 3";
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen /* The bottom 6 bits should be irrelevant to code coverage,
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen only test 000000, 111111, and something in between. */
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen chr += rand() % 62; /* After 0, somewhere between 1 and 62 */
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_str_is_valid(str_c(str)));
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_get_char(str_c(str), &chr2) == (int)uni_utf8_char_bytes(*str_data(str)));
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen unsigned int utf8len = uni_utf8_char_bytes(*str_c(str));
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen /* virtually truncate the byte string */
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen while (--utf8len > 0)
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_get_char_n(str_c(str), utf8len, &chr2) == 0);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen /* actually truncate the byte stream */
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen while (--utf8len > 0) {
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(!uni_utf8_str_is_valid(str_c(str)));
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(uni_utf8_get_char(str_c(str), &chr2) == 0);
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen collate_out = buffer_create_dynamic(default_pool, 32);
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen uni_utf8_to_decomposed_titlecase(collate_in, sizeof(collate_in),
7ca63fa4166f89fee900b7c14d87d53fbac47242Timo Sirainen test_assert(!strcmp(collate_out->data, collate_exp));
00e7c3010f7da4a49881a7feb05e413af353af0aTimo Sirainen test_assert(!uni_utf8_str_is_valid(overlong_utf8));