Path: blob/main/lib/libc/tests/locale/wcrtomb_test.c
39491 views
/*-1* Copyright (c) 2002 Tim J. Robbins2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Test program for wcrtomb(), as specified by IEEE Std. 1003.1-2001 and28* ISO/IEC 9899:1999.29*30* The function is tested with both the "C" ("POSIX") LC_CTYPE setting and31* "ja_JP.eucJP". Other encodings are not tested.32*/3334#include <errno.h>35#include <limits.h>36#include <locale.h>37#include <stdio.h>38#include <stdlib.h>39#include <string.h>40#include <wchar.h>4142#include <atf-c.h>4344ATF_TC_WITHOUT_HEAD(wcrtomb_test);45ATF_TC_BODY(wcrtomb_test, tc)46{47mbstate_t s;48size_t len;49char buf[MB_LEN_MAX + 1];5051/* C/POSIX locale. */5253ATF_REQUIRE(MB_CUR_MAX == 1);5455/*56* If the buffer argument is NULL, wc is implicitly L'\0',57* wcrtomb() resets its internal state.58*/59ATF_REQUIRE(wcrtomb(NULL, L'\0', NULL) == 1);60ATF_REQUIRE(wcrtomb(NULL, UCHAR_MAX + 1, NULL) == 1);6162/* Null wide character. */63memset(&s, 0, sizeof(s));64memset(buf, 0xcc, sizeof(buf));65len = wcrtomb(buf, L'\0', &s);66ATF_REQUIRE(len == 1);67ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);6869/* Latin letter A, internal state. */70ATF_REQUIRE(wcrtomb(NULL, L'\0', NULL) == 1);71ATF_REQUIRE(wcrtomb(NULL, L'A', NULL) == 1);7273/* Latin letter A. */74memset(&s, 0, sizeof(s));75memset(buf, 0xcc, sizeof(buf));76len = wcrtomb(buf, L'A', &s);77ATF_REQUIRE(len == 1);78ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);7980/* Invalid code. */81ATF_REQUIRE(wcrtomb(buf, UCHAR_MAX + 1, NULL) == (size_t)-1);82ATF_REQUIRE(errno == EILSEQ);8384/*85* Japanese (EUC) locale.86*/8788ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);89ATF_REQUIRE(MB_CUR_MAX == 3);9091/*92* If the buffer argument is NULL, wc is implicitly L'\0',93* wcrtomb() resets its internal state.94*/95ATF_REQUIRE(wcrtomb(NULL, L'\0', NULL) == 1);9697/* Null wide character. */98memset(&s, 0, sizeof(s));99memset(buf, 0xcc, sizeof(buf));100len = wcrtomb(buf, L'\0', &s);101ATF_REQUIRE(len == 1);102ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);103104/* Latin letter A, internal state. */105ATF_REQUIRE(wcrtomb(NULL, L'\0', NULL) == 1);106ATF_REQUIRE(wcrtomb(NULL, L'A', NULL) == 1);107108/* Latin letter A. */109memset(&s, 0, sizeof(s));110memset(buf, 0xcc, sizeof(buf));111len = wcrtomb(buf, L'A', &s);112ATF_REQUIRE(len == 1);113ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);114115/* Full width letter A. */116memset(&s, 0, sizeof(s));117memset(buf, 0xcc, sizeof(buf));118len = wcrtomb(buf, 0xa3c1, &s);119ATF_REQUIRE(len == 2);120ATF_REQUIRE((unsigned char)buf[0] == 0xa3 &&121(unsigned char)buf[1] == 0xc1 &&122(unsigned char)buf[2] == 0xcc);123}124125ATF_TP_ADD_TCS(tp)126{127128ATF_TP_ADD_TC(tp, wcrtomb_test);129130return (atf_no_error());131}132133134