Blame stdio-common/tst-printf-round.c

Packit 6c4009
/* Test for correct rounding of printf floating-point output.
Packit 6c4009
   Copyright (C) 2012-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
#include <array_length.h>
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
struct dec_test {
Packit 6c4009
  double d;
Packit 6c4009
  const char *fmt;
Packit 6c4009
  const char *rd, *rn, *rz, *ru;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const struct dec_test dec_tests[] = {
Packit 6c4009
  { 1.5, "%.0f", "1", "2", "1", "2" },
Packit 6c4009
  { -1.5, "%.0f", "-2", "-2", "-1", "-1" },
Packit 6c4009
  { 2.5, "%.0f", "2", "2", "2", "3" },
Packit 6c4009
  { -2.5, "%.0f", "-3", "-2", "-2", "-2" },
Packit 6c4009
  { 1.4999, "%.0f", "1", "1", "1", "2" },
Packit 6c4009
  { -1.4999, "%.0f", "-2", "-1", "-1", "-1" },
Packit 6c4009
  { 1.5001, "%.0f", "1", "2", "1", "2" },
Packit 6c4009
  { -1.5001, "%.0f", "-2", "-2", "-1", "-1" },
Packit 6c4009
  { 2.4999, "%.0f", "2", "2", "2", "3" },
Packit 6c4009
  { -2.4999, "%.0f", "-3", "-2", "-2", "-2" },
Packit 6c4009
  { 2.5001, "%.0f", "2", "3", "2", "3" },
Packit 6c4009
  { -2.5001, "%.0f", "-3", "-3", "-2", "-2" },
Packit 6c4009
  { 1.0 / 3.0, "%f", "0.333333", "0.333333", "0.333333", "0.333334" },
Packit 6c4009
  { -1.0 / 3.0, "%f", "-0.333334", "-0.333333", "-0.333333", "-0.333333" },
Packit 6c4009
  { 0.2500001, "%.2e", "2.50e-01", "2.50e-01", "2.50e-01", "2.51e-01" },
Packit 6c4009
  { -0.2500001, "%.2e", "-2.51e-01", "-2.50e-01", "-2.50e-01", "-2.50e-01" },
Packit 6c4009
  { 1000001.0, "%.1e", "1.0e+06", "1.0e+06", "1.0e+06", "1.1e+06" },
Packit 6c4009
  { -1000001.0, "%.1e", "-1.1e+06", "-1.0e+06", "-1.0e+06", "-1.0e+06" },
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
test_dec_in_one_mode (double d, const char *fmt, const char *expected,
Packit 6c4009
		      const char *mode_name)
Packit 6c4009
{
Packit 6c4009
  char buf[100];
Packit 6c4009
  int ret = snprintf (buf, sizeof buf, fmt, d);
Packit 6c4009
  if (ret <= 0 || ret >= (int) sizeof buf)
Packit 6c4009
    {
Packit 6c4009
      printf ("snprintf for %a returned %d\n", d, ret);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (strcmp (buf, expected) == 0)
Packit 6c4009
    return 0;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      printf ("snprintf (\"%s\", %a) returned \"%s\" not \"%s\" (%s)\n",
Packit 6c4009
	      fmt, d, buf, expected, mode_name);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
struct hex_test
Packit 6c4009
{
Packit 6c4009
  double d;
Packit 6c4009
  const char *fmt;
Packit 6c4009
  const char *rd[4], *rn[4], *rz[4], *ru[4];
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const struct hex_test hex_tests[] =
Packit 6c4009
  {
Packit 6c4009
    {
Packit 6c4009
      0x1.fffffp+4, "%.1a",
Packit 6c4009
      { "0x1.fp+4", "0x3.fp+3", "0x7.fp+2", "0xf.fp+1" },
Packit 6c4009
      { "0x2.0p+4", "0x4.0p+3", "0x8.0p+2", "0x1.0p+5" },
Packit 6c4009
      { "0x1.fp+4", "0x3.fp+3", "0x7.fp+2", "0xf.fp+1" },
Packit 6c4009
      { "0x2.0p+4", "0x4.0p+3", "0x8.0p+2", "0x1.0p+5" }
Packit 6c4009
    },
Packit 6c4009
    {
Packit 6c4009
      -0x1.fffffp+4, "%.1a",
Packit 6c4009
      { "-0x2.0p+4", "-0x4.0p+3", "-0x8.0p+2", "-0x1.0p+5" },
Packit 6c4009
      { "-0x2.0p+4", "-0x4.0p+3", "-0x8.0p+2", "-0x1.0p+5" },
Packit 6c4009
      { "-0x1.fp+4", "-0x3.fp+3", "-0x7.fp+2", "-0xf.fp+1" },
Packit 6c4009
      { "-0x1.fp+4", "-0x3.fp+3", "-0x7.fp+2", "-0xf.fp+1" }
Packit 6c4009
    },
Packit 6c4009
    {
Packit 6c4009
      0x1.88p+4, "%.1a",
Packit 6c4009
      { "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
Packit 6c4009
      { "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
Packit 6c4009
      { "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
Packit 6c4009
      { "0x1.9p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" }
Packit 6c4009
    },
Packit 6c4009
    {
Packit 6c4009
      -0x1.88p+4, "%.1a",
Packit 6c4009
      { "-0x1.9p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
Packit 6c4009
      { "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
Packit 6c4009
      { "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
Packit 6c4009
      { "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" }
Packit 6c4009
    },
Packit 6c4009
    {
Packit 6c4009
      0x1.78p+4, "%.1a",
Packit 6c4009
      { "0x1.7p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
Packit 6c4009
      { "0x1.8p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
Packit 6c4009
      { "0x1.7p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
Packit 6c4009
      { "0x1.8p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" }
Packit 6c4009
    },
Packit 6c4009
    {
Packit 6c4009
      -0x1.78p+4, "%.1a",
Packit 6c4009
      { "-0x1.8p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
Packit 6c4009
      { "-0x1.8p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
Packit 6c4009
      { "-0x1.7p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
Packit 6c4009
      { "-0x1.7p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" }
Packit 6c4009
    },
Packit 6c4009
    {
Packit 6c4009
      64.0 / 3.0, "%.1a",
Packit 6c4009
      { "0x1.5p+4", "0x2.ap+3", "0x5.5p+2", "0xa.ap+1" },
Packit 6c4009
      { "0x1.5p+4", "0x2.bp+3", "0x5.5p+2", "0xa.bp+1" },
Packit 6c4009
      { "0x1.5p+4", "0x2.ap+3", "0x5.5p+2", "0xa.ap+1" },
Packit 6c4009
      { "0x1.6p+4", "0x2.bp+3", "0x5.6p+2", "0xa.bp+1" }
Packit 6c4009
    },
Packit 6c4009
    {
Packit 6c4009
      -64.0 / 3.0, "%.1a",
Packit 6c4009
      { "-0x1.6p+4", "-0x2.bp+3", "-0x5.6p+2", "-0xa.bp+1" },
Packit 6c4009
      { "-0x1.5p+4", "-0x2.bp+3", "-0x5.5p+2", "-0xa.bp+1" },
Packit 6c4009
      { "-0x1.5p+4", "-0x2.ap+3", "-0x5.5p+2", "-0xa.ap+1" },
Packit 6c4009
      { "-0x1.5p+4", "-0x2.ap+3", "-0x5.5p+2", "-0xa.ap+1" }
Packit 6c4009
    },
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
test_hex_in_one_mode (double d, const char *fmt, const char *const expected[4],
Packit 6c4009
		      const char *mode_name)
Packit 6c4009
{
Packit 6c4009
  char buf[100];
Packit 6c4009
  int ret = snprintf (buf, sizeof buf, fmt, d);
Packit 6c4009
  if (ret <= 0 || ret >= (int) sizeof buf)
Packit 6c4009
    {
Packit 6c4009
      printf ("snprintf for %a returned %d\n", d, ret);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (strcmp (buf, expected[0]) == 0
Packit 6c4009
      || strcmp (buf, expected[1]) == 0
Packit 6c4009
      || strcmp (buf, expected[2]) == 0
Packit 6c4009
      || strcmp (buf, expected[3]) == 0)
Packit 6c4009
    return 0;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      printf ("snprintf (\"%s\", %a) returned \"%s\" not "
Packit 6c4009
	      "\"%s\" or \"%s\" or \"%s\" or \"%s\" (%s)\n",
Packit 6c4009
	      fmt, d, buf, expected[0], expected[1], expected[2], expected[3],
Packit 6c4009
	      mode_name);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int save_round_mode __attribute__ ((unused)) = fegetround ();
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  for (size_t i = 0; i < array_length (dec_tests); i++)
Packit 6c4009
    {
Packit 6c4009
      result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
Packit 6c4009
				      dec_tests[i].rn, "default rounding mode");
Packit 6c4009
#ifdef FE_DOWNWARD
Packit 6c4009
      if (!fesetround (FE_DOWNWARD))
Packit 6c4009
	{
Packit 6c4009
	  result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
Packit 6c4009
					  dec_tests[i].rd, "FE_DOWNWARD");
Packit 6c4009
	  fesetround (save_round_mode);
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_TOWARDZERO
Packit 6c4009
      if (!fesetround (FE_TOWARDZERO))
Packit 6c4009
	{
Packit 6c4009
	  result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
Packit 6c4009
					  dec_tests[i].rz, "FE_TOWARDZERO");
Packit 6c4009
	  fesetround (save_round_mode);
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_UPWARD
Packit 6c4009
      if (!fesetround (FE_UPWARD))
Packit 6c4009
	{
Packit 6c4009
	  result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
Packit 6c4009
					  dec_tests[i].ru, "FE_UPWARD");
Packit 6c4009
	  fesetround (save_round_mode);
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (size_t i = 0; i < array_length (hex_tests); i++)
Packit 6c4009
    {
Packit 6c4009
      result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
Packit 6c4009
				      hex_tests[i].rn, "default rounding mode");
Packit 6c4009
#ifdef FE_DOWNWARD
Packit 6c4009
      if (!fesetround (FE_DOWNWARD))
Packit 6c4009
	{
Packit 6c4009
	  result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
Packit 6c4009
					  hex_tests[i].rd, "FE_DOWNWARD");
Packit 6c4009
	  fesetround (save_round_mode);
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_TOWARDZERO
Packit 6c4009
      if (!fesetround (FE_TOWARDZERO))
Packit 6c4009
	{
Packit 6c4009
	  result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
Packit 6c4009
					  hex_tests[i].rz, "FE_TOWARDZERO");
Packit 6c4009
	  fesetround (save_round_mode);
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_UPWARD
Packit 6c4009
      if (!fesetround (FE_UPWARD))
Packit 6c4009
	{
Packit 6c4009
	  result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
Packit 6c4009
					  hex_tests[i].ru, "FE_UPWARD");
Packit 6c4009
	  fesetround (save_round_mode);
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"