xref: /netbsd-src/tests/lib/libc/string/t_strcoll.c (revision 02698c5b7fae8667658bdf0407f7c8448c10f964)
1 /* $NetBSD: t_strcoll.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_strcoll.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 
42 #include <atf-c.h>
43 
44 static struct test {
45 	const char *locale;
46 	const char * const data[5];
47 } tests[] = {
48 	{
49 		"C",
50 		{ "aardvark", "absolution", "zyzygy", NULL },
51 	}, {
52 		"ru_RU.KOI8-R",
53 		{ "\xc5\xc4\xcf\xcb", "\xa3\xd6", "\xc5\xda\xc4\xc9\xd4\xd8", NULL },
54 	}, {
55 		NULL,
56 		{ NULL, NULL, NULL, NULL },
57 	}
58 };
59 
60 static void
h_ordering(const struct test * t)61 h_ordering(const struct test *t)
62 {
63 	const char * const *a;
64 	const char * const *b;
65 	char buf_a[1024], buf_b[1024];
66 
67 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
68 	printf("Trying locale %s...\n", t->locale);
69 	ATF_REQUIRE(setlocale(LC_COLLATE, t->locale) != NULL);
70 
71 	for (a = t->data; *a != NULL; ++a) {
72 		strvis(buf_a, *a, VIS_WHITE | VIS_OCTAL);
73 		for (b = a + 1; *b != NULL; ++b) {
74 			strvis(buf_b, *b, VIS_WHITE | VIS_OCTAL);
75 			printf("Checking \"%s\" < \"%s\"\n", buf_a, buf_b);
76 			ATF_REQUIRE(strcoll(*a, *b) < 0);
77 			printf("...good\n");
78 		}
79 	}
80 }
81 
82 ATF_TC(ordering);
83 
ATF_TC_HEAD(ordering,tc)84 ATF_TC_HEAD(ordering, tc)
85 {
86 	atf_tc_set_md_var(tc, "descr",
87 		"Checks collation ordering under different locales");
88 }
89 
ATF_TC_BODY(ordering,tc)90 ATF_TC_BODY(ordering, tc)
91 {
92 	struct test *t;
93 
94 	atf_tc_expect_fail("%s", "LC_COLLATE not supported");
95 	for (t = &tests[0]; t->locale != NULL; ++t)
96 		h_ordering(t);
97 	atf_tc_expect_pass();
98 }
99 
ATF_TP_ADD_TCS(tp)100 ATF_TP_ADD_TCS(tp)
101 {
102 
103 	ATF_TP_ADD_TC(tp, ordering);
104 
105 	return atf_no_error();
106 }
107