Path: blob/main/lib/libc/tests/string/wcscoll_test.c
39485 views
/*-1* Copyright (c) 2016 Baptiste Daroussin <[email protected]>2* Copyright 2016 Tom Lane <[email protected]>3* Copyright 2017 Nexenta Systems, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <wchar.h>29#include <locale.h>30#include <stdlib.h>31#include <time.h>32#include <errno.h>3334#include <atf-c.h>3536static int37cmp(const void *a, const void *b)38{39const wchar_t wa[2] = { *(const wchar_t *)a, 0 };40const wchar_t wb[2] = { *(const wchar_t *)b, 0 };4142return (wcscoll(wa, wb));43}4445ATF_TC_WITHOUT_HEAD(russian_collation);46ATF_TC_BODY(russian_collation, tc)47{48wchar_t c[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzЁАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяё";49wchar_t res[] = L"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZаАбБвВгГдДеЕёЁжЖзЗиИйЙкКлЛмМнНоОпПрРсСтТуУфФхХцЦчЧшШщЩъЪыЫьЬэЭюЮяЯ";5051ATF_CHECK_MSG(setlocale(LC_ALL, "ru_RU.UTF-8") != NULL,52"Fail to set locale to \"ru_RU.UTF-8\"");53qsort(c, wcslen(c), sizeof(wchar_t), cmp);54ATF_CHECK_MSG(wcscmp(c, res) == 0,55"Bad collation, expected: '%ls' got '%ls'", res, c);56}5758#define NSTRINGS 200059#define MAXSTRLEN 2060#define MAXXFRMLEN (MAXSTRLEN * 20)6162typedef struct {63char sval[MAXSTRLEN];64char xval[MAXXFRMLEN];65} cstr;6667ATF_TC_WITHOUT_HEAD(strcoll_vs_strxfrm);68ATF_TC_BODY(strcoll_vs_strxfrm, tc)69{70cstr data[NSTRINGS];71char *curloc;72int i, j;7374curloc = setlocale(LC_ALL, "en_US.UTF-8");75ATF_CHECK_MSG(curloc != NULL, "Fail to set locale");7677/* Ensure new random() values on every run */78srandom((unsigned int) time(NULL));7980/* Generate random UTF8 strings of length less than MAXSTRLEN bytes */81for (i = 0; i < NSTRINGS; i++) {82char *p;83int len;8485again:86p = data[i].sval;87len = 1 + (random() % (MAXSTRLEN - 1));88while (len > 0) {89int c;90/*91* Generate random printable char in ISO8859-1 range.92* Bias towards producing a lot of spaces.93*/9495if ((random() % 16) < 3) {96c = ' ';97} else {98do {99c = random() & 0xFF;100} while (!((c >= ' ' && c <= 127) ||101(c >= 0xA0 && c <= 0xFF)));102}103104if (c <= 127) {105*p++ = c;106len--;107} else {108if (len < 2)109break;110/* Poor man's utf8-ification */111*p++ = 0xC0 + (c >> 6);112len--;113*p++ = 0x80 + (c & 0x3F);114len--;115}116}117*p = '\0';118/* strxfrm() each string as we produce it */119errno = 0;120ATF_CHECK_MSG(strxfrm(data[i].xval, data[i].sval,121MAXXFRMLEN) < MAXXFRMLEN, "strxfrm() result for %d-length "122" string exceeded %d bytes", (int)strlen(data[i].sval),123MAXXFRMLEN);124125/*126* Amend strxfrm() failing on certain characters to be fixed and127* test later128*/129if (errno != 0)130goto again;131}132133for (i = 0; i < NSTRINGS; i++) {134for (j = 0; j < NSTRINGS; j++) {135int sr = strcoll(data[i].sval, data[j].sval);136int sx = strcmp(data[i].xval, data[j].xval);137138ATF_CHECK_MSG(!((sr * sx < 0) ||139(sr * sx == 0 && sr + sx != 0)),140"%s: diff for \"%s\" and \"%s\"",141curloc, data[i].sval, data[j].sval);142}143}144}145146ATF_TP_ADD_TCS(tp)147{148ATF_TP_ADD_TC(tp, russian_collation);149ATF_TP_ADD_TC(tp, strcoll_vs_strxfrm);150151return (atf_no_error());152}153154155