Home | History | Annotate | Line # | Download | only in locale
t_sprintf.c revision 1.2
      1  1.2  perseant /* $NetBSD: t_sprintf.c,v 1.2 2017/06/07 22:59:42 perseant Exp $ */
      2  1.1  perseant 
      3  1.1  perseant /*-
      4  1.1  perseant  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5  1.1  perseant  * All rights reserved.
      6  1.1  perseant  *
      7  1.1  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  perseant  * by Konrad Schroder.
      9  1.1  perseant  *
     10  1.1  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.1  perseant  * modification, are permitted provided that the following conditions
     12  1.1  perseant  * are met:
     13  1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.1  perseant  *
     19  1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  perseant  */
     31  1.1  perseant 
     32  1.1  perseant #include <sys/cdefs.h>
     33  1.1  perseant __COPYRIGHT("@(#) Copyright (c) 2017\
     34  1.1  perseant  The NetBSD Foundation, inc. All rights reserved.");
     35  1.2  perseant __RCSID("$NetBSD: t_sprintf.c,v 1.2 2017/06/07 22:59:42 perseant Exp $");
     36  1.1  perseant 
     37  1.1  perseant #include <locale.h>
     38  1.1  perseant #include <stdio.h>
     39  1.1  perseant #include <stdlib.h>
     40  1.1  perseant #include <string.h>
     41  1.1  perseant #include <limits.h>
     42  1.1  perseant #include <ctype.h>
     43  1.1  perseant 
     44  1.1  perseant #include <atf-c.h>
     45  1.1  perseant 
     46  1.1  perseant static struct test {
     47  1.1  perseant 	const char *locale;
     48  1.1  perseant 	const int int_value;
     49  1.1  perseant 	const char *int_result;
     50  1.1  perseant 	const char *int_input;
     51  1.1  perseant 	const double double_value;
     52  1.1  perseant 	const char *double_result;
     53  1.1  perseant 	const char *double_input;
     54  1.1  perseant } tests[] = {
     55  1.1  perseant 	{
     56  1.2  perseant 		"en_US.UTF-8",
     57  1.1  perseant 		-12345,
     58  1.1  perseant 		"-12,345",
     59  1.1  perseant 		"-12345",
     60  1.1  perseant 		-12345.6789,
     61  1.1  perseant 		"-12,345.678900",
     62  1.1  perseant 		"-12345.678900",
     63  1.1  perseant 	}, {
     64  1.2  perseant 		"fr_FR.ISO8859-1",
     65  1.1  perseant 		-12345,
     66  1.2  perseant 		"-12\240345",
     67  1.1  perseant 		"-12345",
     68  1.1  perseant 		-12345.6789,
     69  1.2  perseant 		"-12\240345,678900",
     70  1.2  perseant 		"-12345,678900",
     71  1.1  perseant 	}, {
     72  1.2  perseant 		"it_IT.ISO8859-1",
     73  1.1  perseant 		-12345,
     74  1.2  perseant 		"-12.345",
     75  1.1  perseant 		"-12345",
     76  1.1  perseant 		-12345.6789,
     77  1.2  perseant 		"-12.345,678900",
     78  1.1  perseant 		"-12345,678900",
     79  1.1  perseant 	}, {
     80  1.2  perseant 		"POSIX",
     81  1.2  perseant 		/*
     82  1.2  perseant 		 * POSIX-1.2008 specifies that the C and POSIX
     83  1.2  perseant 		 * locales shall be identical (section 7.2) and
     84  1.2  perseant 		 * that the POSIX locale shall have an empty
     85  1.2  perseant 		 * thousands separator and "<period>" as its
     86  1.2  perseant 		 * decimal point (section 7.3.4).  *printf
     87  1.2  perseant 		 * ought to honor these settings.
     88  1.2  perseant 		 */
     89  1.2  perseant 		-12345,
     90  1.2  perseant 		"-12345",
     91  1.2  perseant 		"-12345",
     92  1.2  perseant 		-12345.6789,
     93  1.2  perseant 		"-12345.678900",
     94  1.2  perseant 		"-12345.678900",
     95  1.2  perseant 	}, {
     96  1.1  perseant 		NULL,
     97  1.1  perseant 		0,
     98  1.1  perseant 		NULL,
     99  1.1  perseant 		NULL,
    100  1.1  perseant 		0.0,
    101  1.1  perseant 		NULL,
    102  1.1  perseant 		NULL,
    103  1.1  perseant 	}
    104  1.1  perseant };
    105  1.1  perseant 
    106  1.1  perseant static void
    107  1.1  perseant h_sprintf(const struct test *t)
    108  1.1  perseant {
    109  1.1  perseant 	char buf[1024];
    110  1.1  perseant 
    111  1.1  perseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
    112  1.1  perseant 	printf("Trying locale %s...\n", t->locale);
    113  1.1  perseant 	ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
    114  1.2  perseant 	printf("Using locale: %s\n", setlocale(LC_ALL, NULL));
    115  1.2  perseant 
    116  1.2  perseant 	if (!strcmp("POSIX", t->locale))
    117  1.2  perseant         	atf_tc_expect_fail("%s", "PR standards/52282, printf doesn't respect empty thousands separator");
    118  1.1  perseant 
    119  1.1  perseant 	sprintf(buf, "%'f", t->double_value);
    120  1.1  perseant 	ATF_REQUIRE_STREQ(buf, t->double_result);
    121  1.1  perseant 
    122  1.1  perseant 	sprintf(buf, "%'d", t->int_value);
    123  1.1  perseant 	ATF_REQUIRE_STREQ(buf, t->int_result);
    124  1.2  perseant 
    125  1.2  perseant         atf_tc_expect_pass();
    126  1.1  perseant }
    127  1.1  perseant 
    128  1.1  perseant static void
    129  1.1  perseant h_strto(const struct test *t)
    130  1.1  perseant {
    131  1.1  perseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
    132  1.1  perseant 	printf("Trying locale %s...\n", t->locale);
    133  1.1  perseant 	ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
    134  1.1  perseant 
    135  1.1  perseant 	ATF_REQUIRE_EQ((int)strtol(t->int_input, NULL, 10), t->int_value);
    136  1.1  perseant 	ATF_REQUIRE_EQ(strtod(t->double_input, NULL), t->double_value);
    137  1.1  perseant }
    138  1.1  perseant 
    139  1.1  perseant static void
    140  1.1  perseant h_sscanf(const struct test *t)
    141  1.1  perseant {
    142  1.1  perseant 	int int_reported;
    143  1.1  perseant 	double double_reported;
    144  1.1  perseant 
    145  1.1  perseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
    146  1.1  perseant 	printf("Trying locale %s...\n", t->locale);
    147  1.1  perseant 	ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
    148  1.1  perseant 
    149  1.1  perseant 	sscanf(t->int_input, "%d", &int_reported);
    150  1.1  perseant 	ATF_REQUIRE_EQ(int_reported, t->int_value);
    151  1.1  perseant 	sscanf(t->double_input, "%lf", &double_reported);
    152  1.1  perseant 	ATF_REQUIRE_EQ(double_reported, t->double_value);
    153  1.1  perseant }
    154  1.1  perseant 
    155  1.1  perseant ATF_TC(sprintf);
    156  1.1  perseant ATF_TC_HEAD(sprintf, tc)
    157  1.1  perseant {
    158  1.1  perseant 	atf_tc_set_md_var(tc, "descr",
    159  1.1  perseant 		"Checks sprintf %%'d and %%'f under diferent locales");
    160  1.1  perseant }
    161  1.1  perseant ATF_TC_BODY(sprintf, tc)
    162  1.1  perseant {
    163  1.1  perseant 	struct test *t;
    164  1.1  perseant 
    165  1.1  perseant 	for (t = &tests[0]; t->locale != NULL; ++t)
    166  1.1  perseant 		h_sprintf(t);
    167  1.1  perseant }
    168  1.1  perseant 
    169  1.1  perseant ATF_TC(strto);
    170  1.1  perseant ATF_TC_HEAD(strto, tc)
    171  1.1  perseant {
    172  1.1  perseant 	atf_tc_set_md_var(tc, "descr",
    173  1.1  perseant 		"Checks strtol and strtod under diferent locales");
    174  1.1  perseant }
    175  1.1  perseant ATF_TC_BODY(strto, tc)
    176  1.1  perseant {
    177  1.1  perseant 	struct test *t;
    178  1.1  perseant 
    179  1.1  perseant 	for (t = &tests[0]; t->locale != NULL; ++t)
    180  1.1  perseant 		h_strto(t);
    181  1.1  perseant }
    182  1.1  perseant 
    183  1.1  perseant ATF_TC(sscanf);
    184  1.1  perseant ATF_TC_HEAD(sscanf, tc)
    185  1.1  perseant {
    186  1.1  perseant 	atf_tc_set_md_var(tc, "descr",
    187  1.1  perseant 		"Checks sscanf under diferent locales");
    188  1.1  perseant }
    189  1.1  perseant ATF_TC_BODY(sscanf, tc)
    190  1.1  perseant {
    191  1.1  perseant 	struct test *t;
    192  1.1  perseant 
    193  1.1  perseant 	for (t = &tests[0]; t->locale != NULL; ++t)
    194  1.1  perseant 		h_sscanf(t);
    195  1.1  perseant }
    196  1.1  perseant 
    197  1.1  perseant ATF_TP_ADD_TCS(tp)
    198  1.1  perseant {
    199  1.1  perseant 
    200  1.1  perseant 	ATF_TP_ADD_TC(tp, sprintf);
    201  1.1  perseant 	ATF_TP_ADD_TC(tp, sscanf);
    202  1.1  perseant 	ATF_TP_ADD_TC(tp, strto);
    203  1.1  perseant 
    204  1.1  perseant 	return atf_no_error();
    205  1.1  perseant }
    206