hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame misc/tst-efgcvt.c

Packit 6c4009
/* Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _GNU_SOURCE
Packit 6c4009
# define _GNU_SOURCE	1
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <float.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
int error_count;
Packit 6c4009
Packit 6c4009
typedef struct
Packit 6c4009
{
Packit 6c4009
  double value;
Packit 6c4009
  int ndigit;
Packit 6c4009
  int decpt;
Packit 6c4009
  char result[30];
Packit 6c4009
} testcase;
Packit 6c4009
Packit 6c4009
typedef char * ((*efcvt_func) (double, int, int *, int *));
Packit 6c4009
Packit 6c4009
typedef int ((*efcvt_r_func) (double, int, int *, int *, char *, size_t));
Packit 6c4009
Packit 6c4009
Packit 6c4009
static testcase ecvt_tests[] =
Packit 6c4009
{
Packit 6c4009
  { 0.0, 0, 1, "" },
Packit 6c4009
  { 10.0, 0, 2, "" },
Packit 6c4009
  { 10.0, 1, 2, "1" },
Packit 6c4009
  { 10.0, 5, 2, "10000" },
Packit 6c4009
  { -12.0, 5, 2, "12000" },
Packit 6c4009
  { 0.2, 4, 0, "2000" },
Packit 6c4009
  { 0.02, 4, -1, "2000" },
Packit 6c4009
  { 5.5, 1, 1, "6" },
Packit 6c4009
  { 1.0, -1, 1, "" },
Packit 6c4009
  { 0.01, 2, -1, "10" },
Packit 6c4009
  { 100.0, -2, 3, "" },
Packit 6c4009
  { 100.0, -5, 3, "" },
Packit 6c4009
  { 100.0, -4, 3, "" },
Packit 6c4009
  { 100.01, -4, 3, "" },
Packit 6c4009
  { 123.01, -4, 3, "" },
Packit 6c4009
  { 126.71, -4, 3, "" },
Packit 6c4009
  { 0.0, 4, 1, "0000" },
Packit 6c4009
#if DBL_MANT_DIG == 53
Packit 6c4009
  { 0x1p-1074, 3, -323, "494" },
Packit 6c4009
  { -0x1p-1074, 3, -323, "494" },
Packit 6c4009
#endif
Packit 6c4009
  /* -1.0 is end marker.  */
Packit 6c4009
  { -1.0, 0, 0, "" }
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static testcase fcvt_tests[] =
Packit 6c4009
{
Packit 6c4009
  { 0.0, 0, 1, "0" },
Packit 6c4009
  { 10.0, 0, 2, "10" },
Packit 6c4009
  { 10.0, 1, 2, "100" },
Packit 6c4009
  { 10.0, 4, 2, "100000" },
Packit 6c4009
  { -12.0, 5, 2, "1200000" },
Packit 6c4009
  { 0.2, 4, 0, "2000" },
Packit 6c4009
  { 0.02, 4, -1, "200" },
Packit 6c4009
  { 5.5, 1, 1, "55" },
Packit 6c4009
  { 5.5, 0, 1, "6" },
Packit 6c4009
  { 0.01, 2, -1, "1" },
Packit 6c4009
  { 100.0, -2, 3, "100" },
Packit 6c4009
  { 100.0, -5, 3, "100" },
Packit 6c4009
  { 100.0, -4, 3, "100" },
Packit 6c4009
  { 100.01, -4, 3, "100" },
Packit 6c4009
  { 123.01, -4, 3, "100" },
Packit 6c4009
  { 126.71, -4, 3, "100" },
Packit 6c4009
  { 322.5, 16, 3, "3225000000000000000" },
Packit 6c4009
  /* -1.0 is end marker.  */
Packit 6c4009
  { -1.0, 0, 0, "" }
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
output_error (const char *name, double value, int ndigit,
Packit 6c4009
	      const char *exp_p, int exp_decpt, int exp_sign,
Packit 6c4009
	      char *res_p, int res_decpt, int res_sign)
Packit 6c4009
{
Packit 6c4009
  printf ("%s returned wrong result for value: %f, ndigits: %d\n",
Packit 6c4009
	  name, value, ndigit);
Packit 6c4009
  printf ("Result was p: \"%s\", decpt: %d, sign: %d\n",
Packit 6c4009
	  res_p, res_decpt, res_sign);
Packit 6c4009
  printf ("Should be  p: \"%s\", decpt: %d, sign: %d\n",
Packit 6c4009
	  exp_p, exp_decpt, exp_sign);
Packit 6c4009
  ++error_count;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
output_r_error (const char *name, double value, int ndigit,
Packit 6c4009
		const char *exp_p, int exp_decpt, int exp_sign, int exp_return,
Packit 6c4009
		char *res_p, int res_decpt, int res_sign, int res_return)
Packit 6c4009
{
Packit 6c4009
  printf ("%s returned wrong result for value: %f, ndigits: %d\n",
Packit 6c4009
	  name, value, ndigit);
Packit 6c4009
  printf ("Result was buf: \"%s\", decpt: %d, sign: %d return value: %d\n",
Packit 6c4009
	  res_p, res_decpt, res_sign, res_return);
Packit 6c4009
  printf ("Should be  buf: \"%s\", decpt: %d, sign: %d\n",
Packit 6c4009
	  exp_p, exp_decpt, exp_sign);
Packit 6c4009
  ++error_count;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test (testcase tests[], efcvt_func efcvt, const char *name)
Packit 6c4009
{
Packit 6c4009
  int no = 0;
Packit 6c4009
  int decpt, sign;
Packit 6c4009
  char *p;
Packit 6c4009
Packit 6c4009
  while (tests[no].value != -1.0)
Packit 6c4009
    {
Packit 6c4009
      p = efcvt (tests[no].value, tests[no].ndigit, &decpt, &sign;;
Packit 6c4009
      if (decpt != tests[no].decpt
Packit 6c4009
	  || sign != (tests[no].value < 0)
Packit 6c4009
	  || strcmp (p, tests[no].result) != 0)
Packit 6c4009
	output_error (name, tests[no].value, tests[no].ndigit,
Packit 6c4009
		      tests[no].result, tests[no].decpt,
Packit 6c4009
		      (tests[no].value < 0),
Packit 6c4009
		      p, decpt, sign);
Packit 6c4009
      ++no;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_r (testcase tests[], efcvt_r_func efcvt_r, const char *name)
Packit 6c4009
{
Packit 6c4009
  int no = 0;
Packit 6c4009
  int decpt, sign, res;
Packit 6c4009
  char buf [1024];
Packit 6c4009
Packit 6c4009
Packit 6c4009
  while (tests[no].value != -1.0)
Packit 6c4009
    {
Packit 6c4009
      res = efcvt_r (tests[no].value, tests[no].ndigit, &decpt, &sign,
Packit 6c4009
		     buf, sizeof (buf));
Packit 6c4009
      if (res != 0
Packit 6c4009
	  || decpt != tests[no].decpt
Packit 6c4009
	  || sign != (tests[no].value < 0)
Packit 6c4009
	  || strcmp (buf, tests[no].result) != 0)
Packit 6c4009
	output_r_error (name, tests[no].value, tests[no].ndigit,
Packit 6c4009
			tests[no].result, tests[no].decpt, 0,
Packit 6c4009
			(tests[no].value < 0),
Packit 6c4009
			buf, decpt, sign, res);
Packit 6c4009
      ++no;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
special (void)
Packit 6c4009
{
Packit 6c4009
  int decpt, sign, res;
Packit 6c4009
  char *p;
Packit 6c4009
  char buf [1024];
Packit 6c4009
Packit 6c4009
  p = ecvt (NAN, 10, &decpt, &sign;;
Packit 6c4009
  if (sign != 0 || strcmp (p, "nan") != 0)
Packit 6c4009
    output_error ("ecvt", NAN, 10, "nan", 0, 0, p, decpt, sign);
Packit 6c4009
Packit 6c4009
  p = ecvt (INFINITY, 10, &decpt, &sign;;
Packit 6c4009
  if (sign != 0 || strcmp (p, "inf") != 0)
Packit 6c4009
    output_error ("ecvt", INFINITY, 10, "inf", 0, 0, p, decpt, sign);
Packit 6c4009
Packit 6c4009
  /* Simply make sure these calls with large NDIGITs don't crash.  */
Packit 6c4009
  (void) ecvt (123.456, 10000, &decpt, &sign;;
Packit 6c4009
  (void) fcvt (123.456, 10000, &decpt, &sign;;
Packit 6c4009
Packit 6c4009
  /* Some tests for the reentrant functions.  */
Packit 6c4009
  /* Use a too small buffer.  */
Packit 6c4009
  res = ecvt_r (123.456, 10, &decpt, &sign, buf, 1);
Packit 6c4009
  if (res == 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("ecvt_r with a too small buffer was succesful.\n");
Packit 6c4009
      ++error_count;
Packit 6c4009
    }
Packit 6c4009
  res = fcvt_r (123.456, 10, &decpt, &sign, buf, 1);
Packit 6c4009
  if (res == 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fcvt_r with a too small buffer was succesful.\n");
Packit 6c4009
      ++error_count;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  test (ecvt_tests, ecvt, "ecvt");
Packit 6c4009
  test (fcvt_tests, fcvt, "fcvt");
Packit 6c4009
  test_r (ecvt_tests, ecvt_r, "ecvt_r");
Packit 6c4009
  test_r (fcvt_tests, fcvt_r, "fcvt_r");
Packit 6c4009
  special ();
Packit 6c4009
Packit 6c4009
  return error_count;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"