Home | History | Annotate | Line # | Download | only in misc
t-locale.c revision 1.1.1.3
      1      1.1  mrg /* Test locale support, or attempt to do so.
      2      1.1  mrg 
      3  1.1.1.3  mrg Copyright 2001, 2002, 2011, 2014 Free Software Foundation, Inc.
      4      1.1  mrg 
      5  1.1.1.2  mrg This file is part of the GNU MP Library test suite.
      6      1.1  mrg 
      7  1.1.1.2  mrg The GNU MP Library test suite is free software; you can redistribute it
      8  1.1.1.2  mrg and/or modify it under the terms of the GNU General Public License as
      9  1.1.1.2  mrg published by the Free Software Foundation; either version 3 of the License,
     10  1.1.1.2  mrg or (at your option) any later version.
     11  1.1.1.2  mrg 
     12  1.1.1.2  mrg The GNU MP Library test suite is distributed in the hope that it will be
     13  1.1.1.2  mrg useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1.1.2  mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
     15  1.1.1.2  mrg Public License for more details.
     16      1.1  mrg 
     17  1.1.1.2  mrg You should have received a copy of the GNU General Public License along with
     18  1.1.1.3  mrg the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
     19      1.1  mrg 
     20      1.1  mrg #define _GNU_SOURCE    /* for DECIMAL_POINT in glibc langinfo.h */
     21      1.1  mrg 
     22      1.1  mrg #include "config.h"
     23      1.1  mrg 
     24      1.1  mrg #include <stdio.h>
     25      1.1  mrg #include <stdlib.h>
     26      1.1  mrg #include <string.h>
     27      1.1  mrg 
     28      1.1  mrg #if HAVE_NL_TYPES_H
     29      1.1  mrg #include <nl_types.h>  /* for nl_item (on netbsd 1.4.1 at least) */
     30      1.1  mrg #endif
     31      1.1  mrg 
     32      1.1  mrg #if HAVE_LANGINFO_H
     33      1.1  mrg #include <langinfo.h>  /* for nl_langinfo */
     34      1.1  mrg #endif
     35      1.1  mrg 
     36      1.1  mrg #if HAVE_LOCALE_H
     37      1.1  mrg #include <locale.h>    /* for lconv */
     38      1.1  mrg #endif
     39      1.1  mrg 
     40      1.1  mrg #include "gmp.h"
     41      1.1  mrg #include "gmp-impl.h"
     42      1.1  mrg #include "tests.h"
     43      1.1  mrg 
     44  1.1.1.2  mrg const char *decimal_point;
     45      1.1  mrg 
     46      1.1  mrg /* Replace the libc localeconv with one we can manipulate. */
     47  1.1.1.3  mrg #if HAVE_LOCALECONV && ! defined __MINGW32__
     48      1.1  mrg struct lconv *
     49      1.1  mrg localeconv (void)
     50  1.1.1.3  mrg #if defined __cplusplus && defined __GLIBC__
     51  1.1.1.3  mrg   throw()
     52  1.1.1.3  mrg #endif
     53      1.1  mrg {
     54      1.1  mrg   static struct lconv  l;
     55  1.1.1.2  mrg   l.decimal_point = (char *) decimal_point;
     56      1.1  mrg   return &l;
     57      1.1  mrg }
     58      1.1  mrg #endif
     59      1.1  mrg 
     60      1.1  mrg /* Replace the libc nl_langinfo with one we can manipulate. */
     61      1.1  mrg #if HAVE_NL_LANGINFO
     62      1.1  mrg char *
     63      1.1  mrg nl_langinfo (nl_item n)
     64  1.1.1.3  mrg #if defined __cplusplus && defined __GLIBC__
     65  1.1.1.3  mrg   throw()
     66  1.1.1.3  mrg #endif
     67      1.1  mrg {
     68      1.1  mrg #if defined (DECIMAL_POINT)
     69      1.1  mrg   if (n == DECIMAL_POINT)
     70  1.1.1.2  mrg     return (char *) decimal_point;
     71      1.1  mrg #endif
     72      1.1  mrg #if defined (RADIXCHAR)
     73      1.1  mrg   if (n == RADIXCHAR)
     74  1.1.1.2  mrg     return (char *) decimal_point;
     75      1.1  mrg #endif
     76  1.1.1.2  mrg   return (char *) "";
     77      1.1  mrg }
     78      1.1  mrg #endif
     79      1.1  mrg 
     80      1.1  mrg void
     81      1.1  mrg check_input (void)
     82      1.1  mrg {
     83  1.1.1.2  mrg   static const char *point[] = {
     84      1.1  mrg     ".", ",", "WU", "STR", "ZTV***"
     85      1.1  mrg   };
     86      1.1  mrg 
     87      1.1  mrg   static const struct {
     88      1.1  mrg     const char  *str;
     89      1.1  mrg     double      d;
     90      1.1  mrg   } data[] = {
     91      1.1  mrg 
     92      1.1  mrg     { "1%s",   1.0 },
     93      1.1  mrg     { "1%s0",  1.0 },
     94      1.1  mrg     { "1%s00", 1.0 },
     95      1.1  mrg 
     96      1.1  mrg     { "%s5",    0.5 },
     97      1.1  mrg     { "0%s5",   0.5 },
     98      1.1  mrg     { "00%s5",  0.5 },
     99      1.1  mrg     { "00%s50", 0.5 },
    100      1.1  mrg 
    101      1.1  mrg     { "1%s5",    1.5 },
    102      1.1  mrg     { "1%s5e1", 15.0 },
    103      1.1  mrg   };
    104      1.1  mrg 
    105      1.1  mrg   int     i, j, neg, ret;
    106      1.1  mrg   char    str[128];
    107      1.1  mrg   mpf_t   f;
    108      1.1  mrg   double  d;
    109      1.1  mrg 
    110      1.1  mrg   mpf_init (f);
    111      1.1  mrg 
    112      1.1  mrg   for (i = 0; i < numberof (point); i++)
    113      1.1  mrg     {
    114  1.1.1.2  mrg       decimal_point = (const char *) point[i];
    115      1.1  mrg 
    116      1.1  mrg       for (neg = 0; neg <= 1; neg++)
    117      1.1  mrg         {
    118      1.1  mrg           for (j = 0; j < numberof (data); j++)
    119      1.1  mrg             {
    120      1.1  mrg               strcpy (str, neg ? "-" : "");
    121      1.1  mrg               sprintf (str+strlen(str), data[j].str, decimal_point);
    122      1.1  mrg 
    123      1.1  mrg               d = data[j].d;
    124      1.1  mrg               if (neg)
    125      1.1  mrg                 d = -d;
    126      1.1  mrg 
    127      1.1  mrg               mpf_set_d (f, 123.0);
    128      1.1  mrg               if (mpf_set_str (f, str, 10) != 0)
    129      1.1  mrg                 {
    130      1.1  mrg                   printf ("mpf_set_str error\n");
    131      1.1  mrg                   printf ("  point  %s\n", decimal_point);
    132      1.1  mrg                   printf ("  str    %s\n", str);
    133      1.1  mrg                   abort ();
    134      1.1  mrg                 }
    135      1.1  mrg               if (mpf_cmp_d (f, d) != 0)
    136      1.1  mrg                 {
    137      1.1  mrg                   printf    ("mpf_set_str wrong result\n");
    138      1.1  mrg                   printf    ("  point  %s\n", decimal_point);
    139      1.1  mrg                   printf    ("  str    %s\n", str);
    140      1.1  mrg                   mpf_trace ("  f", f);
    141      1.1  mrg                   printf    ("  d=%g\n", d);
    142      1.1  mrg                   abort ();
    143      1.1  mrg                 }
    144      1.1  mrg 
    145      1.1  mrg               mpf_set_d (f, 123.0);
    146      1.1  mrg               ret = gmp_sscanf (str, "%Ff", f);
    147      1.1  mrg               if (ret != 1)
    148      1.1  mrg                 {
    149      1.1  mrg                   printf ("gmp_sscanf wrong return value\n");
    150      1.1  mrg                   printf ("  point  %s\n", decimal_point);
    151      1.1  mrg                   printf ("  str    %s\n", str);
    152      1.1  mrg                   printf ("  ret    %d\n", ret);
    153      1.1  mrg                   abort ();
    154      1.1  mrg                 }
    155      1.1  mrg               if (mpf_cmp_d (f, d) != 0)
    156      1.1  mrg                 {
    157      1.1  mrg                   printf    ("gmp_sscanf wrong result\n");
    158      1.1  mrg                   printf    ("  point  %s\n", decimal_point);
    159      1.1  mrg                   printf    ("  str    %s\n", str);
    160      1.1  mrg                   mpf_trace ("  f", f);
    161      1.1  mrg                   printf    ("  d=%g\n", d);
    162      1.1  mrg                   abort ();
    163      1.1  mrg                 }
    164      1.1  mrg             }
    165      1.1  mrg         }
    166      1.1  mrg     }
    167      1.1  mrg   mpf_clear (f);
    168      1.1  mrg }
    169      1.1  mrg 
    170      1.1  mrg int
    171      1.1  mrg main (void)
    172      1.1  mrg {
    173      1.1  mrg   /* The localeconv replacement breaks printf "%lu" on SunOS 4, so we can't
    174      1.1  mrg      print the seed in tests_rand_start().  Nothing random is used in this
    175      1.1  mrg      program though, so just use the memory tests alone.  */
    176      1.1  mrg   tests_memory_start ();
    177      1.1  mrg 
    178      1.1  mrg   {
    179      1.1  mrg     mpf_t  f;
    180      1.1  mrg     char   buf[128];
    181      1.1  mrg     mpf_init (f);
    182      1.1  mrg     decimal_point = ",";
    183      1.1  mrg     mpf_set_d (f, 1.5);
    184      1.1  mrg     gmp_snprintf (buf, sizeof(buf), "%.1Ff", f);
    185      1.1  mrg     mpf_clear (f);
    186      1.1  mrg     if (strcmp (buf, "1,5") != 0)
    187      1.1  mrg       {
    188      1.1  mrg         printf ("Test skipped, replacing localeconv/nl_langinfo doesn't work\n");
    189      1.1  mrg         goto done;
    190      1.1  mrg       }
    191      1.1  mrg   }
    192      1.1  mrg 
    193      1.1  mrg   check_input ();
    194      1.1  mrg 
    195      1.1  mrg  done:
    196      1.1  mrg   tests_memory_end ();
    197      1.1  mrg   exit (0);
    198      1.1  mrg }
    199