Path: blob/main/lib/libc/tests/locale/wcstombs_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 wcstombs(), 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(wcstombs_test);45ATF_TC_BODY(wcstombs_test, tc)46{47wchar_t srcbuf[128];48char dstbuf[128];4950/* C/POSIX locale. */5152/* Simple null terminated string. */53wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));54wcscpy(srcbuf, L"hello");55memset(dstbuf, 0xcc, sizeof(dstbuf));56ATF_REQUIRE(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 5);57ATF_REQUIRE(strcmp(dstbuf, "hello") == 0);58ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc);5960/* Not enough space in destination buffer. */61wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));62wcscpy(srcbuf, L"hello");63memset(dstbuf, 0xcc, sizeof(dstbuf));64ATF_REQUIRE(wcstombs(dstbuf, srcbuf, 4) == 4);65ATF_REQUIRE(memcmp(dstbuf, "hell", 4) == 0);66ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc);6768/* Null terminated string, internal dest. buffer */69wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));70wcscpy(srcbuf, L"hello");71ATF_REQUIRE(wcstombs(NULL, srcbuf, sizeof(dstbuf)) == 5);7273/* Null terminated string, internal state. */74wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));75wcscpy(srcbuf, L"hello");76memset(dstbuf, 0xcc, sizeof(dstbuf));77ATF_REQUIRE(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 5);78ATF_REQUIRE(strcmp(dstbuf, "hello") == 0);79ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc);8081/* Null terminated string, internal state, internal dest. buffer. */82wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));83wcscpy(srcbuf, L"hello");84ATF_REQUIRE(wcstombs(NULL, srcbuf, 0) == 5);8586/* Empty source buffer. */87wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));88srcbuf[0] = L'\0';89memset(dstbuf, 0xcc, sizeof(dstbuf));90ATF_REQUIRE(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 0);91ATF_REQUIRE(dstbuf[0] == L'\0');9293/* Zero length destination buffer. */94wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));95wcscpy(srcbuf, L"hello");96memset(dstbuf, 0xcc, sizeof(dstbuf));97ATF_REQUIRE(wcstombs(dstbuf, srcbuf, 0) == 0);98ATF_REQUIRE((unsigned char)dstbuf[0] == 0xcc);99100/*101* Japanese (EUC) locale.102*/103104ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);105ATF_REQUIRE(MB_CUR_MAX > 1);106107wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));108srcbuf[0] = 0xA3C1;109srcbuf[1] = 0x0020;110srcbuf[2] = 0x0042;111srcbuf[3] = 0x0020;112srcbuf[4] = 0xA3C3;113srcbuf[5] = 0x0000;114memset(dstbuf, 0xcc, sizeof(dstbuf));115ATF_REQUIRE(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 7);116ATF_REQUIRE(strcmp(dstbuf, "\xA3\xC1 B \xA3\xC3") == 0);117ATF_REQUIRE((unsigned char)dstbuf[8] == 0xcc);118}119120ATF_TP_ADD_TCS(tp)121{122123ATF_TP_ADD_TC(tp, wcstombs_test);124125return (atf_no_error());126}127128129