Blame tests/misc/t-locale.c

Packit 5c3484
/* Test locale support, or attempt to do so.
Packit 5c3484
Packit 5c3484
Copyright 2001, 2002, 2011, 2014 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library test suite.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is free software; you can redistribute it
Packit 5c3484
and/or modify it under the terms of the GNU General Public License as
Packit 5c3484
published by the Free Software Foundation; either version 3 of the License,
Packit 5c3484
or (at your option) any later version.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is distributed in the hope that it will be
Packit 5c3484
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5c3484
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 5c3484
Public License for more details.
Packit 5c3484
Packit 5c3484
You should have received a copy of the GNU General Public License along with
Packit 5c3484
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#define _GNU_SOURCE    /* for DECIMAL_POINT in glibc langinfo.h */
Packit 5c3484
Packit 5c3484
#include "config.h"
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
#include <string.h>
Packit 5c3484
Packit 5c3484
#if HAVE_NL_TYPES_H
Packit 5c3484
#include <nl_types.h>  /* for nl_item (on netbsd 1.4.1 at least) */
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#if HAVE_LANGINFO_H
Packit 5c3484
#include <langinfo.h>  /* for nl_langinfo */
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#if HAVE_LOCALE_H
Packit 5c3484
#include <locale.h>    /* for lconv */
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "tests.h"
Packit 5c3484
Packit 5c3484
const char *decimal_point;
Packit 5c3484
Packit 5c3484
/* Replace the libc localeconv with one we can manipulate. */
Packit 5c3484
#if HAVE_LOCALECONV && ! defined __MINGW32__
Packit 5c3484
struct lconv *
Packit 5c3484
localeconv (void)
Packit 5c3484
#if defined __cplusplus && defined __GLIBC__
Packit 5c3484
  throw()
Packit 5c3484
#endif
Packit 5c3484
{
Packit 5c3484
  static struct lconv  l;
Packit 5c3484
  l.decimal_point = (char *) decimal_point;
Packit 5c3484
  return &l;
Packit 5c3484
}
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
/* Replace the libc nl_langinfo with one we can manipulate. */
Packit 5c3484
#if HAVE_NL_LANGINFO
Packit 5c3484
char *
Packit 5c3484
nl_langinfo (nl_item n)
Packit 5c3484
#if defined __cplusplus && defined __GLIBC__
Packit 5c3484
  throw()
Packit 5c3484
#endif
Packit 5c3484
{
Packit 5c3484
#if defined (DECIMAL_POINT)
Packit 5c3484
  if (n == DECIMAL_POINT)
Packit 5c3484
    return (char *) decimal_point;
Packit 5c3484
#endif
Packit 5c3484
#if defined (RADIXCHAR)
Packit 5c3484
  if (n == RADIXCHAR)
Packit 5c3484
    return (char *) decimal_point;
Packit 5c3484
#endif
Packit 5c3484
  return (char *) "";
Packit 5c3484
}
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
check_input (void)
Packit 5c3484
{
Packit 5c3484
  static const char *point[] = {
Packit 5c3484
    ".", ",", "WU", "STR", "ZTV***"
Packit 5c3484
  };
Packit 5c3484
Packit 5c3484
  static const struct {
Packit 5c3484
    const char  *str;
Packit 5c3484
    double      d;
Packit 5c3484
  } data[] = {
Packit 5c3484
Packit 5c3484
    { "1%s",   1.0 },
Packit 5c3484
    { "1%s0",  1.0 },
Packit 5c3484
    { "1%s00", 1.0 },
Packit 5c3484
Packit 5c3484
    { "%s5",    0.5 },
Packit 5c3484
    { "0%s5",   0.5 },
Packit 5c3484
    { "00%s5",  0.5 },
Packit 5c3484
    { "00%s50", 0.5 },
Packit 5c3484
Packit 5c3484
    { "1%s5",    1.5 },
Packit 5c3484
    { "1%s5e1", 15.0 },
Packit 5c3484
  };
Packit 5c3484
Packit 5c3484
  int     i, j, neg, ret;
Packit 5c3484
  char    str[128];
Packit 5c3484
  mpf_t   f;
Packit 5c3484
  double  d;
Packit 5c3484
Packit 5c3484
  mpf_init (f);
Packit 5c3484
Packit 5c3484
  for (i = 0; i < numberof (point); i++)
Packit 5c3484
    {
Packit 5c3484
      decimal_point = (const char *) point[i];
Packit 5c3484
Packit 5c3484
      for (neg = 0; neg <= 1; neg++)
Packit 5c3484
        {
Packit 5c3484
          for (j = 0; j < numberof (data); j++)
Packit 5c3484
            {
Packit 5c3484
              strcpy (str, neg ? "-" : "");
Packit 5c3484
              sprintf (str+strlen(str), data[j].str, decimal_point);
Packit 5c3484
Packit 5c3484
              d = data[j].d;
Packit 5c3484
              if (neg)
Packit 5c3484
                d = -d;
Packit 5c3484
Packit 5c3484
              mpf_set_d (f, 123.0);
Packit 5c3484
              if (mpf_set_str (f, str, 10) != 0)
Packit 5c3484
                {
Packit 5c3484
                  printf ("mpf_set_str error\n");
Packit 5c3484
                  printf ("  point  %s\n", decimal_point);
Packit 5c3484
                  printf ("  str    %s\n", str);
Packit 5c3484
                  abort ();
Packit 5c3484
                }
Packit 5c3484
              if (mpf_cmp_d (f, d) != 0)
Packit 5c3484
                {
Packit 5c3484
                  printf    ("mpf_set_str wrong result\n");
Packit 5c3484
                  printf    ("  point  %s\n", decimal_point);
Packit 5c3484
                  printf    ("  str    %s\n", str);
Packit 5c3484
                  mpf_trace ("  f", f);
Packit 5c3484
                  printf    ("  d=%g\n", d);
Packit 5c3484
                  abort ();
Packit 5c3484
                }
Packit 5c3484
Packit 5c3484
              mpf_set_d (f, 123.0);
Packit 5c3484
              ret = gmp_sscanf (str, "%Ff", f);
Packit 5c3484
              if (ret != 1)
Packit 5c3484
                {
Packit 5c3484
                  printf ("gmp_sscanf wrong return value\n");
Packit 5c3484
                  printf ("  point  %s\n", decimal_point);
Packit 5c3484
                  printf ("  str    %s\n", str);
Packit 5c3484
                  printf ("  ret    %d\n", ret);
Packit 5c3484
                  abort ();
Packit 5c3484
                }
Packit 5c3484
              if (mpf_cmp_d (f, d) != 0)
Packit 5c3484
                {
Packit 5c3484
                  printf    ("gmp_sscanf wrong result\n");
Packit 5c3484
                  printf    ("  point  %s\n", decimal_point);
Packit 5c3484
                  printf    ("  str    %s\n", str);
Packit 5c3484
                  mpf_trace ("  f", f);
Packit 5c3484
                  printf    ("  d=%g\n", d);
Packit 5c3484
                  abort ();
Packit 5c3484
                }
Packit 5c3484
            }
Packit 5c3484
        }
Packit 5c3484
    }
Packit 5c3484
  mpf_clear (f);
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
main (void)
Packit 5c3484
{
Packit 5c3484
  /* The localeconv replacement breaks printf "%lu" on SunOS 4, so we can't
Packit 5c3484
     print the seed in tests_rand_start().  Nothing random is used in this
Packit 5c3484
     program though, so just use the memory tests alone.  */
Packit 5c3484
  tests_memory_start ();
Packit 5c3484
Packit 5c3484
  {
Packit 5c3484
    mpf_t  f;
Packit 5c3484
    char   buf[128];
Packit 5c3484
    mpf_init (f);
Packit 5c3484
    decimal_point = ",";
Packit 5c3484
    mpf_set_d (f, 1.5);
Packit 5c3484
    gmp_snprintf (buf, sizeof(buf), "%.1Ff", f);
Packit 5c3484
    mpf_clear (f);
Packit 5c3484
    if (strcmp (buf, "1,5") != 0)
Packit 5c3484
      {
Packit 5c3484
        printf ("Test skipped, replacing localeconv/nl_langinfo doesn't work\n");
Packit 5c3484
        goto done;
Packit 5c3484
      }
Packit 5c3484
  }
Packit 5c3484
Packit 5c3484
  check_input ();
Packit 5c3484
Packit 5c3484
 done:
Packit 5c3484
  tests_memory_end ();
Packit 5c3484
  exit (0);
Packit 5c3484
}