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