Path: blob/main/lib/libc/tests/locale/wcsrtombs_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 wcsrtombs(), 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(wcsrtombs_test);45ATF_TC_BODY(wcsrtombs_test, tc)46{47wchar_t srcbuf[128];48char dstbuf[128];49wchar_t *src;50mbstate_t s;5152/* C/POSIX locale. */5354/* Simple null terminated string. */55wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));56wcscpy(srcbuf, L"hello");57memset(dstbuf, 0xcc, sizeof(dstbuf));58src = srcbuf;59memset(&s, 0, sizeof(s));60ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, sizeof(dstbuf),61&s) == 5);62ATF_REQUIRE(strcmp(dstbuf, "hello") == 0);63ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc);64ATF_REQUIRE(src == NULL);6566/* Not enough space in destination buffer. */67wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));68wcscpy(srcbuf, L"hello");69memset(dstbuf, 0xcc, sizeof(dstbuf));70src = srcbuf;71memset(&s, 0, sizeof(s));72ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, 4,73&s) == 4);74ATF_REQUIRE(memcmp(dstbuf, "hell", 4) == 0);75ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc);76ATF_REQUIRE(src == srcbuf + 4);7778/* Null terminated string, internal dest. buffer */79wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));80wcscpy(srcbuf, L"hello");81src = srcbuf;82memset(&s, 0, sizeof(s));83ATF_REQUIRE(wcsrtombs(NULL, (const wchar_t **)&src, sizeof(dstbuf),84&s) == 5);8586/* Null terminated string, internal state. */87wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));88wcscpy(srcbuf, L"hello");89memset(dstbuf, 0xcc, sizeof(dstbuf));90src = srcbuf;91ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, sizeof(dstbuf),92NULL) == 5);93ATF_REQUIRE(strcmp(dstbuf, "hello") == 0);94ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc);95ATF_REQUIRE(src == NULL);9697/* Null terminated string, internal state, internal dest. buffer. */98wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));99wcscpy(srcbuf, L"hello");100src = srcbuf;101ATF_REQUIRE(wcsrtombs(NULL, (const wchar_t **)&src, 0, NULL) == 5);102103/* Empty source buffer. */104wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));105srcbuf[0] = L'\0';106memset(dstbuf, 0xcc, sizeof(dstbuf));107src = srcbuf;108memset(&s, 0, sizeof(s));109ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, sizeof(dstbuf),110&s) == 0);111ATF_REQUIRE(dstbuf[0] == L'\0');112113/* Zero length destination buffer. */114wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));115wcscpy(srcbuf, L"hello");116memset(dstbuf, 0xcc, sizeof(dstbuf));117src = srcbuf;118memset(&s, 0, sizeof(s));119ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, 0, &s) == 0);120ATF_REQUIRE((unsigned char)dstbuf[0] == 0xcc);121122/*123* Japanese (EUC) locale.124*/125126ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);127ATF_REQUIRE(MB_CUR_MAX > 1);128129wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));130srcbuf[0] = 0xA3C1;131srcbuf[1] = 0x0020;132srcbuf[2] = 0x0042;133srcbuf[3] = 0x0020;134srcbuf[4] = 0xA3C3;135srcbuf[5] = 0x0000;136memset(dstbuf, 0xcc, sizeof(dstbuf));137src = srcbuf;138memset(&s, 0, sizeof(s));139ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, sizeof(dstbuf),140&s) == 7);141ATF_REQUIRE(strcmp(dstbuf, "\xA3\xC1 B \xA3\xC3") == 0);142ATF_REQUIRE((unsigned char)dstbuf[8] == 0xcc);143ATF_REQUIRE(src == NULL);144}145146ATF_TP_ADD_TCS(tp)147{148149ATF_TP_ADD_TC(tp, wcsrtombs_test);150151return (atf_no_error());152}153154155