xref: /netbsd-src/tests/lib/libc/locale/t_wcscoll.c (revision 4a641438c8b5d3c29128a3d0a0368f5b5eeea17c)
1*4a641438Sperseant /* $NetBSD: t_wcscoll.c,v 1.1 2017/07/14 14:57:43 perseant Exp $ */
2*4a641438Sperseant 
3*4a641438Sperseant /*-
4*4a641438Sperseant  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5*4a641438Sperseant  * All rights reserved.
6*4a641438Sperseant  *
7*4a641438Sperseant  * This code is derived from software contributed to The NetBSD Foundation
8*4a641438Sperseant  * by Konrad Schroder.
9*4a641438Sperseant  *
10*4a641438Sperseant  * Redistribution and use in source and binary forms, with or without
11*4a641438Sperseant  * modification, are permitted provided that the following conditions
12*4a641438Sperseant  * are met:
13*4a641438Sperseant  * 1. Redistributions of source code must retain the above copyright
14*4a641438Sperseant  *    notice, this list of conditions and the following disclaimer.
15*4a641438Sperseant  * 2. Redistributions in binary form must reproduce the above copyright
16*4a641438Sperseant  *    notice, this list of conditions and the following disclaimer in the
17*4a641438Sperseant  *    documentation and/or other materials provided with the distribution.
18*4a641438Sperseant  *
19*4a641438Sperseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*4a641438Sperseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*4a641438Sperseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*4a641438Sperseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*4a641438Sperseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*4a641438Sperseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*4a641438Sperseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*4a641438Sperseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*4a641438Sperseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*4a641438Sperseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*4a641438Sperseant  * POSSIBILITY OF SUCH DAMAGE.
30*4a641438Sperseant  */
31*4a641438Sperseant 
32*4a641438Sperseant #include <sys/cdefs.h>
33*4a641438Sperseant __COPYRIGHT("@(#) Copyright (c) 2017\
34*4a641438Sperseant  The NetBSD Foundation, inc. All rights reserved.");
35*4a641438Sperseant __RCSID("$NetBSD: t_wcscoll.c,v 1.1 2017/07/14 14:57:43 perseant Exp $");
36*4a641438Sperseant 
37*4a641438Sperseant #include <locale.h>
38*4a641438Sperseant #include <stdio.h>
39*4a641438Sperseant #include <stdlib.h>
40*4a641438Sperseant #include <string.h>
41*4a641438Sperseant #include <limits.h>
42*4a641438Sperseant #include <wchar.h>
43*4a641438Sperseant 
44*4a641438Sperseant #include <atf-c.h>
45*4a641438Sperseant 
46*4a641438Sperseant static struct test {
47*4a641438Sperseant 	const char *locale;
48*4a641438Sperseant 	const wchar_t *in_order[10];
49*4a641438Sperseant } tests[] = {
50*4a641438Sperseant 	{
51*4a641438Sperseant 		"C", {
52*4a641438Sperseant 			L"A string beginning with a"
53*4a641438Sperseant 			L"Capital Letter",
54*4a641438Sperseant 			L"always comes before",
55*4a641438Sperseant 			L"another beginning lowercase",
56*4a641438Sperseant 			L"assuming ASCII of course",
57*4a641438Sperseant 			NULL }
58*4a641438Sperseant 	} , {
59*4a641438Sperseant 		"en_US.UTF-8", {
60*4a641438Sperseant 			L"A string beginning with a"
61*4a641438Sperseant 			L"Capital Letter",
62*4a641438Sperseant 			L"always comes before",
63*4a641438Sperseant 			L"another beginning lowercase",
64*4a641438Sperseant 			L"assuming ASCII of course",
65*4a641438Sperseant 			NULL }
66*4a641438Sperseant 	},
67*4a641438Sperseant 		/*
68*4a641438Sperseant 		 * The rest of these examples are from Wikipedia.
69*4a641438Sperseant 		 */
70*4a641438Sperseant 	{
71*4a641438Sperseant 		"de_DE.UTF-8", {
72*4a641438Sperseant 			L"Arg",
73*4a641438Sperseant 			L"Ärgerlich",
74*4a641438Sperseant 			L"Arm",
75*4a641438Sperseant 			L"Assistent",
76*4a641438Sperseant 			L"Aßlar",
77*4a641438Sperseant 			L"Assoziation",
78*4a641438Sperseant 			NULL }
79*4a641438Sperseant 	}, {
80*4a641438Sperseant 		"ru_RU.KOI-8", {
81*4a641438Sperseant 			L"едок",
82*4a641438Sperseant 			L"ёж",
83*4a641438Sperseant 			L"ездить",
84*4a641438Sperseant 			NULL }
85*4a641438Sperseant 	}, { /* Old-style Spanish collation, expect fail with DUCET */
86*4a641438Sperseant 		"es_ES.UTF-8", {
87*4a641438Sperseant 			L"cinco",
88*4a641438Sperseant 			L"credo",
89*4a641438Sperseant 			L"chispa",
90*4a641438Sperseant 			L"lomo",
91*4a641438Sperseant 			L"luz",
92*4a641438Sperseant 			L"llama",
93*4a641438Sperseant 			NULL }
94*4a641438Sperseant 	}, { /* We don't have Slovak locale files, expect fail */
95*4a641438Sperseant 		"sk_SK.UTF-8", {
96*4a641438Sperseant 			L"baa",
97*4a641438Sperseant 			L"baá",
98*4a641438Sperseant 			L"báa",
99*4a641438Sperseant 			L"bab",
100*4a641438Sperseant 			L"báb",
101*4a641438Sperseant 			L"bac",
102*4a641438Sperseant 			L"bác",
103*4a641438Sperseant 			L"bač",
104*4a641438Sperseant 			L"báč",
105*4a641438Sperseant 			NULL }
106*4a641438Sperseant 	}, {
107*4a641438Sperseant 		NULL,
108*4a641438Sperseant 		{ NULL }
109*4a641438Sperseant 	}
110*4a641438Sperseant };
111*4a641438Sperseant 
112*4a641438Sperseant static void
h_wcscoll(const struct test * t)113*4a641438Sperseant h_wcscoll(const struct test *t)
114*4a641438Sperseant {
115*4a641438Sperseant 	const wchar_t * const *wcp;
116*4a641438Sperseant 	const wchar_t * const *owcp;
117*4a641438Sperseant 
118*4a641438Sperseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
119*4a641438Sperseant 	printf("Trying locale %s...\n", t->locale);
120*4a641438Sperseant 	ATF_REQUIRE(setlocale(LC_COLLATE, t->locale) != NULL);
121*4a641438Sperseant 	printf("Using locale: %s\n", setlocale(LC_ALL, NULL));
122*4a641438Sperseant 
123*4a641438Sperseant 	for (wcp = &t->in_order[0], owcp = wcp++;
124*4a641438Sperseant 	     *wcp != NULL; owcp = wcp++) {
125*4a641438Sperseant 		printf("Check L\"%S\" < L\"%S\"\n", *owcp, *wcp);
126*4a641438Sperseant 		ATF_CHECK(wcscoll(*owcp, *wcp) < 0);
127*4a641438Sperseant 	}
128*4a641438Sperseant }
129*4a641438Sperseant 
130*4a641438Sperseant ATF_TC(wcscoll);
ATF_TC_HEAD(wcscoll,tc)131*4a641438Sperseant ATF_TC_HEAD(wcscoll, tc)
132*4a641438Sperseant {
133*4a641438Sperseant 	atf_tc_set_md_var(tc, "descr",
134*4a641438Sperseant 		"Checks collation using wcscoll(3) under different locales");
135*4a641438Sperseant }
ATF_TC_BODY(wcscoll,tc)136*4a641438Sperseant ATF_TC_BODY(wcscoll, tc)
137*4a641438Sperseant {
138*4a641438Sperseant 	struct test *t;
139*4a641438Sperseant 
140*4a641438Sperseant 	atf_tc_expect_fail("LC_COLLATE support is not yet fully implemented");
141*4a641438Sperseant 	for (t = &tests[0]; t->locale != NULL; ++t)
142*4a641438Sperseant 		h_wcscoll(t);
143*4a641438Sperseant }
144*4a641438Sperseant 
ATF_TP_ADD_TCS(tp)145*4a641438Sperseant ATF_TP_ADD_TCS(tp)
146*4a641438Sperseant {
147*4a641438Sperseant 
148*4a641438Sperseant 	ATF_TP_ADD_TC(tp, wcscoll);
149*4a641438Sperseant 
150*4a641438Sperseant 	return atf_no_error();
151*4a641438Sperseant }
152