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