2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2016 Tom Lane <tgl@sss.pgh.pa.us>
14 * Copyright 2017 Nexenta Systems, Inc.
26 * #6907: generate random UTF8 strings, strxfrm'ing them in process.
27 * Walk through comparing each string with all strings, and checking
28 * that strcoll() and strcmp() for strxfrm'ed data produce same results.
32 #define MAXXFRMLEN (MAXSTRLEN * 20)
36 char xval
[MAXXFRMLEN
];
46 if ((curloc
= setlocale(LC_ALL
, "")) == NULL
)
49 /* Ensure new random() values on every run */
50 srandom((unsigned int) time(NULL
));
52 /* Generate random UTF8 strings of length less than MAXSTRLEN bytes */
53 for (i
= 0; i
< NSTRINGS
; i
++) {
59 len
= 1 + (random() % (MAXSTRLEN
- 1));
64 * Generate random printable char in ISO8859-1 range.
65 * Bias towards producing a lot of spaces.
67 if ((random() % 16) < 3) {
72 } while (!((c
>= ' ' && c
<= 127) ||
73 (c
>= 0xA0 && c
<= 0xFF)));
82 /* Poor man's utf8-ification */
83 *p
++ = 0xC0 + (c
>> 6);
85 *p
++ = 0x80 + (c
& 0x3F);
91 /* strxfrm() each string as we produce it */
93 if (strxfrm(data
[i
].xval
, data
[i
].sval
,
94 MAXXFRMLEN
) >= MAXXFRMLEN
) {
95 errx(1, "strxfrm() result for %d-length string "
96 "exceeded %d bytes", (int)strlen(data
[i
].sval
),
99 /* Amend strxfrm() failing for certain characters (#7962) */
104 for (i
= 0; i
< NSTRINGS
; i
++) {
105 for (j
= 0; j
< NSTRINGS
; j
++) {
106 int sr
= strcoll(data
[i
].sval
, data
[j
].sval
);
107 int sx
= strcmp(data
[i
].xval
, data
[j
].xval
);
109 if ((sr
* sx
< 0) || (sr
* sx
== 0 && sr
+ sx
!= 0)) {
110 errx(1, "%s: diff for \"%s\" and \"%s\"",
111 curloc
, data
[i
].sval
, data
[j
].sval
);