Path: blob/main/lib/libc/tests/locale/mbstowcs_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 mbstowcs(), 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(mbstowcs_test);45ATF_TC_BODY(mbstowcs_test, tc)46{47char srcbuf[128];48wchar_t dstbuf[128];4950/* C/POSIX locale. */5152/* Simple null terminated string. */53memset(srcbuf, 0xcc, sizeof(srcbuf));54strcpy(srcbuf, "hello");55wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));56ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, sizeof(dstbuf) / sizeof(*dstbuf)) == 5);57ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0);58ATF_REQUIRE(dstbuf[6] == 0xcccc);5960/* Not enough space in destination buffer. */61memset(srcbuf, 0xcc, sizeof(srcbuf));62strcpy(srcbuf, "hello");63wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));64ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, 4) == 4);65ATF_REQUIRE(wmemcmp(dstbuf, L"hell", 4) == 0);66ATF_REQUIRE(dstbuf[5] == 0xcccc);6768/* Null terminated string, internal dest. buffer (XSI extension) */69memset(srcbuf, 0xcc, sizeof(srcbuf));70strcpy(srcbuf, "hello");71ATF_REQUIRE(mbstowcs(NULL, srcbuf, 0) == 5);7273/* Empty source buffer. */74memset(srcbuf, 0xcc, sizeof(srcbuf));75srcbuf[0] = '\0';76wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));77ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, 1) == 0);78ATF_REQUIRE(dstbuf[0] == 0);79ATF_REQUIRE(dstbuf[1] == 0xcccc);8081/* Zero length destination buffer. */82memset(srcbuf, 0xcc, sizeof(srcbuf));83strcpy(srcbuf, "hello");84wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));85ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, 0) == 0);86ATF_REQUIRE(dstbuf[0] == 0xcccc);8788/* Japanese (EUC) locale. */8990ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);91ATF_REQUIRE(MB_CUR_MAX > 1);9293memset(srcbuf, 0xcc, sizeof(srcbuf));94strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3");95wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));96ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, sizeof(dstbuf) / sizeof(*dstbuf)) == 5);97ATF_REQUIRE(dstbuf[0] == 0xA3C1 && dstbuf[1] == 0x20 && dstbuf[2] == 0x42 &&98dstbuf[3] == 0x20 && dstbuf[4] == 0xA3C3 && dstbuf[5] == 0);99}100101ATF_TP_ADD_TCS(tp)102{103104ATF_TP_ADD_TC(tp, mbstowcs_test);105106return (atf_no_error());107}108109110