t_toupper.c revision 1.1
11.1Sperseant/* $NetBSD: t_toupper.c,v 1.1 2017/05/30 02:11:03 perseant Exp $ */
21.1Sperseant
31.1Sperseant/*-
41.1Sperseant * Copyright (c) 2017 The NetBSD Foundation, Inc.
51.1Sperseant * All rights reserved.
61.1Sperseant *
71.1Sperseant * This code is derived from software contributed to The NetBSD Foundation
81.1Sperseant * by Konrad Schroder
91.1Sperseant *
101.1Sperseant * Redistribution and use in source and binary forms, with or without
111.1Sperseant * modification, are permitted provided that the following conditions
121.1Sperseant * are met:
131.1Sperseant * 1. Redistributions of source code must retain the above copyright
141.1Sperseant *    notice, this list of conditions and the following disclaimer.
151.1Sperseant * 2. Redistributions in binary form must reproduce the above copyright
161.1Sperseant *    notice, this list of conditions and the following disclaimer in the
171.1Sperseant *    documentation and/or other materials provided with the distribution.
181.1Sperseant *
191.1Sperseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sperseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sperseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sperseant * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sperseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sperseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sperseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sperseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sperseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sperseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sperseant * POSSIBILITY OF SUCH DAMAGE.
301.1Sperseant */
311.1Sperseant
321.1Sperseant#include <sys/cdefs.h>
331.1Sperseant__COPYRIGHT("@(#) Copyright (c) 2017\
341.1Sperseant The NetBSD Foundation, inc. All rights reserved.");
351.1Sperseant__RCSID("$NetBSD: t_toupper.c,v 1.1 2017/05/30 02:11:03 perseant Exp $");
361.1Sperseant
371.1Sperseant#include <locale.h>
381.1Sperseant#include <stdio.h>
391.1Sperseant#include <string.h>
401.1Sperseant#include <vis.h>
411.1Sperseant#include <ctype.h>
421.1Sperseant
431.1Sperseant#include <atf-c.h>
441.1Sperseant
451.1Sperseantstatic struct test {
461.1Sperseant	const char *locale;
471.1Sperseant	const char *lower;
481.1Sperseant	const char *upper;
491.1Sperseant} tests[] = {
501.1Sperseant	{
511.1Sperseant		"C",
521.1Sperseant		"abcde12345",
531.1Sperseant		"ABCDE12345",
541.1Sperseant	}, {
551.1Sperseant		"ru_RU.KOI8-R",
561.1Sperseant		"abcde12345\xc1\xc2\xd7\xc7\xc4\xc5\xa3",
571.1Sperseant		"ABCDE12345\xe1\xe2\xf7\xe7\xe4\xe5\xb3",
581.1Sperseant	}, {
591.1Sperseant		NULL,
601.1Sperseant		NULL,
611.1Sperseant		NULL,
621.1Sperseant	}
631.1Sperseant};
641.1Sperseant
651.1Sperseantstatic void
661.1Sperseanth_swapcase(const struct test *t, int upperp)
671.1Sperseant{
681.1Sperseant	unsigned int i;
691.1Sperseant	unsigned char answer, reported;
701.1Sperseant
711.1Sperseant	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
721.1Sperseant	printf("Trying locale %s...\n", t->locale);
731.1Sperseant	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
741.1Sperseant
751.1Sperseant	for (i = 0; i < strlen(t->lower); i++) {
761.1Sperseant		printf("Comparing char %d, lower %2.2x, with upper %2.2x\n",
771.1Sperseant			i, (unsigned char)t->lower[i], (unsigned char)t->upper[i]);
781.1Sperseant		if (upperp) {
791.1Sperseant			answer = t->upper[i];
801.1Sperseant			reported = toupper((int)(unsigned char)t->lower[i]);
811.1Sperseant		} else {
821.1Sperseant			answer = t->lower[i];
831.1Sperseant			reported = tolower((int)(unsigned char)t->upper[i]);
841.1Sperseant		}
851.1Sperseant		printf("  expecting %2.2x, reported %2.2x\n", answer, reported);
861.1Sperseant		ATF_REQUIRE_EQ(reported, answer);
871.1Sperseant	}
881.1Sperseant}
891.1Sperseant
901.1SperseantATF_TC(toupper);
911.1Sperseant
921.1SperseantATF_TC_HEAD(toupper, tc)
931.1Sperseant{
941.1Sperseant	atf_tc_set_md_var(tc, "descr",
951.1Sperseant		"Checks toupper under diferent locales");
961.1Sperseant}
971.1Sperseant
981.1SperseantATF_TC_BODY(toupper, tc)
991.1Sperseant{
1001.1Sperseant	struct test *t;
1011.1Sperseant
1021.1Sperseant	for (t = &tests[0]; t->locale != NULL; ++t)
1031.1Sperseant		h_swapcase(t, 1);
1041.1Sperseant}
1051.1Sperseant
1061.1SperseantATF_TC(tolower);
1071.1Sperseant
1081.1SperseantATF_TC_HEAD(tolower, tc)
1091.1Sperseant{
1101.1Sperseant	atf_tc_set_md_var(tc, "descr",
1111.1Sperseant		"Checks tolower under diferent locales");
1121.1Sperseant}
1131.1Sperseant
1141.1SperseantATF_TC_BODY(tolower, tc)
1151.1Sperseant{
1161.1Sperseant	struct test *t;
1171.1Sperseant
1181.1Sperseant	/* atf_tc_expect_fail("%s", "LC_COLLATE not supported"); */
1191.1Sperseant	for (t = &tests[0]; t->locale != NULL; ++t)
1201.1Sperseant		h_swapcase(t, 0);
1211.1Sperseant	/* atf_tc_expect_pass(); */
1221.1Sperseant}
1231.1Sperseant
1241.1SperseantATF_TP_ADD_TCS(tp)
1251.1Sperseant{
1261.1Sperseant
1271.1Sperseant	ATF_TP_ADD_TC(tp, toupper);
1281.1Sperseant	ATF_TP_ADD_TC(tp, tolower);
1291.1Sperseant
1301.1Sperseant	return atf_no_error();
1311.1Sperseant}
132