File: test_wcsfc_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 (541 lines) | stat: -rw-r--r-- 15,585 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*------------------------------------------------------------------
 * test_wcsfc_s
 * File 'wcsfc_s.c'
 * Lines executed:53.24% of 139
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_str_lib.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <locale.h>

#define LEN (128)
int test_wcsfc_s(void);

#define PERL_TEST
/* Must have the same Unicode version 9.0, at least 5.26.
   Better 5.27.3 with Unicode 10, 5.30 with 12.1, 5.32, 5.36 with 14.0,
   5.38 - 5.40 with 15.0
   perl -MUnicode::UCD -e'print Unicode::UCD::UnicodeVersion()'
*/
#define TESTPL "test-fc.pl"

int test_wcsfc_s(void) {
    errno_t rc;
    wchar_t str[LEN];
    wchar_t str1[LEN];
    size_t len;
    rsize_t ind;
    int wc;
    int errs = 0;
    char *lc_cat, *lang;
#ifdef PERL_TEST
    FILE *pl;
    struct stat st;

    pl = fopen(TESTPL, "w");
#endif

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

    wcscpy(str, L"untouched");
#ifndef HAVE_CT_BOS_OVR
    ind = 4;
    EXPECT_BOS("empty dest")
    rc = wcsfc_s(NULL, LEN, L"test", &ind);
    ERR(ESNULLP);
    INDCMP(!= 0)

    ind = 4;
    EXPECT_BOS("empty dest or dmax")
    rc = wcsfc_s(str, 0, L"test", &ind);
    ERR(ESZEROL)
    WEXPSTR(str, L"untouched")
    INDCMP(!= 0)

    ind = 4;
    EXPECT_BOS("dest overflow")
    rc = wcsfc_s(str, RSIZE_MAX_WSTR + 1, L"test", &ind);
    ERR(ESLEMAX)
    WEXPSTR(str, L"untouched")
    INDCMP(!= 0)

    if (_BOS_KNOWN(str)) {
        EXPECT_BOS("dest overflow")
        rc = wcsfc_s(str, LEN + 1, L"test", &ind);
        ERR(EOVERFLOW)
        WEXPSTR(str, L"") /* cleared */
    }

    ind = 4;
    EXPECT_BOS("empty src")
    rc = wcsfc_s(str, LEN, NULL, &ind);
    ERR(ESNULLP);
    INDCMP(!= 0)
    WEXPSTR(str, L"") /* cleared */

#endif

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

    rc = wcsfc_s(str, LEN, (wchar_t *)L"Name", &ind);
    ERR(EOK);
    WEXPSTR(str, L"name");
    INDCMP(!= 4);
    WCHECK_SLACK(&str[4], LEN - 4);

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

    rc = wcsfc_s(str, LEN, (wchar_t *)L"name", &ind);
    ERR(EOK)
    WEXPSTR(str, L"name");
    INDCMP(!= 4);
    WCHECK_SLACK(&str[4], LEN - 4);

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

    rc = wcsfc_s(str, LEN, (wchar_t *)L"NOWISTHETIM3", NULL);
    ERR(EOK)
    WEXPSTR(str, L"nowisthetim3");
    len = wcslen(L"nowisthetim3");
    WCHECK_SLACK(&str[len], LEN - len);

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

    /* towfc_s() */
    rc = wcsfc_s(str, LEN,
                 (wchar_t *)L"A"
                            L"\x1fb3",
                 &ind); /* casefold ᾳ => 2 */
    ERR(EOK);
    wcscpy(str1, L"a"
                 L"\x3b1"
                 L"\x3b9"); /* but decomp: 1fb3 => 3b1 345 */
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[3], LEN - 3);

    rc = wcsfc_s(str, LEN,
                 (wchar_t *)L"A"
                            L"\x1fb7",
                 &ind); /* casefold ᾷ => 3 */
    ERR(EOK);
    wcscpy(str1, L"a"
                 L"\x3b1"
                 L"\x342"
                 L"\x3b9");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[4], LEN - 4);

    rc =
        wcsfc_s(str, LEN, (wchar_t *)L"\x100", &ind); /* fc=>101, nfd=>61 304 */
    ERR(EOK);
    wcscpy(str1, L"\x61"
                 L"\x304");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, (wchar_t *)L"\x101", &ind); /* fc=101, nfd only */
    ERR(EOK);
    wcscpy(str1, L"\x61"
                 L"\x304"); /* A w/ MACRON */
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, (wchar_t *)L"\x115", &ind); /* fc=115, nfd=65 306 */
    ERR(EOK);
    wcscpy(str1, L"\x65"
                 L"\x306");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, (wchar_t *)L"\xdf", &ind); /* fc=73 73, !nfd */
    ERR(EOK);
    wcscpy(str1, L"\x73"
                 L"\x73");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, (wchar_t *)L"\x385", &ind); /* fc=385, nfd=a8 301 */
    ERR(EOK);
    wcscpy(str1, L"\xa8"
                 L"\x301");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, (wchar_t *)L"\x386", &ind); /* fc=3ac, nfd=3b1 301 */
    ERR(EOK);
    wcscpy(str1, L"\x3b1"
                 L"\x301");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, (wchar_t *)L"\x388",
                 &ind); /* c=0, fc=3ad, nfd=3b5 301 */
    ERR(EOK);
    wcscpy(str1, L"\x3b5"
                 L"\x301"); /* 3ad: 249|TBL(2) */
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, (wchar_t *)L"\x1f71", &ind); /* !fc, nfd=3b1 301 */
    ERR(EOK);
    wcscpy(str1, L"\x3b1"
                 L"\x301");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc =
        wcsfc_s(str, LEN, (wchar_t *)L"\x1f82", &ind); /*  fc=1f02 389 => l=4 */
    ERR(EOK);
    wcscpy(str1, L"\x3b1"
                 L"\x313"
                 L"\x300"
                 L"\x3b9");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[4], LEN - 4);

    rc = wcsfc_s(str, LEN, L"\x1feb", &ind); /*  fc,=17eb nfd=3c5 301 */
    ERR(EOK);
    wcscpy(str1, L"\x3c5"
                 L"\x301");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, L"\x1fee", &ind); /*  !fc, nfd=a8 301 */
    ERR(EOK);
    wcscpy(str1, L"\xa8"
                 L"\x301");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

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

    /* reorder? Aᾷ  fc A=>a 345=>3b9! See [GH #25] */
    /*
      FC        \x{41}\x{3B1}\x{345}\x{342} = \x{61}\x{3B1}\x{3B9}\x{342}
      NFD FC                           Aᾷ  = \x{61}\x{3B1}\x{3B9}\x{342}
      NFD       \x{41}\x{3B1}\x{345}\x{342} = \x{41}\x{3B1}\x{342}\x{345}
      FC NFD                           Aᾷ  = \x{61}\x{3B1}\x{342}\x{3B9} */

    rc = wcsfc_s(str, LEN,
                 L"A"
                 L"\x3b1"
                 L"\x345"
                 L"\x342",
                 &ind);
    ERR(EOK);
    wcscpy(str1, L"a"
                 L"\x3b1"
                 L"\x3b9"
                 L"\x342"); /* no reordering of 3b9 */
    WEXPSTR(str, str1);

    /*rc = wcsnorm_reorder_s(str, LEN, L"a" L"\x3b1" L"\x3b9" L"\x342", 4);
    ERR(EOK);
    wcscpy(str1, L"a" L"\x3b1" L"\x3b9" L"\x342");
    WEXPSTR(str, str1);*/

    rc = wcsfc_s(str, LEN, L"\x1d5", &ind); /* NFD=U\x308\x304 */
    ERR(EOK);
    wcscpy(str1, L"u"
                 L"\x308"
                 L"\x304"); /* no reordering */
    WEXPSTR(str, str1);

    /* no ordering defined, both CC 230 */
    /*rc = wcsnorm_reorder_s(str, LEN, L"U" L"\x304" L"\x308", 3);
    wcscpy(str1, L"U" L"\x304" L"\x308");
    WEXPSTR(str, str1);*/

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

    /* wcsfc_s() locale-independent special rules */
    rc = wcsfc_s(str, LEN,
                 L"\x3a3"
                 L" ",
                 &ind); /* final sigma */
    ERR(EOK);
    wcscpy(str1, L"\x3c2"
                 L" ");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN,
                 L"\x3a3"
                 L"s",
                 &ind); /* non-final sigma */
    ERR(EOK);
    wcscpy(str1, L"\x3c3"
                 L"s");
    WEXPSTR(str, str1);
    WCHECK_SLACK(&str[2], LEN - 2);

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

    /* wcsfc_s() locale-dependent special rules */
    SETLOCALE_C;
    SETLANG("C");
    CHKLOCALE_C;

    rc = wcsfc_s(str, LEN, L"\x130", &ind); /* I-dot İ */
    ERR(EOK);
    WEXPSTR(str, L"i\x307"); /* COMBINING DOT ABOVE */
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, L"I\x307", &ind);
    ERR(EOK);
    WEXPSTR(str, L"i\x307");
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, L"I", &ind);
    ERR(EOK);
    WEXPSTR(str, L"i");
    WCHECK_SLACK(&str[1], LEN - 1);

    rc = wcsfc_s(str, LEN, L"\xc0",
                 &ind); /* accented a. fc => \xe0, nfd => a\x300 */
    ERR(EOK);
    WEXPSTR(str, L"a\x300");
    WCHECK_SLACK(&str[2], LEN - 2);

    rc = wcsfc_s(str, LEN, L"\xcc",
                 &ind); /* accented I/J fc => \xec, nfd => i\x300 */
    ERR(EOK);
    WEXPSTR(str, L"i\x300");
    WCHECK_SLACK(&str[2], LEN - 2);

    SETLOCALE("tr_TR");
    SETLANG("tr");
    CHKLOCALE("tr") {
        /* pair I-Dot with i for Turkish and Azeri. */
        rc = wcsfc_s(str, LEN, L"\x130", &ind); /* I-dot İ */
        ERR(EOK);
        WEXPSTR(str, L"i"); /* i alone */
        WCHECK_SLACK(&str[1], LEN - 1);

        /* Remove dot_above in the sequence
           I + dot_above, which will turn into i */
        rc = wcsfc_s(str, LEN, L"I\x307", &ind); /* skip the I-dot */
        ERR(EOK);
        WEXPSTR(str, L"i"); /* ditto */
        WCHECK_SLACK(&str[1], LEN - 1);

        rc = wcsfc_s(str, LEN, L"A\x307", &ind); /* Not with A */
        ERR(EOK);
        wcscpy(str1, L"a"
                     L"\x307");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[2], LEN - 2);

        /* Unless an I is before a dot_above, it turns
           into a dotless i. */
        rc = wcsfc_s(str, LEN, L"I", &ind);
        ERR(EOK);
        WEXPSTR(str, L"\x131"); /* dotless ı */
        WCHECK_SLACK(&str[1], LEN - 1);

        rc = wcsfc_s(str, LEN,
                     L"I\x307"
                     L"I",
                     &ind);
        ERR(EOK);
        wcscpy(str1, L"i"
                     L"\x131");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[2], LEN - 2);
    }

    SETLOCALE("lt_LT"); /* leaks 91k on darwin */
    SETLANG("lt");
    CHKLOCALE("lt") {
        rc = wcsfc_s(str, LEN, L"\xcc", &ind); /* accented I/J */
        ERR(EOK);
        wcscpy(str1, L"\x69"
                     L"\x307"
                     L"\x300");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[3], LEN - 3);

        rc = wcsfc_s(str, LEN, L"\xcd", &ind); /* accented I/J */
        ERR(EOK);
        wcscpy(str1, L"\x69"
                     L"\x307"
                     L"\x301");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[3], LEN - 3);

        rc = wcsfc_s(str, LEN, L"\x128", &ind); /* accented I/J */
        ERR(EOK);
        wcscpy(str1, L"\x69"
                     L"\x307"
                     L"\x303");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[3], LEN - 3);

        rc = wcsfc_s(str, LEN,
                     L"\x49"
                     L"\x300",
                     &ind); /* accented I/J */
        ERR(EOK);
        wcscpy(str1, L"\x69"
                     L"\x307");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[2], LEN - 2);

        rc = wcsfc_s(str, LEN,
                     L"\x4A"
                     L"\x300",
                     &ind); /* accented I/J */
        ERR(EOK);
        wcscpy(str1, L"\x6a"
                     L"\x307");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[2], LEN - 2);

        rc = wcsfc_s(str, LEN,
                     L"\x49"
                     L"\x302",
                     &ind); /* not accented I/J */
        ERR(EOK);
        wcscpy(str1, L"\x69"
                     L"\x302");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[2], LEN - 2);

        rc = wcsfc_s(str, LEN,
                     L"\x12e"
                     L"\x301",
                     &ind); /* accented I/J */
        ERR(EOK);
        wcscpy(str1, L"\x12f"
                     L"\x307");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[2], LEN - 2);

        rc = wcsfc_s(str, LEN,
                     L"\x12e"
                     L"\x302",
                     &ind); /* not accented I/J */
        ERR(EOK);
        wcscpy(str1, L"\x12f"
                     L"\x302");
        WEXPSTR(str, str1);
        WCHECK_SLACK(&str[2], LEN - 2);
    }

    SETLOCALE_C;
    SETLANG("C");
    CHKLOCALE_C;

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

    /* see if we can lower-case and decompose all */
#ifdef PERL_TEST
    fprintf_s(pl,
              "use v%s;\nno warnings;\nuse Unicode::Normalize;\nmy $err;\n"
              "use Unicode::UCD;\n"
              "warn \"Unicode::UCD::UnicodeVersion() must be "
                "%d.0.0\" if Unicode::UCD::UnicodeVersion() ne "
                "\"%d.0.0\";\n"
              "sub wstr ($) {\n"
              "  join('',map{sprintf'\\x{%%X}',$_} unpack 'W*',shift);\n"
              "}\n"
              "sub chk {\n"
              "  my ($ch, $got) = @_;\n"
              "  my $fc = fc($ch);\n"
              "  my $nfd = NFD($fc);\n"
              "  if ($nfd ne $got) {\n"
              "    print \"Error \" if $fc ne $got;\n"
              "    printf \"NFD \\\\x{%%X} = %%s; fc = %%s; got: %%s\\n\",\n"
              "         unpack('W*',$ch), wstr($nfd), wstr($fc), wstr($got);\n"
              "    1\n"
              "  }\n"
                  "}\n", PERL_VERSION, SAFECLIB_UNICODE_VERSION, SAFECLIB_UNICODE_VERSION);
#endif
    for (wc = 0xc0; wc < 0x02fa20; wc++) {
        static wchar_t src[5];
        wchar_t *dest = &src[0];
        rsize_t dmax = 5;
        int c;
        if (wc == 0xd800) {
            wc = 0xdfff;
            continue;
        }
        _ENC_W16(dest, dmax, wc);
        *dest = 0;
        c = iswfc(wc);
        /*debug_printf("%s %u  U+%04X\n", __FUNCTION__, __LINE__, (int)ind);*/
        rc = wcsfc_s(str, 5, src, &len);
        /* c might be 1 or 0, but still expand to 2. eg U+C0 */
        /* 1fb0-1fff is special, expands via nfd to more */
        if (c >= 2 && c != (int)len && ((wc < 0x1f80) || (wc > 0x1fff))) {
            debug_printf("%s %u  %d U+%04X *lenp %d != iswfc %d  ",
                         __FUNCTION__, __LINE__, (int)rc, (int)wc, (int)len,
                         (int)c);
            WPRINTLSN(str);
        }
        if (!c) {
            ERR(EOK);
        } else if (rc) { /* 409 is not found, a iswfc() mismatch */
            debug_printf("%s %u  Error %d U+%04X  ", __FUNCTION__, __LINE__,
                         (int)rc, (int)wc);
            WPRINTLS(src);
            debug_printf(" => ");
            WPRINTLSN(str);
            errs++;
        }
#ifdef PERL_TEST
        {
            int i;
            len = wcslen(str);
            /* cross-check with perl */
            fprintf_s(pl, "$err += chk(\"\\N{U+%04X}\",\"\\N{U+%04X}", wc,
                      str[0]);
            for (i = 1; i < (int)len; i++) {
                fprintf_s(pl, "\\N{U+%04X}", str[i]);
            }
            fprintf_s(pl, "\");\n");
        }
#endif
    }

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

#ifdef PERL_TEST
    fprintf_s(pl, "exit $err;\n");
#ifdef BSD_ALL_LIKE
    fstat(pl->_file, &st);
    fclose(pl);
#elif defined __GLIBC__
    fstat(pl->_fileno, &st);
    fclose(pl);
#else
    fclose(pl);
    stat(TESTPL, &st);
#endif
    if (st.st_size) {
        printf("Cross check with " PERL ":\n");
        fflush(stdout);
        if (system(PERL " " TESTPL) < 0) {
            printf("Redo with %s (probably wrong Unicode version):\n", PERL);
            fflush(stdout);
            if (!system(PERL " " TESTPL))
                printf(PERL " " TESTPL " failed\n");
        }
    }
#ifndef DEBUG
    unlink(TESTPL);
#endif

#endif /* PERL_TEST */

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

    return (errs);
}

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