[BACK]Return to t-locale.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / misc

Annotation of OpenXM_contrib/gmp/tests/misc/t-locale.c, Revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>