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
|
/*------------------------------------------------------------------
* test_towupper.c
*
* Test the musl-inherited towupper regarding Unicode 12.0
* Unicode has no explicit lower->upper mapping document.
*
*------------------------------------------------------------------
*/
#include "test_private.h"
#include "safe_str_lib.h"
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
#ifdef HAVE_TOWUPPER
#include <wctype.h>
#endif
#ifndef HAVE_TOWUPPER_OK
EXTERN uint32_t _towupper(uint32_t wc);
#endif
EXTERN uint32_t _towcase(uint32_t wc, int lower);
int check_casefolding(uint32_t lwr, uint32_t upr);
int check(uint32_t wc, const char *status, const char *name);
int test_towupper(void);
#define GENCAT "/usr/share/unicode/extracted/DerivedGeneralCategory.txt"
#define CFOLD "/usr/share/unicode/CaseFolding.txt"
#ifndef PERL
/* Must have the same Unicode version 9.0, at least 5.26.
Better 5.27.3 with Unicode 10. */
/*# define PERL "perl" */
/*# define PERL "cperl5.27.2"*/
#define PERL "perl5.27.3"
#endif
#define TESTPL "test-upr.pl"
char s[128];
char code[16];
char status[3];
char mapping[24];
char name[80];
FILE *f, *cf, *pl;
static int init = 0;
/* TODO: try to malloc the whole table with 2 status bits.
This linear-searches in the file buffer for extremely low-memory systems.
*/
int check_casefolding(uint32_t lwr, uint32_t upr) {
rewind(cf);
while (!feof(cf)) {
int l;
char *p = fgets(s, sizeof(s), cf);
char *p1;
uint32_t wc, mp;
int c;
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)
continue;
if (wc != upr)
continue;
c = sscanf(mapping, "%X", &mp);
if (!c)
continue;
if (lwr != mp) {
if (*status != 'F') {
/* false positives */
debug_printf(
"%u U+%04X: U+%04X != U+%04X status=%s, name=%s\n",
__LINE__, wc, lwr, mp, status, name);
/* cross-check with perl */
if (!init) {
fprintf(pl, "use v5.26;\n");
init = 1;
}
fprintf(pl,
"$l=\"\\x{%X}\";$u=uc $l;$got=\"\\x{%X}\";"
"printf \"uc %X = %%X; got: %X\\n\","
"unpack(\"W*\",$u) if $u ne $got;\n",
lwr, lwr, upr, upr);
return 1;
/*} else {
printf("%u U+%04X: U+%04X != U+%04X status=F,
name=%s\n",
__LINE__, wc, lwr, mp, name);*/
}
}
return 0;
}
}
/*debug_printf("%u U+%04X Ll: not found in %s for U+%04X\n",
__LINE__, upr, CFOLD, lwr);*/
return 0;
}
/* checks a lower case letter from DerivedGeneralCategory */
int check(uint32_t wc, const char *_status, const char *_name) {
int errs = 0;
uint32_t upr;
upr = wc < 128 ? (uint32_t)toupper(wc) : _towcase(wc, 0);
if (upr == wc) {
check_casefolding(wc, upr);
#ifdef HAVE_TOWUPPER_OK
upr = (uint32_t)towupper(wc);
if (upr != wc) {
printf("%u Error towupper(U+%04X) = U+%04X status=%s, name=%s\n",
__LINE__, wc, upr, _status, _name);
errs++;
/* else system libc agrees with us.
which says nothing. system wctype functions are generally
extremely unreliable.
*/
}
#elif defined HAVE_TOWUPPER
upr = _towupper(wc);
if (upr != wc) {
printf("%u _towupper(U+%04X) = U+%04X status=%s, name=%s\n",
__LINE__, wc, upr, _status, _name);
}
#endif
}
return errs;
}
int test_towupper(void) {
int errs = 0;
int lowfound = 0;
uint32_t wc;
struct stat st;
/*--------------------------------------------------*/
pl = fopen(TESTPL, "w");
f = fopen(GENCAT, "r");
if (!f) {
printf("not downloading %s ...", GENCAT);
return 1;
}
cf = fopen(CFOLD, "r");
if (!cf) {
printf("not downloading %s ...", CFOLD);
return 1;
}
while (!feof(f)) {
int l;
char *p = fgets(s, sizeof(s), f);
char *p1;
/* # General_Category=Lowercase_Letter */
if (!(p && *p))
continue;
if (!lowfound) {
if (!strcmp(p, "# General_Category=Lowercase_Letter\n")) {
lowfound = 1;
}
continue;
}
if (!memcmp(p, "# General_Category=", 19)) {
lowfound = 0;
continue;
}
if (s[0] != '#' && s[0] != '\n') {
uint32_t from, to;
int c;
p = strstr(s, "; ");
if (!p)
continue;
l = p - s;
memcpy(code, s, l);
code[l] = 0; /* uppercase only */
status[0] = p[2];
status[1] = p[3];
status[2] = 0;
p1 = strstr(&p[5], "# ");
l = p1 - p - 5;
strncpy(name, &p1[3], 79);
name[strlen(name) - 1] = 0;
strremovews_s(name, 80);
c = sscanf(code, "%X..%X", &from, &to);
if (c) {
for (wc = from; wc <= to; wc++) {
check(wc, status, name);
}
} else {
c = sscanf(code, "%X", &wc); /* upr */
}
if (c) {
errs += check(wc, status, name);
} else {
printf("!code=%s status=%s name=%s\n", code, status, name);
}
}
}
fclose(f);
fclose(cf);
#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)) {
printf("Redo with perl (probably wrong Unicode version):\n");
fflush(stdout);
if (!system("perl " TESTPL))
printf("perl " TESTPL " failed\n");
}
}
#ifndef DEBUG
unlink(TESTPL);
#endif
return (errs);
}
#ifndef __KERNEL__
/* simple hack to get this to work for both userspace and Linux kernel,
until a better solution can be created. */
int main(void) { return (test_towupper()); }
#endif
|