Path: blob/main/lib/libc/tests/locale/mbrlen_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 mbrlen(), 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(mbrlen_test);45ATF_TC_BODY(mbrlen_test, tc)46{47mbstate_t s;48char buf[MB_LEN_MAX + 1];4950/* C/POSIX locale. */51ATF_REQUIRE(MB_CUR_MAX == 1);5253/* Null wide character, internal state. */54memset(buf, 0xcc, sizeof(buf));55buf[0] = 0;56ATF_REQUIRE(mbrlen(buf, 1, NULL) == 0);5758/* Null wide character. */59memset(&s, 0, sizeof(s));60ATF_REQUIRE(mbrlen(buf, 1, &s) == 0);6162/* Latin letter A, internal state. */63ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0);64buf[0] = 'A';65ATF_REQUIRE(mbrlen(buf, 1, NULL) == 1);6667/* Latin letter A. */68memset(&s, 0, sizeof(s));69ATF_REQUIRE(mbrlen(buf, 1, &s) == 1);7071/* Incomplete character sequence. */72memset(&s, 0, sizeof(s));73ATF_REQUIRE(mbrlen(buf, 0, &s) == (size_t)-2);7475/* Japanese (EUC) locale. */7677ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);78ATF_REQUIRE(MB_CUR_MAX > 1);7980/* Null wide character, internal state. */81ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0);82memset(buf, 0xcc, sizeof(buf));83buf[0] = 0;84ATF_REQUIRE(mbrlen(buf, 1, NULL) == 0);8586/* Null wide character. */87memset(&s, 0, sizeof(s));88ATF_REQUIRE(mbrlen(buf, 1, &s) == 0);8990/* Latin letter A, internal state. */91ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0);92buf[0] = 'A';93ATF_REQUIRE(mbrlen(buf, 1, NULL) == 1);9495/* Latin letter A. */96memset(&s, 0, sizeof(s));97ATF_REQUIRE(mbrlen(buf, 1, &s) == 1);9899/* Incomplete character sequence (zero length). */100memset(&s, 0, sizeof(s));101ATF_REQUIRE(mbrlen(buf, 0, &s) == (size_t)-2);102103/* Incomplete character sequence (truncated double-byte). */104memset(buf, 0xcc, sizeof(buf));105buf[0] = 0xa3;106buf[1] = 0x00;107memset(&s, 0, sizeof(s));108ATF_REQUIRE(mbrlen(buf, 1, &s) == (size_t)-2);109110/* Same as above, but complete. */111buf[1] = 0xc1;112memset(&s, 0, sizeof(s));113ATF_REQUIRE(mbrlen(buf, 2, &s) == 2);114}115116ATF_TP_ADD_TCS(tp)117{118119ATF_TP_ADD_TC(tp, mbrlen_test);120121return (atf_no_error());122}123124125