xref: /netbsd-src/tests/lib/libc/locale/t_toupper.c (revision 02698c5b7fae8667658bdf0407f7c8448c10f964)
1*02698c5bSandvar /* $NetBSD: t_toupper.c,v 1.2 2021/08/02 17:41:07 andvar Exp $ */
24f17535aSperseant 
34f17535aSperseant /*-
44f17535aSperseant  * Copyright (c) 2017 The NetBSD Foundation, Inc.
54f17535aSperseant  * All rights reserved.
64f17535aSperseant  *
74f17535aSperseant  * This code is derived from software contributed to The NetBSD Foundation
84f17535aSperseant  * by Konrad Schroder
94f17535aSperseant  *
104f17535aSperseant  * Redistribution and use in source and binary forms, with or without
114f17535aSperseant  * modification, are permitted provided that the following conditions
124f17535aSperseant  * are met:
134f17535aSperseant  * 1. Redistributions of source code must retain the above copyright
144f17535aSperseant  *    notice, this list of conditions and the following disclaimer.
154f17535aSperseant  * 2. Redistributions in binary form must reproduce the above copyright
164f17535aSperseant  *    notice, this list of conditions and the following disclaimer in the
174f17535aSperseant  *    documentation and/or other materials provided with the distribution.
184f17535aSperseant  *
194f17535aSperseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204f17535aSperseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214f17535aSperseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224f17535aSperseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234f17535aSperseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244f17535aSperseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254f17535aSperseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264f17535aSperseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274f17535aSperseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284f17535aSperseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294f17535aSperseant  * POSSIBILITY OF SUCH DAMAGE.
304f17535aSperseant  */
314f17535aSperseant 
324f17535aSperseant #include <sys/cdefs.h>
334f17535aSperseant __COPYRIGHT("@(#) Copyright (c) 2017\
344f17535aSperseant  The NetBSD Foundation, inc. All rights reserved.");
35*02698c5bSandvar __RCSID("$NetBSD: t_toupper.c,v 1.2 2021/08/02 17:41:07 andvar Exp $");
364f17535aSperseant 
374f17535aSperseant #include <locale.h>
384f17535aSperseant #include <stdio.h>
394f17535aSperseant #include <string.h>
404f17535aSperseant #include <vis.h>
414f17535aSperseant #include <ctype.h>
424f17535aSperseant 
434f17535aSperseant #include <atf-c.h>
444f17535aSperseant 
454f17535aSperseant static struct test {
464f17535aSperseant 	const char *locale;
474f17535aSperseant 	const char *lower;
484f17535aSperseant 	const char *upper;
494f17535aSperseant } tests[] = {
504f17535aSperseant 	{
514f17535aSperseant 		"C",
524f17535aSperseant 		"abcde12345",
534f17535aSperseant 		"ABCDE12345",
544f17535aSperseant 	}, {
554f17535aSperseant 		"ru_RU.KOI8-R",
564f17535aSperseant 		"abcde12345\xc1\xc2\xd7\xc7\xc4\xc5\xa3",
574f17535aSperseant 		"ABCDE12345\xe1\xe2\xf7\xe7\xe4\xe5\xb3",
584f17535aSperseant 	}, {
594f17535aSperseant 		NULL,
604f17535aSperseant 		NULL,
614f17535aSperseant 		NULL,
624f17535aSperseant 	}
634f17535aSperseant };
644f17535aSperseant 
654f17535aSperseant static void
h_swapcase(const struct test * t,int upperp)664f17535aSperseant h_swapcase(const struct test *t, int upperp)
674f17535aSperseant {
684f17535aSperseant 	unsigned int i;
694f17535aSperseant 	unsigned char answer, reported;
704f17535aSperseant 
714f17535aSperseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
724f17535aSperseant 	printf("Trying locale %s...\n", t->locale);
734f17535aSperseant 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
744f17535aSperseant 
754f17535aSperseant 	for (i = 0; i < strlen(t->lower); i++) {
764f17535aSperseant 		printf("Comparing char %d, lower %2.2x, with upper %2.2x\n",
774f17535aSperseant 			i, (unsigned char)t->lower[i], (unsigned char)t->upper[i]);
784f17535aSperseant 		if (upperp) {
794f17535aSperseant 			answer = t->upper[i];
804f17535aSperseant 			reported = toupper((int)(unsigned char)t->lower[i]);
814f17535aSperseant 		} else {
824f17535aSperseant 			answer = t->lower[i];
834f17535aSperseant 			reported = tolower((int)(unsigned char)t->upper[i]);
844f17535aSperseant 		}
854f17535aSperseant 		printf("  expecting %2.2x, reported %2.2x\n", answer, reported);
864f17535aSperseant 		ATF_REQUIRE_EQ(reported, answer);
874f17535aSperseant 	}
884f17535aSperseant }
894f17535aSperseant 
904f17535aSperseant ATF_TC(toupper);
914f17535aSperseant 
ATF_TC_HEAD(toupper,tc)924f17535aSperseant ATF_TC_HEAD(toupper, tc)
934f17535aSperseant {
944f17535aSperseant 	atf_tc_set_md_var(tc, "descr",
95*02698c5bSandvar 		"Checks toupper under different locales");
964f17535aSperseant }
974f17535aSperseant 
ATF_TC_BODY(toupper,tc)984f17535aSperseant ATF_TC_BODY(toupper, tc)
994f17535aSperseant {
1004f17535aSperseant 	struct test *t;
1014f17535aSperseant 
1024f17535aSperseant 	for (t = &tests[0]; t->locale != NULL; ++t)
1034f17535aSperseant 		h_swapcase(t, 1);
1044f17535aSperseant }
1054f17535aSperseant 
1064f17535aSperseant ATF_TC(tolower);
1074f17535aSperseant 
ATF_TC_HEAD(tolower,tc)1084f17535aSperseant ATF_TC_HEAD(tolower, tc)
1094f17535aSperseant {
1104f17535aSperseant 	atf_tc_set_md_var(tc, "descr",
111*02698c5bSandvar 		"Checks tolower under different locales");
1124f17535aSperseant }
1134f17535aSperseant 
ATF_TC_BODY(tolower,tc)1144f17535aSperseant ATF_TC_BODY(tolower, tc)
1154f17535aSperseant {
1164f17535aSperseant 	struct test *t;
1174f17535aSperseant 
1184f17535aSperseant 	/* atf_tc_expect_fail("%s", "LC_COLLATE not supported"); */
1194f17535aSperseant 	for (t = &tests[0]; t->locale != NULL; ++t)
1204f17535aSperseant 		h_swapcase(t, 0);
1214f17535aSperseant 	/* atf_tc_expect_pass(); */
1224f17535aSperseant }
1234f17535aSperseant 
ATF_TP_ADD_TCS(tp)1244f17535aSperseant ATF_TP_ADD_TCS(tp)
1254f17535aSperseant {
1264f17535aSperseant 
1274f17535aSperseant 	ATF_TP_ADD_TC(tp, toupper);
1284f17535aSperseant 	ATF_TP_ADD_TC(tp, tolower);
1294f17535aSperseant 
1304f17535aSperseant 	return atf_no_error();
1314f17535aSperseant }
132