File: testutf8.c

package info (click to toggle)
fcitx 1%3A4.2.9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,800 kB
  • sloc: ansic: 134,151; cpp: 7,280; sh: 2,778; python: 800; xml: 345; makefile: 42
file content (51 lines) | stat: -rw-r--r-- 1,870 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <assert.h>
#include <string.h>
#include "fcitx-utils/utf8.h"
#include "fcitx/fcitx.h"

#define BUF_SIZE 9

int main()
{
    char buf[BUF_SIZE];
    const char string[] =
        "\xe4\xbd\xa0\xe5\xa5\xbd\xe6\xb5"
        "\x8b\xe8\xaf\x95\xe5\xb8\x8c\xe6";
    const char result[] = {
        '\xe4', '\xbd', '\xa0', '\xe5', '\xa5', '\xbd', '\0', '\0', '\0'
    };
    fcitx_utf8_strncpy(buf, string, BUF_SIZE - 1);
    buf[BUF_SIZE - 1] = 0;
    assert(memcmp(buf, result, BUF_SIZE) == 0);
    FCITX_UNUSED(result);

    assert(fcitx_utf8_strnlen(string, 0) == 0);
    assert(fcitx_utf8_strnlen(string, 1) == 0);
    assert(fcitx_utf8_strnlen(string, 2) == 0);
    assert(fcitx_utf8_strnlen(string, 3) == 1);
    assert(fcitx_utf8_strnlen(string, 6) == 2);
    assert(fcitx_utf8_strnlen(string, 8) == 2);
    assert(fcitx_utf8_strnlen(string, 9) == 3);

#define ASCII_PART "ascii_part"
#define ASCII_PART2 "ascii"
#define UTF8_PART "随便测几个例子"
#define UTF8_PART2 "一个有两段"
#define UTF8_PART3 "的"
    assert(strcmp(fcitx_utils_get_ascii_part(UTF8_PART ASCII_PART),
                  ASCII_PART) == 0);
    assert(strcmp(fcitx_utils_get_ascii_part(ASCII_PART),
                  ASCII_PART) == 0);
    assert(strcmp(fcitx_utils_get_ascii_part(""), "") == 0);
    assert(strcmp(fcitx_utils_get_ascii_part(UTF8_PART2 ASCII_PART2 UTF8_PART3),
                  "") == 0);
    assert(strcmp(fcitx_utils_get_ascii_part(UTF8_PART2 ASCII_PART2
                                             UTF8_PART3 ASCII_PART),
                  ASCII_PART) == 0);
    assert(strncmp(fcitx_utils_get_ascii_partn(UTF8_PART2 ASCII_PART2
                                               UTF8_PART3 ASCII_PART,
                                               strlen(UTF8_PART2 ASCII_PART2)),
                   ASCII_PART2, strlen(ASCII_PART2)) == 0);

    return 0;
}