Path: blob/main/lib/libc/tests/locale/c16rtomb_test.c
39491 views
/*-1* Copyright (c) 2002 Tim J. Robbins2* All rights reserved.3*4* Copyright (c) 2013 Ed Schouten <[email protected]>5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/28/*29* Test program for c16rtomb() as specified by ISO/IEC 9899:2011.30*/3132#include <errno.h>33#include <limits.h>34#include <locale.h>35#include <stdio.h>36#include <string.h>37#include <uchar.h>3839#include <atf-c.h>4041static void42require_lc_ctype(const char *locale_name)43{44char *lc_ctype_set;4546lc_ctype_set = setlocale(LC_CTYPE, locale_name);47if (lc_ctype_set == NULL)48atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",49locale_name, errno);5051ATF_REQUIRE(strcmp(lc_ctype_set, locale_name) == 0);52}5354static mbstate_t s;55static char buf[MB_LEN_MAX + 1];5657ATF_TC_WITHOUT_HEAD(c16rtomb_c_locale_test);58ATF_TC_BODY(c16rtomb_c_locale_test, tc)59{6061require_lc_ctype("C");6263/*64* If the buffer argument is NULL, c16 is implicitly 0,65* c16rtomb() resets its internal state.66*/67ATF_REQUIRE(c16rtomb(NULL, L'\0', NULL) == 1);68ATF_REQUIRE(c16rtomb(NULL, 0xdc00, NULL) == 1);6970/* Null wide character. */71memset(&s, 0, sizeof(s));72memset(buf, 0xcc, sizeof(buf));73ATF_REQUIRE(c16rtomb(buf, 0, &s) == 1);74ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);7576/* Latin letter A, internal state. */77ATF_REQUIRE(c16rtomb(NULL, L'\0', NULL) == 1);78ATF_REQUIRE(c16rtomb(NULL, L'A', NULL) == 1);7980/* Latin letter A. */81memset(&s, 0, sizeof(s));82memset(buf, 0xcc, sizeof(buf));83ATF_REQUIRE(c16rtomb(buf, L'A', &s) == 1);84ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);8586/* Unicode character 'Pile of poo'. */87memset(&s, 0, sizeof(s));88memset(buf, 0xcc, sizeof(buf));89ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0);90ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == (size_t)-1);91ATF_REQUIRE(errno == EILSEQ);92ATF_REQUIRE((unsigned char)buf[0] == 0xcc);93}9495ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_1_test);96ATF_TC_BODY(c16rtomb_iso_8859_1_test, tc)97{9899require_lc_ctype("en_US.ISO8859-1");100101/* Unicode character 'Euro sign'. */102memset(&s, 0, sizeof(s));103memset(buf, 0xcc, sizeof(buf));104ATF_REQUIRE(c16rtomb(buf, 0x20ac, &s) == (size_t)-1);105ATF_REQUIRE(errno == EILSEQ);106ATF_REQUIRE((unsigned char)buf[0] == 0xcc);107}108109ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_15_test);110ATF_TC_BODY(c16rtomb_iso_8859_15_test, tc)111{112113require_lc_ctype("en_US.ISO8859-15");114115/* Unicode character 'Euro sign'. */116memset(&s, 0, sizeof(s));117memset(buf, 0xcc, sizeof(buf));118ATF_REQUIRE(c16rtomb(buf, 0x20ac, &s) == 1);119ATF_REQUIRE((unsigned char)buf[0] == 0xa4 && (unsigned char)buf[1] == 0xcc);120}121122ATF_TC_WITHOUT_HEAD(c16rtomb_utf_8_test);123ATF_TC_BODY(c16rtomb_utf_8_test, tc)124{125126require_lc_ctype("en_US.UTF-8");127128/* Unicode character 'Pile of poo'. */129memset(&s, 0, sizeof(s));130memset(buf, 0xcc, sizeof(buf));131ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0);132ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == 4);133ATF_REQUIRE((unsigned char)buf[0] == 0xf0 && (unsigned char)buf[1] == 0x9f &&134(unsigned char)buf[2] == 0x92 && (unsigned char)buf[3] == 0xa9 &&135(unsigned char)buf[4] == 0xcc);136137/* Invalid code; 'Pile of poo' without the trail surrogate. */138memset(&s, 0, sizeof(s));139memset(buf, 0xcc, sizeof(buf));140ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0);141ATF_REQUIRE(c16rtomb(buf, L'A', &s) == (size_t)-1);142ATF_REQUIRE(errno == EILSEQ);143ATF_REQUIRE((unsigned char)buf[0] == 0xcc);144145/* Invalid code; 'Pile of poo' without the lead surrogate. */146memset(&s, 0, sizeof(s));147memset(buf, 0xcc, sizeof(buf));148ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == (size_t)-1);149ATF_REQUIRE(errno == EILSEQ);150ATF_REQUIRE((unsigned char)buf[0] == 0xcc);151}152153ATF_TP_ADD_TCS(tp)154{155156ATF_TP_ADD_TC(tp, c16rtomb_c_locale_test);157ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_1_test);158ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_15_test);159ATF_TP_ADD_TC(tp, c16rtomb_utf_8_test);160161return (atf_no_error());162}163164165