1/* $NetBSD: t_toupper.c,v 1.2 2021/08/02 17:41:07 andvar Exp $ */
2
3/*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad Schroder
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__COPYRIGHT("@(#) Copyright (c) 2017\
34 The NetBSD Foundation, inc. All rights reserved.");
35__RCSID("$NetBSD: t_toupper.c,v 1.2 2021/08/02 17:41:07 andvar Exp $");
36
37#include <locale.h>
38#include <stdio.h>
39#include <string.h>
40#include <vis.h>
41#include <ctype.h>
42
43#include <atf-c.h>
44
45static struct test {
46	const char *locale;
47	const char *lower;
48	const char *upper;
49} tests[] = {
50	{
51		"C",
52		"abcde12345",
53		"ABCDE12345",
54	}, {
55		"ru_RU.KOI8-R",
56		"abcde12345\xc1\xc2\xd7\xc7\xc4\xc5\xa3",
57		"ABCDE12345\xe1\xe2\xf7\xe7\xe4\xe5\xb3",
58	}, {
59		NULL,
60		NULL,
61		NULL,
62	}
63};
64
65static void
66h_swapcase(const struct test *t, int upperp)
67{
68	unsigned int i;
69	unsigned char answer, reported;
70
71	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
72	printf("Trying locale %s...\n", t->locale);
73	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
74
75	for (i = 0; i < strlen(t->lower); i++) {
76		printf("Comparing char %d, lower %2.2x, with upper %2.2x\n",
77			i, (unsigned char)t->lower[i], (unsigned char)t->upper[i]);
78		if (upperp) {
79			answer = t->upper[i];
80			reported = toupper((int)(unsigned char)t->lower[i]);
81		} else {
82			answer = t->lower[i];
83			reported = tolower((int)(unsigned char)t->upper[i]);
84		}
85		printf("  expecting %2.2x, reported %2.2x\n", answer, reported);
86		ATF_REQUIRE_EQ(reported, answer);
87	}
88}
89
90ATF_TC(toupper);
91
92ATF_TC_HEAD(toupper, tc)
93{
94	atf_tc_set_md_var(tc, "descr",
95		"Checks toupper under different locales");
96}
97
98ATF_TC_BODY(toupper, tc)
99{
100	struct test *t;
101
102	for (t = &tests[0]; t->locale != NULL; ++t)
103		h_swapcase(t, 1);
104}
105
106ATF_TC(tolower);
107
108ATF_TC_HEAD(tolower, tc)
109{
110	atf_tc_set_md_var(tc, "descr",
111		"Checks tolower under different locales");
112}
113
114ATF_TC_BODY(tolower, tc)
115{
116	struct test *t;
117
118	/* atf_tc_expect_fail("%s", "LC_COLLATE not supported"); */
119	for (t = &tests[0]; t->locale != NULL; ++t)
120		h_swapcase(t, 0);
121	/* atf_tc_expect_pass(); */
122}
123
124ATF_TP_ADD_TCS(tp)
125{
126
127	ATF_TP_ADD_TC(tp, toupper);
128	ATF_TP_ADD_TC(tp, tolower);
129
130	return atf_no_error();
131}
132