#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2017\
The NetBSD Foundation, inc. All rights reserved.");
__RCSID("$NetBSD: t_btowc.c,v 1.3 2017/08/10 19:08:43 perseant Exp $");
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <wchar.h>
#include <atf-c.h>
struct test {
const char *locale;
const char *illegal;
const char *legal;
const wchar_t wlegal[8];
const wchar_t willegal[8];
} tests[] = {
{
"en_US.UTF-8",
"\200",
"ABC123@\t",
{ 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
{ 0xfdd0, 0x10fffe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
},
{
"ru_RU.KOI8-R",
"",
"A\xc2\xd7\xc7\xc4\xc5\xa3",
{ 'A', 0x0431, 0x432, 0x0433, 0x0434, 0x0435, 0x0451, 0x0 },
{ 0x00c5, 0x00e6, 0x00fe, 0x0630, 0x06fc, 0x56cd, 0x0, 0x0 }
},
{
NULL,
NULL,
NULL,
{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
},
};
#ifdef __STDC_ISO_10646__
static void
h_iso10646(struct test *t)
{
const char *cp;
int c, wc;
char *str;
const wchar_t *wcp;
ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
printf("Trying locale: %s\n", t->locale);
ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
(void)printf("Using locale: %s\n", str);
for (cp = t->legal, wcp = t->wlegal; *cp != '\0'; ++cp, ++wcp) {
c = (int)(unsigned char)*cp;
printf("Checking legal character 0x%x\n", c);
wc = btowc(c);
if (errno != 0)
printf(" btowc() failed with errno=%d\n", errno);
printf("btowc(0x%2.2x) = 0x%x, expecting 0x%x\n",
c, wc, *wcp);
ATF_REQUIRE(btowc(c) == *wcp);
}
for (wcp = t->willegal; *wcp != '\0'; ++wcp) {
printf("Checking illegal wide character 0x%lx\n",
(unsigned long)*wcp);
ATF_REQUIRE_EQ(wctob(*wcp), EOF);
}
}
#endif
static void
h_btowc(struct test *t)
{
const char *cp;
unsigned char c;
char *str;
const wchar_t *wcp;
ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
printf("Trying locale: %s\n", t->locale);
ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
(void)printf("Using locale: %s\n", str);
ATF_REQUIRE_EQ(btowc(EOF), WEOF);
ATF_REQUIRE_EQ(wctob(WEOF), EOF);
for (cp = t->illegal; *cp != '\0'; ++cp) {
printf("Checking illegal character 0x%x\n",
(unsigned char)*cp);
ATF_REQUIRE_EQ(btowc(*cp), WEOF);
}
for (cp = t->legal; *cp != '\0'; ++cp) {
c = (unsigned char)*cp;
printf("Checking legal character 0x%x\n", c);
ATF_REQUIRE(btowc(c) != WEOF);
printf("0x%x -> wide 0x%x -> 0x%x\n",
c, btowc(c), (unsigned char)wctob(btowc(c)));
ATF_REQUIRE_EQ(wctob(btowc(c)), c);
}
}
ATF_TC(btowc);
ATF_TC_HEAD(btowc, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3)");
}
ATF_TC_BODY(btowc, tc)
{
struct test *t;
for (t = tests; t->locale != NULL; ++t)
h_btowc(t);
}
ATF_TC(stdc_iso_10646);
ATF_TC_HEAD(stdc_iso_10646, tc)
{
atf_tc_set_md_var(tc, "descr",
"Checks btowc(3) conversion to ISO10646");
}
ATF_TC_BODY(stdc_iso_10646, tc)
{
struct test *t;
#ifdef __STDC_ISO_10646__
for (t = tests; t->locale != NULL; ++t)
h_iso10646(t);
#else
atf_tc_skip("__STDC_ISO_10646__ not defined");
#endif
}
ATF_TC(btowc_posix);
ATF_TC_HEAD(btowc_posix, tc)
{
atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3) for POSIX locale");
}
ATF_TC_BODY(btowc_posix, tc)
{
const char *cp;
unsigned char c;
char *str;
const wchar_t *wcp;
int i;
ATF_REQUIRE_STREQ(setlocale(LC_ALL, "POSIX"), "POSIX");
ATF_REQUIRE_EQ(btowc(EOF), WEOF);
ATF_REQUIRE_EQ(wctob(WEOF), EOF);
for (i = 0; i <= 255; i++) {
ATF_REQUIRE_EQ(btowc(i), (wchar_t)(unsigned char)(i));
ATF_REQUIRE_EQ((unsigned char)wctob(i), (wchar_t)i);
}
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, btowc);
ATF_TP_ADD_TC(tp, btowc_posix);
ATF_TP_ADD_TC(tp, stdc_iso_10646);
return atf_no_error();
}