Race the UTF-8 decoder against coreutils
Every case in the suite pinned LC_ALL=C, so the multibyte path - the one whose counts go wrong quietly rather than crashing - had no contract behind it at all. genfile grew a --utf8 mode that writes mixed CJK, ASCII words, a tab, an ideographic space, a no-break space and a combining accent, and the words suite now races -w, -m and -L over 300k lines of it under en_US.UTF-8 (C.UTF-8 as a fallback), against coreutils. The case runs the locale on both sides rather than inheriting the C pin, because the oracle's own decoder is the reference. It prints SKIP rather than passing when the host has no UTF-8 locale: a multibyte count cannot be checked in a single-byte one.
This commit is contained in:
@@ -4,9 +4,15 @@
|
||||
* (createtxt in std.sh) to build test data quickly; std.sh falls back to a
|
||||
* slow shell loop when this binary has not been built.
|
||||
*
|
||||
* With --utf8 the lines are mixed text instead: CJK runs, ASCII words, a
|
||||
* tab, and the Unicode spaces, which is the data the multibyte path was
|
||||
* built for.
|
||||
*
|
||||
* usage: genfile <lines>
|
||||
* genfile --utf8 <lines>
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
@@ -23,6 +29,18 @@ static const char alpha[] =
|
||||
|
||||
static uint64_t state;
|
||||
|
||||
/* One three-byte code point, U+4E00..U+9FFF, which is where dense CJK
|
||||
* lives and what the block kernel is written for. */
|
||||
static size_t emit_cjk(char *out, unsigned r)
|
||||
{
|
||||
unsigned cp = 0x4e00 + r % 0x5200;
|
||||
|
||||
out[0] = (char)(0xe0 | (cp >> 12));
|
||||
out[1] = (char)(0x80 | ((cp >> 6) & 0x3f));
|
||||
out[2] = (char)(0x80 | (cp & 0x3f));
|
||||
return 3;
|
||||
}
|
||||
|
||||
static uint64_t next(void)
|
||||
{
|
||||
state ^= state << 13;
|
||||
@@ -39,8 +57,15 @@ int main(int argc, char **argv)
|
||||
char buf[CHUNK];
|
||||
size_t used = 0;
|
||||
|
||||
int utf8 = 0;
|
||||
|
||||
if (argc == 3 && strcmp(argv[1], "--utf8") == 0) {
|
||||
utf8 = 1;
|
||||
argv++;
|
||||
argc--;
|
||||
}
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "usage: %s <lines>\n", argv[0]);
|
||||
fprintf(stderr, "usage: %s [--utf8] <lines>\n", argv[0]);
|
||||
return 2;
|
||||
}
|
||||
n = atol(argv[1]);
|
||||
@@ -53,10 +78,47 @@ int main(int argc, char **argv)
|
||||
state = (uint64_t)time(NULL) ^ ((uint64_t)getpid() << 32) ^ 0x9E3779B97F4A7C15ULL;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = 0; j < LINE_LEN; j++)
|
||||
buf[used++] = alpha[next() % ALPHA_LEN];
|
||||
if (!utf8) {
|
||||
for (j = 0; j < LINE_LEN; j++)
|
||||
buf[used++] = alpha[next() % ALPHA_LEN];
|
||||
} else {
|
||||
unsigned pick = (unsigned)(next() % 100);
|
||||
unsigned k, runs;
|
||||
|
||||
if (pick < 8) { /* a tab indented line */
|
||||
buf[used++] = '\t';
|
||||
}
|
||||
if (pick < 55) { /* a run of CJK, the dense case */
|
||||
runs = 1 + (unsigned)(next() % 12);
|
||||
for (k = 0; k < runs; k++)
|
||||
used += emit_cjk(buf + used, (unsigned)next());
|
||||
if (pick < 15) /* ... followed by an ASCII word */
|
||||
for (j = 0, k = 3 + (unsigned)(next() % 6); j < (int)k; j++)
|
||||
buf[used++] = alpha[next() % 26];
|
||||
} else if (pick < 85) { /* plain words */
|
||||
runs = 1 + (unsigned)(next() % 5);
|
||||
for (k = 0; k < runs; k++) {
|
||||
if (k)
|
||||
buf[used++] = ' ';
|
||||
for (j = 0; j < 7; j++)
|
||||
buf[used++] = alpha[next() % 26];
|
||||
}
|
||||
} else if (pick < 92) { /* an ideographic space */
|
||||
buf[used++] = '\xe3';
|
||||
buf[used++] = '\x80';
|
||||
buf[used++] = '\x80';
|
||||
} else if (pick < 97) { /* a no-break space */
|
||||
buf[used++] = '\xc2';
|
||||
buf[used++] = '\xa0';
|
||||
} else { /* a combining accent, width zero */
|
||||
buf[used++] = 'e';
|
||||
buf[used++] = '\xcc';
|
||||
buf[used++] = '\x81';
|
||||
}
|
||||
}
|
||||
buf[used++] = '\n';
|
||||
if (used + LINE_LEN + 1 > CHUNK) {
|
||||
/* a UTF-8 line is up to ~50 bytes, an ASCII line exactly 11 */
|
||||
if (used + 128 > CHUNK) {
|
||||
if (fwrite(buf, 1, used, stdout) != used)
|
||||
return 1;
|
||||
used = 0;
|
||||
|
||||
Reference in New Issue
Block a user