1/* $NetBSD: t_digittoint.c,v 1.3 2022/05/24 20:50:20 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_digittoint.c,v 1.3 2022/05/24 20:50:20 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
45/* Use this until we have a better way to tell if it is defined */
46#ifdef digittoint
47# define DIGITTOINT_DEFINED
48#endif
49
50static struct test {
51	const char *locale;
52	const char *digits;
53} tests[] = {
54	{
55		"C",
56		"0123456789AbcDeF",
57	}, {
58		"en_US.UTF-8",
59		"0123456789AbcDeF",
60	}, {
61		"ru_RU.KOI-8",
62		"0123456789AbcDeF",
63	}, {
64		NULL,
65		NULL,
66	}
67};
68
69#ifdef DIGITTOINT_DEFINED
70static void
71h_digittoint(const struct test *t)
72{
73	int i;
74
75	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
76	printf("Trying locale %s...\n", t->locale);
77	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
78
79	for (i = 0; i < 16; i++) {
80		printf(" char %2.2x in position %d\n", t->digits[i], i);
81		ATF_REQUIRE_EQ(digittoint(t->digits[i]), i);
82	}
83}
84#endif /* DIGITTOINT_DEFINED */
85
86ATF_TC(digittoint);
87
88ATF_TC_HEAD(digittoint, tc)
89{
90	atf_tc_set_md_var(tc, "descr",
91		"Checks digittoint under different locales");
92}
93
94ATF_TC_BODY(digittoint, tc)
95{
96	struct test *t;
97
98#ifdef DIGITTOINT_DEFINED
99	for (t = &tests[0]; t->locale != NULL; ++t)
100		h_digittoint(t);
101#else /* ! DIGITTOINT_DEFINED */
102	atf_tc_skip("digittoint(3) not present to test");
103#endif /* DIGITTOINT_DEFINED */
104}
105
106ATF_TP_ADD_TCS(tp)
107{
108
109	ATF_TP_ADD_TC(tp, digittoint);
110
111	return atf_no_error();
112}
113