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