xref: /netbsd-src/tests/lib/libc/locale/t_ducet.c (revision 508bf0d1f154dba31e398c71eefd1982de42ca6e)
1*508bf0d1Sperseant /* $NetBSD: t_ducet.c,v 1.2 2017/07/23 18:51:21 perseant Exp $ */
2*508bf0d1Sperseant 
3*508bf0d1Sperseant /*-
4*508bf0d1Sperseant  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*508bf0d1Sperseant  * All rights reserved.
6*508bf0d1Sperseant  *
7*508bf0d1Sperseant  * Redistribution and use in source and binary forms, with or without
8*508bf0d1Sperseant  * modification, are permitted provided that the following conditions
9*508bf0d1Sperseant  * are met:
10*508bf0d1Sperseant  * 1. Redistributions of source code must retain the above copyright
11*508bf0d1Sperseant  *    notice, this list of conditions and the following disclaimer.
12*508bf0d1Sperseant  * 2. Redistributions in binary form must reproduce the above copyright
13*508bf0d1Sperseant  *    notice, this list of conditions and the following disclaimer in the
14*508bf0d1Sperseant  *    documentation and/or other materials provided with the distribution.
15*508bf0d1Sperseant  *
16*508bf0d1Sperseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*508bf0d1Sperseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*508bf0d1Sperseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*508bf0d1Sperseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*508bf0d1Sperseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*508bf0d1Sperseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*508bf0d1Sperseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*508bf0d1Sperseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*508bf0d1Sperseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*508bf0d1Sperseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*508bf0d1Sperseant  * POSSIBILITY OF SUCH DAMAGE.
27*508bf0d1Sperseant  */
28*508bf0d1Sperseant #include <sys/cdefs.h>
29*508bf0d1Sperseant __COPYRIGHT("@(#) Copyright (c) 2011\
30*508bf0d1Sperseant  The NetBSD Foundation, inc. All rights reserved.");
31*508bf0d1Sperseant __RCSID("$NetBSD: t_ducet.c,v 1.2 2017/07/23 18:51:21 perseant Exp $");
32*508bf0d1Sperseant 
33*508bf0d1Sperseant #include <sys/param.h>
34*508bf0d1Sperseant #include <errno.h>
35*508bf0d1Sperseant #include <locale.h>
36*508bf0d1Sperseant #include <stdio.h>
37*508bf0d1Sperseant #include <stdlib.h>
38*508bf0d1Sperseant #include <string.h>
39*508bf0d1Sperseant #include <wchar.h>
40*508bf0d1Sperseant #include "ducet_test.h"
41*508bf0d1Sperseant 
42*508bf0d1Sperseant #include <atf-c.h>
43*508bf0d1Sperseant 
44*508bf0d1Sperseant ATF_TC(wcscoll_ducet);
ATF_TC_HEAD(wcscoll_ducet,tc)45*508bf0d1Sperseant ATF_TC_HEAD(wcscoll_ducet, tc)
46*508bf0d1Sperseant {
47*508bf0d1Sperseant 	atf_tc_set_md_var(tc, "descr", "Test collation algorithm against DUCET test data");
48*508bf0d1Sperseant }
49*508bf0d1Sperseant 
ATF_TC_BODY(wcscoll_ducet,tc)50*508bf0d1Sperseant ATF_TC_BODY(wcscoll_ducet, tc)
51*508bf0d1Sperseant {
52*508bf0d1Sperseant #ifndef __STDC_ISO_10646__
53*508bf0d1Sperseant 	atf_tc_skip("Cannot test DUCET without __STDC_ISO_10646__");
54*508bf0d1Sperseant #else
55*508bf0d1Sperseant 	wchar_t *oline = NULL, *line;
56*508bf0d1Sperseant 	int i, lineno = 0;
57*508bf0d1Sperseant 
58*508bf0d1Sperseant 	setlocale(LC_COLLATE, "en_US.UTF-8"); /* should be "vanilla" DUCET, but en_US will do */
59*508bf0d1Sperseant 	ATF_REQUIRE_STREQ("en_US.UTF-8", setlocale(LC_COLLATE, NULL));
60*508bf0d1Sperseant 
61*508bf0d1Sperseant 	for (line = &ducet_test_data[0][0]; line[0]; line += MAX_TS_LEN * sizeof(*line)) {
62*508bf0d1Sperseant 		++lineno;
63*508bf0d1Sperseant 
64*508bf0d1Sperseant 		if (oline == NULL) {
65*508bf0d1Sperseant 			oline = line;
66*508bf0d1Sperseant 			continue;
67*508bf0d1Sperseant 		}
68*508bf0d1Sperseant 
69*508bf0d1Sperseant 		printf(" line %d : ", lineno);
70*508bf0d1Sperseant 		for (i = 0; oline[i] != 0; i++)
71*508bf0d1Sperseant 			printf("0x%lx ", (long)oline[i]);
72*508bf0d1Sperseant 		printf(" < ");
73*508bf0d1Sperseant 		for (i = 0; line[i] != 0; i++)
74*508bf0d1Sperseant 			printf("0x%lx ", (long)line[i]);
75*508bf0d1Sperseant 		printf("\n");
76*508bf0d1Sperseant 
77*508bf0d1Sperseant 		/* Compare, expect oline <= line, a non-positive result */
78*508bf0d1Sperseant 		ATF_CHECK(wcscoll(oline, line) <= 0);
79*508bf0d1Sperseant 
80*508bf0d1Sperseant 		oline = line;
81*508bf0d1Sperseant 	}
82*508bf0d1Sperseant #endif
83*508bf0d1Sperseant }
84*508bf0d1Sperseant 
85*508bf0d1Sperseant ATF_TC(wcsxfrm_ducet);
ATF_TC_HEAD(wcsxfrm_ducet,tc)86*508bf0d1Sperseant ATF_TC_HEAD(wcsxfrm_ducet, tc)
87*508bf0d1Sperseant {
88*508bf0d1Sperseant 	atf_tc_set_md_var(tc, "descr", "Test collation algorithm against DUCET test data");
89*508bf0d1Sperseant }
90*508bf0d1Sperseant 
91*508bf0d1Sperseant #define BUFLEN 1024
92*508bf0d1Sperseant 
ATF_TC_BODY(wcsxfrm_ducet,tc)93*508bf0d1Sperseant ATF_TC_BODY(wcsxfrm_ducet, tc)
94*508bf0d1Sperseant {
95*508bf0d1Sperseant #ifndef __STDC_ISO_10646__
96*508bf0d1Sperseant 	atf_tc_skip("Cannot test DUCET without __STDC_ISO_10646__");
97*508bf0d1Sperseant #else
98*508bf0d1Sperseant 	wchar_t *tmp, *oline = NULL, *line;
99*508bf0d1Sperseant 	wchar_t xfb1[BUFLEN], xfb2[BUFLEN]; /* Gross overestimates */
100*508bf0d1Sperseant 	wchar_t *oxf = xfb1, *xf = xfb2;
101*508bf0d1Sperseant 	int i, lineno = 0, result;
102*508bf0d1Sperseant 
103*508bf0d1Sperseant 	setlocale(LC_COLLATE, "en_US.UTF-8"); /* should be "vanilla" DUCET, but en_US will do */
104*508bf0d1Sperseant 	ATF_REQUIRE_STREQ("en_US.UTF-8", setlocale(LC_COLLATE, NULL));
105*508bf0d1Sperseant 
106*508bf0d1Sperseant 	memset(xfb1, 0, BUFLEN);
107*508bf0d1Sperseant 	memset(xfb2, 0, BUFLEN);
108*508bf0d1Sperseant 	for (line = &ducet_test_data[0][0]; line[0]; line += MAX_TS_LEN * sizeof(*line)) {
109*508bf0d1Sperseant 		++lineno;
110*508bf0d1Sperseant 
111*508bf0d1Sperseant 		if (oline == NULL) {
112*508bf0d1Sperseant 			oline = line;
113*508bf0d1Sperseant 			continue;
114*508bf0d1Sperseant 		}
115*508bf0d1Sperseant 
116*508bf0d1Sperseant 		printf(" line %d : ", lineno);
117*508bf0d1Sperseant 		for (i = 0; oline[i] != 0; i++)
118*508bf0d1Sperseant 			printf("0x%lx ", (long)oline[i]);
119*508bf0d1Sperseant 		printf(" < ");
120*508bf0d1Sperseant 		for (i = 0; line[i] != 0; i++)
121*508bf0d1Sperseant 			printf("0x%lx ", (long)line[i]);
122*508bf0d1Sperseant 		printf("\n");
123*508bf0d1Sperseant 
124*508bf0d1Sperseant 		wcsxfrm(xf, line, BUFLEN);
125*508bf0d1Sperseant 		result = wcscmp(oxf, xf);
126*508bf0d1Sperseant 		if (result > 0) {
127*508bf0d1Sperseant 			printf("FAILED result was %d\nweights were ", result);
128*508bf0d1Sperseant 			for (i = 0; oxf[i] != 0; i++)
129*508bf0d1Sperseant 				printf("0x%lx ", (long)oline[i]);
130*508bf0d1Sperseant 			printf(" and ");
131*508bf0d1Sperseant 			for (i = 0; xf[i] != 0; i++)
132*508bf0d1Sperseant 				printf("0x%lx ", (long)line[i]);
133*508bf0d1Sperseant 			printf("\n");
134*508bf0d1Sperseant 
135*508bf0d1Sperseant 		}
136*508bf0d1Sperseant 		ATF_CHECK(result <= 0);
137*508bf0d1Sperseant 
138*508bf0d1Sperseant 		/* Swap buffers */
139*508bf0d1Sperseant 		tmp = oxf;
140*508bf0d1Sperseant 		oxf = xf;
141*508bf0d1Sperseant 		xf = tmp;
142*508bf0d1Sperseant 		oline = line;
143*508bf0d1Sperseant 	}
144*508bf0d1Sperseant #endif
145*508bf0d1Sperseant }
146*508bf0d1Sperseant 
ATF_TP_ADD_TCS(tp)147*508bf0d1Sperseant ATF_TP_ADD_TCS(tp)
148*508bf0d1Sperseant {
149*508bf0d1Sperseant 	ATF_TP_ADD_TC(tp, wcscoll_ducet);
150*508bf0d1Sperseant 	ATF_TP_ADD_TC(tp, wcsxfrm_ducet);
151*508bf0d1Sperseant 
152*508bf0d1Sperseant 	return atf_no_error();
153*508bf0d1Sperseant }
154