File: test_towlower.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 (111 lines) | stat: -rw-r--r-- 3,431 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
/*------------------------------------------------------------------
 * test_towlower.c
 *
 * Test our musl-inherited towupper/towlower regarding Unicode 12.0
 * CaseFolding.txt
 * Some F characters fold to multiples, which cannot be handled here,
 * see test_towcf_s.c instead.
 *------------------------------------------------------------------
 */

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

EXTERN uint32_t _towcase(uint32_t wc, int lower);
int test_towlower(void);

#define CFOLD "CaseFolding.txt"

int ignore_f = 1;

int test_towlower(void) {
    int errs = 0;
    int c;
    char s[128];
    char code[8];
    char status[2];
    char mapping[24];
    char name[80];
    FILE *f;

    uint32_t wc, lwr;

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

    f = fopen(CFOLD, "r");
    if (!f) {
        char url[256];
        snprintf(url, 255,
                 "wget https://www.unicode.org/Public/%d.0.0/ucd/%s",
                 SAFECLIB_UNICODE_VERSION, CFOLD);
        printf("downloading %s ...", CFOLD);
        fflush(stdout);
        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); /* the other cases */
            mapping[l] = 0;
            strcpy(name, &p1[4]);
            name[strlen(name) - 1] = 0;

            c = sscanf(code, "%X", &wc);
            if (c) {
                uint32_t mp;
                lwr = wc < 128 ? (uint32_t)tolower(wc) : _towcase(wc, 1);
                c = sscanf(mapping, "%X", &mp);
                if (*status == 'T')
                    mp = lwr;
                /* we have 104 unhandled F multi-char mappings */
                else if (*status ==
                         'F') { /* lower is bigger than upper, ignored */
                    if (!ignore_f) {
                        if (lwr != wc)
                            printf("U+%04X => U+%04X lower=(%s) F %s\n", wc,
                                   lwr, mapping, name);
                        else
                            printf("U+%04X lower=(%s) F %s\n", wc, mapping,
                                   name);
                    }
                } else if (mp != lwr) {
                    lwr = _towcase(wc, 1);
                    if (wc != lwr)
                        printf("Error U+%04X => U+%04X lower=%s status=%s "
                               "name=%s\n",
                               wc, lwr, mapping, status, name);
                    else
                        printf("Error U+%04X lower=%s status=%s name=%s\n", wc,
                               mapping, status, name);
                }
            } else {
                printf("Error code=%s status=%s lower=%s name=%s\n", code,
                       status, mapping, name);
            }
        }
    }
    fclose(f);
    return (errs);
}

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