#include <err.h>
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define NSTRINGS 2000
#define MAXSTRLEN 20
#define MAXXFRMLEN (MAXSTRLEN * 20)
typedef struct {
char sval[MAXSTRLEN];
char xval[MAXXFRMLEN];
} cstr;
int
main(void)
{
cstr data[NSTRINGS];
char *curloc;
int i, j;
if ((curloc = setlocale(LC_ALL, "")) == NULL)
err(1, "setlocale");
srandom((unsigned int) time(NULL));
for (i = 0; i < NSTRINGS; i++) {
char *p;
int len;
again:
p = data[i].sval;
len = 1 + (random() % (MAXSTRLEN - 1));
while (len > 0) {
int c;
if ((random() % 16) < 3) {
c = ' ';
} else {
do {
c = random() & 0xFF;
} while (!((c >= ' ' && c <= 127) ||
(c >= 0xA0 && c <= 0xFF)));
}
if (c <= 127) {
*p++ = c;
len--;
} else {
if (len < 2)
break;
*p++ = 0xC0 + (c >> 6);
len--;
*p++ = 0x80 + (c & 0x3F);
len--;
}
}
*p = '\0';
errno = 0;
if (strxfrm(data[i].xval, data[i].sval,
MAXXFRMLEN) >= MAXXFRMLEN) {
errx(1, "strxfrm() result for %d-length string "
"exceeded %d bytes", (int)strlen(data[i].sval),
MAXXFRMLEN);
}
if (errno != 0)
goto again;
}
for (i = 0; i < NSTRINGS; i++) {
for (j = 0; j < NSTRINGS; j++) {
int sr = strcoll(data[i].sval, data[j].sval);
int sx = strcmp(data[i].xval, data[j].xval);
if ((sr * sx < 0) || (sr * sx == 0 && sr + sx != 0)) {
errx(1, "%s: diff for \"%s\" and \"%s\"",
curloc, data[i].sval, data[j].sval);
}
}
}
return (0);
}