Path: blob/main/lib/libc/tests/locale/wctomb_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 wctomb(), 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>4041#include <atf-c.h>4243ATF_TC_WITHOUT_HEAD(euccs1_test);44ATF_TC_BODY(euccs1_test, tc)45{46wchar_t wc = 0x8e000000;47char buf[MB_LEN_MAX];4849ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "zh_CN.eucCN"),50"zh_CN.eucCN") == 0);5152ATF_REQUIRE(wctomb(&buf[0], wc) == 4);53}5455ATF_TC_WITHOUT_HEAD(wctomb_test);56ATF_TC_BODY(wctomb_test, tc)57{58size_t len;59char buf[MB_LEN_MAX + 1];6061/* C/POSIX locale. */6263ATF_REQUIRE(MB_CUR_MAX == 1);6465/* No shift states in C locale. */66ATF_REQUIRE(wctomb(NULL, L'\0') == 0);6768/* Null wide character. */69memset(buf, 0xcc, sizeof(buf));70len = wctomb(buf, L'\0');71ATF_REQUIRE(len == 1);72ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);7374/* Latin letter A. */75memset(buf, 0xcc, sizeof(buf));76len = wctomb(buf, L'A');77ATF_REQUIRE(len == 1);78ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);7980/* Invalid code. */81ATF_REQUIRE(wctomb(buf, UCHAR_MAX + 1) == -1);82ATF_REQUIRE(wctomb(NULL, 0) == 0);8384/*85* Japanese (EUC) locale.86*/8788ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);89ATF_REQUIRE(MB_CUR_MAX == 3);9091/* No shift states in EUC encoding. */92ATF_REQUIRE(wctomb(NULL, L'\0') == 0);9394/* Null wide character. */95memset(buf, 0xcc, sizeof(buf));96len = wctomb(buf, L'\0');97ATF_REQUIRE(len == 1);98ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);99100/* Latin letter A. */101memset(buf, 0xcc, sizeof(buf));102len = wctomb(buf, L'A');103ATF_REQUIRE(len == 1);104ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);105106/* Full width letter A. */107memset(buf, 0xcc, sizeof(buf));108len = wctomb(buf, 0xa3c1);109ATF_REQUIRE(len == 2);110ATF_REQUIRE((unsigned char)buf[0] == 0xa3 &&111(unsigned char)buf[1] == 0xc1 &&112(unsigned char)buf[2] == 0xcc);113}114115ATF_TP_ADD_TCS(tp)116{117118ATF_TP_ADD_TC(tp, euccs1_test);119ATF_TP_ADD_TC(tp, wctomb_test);120121return (atf_no_error());122}123124125