Path: blob/main/lib/libc/tests/locale/mblen_test.c
39491 views
/*-1* Copyright (c) 2002-2004 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 mblen(), as specified by IEEE Std. 1003.1-2001 and28* ISO/IEC 9899:1990.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 <limits.h>35#include <locale.h>36#include <stdio.h>37#include <stdlib.h>38#include <string.h>3940#include <atf-c.h>4142ATF_TC_WITHOUT_HEAD(mblen_test);43ATF_TC_BODY(mblen_test, tc)44{45char buf[MB_LEN_MAX + 1];4647/*48* C/POSIX locale.49*/5051ATF_REQUIRE(MB_CUR_MAX == 1);5253/* No shift states in C locale. */54ATF_REQUIRE(mblen(NULL, 0) == 0);5556/* Null wide character. */57memset(buf, 0xcc, sizeof(buf));58buf[0] = '\0';59ATF_REQUIRE(mblen(buf, 1) == 0);6061/* Latin letter A. */62buf[0] = 'A';63ATF_REQUIRE(mblen(buf, 1) == 1);6465/* Incomplete character sequence. */66buf[0] = '\0';67ATF_REQUIRE(mblen(buf, 0) == -1);68ATF_REQUIRE(mblen(NULL, 0) == 0);6970/*71* Japanese (EUC) locale.72*/7374ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);75ATF_REQUIRE(MB_CUR_MAX > 1);7677/* No shift states in EUC. */78ATF_REQUIRE(mblen(NULL, 0) == 0);7980/* Null wide character. */81memset(buf, 0xcc, sizeof(buf));82buf[0] = '\0';83ATF_REQUIRE(mblen(buf, 1) == 0);8485/* Latin letter A. */86buf[0] = 'A';87ATF_REQUIRE(mblen(buf, 1) == 1);8889/* Incomplete character sequence. */90buf[0] = '\0';91ATF_REQUIRE(mblen(buf, 0) == -1);92ATF_REQUIRE(mblen(NULL, 0) == 0);9394/* Incomplete character sequence (truncated double-byte). */95memset(buf, 0xcc, sizeof(buf));96buf[0] = 0xa3;97buf[1] = 0x00;98ATF_REQUIRE(mblen(buf, 1) == -1);99ATF_REQUIRE(mblen(NULL, 0) == 0);100101/* Same as above, but complete. */102buf[1] = 0xc1;103ATF_REQUIRE(mblen(buf, 2) == 2);104}105106ATF_TP_ADD_TCS(tp)107{108109ATF_TP_ADD_TC(tp, mblen_test);110111return (atf_no_error());112}113114115