Path: blob/main/lib/libc/tests/locale/mbrtowc_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 mbrtowc(), 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(mbrtowc_test);45ATF_TC_BODY(mbrtowc_test, tc)46{47mbstate_t s;48wchar_t wc;49char buf[MB_LEN_MAX + 1];5051/*52* C/POSIX locale.53*/5455ATF_REQUIRE(MB_CUR_MAX == 1);5657/* Null wide character, internal state. */58memset(buf, 0xcc, sizeof(buf));59buf[0] = 0;60ATF_REQUIRE(mbrtowc(&wc, buf, 1, NULL) == 0);61ATF_REQUIRE(wc == 0);6263/* Null wide character. */64memset(&s, 0, sizeof(s));65ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 0);66ATF_REQUIRE(wc == 0);6768/* Latin letter A, internal state. */69ATF_REQUIRE(mbrtowc(NULL, 0, 0, NULL) == 0);70buf[0] = 'A';71ATF_REQUIRE(mbrtowc(&wc, buf, 1, NULL) == 1);72ATF_REQUIRE(wc == L'A');7374/* Latin letter A. */75memset(&s, 0, sizeof(s));76ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 1);77ATF_REQUIRE(wc == L'A');7879/* Incomplete character sequence. */80wc = L'z';81memset(&s, 0, sizeof(s));82ATF_REQUIRE(mbrtowc(&wc, buf, 0, &s) == (size_t)-2);83ATF_REQUIRE(wc == L'z');8485/* Check that mbrtowc() doesn't access the buffer when n == 0. */86wc = L'z';87memset(&s, 0, sizeof(s));88buf[0] = '\0';89ATF_REQUIRE(mbrtowc(&wc, buf, 0, &s) == (size_t)-2);90ATF_REQUIRE(wc == L'z');9192/*93* Japanese (EUC) locale.94*/9596ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);97ATF_REQUIRE(MB_CUR_MAX > 1);9899/* Null wide character, internal state. */100ATF_REQUIRE(mbrtowc(NULL, 0, 0, NULL) == 0);101memset(buf, 0xcc, sizeof(buf));102buf[0] = 0;103ATF_REQUIRE(mbrtowc(&wc, buf, 1, NULL) == 0);104ATF_REQUIRE(wc == 0);105106/* Null wide character. */107memset(&s, 0, sizeof(s));108ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 0);109ATF_REQUIRE(wc == 0);110111/* Latin letter A, internal state. */112ATF_REQUIRE(mbrtowc(NULL, 0, 0, NULL) == 0);113buf[0] = 'A';114ATF_REQUIRE(mbrtowc(&wc, buf, 1, NULL) == 1);115ATF_REQUIRE(wc == L'A');116117/* Latin letter A. */118memset(&s, 0, sizeof(s));119ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 1);120ATF_REQUIRE(wc == L'A');121122/* Incomplete character sequence (zero length). */123wc = L'z';124memset(&s, 0, sizeof(s));125ATF_REQUIRE(mbrtowc(&wc, buf, 0, &s) == (size_t)-2);126ATF_REQUIRE(wc == L'z');127128/* Incomplete character sequence (truncated double-byte). */129memset(buf, 0xcc, sizeof(buf));130buf[0] = 0xa3;131buf[1] = 0x00;132memset(&s, 0, sizeof(s));133wc = 0;134ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == (size_t)-2);135136/* Same as above, but complete. */137buf[1] = 0xc1;138memset(&s, 0, sizeof(s));139wc = 0;140ATF_REQUIRE(mbrtowc(&wc, buf, 2, &s) == 2);141ATF_REQUIRE(wc == 0xa3c1);142143/* Test restarting behaviour. */144memset(buf, 0xcc, sizeof(buf));145buf[0] = 0xa3;146memset(&s, 0, sizeof(s));147wc = 0;148ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == (size_t)-2);149ATF_REQUIRE(wc == 0);150buf[0] = 0xc1;151ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 1);152ATF_REQUIRE(wc == 0xa3c1);153}154155ATF_TP_ADD_TCS(tp)156{157158ATF_TP_ADD_TC(tp, mbrtowc_test);159160return (atf_no_error());161}162163164