Path: blob/main/lib/libc/tests/locale/mbsrtowcs_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 mbsrtowcs(), 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(mbsrtowcs_test);45ATF_TC_BODY(mbsrtowcs_test, tc)46{47char srcbuf[128];48wchar_t dstbuf[128];49char *src;50mbstate_t s;5152/*53* C/POSIX locale.54*/5556/* Simple null terminated string. */57memset(srcbuf, 0xcc, sizeof(srcbuf));58strcpy(srcbuf, "hello");59wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));60src = srcbuf;61memset(&s, 0, sizeof(s));62ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, sizeof(dstbuf) /63sizeof(*dstbuf), &s) == 5);64ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0);65ATF_REQUIRE(dstbuf[6] == 0xcccc);66ATF_REQUIRE(src == NULL);6768/* Not enough space in destination buffer. */69memset(srcbuf, 0xcc, sizeof(srcbuf));70strcpy(srcbuf, "hello");71wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));72src = srcbuf;73memset(&s, 0, sizeof(s));74ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, 4, &s) == 4);75ATF_REQUIRE(wmemcmp(dstbuf, L"hell", 4) == 0);76ATF_REQUIRE(dstbuf[5] == 0xcccc);77ATF_REQUIRE(src == srcbuf + 4);7879/* Null terminated string, internal dest. buffer */80memset(srcbuf, 0xcc, sizeof(srcbuf));81strcpy(srcbuf, "hello");82src = srcbuf;83memset(&s, 0, sizeof(s));84ATF_REQUIRE(mbsrtowcs(NULL, (const char **)&src, 0, &s) == 5);8586/* Null terminated string, internal state. */87memset(srcbuf, 0xcc, sizeof(srcbuf));88strcpy(srcbuf, "hello");89wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));90src = srcbuf;91ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, sizeof(dstbuf) /92sizeof(*dstbuf), NULL) == 5);93ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0);94ATF_REQUIRE(dstbuf[6] == 0xcccc);95ATF_REQUIRE(src == NULL);9697/* Null terminated string, internal state, internal dest. buffer. */98memset(srcbuf, 0xcc, sizeof(srcbuf));99strcpy(srcbuf, "hello");100src = srcbuf;101ATF_REQUIRE(mbsrtowcs(NULL, (const char **)&src, 0, NULL) == 5);102103/* Empty source buffer. */104memset(srcbuf, 0xcc, sizeof(srcbuf));105srcbuf[0] = '\0';106src = srcbuf;107memset(&s, 0, sizeof(s));108wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));109ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, 1, &s) == 0);110ATF_REQUIRE(dstbuf[0] == 0);111ATF_REQUIRE(dstbuf[1] == 0xcccc);112ATF_REQUIRE(src == NULL);113114/* Zero length destination buffer. */115memset(srcbuf, 0xcc, sizeof(srcbuf));116strcpy(srcbuf, "hello");117src = srcbuf;118memset(&s, 0, sizeof(s));119wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));120ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, 0, &s) == 0);121ATF_REQUIRE(dstbuf[0] == 0xcccc);122ATF_REQUIRE(src == srcbuf);123124/*125* Japanese (EUC) locale.126*/127128ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);129ATF_REQUIRE(MB_CUR_MAX > 1);130131memset(srcbuf, 0xcc, sizeof(srcbuf));132strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3");133src = srcbuf;134memset(&s, 0, sizeof(s));135wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));136ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, sizeof(dstbuf) /137sizeof(*dstbuf), &s) == 5);138ATF_REQUIRE(dstbuf[0] == 0xA3C1 && dstbuf[1] == 0x20 && dstbuf[2] == 0x42 &&139dstbuf[3] == 0x20 && dstbuf[4] == 0xA3C3 && dstbuf[5] == 0);140ATF_REQUIRE(src == NULL);141}142143ATF_TP_ADD_TCS(tp)144{145146ATF_TP_ADD_TC(tp, mbsrtowcs_test);147148return (atf_no_error());149}150151152