1/* $NetBSD: t_wcscoll.c,v 1.1 2017/07/14 14:57:43 perseant 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_wcscoll.c,v 1.1 2017/07/14 14:57:43 perseant Exp $");
36
37#include <locale.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <limits.h>
42#include <wchar.h>
43
44#include <atf-c.h>
45
46static struct test {
47	const char *locale;
48	const wchar_t *in_order[10];
49} tests[] = {
50	{
51		"C", {
52			L"A string beginning with a"
53			L"Capital Letter",
54			L"always comes before",
55			L"another beginning lowercase",
56			L"assuming ASCII of course",
57			NULL }
58	} , {
59		"en_US.UTF-8", {
60			L"A string beginning with a"
61			L"Capital Letter",
62			L"always comes before",
63			L"another beginning lowercase",
64			L"assuming ASCII of course",
65			NULL }
66	},
67		/*
68		 * The rest of these examples are from Wikipedia.
69		 */
70	{
71		"de_DE.UTF-8", {
72			L"Arg",
73			L"��rgerlich",
74			L"Arm",
75			L"Assistent",
76			L"A��lar",
77			L"Assoziation",
78			NULL }
79	}, {
80		"ru_RU.KOI-8", {
81			L"��������",
82			L"����",
83			L"������������",
84			NULL }
85	}, { /* Old-style Spanish collation, expect fail with DUCET */
86		"es_ES.UTF-8", {
87			L"cinco",
88			L"credo",
89			L"chispa",
90			L"lomo",
91			L"luz",
92			L"llama",
93			NULL }
94	}, { /* We don't have Slovak locale files, expect fail */
95		"sk_SK.UTF-8", {
96			L"baa",
97			L"ba��",
98			L"b��a",
99			L"bab",
100			L"b��b",
101			L"bac",
102			L"b��c",
103			L"ba��",
104			L"b����",
105			NULL }
106	}, {
107		NULL,
108		{ NULL }
109	}
110};
111
112static void
113h_wcscoll(const struct test *t)
114{
115	const wchar_t * const *wcp;
116	const wchar_t * const *owcp;
117
118	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
119	printf("Trying locale %s...\n", t->locale);
120	ATF_REQUIRE(setlocale(LC_COLLATE, t->locale) != NULL);
121	printf("Using locale: %s\n", setlocale(LC_ALL, NULL));
122
123	for (wcp = &t->in_order[0], owcp = wcp++;
124	     *wcp != NULL; owcp = wcp++) {
125		printf("Check L\"%S\" < L\"%S\"\n", *owcp, *wcp);
126		ATF_CHECK(wcscoll(*owcp, *wcp) < 0);
127	}
128}
129
130ATF_TC(wcscoll);
131ATF_TC_HEAD(wcscoll, tc)
132{
133	atf_tc_set_md_var(tc, "descr",
134		"Checks collation using wcscoll(3) under different locales");
135}
136ATF_TC_BODY(wcscoll, tc)
137{
138	struct test *t;
139
140	atf_tc_expect_fail("LC_COLLATE support is not yet fully implemented");
141	for (t = &tests[0]; t->locale != NULL; ++t)
142		h_wcscoll(t);
143}
144
145ATF_TP_ADD_TCS(tp)
146{
147
148	ATF_TP_ADD_TC(tp, wcscoll);
149
150	return atf_no_error();
151}
152