File: test_towfc_s.c

package info (click to toggle)
safeclib 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: ansic: 52,639; makefile: 1,271; perl: 528; sh: 518
file content (233 lines) | stat: -rw-r--r-- 8,065 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*------------------------------------------------------------------
 * test_towfc_s.c
 * File 'towfc_s.c'
 * Lines executed:93.75% of 176
 *
 * Full case-folding regarding latest Unicode (15.0) CaseFolding.txt
 * Some F characters fold to multiples.
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_str_lib.h"
#include <stdlib.h>
#include <unistd.h>

#define CFOLD "CaseFolding.txt"

#if SIZEOF_WCHAR_T > 2
#define MAX_LEN 4
#define _dec_w16(src) *(src)
#else
#define MAX_LEN 8
EXTERN uint32_t _dec_w16(wchar_t *src);
#endif
int test_towfc_s(void);

int test_towfc_s(void) {
    int rc;
    int ind;
    int errs = 0;
    int c;
    wchar_t result[MAX_LEN];
    char s[128];
    char code[8];
    char status[2];
    char mapping[24];
    char name[80];
    FILE *f;

    uint32_t wc;

    /*--------------------------------------------------*/

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty dest")
    rc = towfc_s(NULL, MAX_LEN, 1);
    NEGERR(ESNULLP);

    EXPECT_BOS("dmax underflow <4")
    rc = towfc_s(result, 3, 1);
    NEGERR(ESLEMIN);

    EXPECT_BOS("dest overflow")
    rc = towfc_s(result, RSIZE_MAX_WSTR + 1, 1);
    NEGERR(ESLEMAX);

    if (_BOS_KNOWN(result)) {
        EXPECT_BOS("dest overflow")
        rc = towfc_s(result, MAX_LEN + 1, 1);
        NEGERR(EOVERFLOW);
    }
#endif

    /*--------------------------------------------------*/

    f = fopen(CFOLD, "r");
    if (!f) {
        char url[256];
        printf("downloading %s ...", CFOLD);
        fflush(stdout);
        snprintf(url, 255,
                 "wget https://www.unicode.org/Public/%d.0.0/ucd/%s",
                 SAFECLIB_UNICODE_VERSION, CFOLD);
        if (system(url))
            printf(" done\n");
        else {
            printf(" failed\n");
            return 0;
        }
        f = fopen(CFOLD, "r");
        if (!f)
            return 0;
    }
    while (!feof(f)) {
        int l;
        char *p = fgets(s, sizeof(s), f);
        char *p1;
        if (p && *p && s[0] != '#' && s[0] != '\n') {
            p = strstr(s, "; ");
            l = p - s;
            memcpy(code, s, l);
            code[l] = 0;
            *status = p[2];
            status[1] = 0;
            p1 = strstr(&p[5], "; ");
            l = p1 - p - 5;
            memcpy(mapping, &p[5], l);
            mapping[l] = 0;
            strcpy(name, &p1[4]);
            if (strlen(name))
                name[strlen(name) - 1] = 0;

            c = sscanf(code, "%X", &wc);
            if (c) {
                uint32_t m0;
                int n, len;
                uint32_t cp;

                result[0] = L'\0';
                n = iswfc(wc);

                rc = towfc_s(result, MAX_LEN, wc);
                len = wcslen(result);
                cp = _dec_w16(result);
                if (cp > 0x20000) /* XXX */
                    cp -= 0x10000;
                if (rc < 0) {
                    ERR(ESNOTFND);
                    if (n) {
                        printf("%s %u  Error %d U+%04X n=%d 0x%x\n",
                               __FUNCTION__, __LINE__, rc, wc, n, (unsigned)cp);
                        errs++;
                    }
                }
#if SIZEOF_WCHAR_T > 2
                else if (rc != n && n) { /* n may be 0, but fc 1 */
                    printf("%s %u  Error %d U+%04X n=%d 0x%x\n", __FUNCTION__,
                           __LINE__, rc, wc, n, (int)cp);
                    errs++;
                }
#endif
                c = sscanf(mapping, "%X", &m0);

                /* we have 104 unhandled F multi-char mappings,
                   handle them via towfc() (wide full-lowercase) */
                if (*status == 'F') { /* lower is bigger than upper, ignored */
                    uint32_t m1, m2;
                    /* check the length, must be or 3 */
                    if (n < 2) {
                        errs++;
                        printf("%s %u  Error: iswfc(U+%04X) => %d (towfc=>%d) "
                               "\"%s\" %s\n",
                               __FUNCTION__, __LINE__, wc, n, len, mapping,
                               name);
                    } else {
                        static wchar_t cmp[MAX_LEN];
                        if (n != len) {
                            errs++;
                            printf("%s %u  Error: towfc(U+%04X) => %d "
                                   "(iswfc=>%d) \"%s\" %s\n",
                                   __FUNCTION__, __LINE__, wc, len, n, mapping,
                                   name);
                        }
                        /* also compare the content */
                        if (n == 2) {
                            c = sscanf(mapping, "%4X %4X", &m0, &m1);
                            cmp[2] = 0;
                        } else if (n == 3) {
                            c = sscanf(mapping, "%4X %4X %4X", &m0, &m1, &m2);
                            cmp[2] = (wchar_t)m2;
                            cmp[3] = 0;
                        } else {
                            errs++;
                            printf("%s %u  Error: Wrong n=%d\n", __FUNCTION__,
                                   __LINE__, n);
                            goto next_line;
                        }
                        cmp[0] = (wchar_t)m0;
                        cmp[1] = (wchar_t)m1;
                        ind = wcscmp(result, cmp);
                        INDZERO();
                        if (ind) {
                            printf(
                                "%s %u  Error: towfc(U+%04X) => %X... \"%s\"\n",
                                __FUNCTION__, __LINE__, wc, cp, mapping);
                            if (n == 3)
                                printf("    { 0x%04X, 0x%04X,0x%04X,0x%04X "
                                       "},\t/* %s */\n",
                                       wc, m0, m1, m2, name);
                            else
                                printf("    { 0x%04X, 0x%04X,0x%04X },\t/* %s "
                                       "*/\n",
                                       wc, m0, m1, name);
                        }
                    }
                } else if (*status == 'T') { /* Turkish special case */
                    if (wc != 0x130 && wc != 0x49) {
                        printf("%s %u  Error: Unknown turkish wc=%d\n",
                               __FUNCTION__, __LINE__, wc);
                        errs++;
                    } else {
                        ind = n;
                        /* this is turkish insensitive, only wcsfc_s checks the
                         * locale */
                        if (wc == 0x130) {
                            INDCMP(!= 2);
                            if (_BOS_KNOWN(result)) {
                              WEXPSTR(result, L"i̇");
                            }
                        } else {
                            INDCMP(!= 1);
                            WEXPSTR(result, L"i");
                        }
                    }
                } else if (*status == 'S') { /* ignore as we handle the other F case */
                    ;
                } else { /* the simple 1:1 C case */
#if SIZEOF_WCHAR_T > 2
                    if (n > 1)
#else
                    if (n > 2)
#endif
                    {
                        errs++;
                        printf("%s %u  Error: iswfc(U+%04X) => %d (towfc=>%d) "
                               "\"%s\" status=%s %s\n", __FUNCTION__, __LINE__, wc,
                               n, len, mapping, status, name);
                    } else if (cp != m0) {
                        errs++;
                        printf("%s %u  Error: towfc(U+%04X) => %X != %X \"%s\" "
                               "status=%s %s\n", __FUNCTION__, __LINE__, wc, cp, m0,
                               mapping, status, name);
                    }
                }
            }
        }
    next_line:;
    }
    fclose(f);
    return (errs);
}

int main(void) { return (test_towfc_s()); }