summaryrefslogtreecommitdiffstats
path: root/tests/strutil.c
blob: 3d065eee6718c8f2f1cdba0db9941dd4a998dac1 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "common/strutil.h"
#include "common/textModifier.h"

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

static void verify(bool expression, const char* expressionStr, int lineNo) {
    if (expression) {
        return;
    }

    fprintf(stderr, FASTFETCH_TEXT_MODIFIER_ERROR "[%d] %s\n" FASTFETCH_TEXT_MODIFIER_RESET, lineNo, expressionStr);
    exit(1);
}

#define VERIFY(expression) verify((expression), #expression, __LINE__)

int main(void) {
    #if FF_ENABLE_WCWIDTH
    {
        uint8_t width = 255;
        uint8_t bytes = ffUtf8CharLenWidth("", 0, &width);
        VERIFY(bytes == 0);
        VERIFY(width == 0);
    }

    {
        uint8_t width = 0;
        uint8_t bytes = ffUtf8CharLenWidth("A", 1, &width);
        VERIFY(bytes == 1);
        VERIFY(width == 1);
    }

    {
        const char* ch = "\xE6\x96\x87"; // 文 U+6587
        uint8_t width = 0;
        uint8_t bytes = ffUtf8CharLenWidth(ch, 3, &width);
        VERIFY(bytes == 3);
        VERIFY(width == 2);
    }

    {
        const char* combining = "\xCC\x81"; // ◌́ U+0301
        uint8_t width = 0;
        uint8_t bytes = ffUtf8CharLenWidth(combining, 2, &width);
        VERIFY(bytes == 2);
        VERIFY(width == 0); // Should be 1 since there's no base character
    }

    {
        uint8_t width = 0;
        uint8_t bytes = ffUtf8CharLenWidth("\xE6\x96\x87", 1, &width); // truncated
        VERIFY(bytes == 1);
        VERIFY(width == 1);
    }

    {
        uint8_t width = 0;
        uint8_t bytes = ffUtf8CharLenWidth("\xE6"
                                           "A",
            2,
            &width); // invalid continuation
        VERIFY(bytes == 1);
        VERIFY(width == 1);
    }

    {
        VERIFY(ffUtf8StrWidth("abc", 3) == 3);
    }

    {
        const char* mixed = "A"
                            "\xE6\x96\x87" // 文 U+6587
                            "B";
        VERIFY(ffUtf8StrWidth(mixed, 5) == 4);
    }

    {
        const char* combining = "A\xCC\x81"; // Á
        VERIFY(ffUtf8StrWidth(combining, 3) == 1);
    }

    {
        VERIFY(ffUtf8StrWidth("\xE6"
                              "A",
                   2) == 2);
    }

    {
        VERIFY(ffUtf8StrWidth("", 0) == 0);
        VERIFY(ffUtf8StrWidth("A\0B", 3) == 1);
    }

    {
        const char* emoji = "\xF0\x9F\x98\x80"; // U+1F600 😀
        uint8_t width = 0;
        uint8_t bytes = ffUtf8CharLenWidth(emoji, 4, &width);
        VERIFY(bytes == 4);
        VERIFY(width == 2);
    }

    puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
    #else
    puts("\033[33mTests skipped because wcwidth support is disabled." FASTFETCH_TEXT_MODIFIER_RESET);
    #endif
    return 0;
}