Files
fastwc/benchmarks/tools/genfile.c
T
huntedbytheirs c45de29b0c 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.
2026-11-15 14:00:07 -05:00

133 lines
3.9 KiB
C

/*
* genfile — generate a text file of N lines, each a random 10-character
* alphanumeric string followed by a newline. Used by the fastwc benchmarks
* (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>
#include <unistd.h>
#define LINE_LEN 10
#define CHUNK (64 * 1024)
static const char alpha[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
#define ALPHA_LEN (sizeof(alpha) - 1)
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;
state ^= state >> 7;
state ^= state << 17;
return state;
}
int main(int argc, char **argv)
{
long n;
long i;
int j;
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 [--utf8] <lines>\n", argv[0]);
return 2;
}
n = atol(argv[1]);
if (n < 0) {
fprintf(stderr, "genfile: invalid line count: %s\n", argv[1]);
return 2;
}
/* seed from time + pid so each run produces fresh data */
state = (uint64_t)time(NULL) ^ ((uint64_t)getpid() << 32) ^ 0x9E3779B97F4A7C15ULL;
for (i = 0; i < n; i++) {
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';
/* 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;
}
}
if (used && fwrite(buf, 1, used, stdout) != used)
return 1;
if (fclose(stdout) != 0)
return 1;
return 0;
}