xref: /netbsd-src/tests/lib/libc/string/t_strcoll.c (revision 02698c5b7fae8667658bdf0407f7c8448c10f964)
1*02698c5bSandvar /* $NetBSD: t_strcoll.c,v 1.2 2021/08/02 17:41:07 andvar Exp $ */
24e3374a7Sperseant 
34e3374a7Sperseant /*-
44e3374a7Sperseant  * Copyright (c) 2017 The NetBSD Foundation, Inc.
54e3374a7Sperseant  * All rights reserved.
64e3374a7Sperseant  *
74e3374a7Sperseant  * This code is derived from software contributed to The NetBSD Foundation
84e3374a7Sperseant  * by Konrad Schroder
94e3374a7Sperseant  *
104e3374a7Sperseant  * Redistribution and use in source and binary forms, with or without
114e3374a7Sperseant  * modification, are permitted provided that the following conditions
124e3374a7Sperseant  * are met:
134e3374a7Sperseant  * 1. Redistributions of source code must retain the above copyright
144e3374a7Sperseant  *    notice, this list of conditions and the following disclaimer.
154e3374a7Sperseant  * 2. Redistributions in binary form must reproduce the above copyright
164e3374a7Sperseant  *    notice, this list of conditions and the following disclaimer in the
174e3374a7Sperseant  *    documentation and/or other materials provided with the distribution.
184e3374a7Sperseant  *
194e3374a7Sperseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204e3374a7Sperseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214e3374a7Sperseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224e3374a7Sperseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234e3374a7Sperseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244e3374a7Sperseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254e3374a7Sperseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264e3374a7Sperseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274e3374a7Sperseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284e3374a7Sperseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294e3374a7Sperseant  * POSSIBILITY OF SUCH DAMAGE.
304e3374a7Sperseant  */
314e3374a7Sperseant 
324e3374a7Sperseant #include <sys/cdefs.h>
334e3374a7Sperseant __COPYRIGHT("@(#) Copyright (c) 2017\
344e3374a7Sperseant  The NetBSD Foundation, inc. All rights reserved.");
35*02698c5bSandvar __RCSID("$NetBSD: t_strcoll.c,v 1.2 2021/08/02 17:41:07 andvar Exp $");
364e3374a7Sperseant 
374e3374a7Sperseant #include <locale.h>
384e3374a7Sperseant #include <stdio.h>
394e3374a7Sperseant #include <string.h>
404e3374a7Sperseant #include <vis.h>
414e3374a7Sperseant 
424e3374a7Sperseant #include <atf-c.h>
434e3374a7Sperseant 
444e3374a7Sperseant static struct test {
454e3374a7Sperseant 	const char *locale;
464e3374a7Sperseant 	const char * const data[5];
474e3374a7Sperseant } tests[] = {
484e3374a7Sperseant 	{
494e3374a7Sperseant 		"C",
504e3374a7Sperseant 		{ "aardvark", "absolution", "zyzygy", NULL },
514e3374a7Sperseant 	}, {
524e3374a7Sperseant 		"ru_RU.KOI8-R",
534e3374a7Sperseant 		{ "\xc5\xc4\xcf\xcb", "\xa3\xd6", "\xc5\xda\xc4\xc9\xd4\xd8", NULL },
544e3374a7Sperseant 	}, {
554e3374a7Sperseant 		NULL,
564e3374a7Sperseant 		{ NULL, NULL, NULL, NULL },
574e3374a7Sperseant 	}
584e3374a7Sperseant };
594e3374a7Sperseant 
604e3374a7Sperseant static void
h_ordering(const struct test * t)614e3374a7Sperseant h_ordering(const struct test *t)
624e3374a7Sperseant {
634e3374a7Sperseant 	const char * const *a;
644e3374a7Sperseant 	const char * const *b;
654e3374a7Sperseant 	char buf_a[1024], buf_b[1024];
664e3374a7Sperseant 
674e3374a7Sperseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
684e3374a7Sperseant 	printf("Trying locale %s...\n", t->locale);
694e3374a7Sperseant 	ATF_REQUIRE(setlocale(LC_COLLATE, t->locale) != NULL);
704e3374a7Sperseant 
714e3374a7Sperseant 	for (a = t->data; *a != NULL; ++a) {
724e3374a7Sperseant 		strvis(buf_a, *a, VIS_WHITE | VIS_OCTAL);
734e3374a7Sperseant 		for (b = a + 1; *b != NULL; ++b) {
744e3374a7Sperseant 			strvis(buf_b, *b, VIS_WHITE | VIS_OCTAL);
754e3374a7Sperseant 			printf("Checking \"%s\" < \"%s\"\n", buf_a, buf_b);
764e3374a7Sperseant 			ATF_REQUIRE(strcoll(*a, *b) < 0);
774e3374a7Sperseant 			printf("...good\n");
784e3374a7Sperseant 		}
794e3374a7Sperseant 	}
804e3374a7Sperseant }
814e3374a7Sperseant 
824e3374a7Sperseant ATF_TC(ordering);
834e3374a7Sperseant 
ATF_TC_HEAD(ordering,tc)844e3374a7Sperseant ATF_TC_HEAD(ordering, tc)
854e3374a7Sperseant {
864e3374a7Sperseant 	atf_tc_set_md_var(tc, "descr",
87*02698c5bSandvar 		"Checks collation ordering under different locales");
884e3374a7Sperseant }
894e3374a7Sperseant 
ATF_TC_BODY(ordering,tc)904e3374a7Sperseant ATF_TC_BODY(ordering, tc)
914e3374a7Sperseant {
924e3374a7Sperseant 	struct test *t;
934e3374a7Sperseant 
944e3374a7Sperseant 	atf_tc_expect_fail("%s", "LC_COLLATE not supported");
954e3374a7Sperseant 	for (t = &tests[0]; t->locale != NULL; ++t)
964e3374a7Sperseant 		h_ordering(t);
974e3374a7Sperseant 	atf_tc_expect_pass();
984e3374a7Sperseant }
994e3374a7Sperseant 
ATF_TP_ADD_TCS(tp)1004e3374a7Sperseant ATF_TP_ADD_TCS(tp)
1014e3374a7Sperseant {
1024e3374a7Sperseant 
1034e3374a7Sperseant 	ATF_TP_ADD_TC(tp, ordering);
1044e3374a7Sperseant 
1054e3374a7Sperseant 	return atf_no_error();
1064e3374a7Sperseant }
107