xref: /netbsd-src/tests/lib/libc/locale/t_digittoint.c (revision cdc507f0d27576e5c4f30629a5cfe9e1f5271dbe)
1*cdc507f0Sandvar /* $NetBSD: t_digittoint.c,v 1.3 2022/05/24 20:50:20 andvar Exp $ */
2ce11903fSperseant 
3ce11903fSperseant /*-
4ce11903fSperseant  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5ce11903fSperseant  * All rights reserved.
6ce11903fSperseant  *
7ce11903fSperseant  * This code is derived from software contributed to The NetBSD Foundation
8ce11903fSperseant  * by Konrad Schroder
9ce11903fSperseant  *
10ce11903fSperseant  * Redistribution and use in source and binary forms, with or without
11ce11903fSperseant  * modification, are permitted provided that the following conditions
12ce11903fSperseant  * are met:
13ce11903fSperseant  * 1. Redistributions of source code must retain the above copyright
14ce11903fSperseant  *    notice, this list of conditions and the following disclaimer.
15ce11903fSperseant  * 2. Redistributions in binary form must reproduce the above copyright
16ce11903fSperseant  *    notice, this list of conditions and the following disclaimer in the
17ce11903fSperseant  *    documentation and/or other materials provided with the distribution.
18ce11903fSperseant  *
19ce11903fSperseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20ce11903fSperseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21ce11903fSperseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22ce11903fSperseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23ce11903fSperseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ce11903fSperseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ce11903fSperseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ce11903fSperseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ce11903fSperseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ce11903fSperseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ce11903fSperseant  * POSSIBILITY OF SUCH DAMAGE.
30ce11903fSperseant  */
31ce11903fSperseant 
32ce11903fSperseant #include <sys/cdefs.h>
33ce11903fSperseant __COPYRIGHT("@(#) Copyright (c) 2017\
34ce11903fSperseant  The NetBSD Foundation, inc. All rights reserved.");
35*cdc507f0Sandvar __RCSID("$NetBSD: t_digittoint.c,v 1.3 2022/05/24 20:50:20 andvar Exp $");
36ce11903fSperseant 
37ce11903fSperseant #include <locale.h>
38ce11903fSperseant #include <stdio.h>
39ce11903fSperseant #include <string.h>
40ce11903fSperseant #include <vis.h>
41ce11903fSperseant #include <ctype.h>
42ce11903fSperseant 
43ce11903fSperseant #include <atf-c.h>
44ce11903fSperseant 
45d4f6523bSperseant /* Use this until we have a better way to tell if it is defined */
46d4f6523bSperseant #ifdef digittoint
47d4f6523bSperseant # define DIGITTOINT_DEFINED
48d4f6523bSperseant #endif
49d4f6523bSperseant 
50ce11903fSperseant static struct test {
51ce11903fSperseant 	const char *locale;
52ce11903fSperseant 	const char *digits;
53ce11903fSperseant } tests[] = {
54ce11903fSperseant 	{
55ce11903fSperseant 		"C",
56ce11903fSperseant 		"0123456789AbcDeF",
57ce11903fSperseant 	}, {
58ce11903fSperseant 		"en_US.UTF-8",
59ce11903fSperseant 		"0123456789AbcDeF",
60ce11903fSperseant 	}, {
61ce11903fSperseant 		"ru_RU.KOI-8",
62ce11903fSperseant 		"0123456789AbcDeF",
63ce11903fSperseant 	}, {
64ce11903fSperseant 		NULL,
65ce11903fSperseant 		NULL,
66ce11903fSperseant 	}
67ce11903fSperseant };
68ce11903fSperseant 
69d4f6523bSperseant #ifdef DIGITTOINT_DEFINED
70ce11903fSperseant static void
h_digittoint(const struct test * t)71ce11903fSperseant h_digittoint(const struct test *t)
72ce11903fSperseant {
73ce11903fSperseant 	int i;
74ce11903fSperseant 
75ce11903fSperseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
76ce11903fSperseant 	printf("Trying locale %s...\n", t->locale);
77ce11903fSperseant 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
78ce11903fSperseant 
79ce11903fSperseant 	for (i = 0; i < 16; i++) {
80ce11903fSperseant 		printf(" char %2.2x in position %d\n", t->digits[i], i);
81ce11903fSperseant 		ATF_REQUIRE_EQ(digittoint(t->digits[i]), i);
82ce11903fSperseant 	}
83ce11903fSperseant }
84d4f6523bSperseant #endif /* DIGITTOINT_DEFINED */
85ce11903fSperseant 
86ce11903fSperseant ATF_TC(digittoint);
87ce11903fSperseant 
ATF_TC_HEAD(digittoint,tc)88ce11903fSperseant ATF_TC_HEAD(digittoint, tc)
89ce11903fSperseant {
90ce11903fSperseant 	atf_tc_set_md_var(tc, "descr",
91*cdc507f0Sandvar 		"Checks digittoint under different locales");
92ce11903fSperseant }
93ce11903fSperseant 
ATF_TC_BODY(digittoint,tc)94ce11903fSperseant ATF_TC_BODY(digittoint, tc)
95ce11903fSperseant {
96ce11903fSperseant 	struct test *t;
97ce11903fSperseant 
98d4f6523bSperseant #ifdef DIGITTOINT_DEFINED
99ce11903fSperseant 	for (t = &tests[0]; t->locale != NULL; ++t)
100ce11903fSperseant 		h_digittoint(t);
101d4f6523bSperseant #else /* ! DIGITTOINT_DEFINED */
102d4f6523bSperseant 	atf_tc_skip("digittoint(3) not present to test");
103d4f6523bSperseant #endif /* DIGITTOINT_DEFINED */
104ce11903fSperseant }
105ce11903fSperseant 
ATF_TP_ADD_TCS(tp)106ce11903fSperseant ATF_TP_ADD_TCS(tp)
107ce11903fSperseant {
108ce11903fSperseant 
109ce11903fSperseant 	ATF_TP_ADD_TC(tp, digittoint);
110ce11903fSperseant 
111ce11903fSperseant 	return atf_no_error();
112ce11903fSperseant }
113