Blame stdio-common/tst-printf.c

Packit 6c4009
/* Copyright (C) 1991-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
#ifdef	BSD
Packit 6c4009
#include </usr/include/stdio.h>
Packit 6c4009
#define EXIT_SUCCESS 0
Packit 6c4009
#else
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <float.h>
Packit 6c4009
#include <libc-diag.h>
Packit 6c4009
Packit 6c4009
/* This whole file is picayune tests of corner cases of printf format strings.
Packit 6c4009
   The compiler warnings are not useful here.  */
Packit 6c4009
DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
Packit 6c4009
Packit 6c4009
#if __GNUC_PREREQ (7, 0)
Packit 6c4009
/* Compiler warnings about snprintf output truncation should also be
Packit 6c4009
   ignored.  */
Packit 6c4009
DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-truncation");
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static void rfg1 (void);
Packit 6c4009
static void rfg2 (void);
Packit 6c4009
static void rfg3 (void);
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
fmtchk (const char *fmt)
Packit 6c4009
{
Packit 6c4009
  (void) fputs(fmt, stdout);
Packit 6c4009
  (void) printf(":\t`");
Packit 6c4009
  (void) printf(fmt, 0x12);
Packit 6c4009
  (void) printf("'\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
fmtst1chk (const char *fmt)
Packit 6c4009
{
Packit 6c4009
  (void) fputs(fmt, stdout);
Packit 6c4009
  (void) printf(":\t`");
Packit 6c4009
  (void) printf(fmt, 4, 0x12);
Packit 6c4009
  (void) printf("'\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
fmtst2chk (const char *fmt)
Packit 6c4009
{
Packit 6c4009
  (void) fputs(fmt, stdout);
Packit 6c4009
  (void) printf(":\t`");
Packit 6c4009
  (void) printf(fmt, 4, 4, 0x12);
Packit 6c4009
  (void) printf("'\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  static char shortstr[] = "Hi, Z.";
Packit 6c4009
  static char longstr[] = "Good morning, Doctor Chandra.  This is Hal.  \
Packit 6c4009
I am ready for my first lesson today.";
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  fmtchk("%.4x");
Packit 6c4009
  fmtchk("%04x");
Packit 6c4009
  fmtchk("%4.4x");
Packit 6c4009
  fmtchk("%04.4x");
Packit 6c4009
  fmtchk("%4.3x");
Packit 6c4009
  fmtchk("%04.3x");
Packit 6c4009
Packit 6c4009
  fmtst1chk("%.*x");
Packit 6c4009
  fmtst1chk("%0*x");
Packit 6c4009
  fmtst2chk("%*.*x");
Packit 6c4009
  fmtst2chk("%0*.*x");
Packit 6c4009
Packit 6c4009
#ifndef	BSD
Packit 6c4009
  printf("bad format:\t\"%b\"\n");
Packit 6c4009
  printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  printf("decimal negative:\t\"%d\"\n", -2345);
Packit 6c4009
  printf("octal negative:\t\"%o\"\n", -2345);
Packit 6c4009
  printf("hex negative:\t\"%x\"\n", -2345);
Packit 6c4009
  printf("long decimal number:\t\"%ld\"\n", -123456L);
Packit 6c4009
  printf("long octal negative:\t\"%lo\"\n", -2345L);
Packit 6c4009
  printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
Packit 6c4009
  printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
Packit 6c4009
  printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
Packit 6c4009
  printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
Packit 6c4009
  printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
Packit 6c4009
Packit 6c4009
  printf("zero-padded string:\t\"%010s\"\n", shortstr);
Packit 6c4009
  printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
Packit 6c4009
  printf("space-padded string:\t\"%10s\"\n", shortstr);
Packit 6c4009
  printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
Packit 6c4009
  printf("null string:\t\"%s\"\n", (char *)NULL);
Packit 6c4009
  printf("limited string:\t\"%.22s\"\n", longstr);
Packit 6c4009
Packit 6c4009
  printf("a-style max:\t\"%a\"\n", DBL_MAX);
Packit 6c4009
  printf("a-style -max:\t\"%a\"\n", -DBL_MAX);
Packit 6c4009
  printf("e-style >= 1:\t\"%e\"\n", 12.34);
Packit 6c4009
  printf("e-style >= .1:\t\"%e\"\n", 0.1234);
Packit 6c4009
  printf("e-style < .1:\t\"%e\"\n", 0.001234);
Packit 6c4009
  printf("e-style big:\t\"%.60e\"\n", 1e20);
Packit 6c4009
  printf ("e-style == .1:\t\"%e\"\n", 0.1);
Packit 6c4009
  printf("f-style >= 1:\t\"%f\"\n", 12.34);
Packit 6c4009
  printf("f-style >= .1:\t\"%f\"\n", 0.1234);
Packit 6c4009
  printf("f-style < .1:\t\"%f\"\n", 0.001234);
Packit 6c4009
  printf("g-style >= 1:\t\"%g\"\n", 12.34);
Packit 6c4009
  printf("g-style >= .1:\t\"%g\"\n", 0.1234);
Packit 6c4009
  printf("g-style < .1:\t\"%g\"\n", 0.001234);
Packit 6c4009
  printf("g-style big:\t\"%.60g\"\n", 1e20);
Packit 6c4009
Packit 6c4009
  printf (" %6.5f\n", .099999999860301614);
Packit 6c4009
  printf (" %6.5f\n", .1);
Packit 6c4009
  printf ("x%5.4fx\n", .5);
Packit 6c4009
Packit 6c4009
  printf ("%#03x\n", 1);
Packit 6c4009
Packit 6c4009
  printf ("something really insane: %.10000f\n", 1.0);
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    double d = FLT_MIN;
Packit 6c4009
    int niter = 17;
Packit 6c4009
Packit 6c4009
    while (niter-- != 0)
Packit 6c4009
      printf ("%.17e\n", d / 2);
Packit 6c4009
    fflush (stdout);
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  printf ("%15.5e\n", 4.9406564584124654e-324);
Packit 6c4009
Packit 6c4009
#define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
Packit 6c4009
  printf (FORMAT, 0.0, 0.0, 0.0);
Packit 6c4009
  printf (FORMAT, 1.0, 1.0, 1.0);
Packit 6c4009
  printf (FORMAT, -1.0, -1.0, -1.0);
Packit 6c4009
  printf (FORMAT, 100.0, 100.0, 100.0);
Packit 6c4009
  printf (FORMAT, 1000.0, 1000.0, 1000.0);
Packit 6c4009
  printf (FORMAT, 10000.0, 10000.0, 10000.0);
Packit 6c4009
  printf (FORMAT, 12345.0, 12345.0, 12345.0);
Packit 6c4009
  printf (FORMAT, 100000.0, 100000.0, 100000.0);
Packit 6c4009
  printf (FORMAT, 123456.0, 123456.0, 123456.0);
Packit 6c4009
#undef	FORMAT
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    char buf[20];
Packit 6c4009
    char buf2[512];
Packit 6c4009
    printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
Packit 6c4009
	    snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
Packit 6c4009
	    buf);
Packit 6c4009
    printf ("snprintf (\"%%.999999u\", 10) == %d\n",
Packit 6c4009
	    snprintf(buf2, sizeof(buf2), "%.999999u", 10));
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  printf("%.8f\n", DBL_MAX);
Packit 6c4009
  printf("%.8f\n", -DBL_MAX);
Packit 6c4009
  printf ("%e should be 1.234568e+06\n", 1234567.8);
Packit 6c4009
  printf ("%f should be 1234567.800000\n", 1234567.8);
Packit 6c4009
  printf ("%g should be 1.23457e+06\n", 1234567.8);
Packit 6c4009
  printf ("%g should be 123.456\n", 123.456);
Packit 6c4009
  printf ("%g should be 1e+06\n", 1000000.0);
Packit 6c4009
  printf ("%g should be 10\n", 10.0);
Packit 6c4009
  printf ("%g should be 0.02\n", 0.02);
Packit 6c4009
Packit 6c4009
#if 0
Packit 6c4009
  /* This test rather checks the way the compiler handles constant
Packit 6c4009
     folding.  gcc behavior wrt to this changed in 3.2 so it is not a
Packit 6c4009
     portable test.  */
Packit 6c4009
  {
Packit 6c4009
    double x=1.0;
Packit 6c4009
    printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
Packit 6c4009
  }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    char buf[200];
Packit 6c4009
Packit 6c4009
    sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
Packit 6c4009
Packit 6c4009
    result |= strcmp (buf,
Packit 6c4009
		      "onetwo                 three                         ");
Packit 6c4009
Packit 6c4009
    puts (result != 0 ? "Test failed!" : "Test ok.");
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    char buf[200];
Packit 6c4009
Packit 6c4009
    sprintf (buf, "%07Lo", 040000000000ll);
Packit 6c4009
    printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
Packit 6c4009
Packit 6c4009
    if (strcmp (buf, "40000000000") != 0)
Packit 6c4009
      {
Packit 6c4009
	result = 1;
Packit 6c4009
	fputs ("\tFAILED", stdout);
Packit 6c4009
      }
Packit 6c4009
    puts ("");
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
Packit 6c4009
  printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
Packit 6c4009
  printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
Packit 6c4009
  printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
Packit 6c4009
Packit 6c4009
  printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
Packit 6c4009
	  UCHAR_MAX + 2, UCHAR_MAX + 2);
Packit 6c4009
  printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
Packit 6c4009
  printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
Packit 6c4009
	  UCHAR_MAX + 2, UCHAR_MAX + 2);
Packit 6c4009
  printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
Packit 6c4009
Packit 6c4009
  puts ("--- Should be no further output. ---");
Packit 6c4009
  rfg1 ();
Packit 6c4009
  rfg2 ();
Packit 6c4009
  rfg3 ();
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    char bytes[7];
Packit 6c4009
    char buf[20];
Packit 6c4009
Packit 6c4009
    memset (bytes, '\xff', sizeof bytes);
Packit 6c4009
    sprintf (buf, "foo%hhn\n", &bytes[3]);
Packit 6c4009
    if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
Packit 6c4009
	|| bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
Packit 6c4009
      {
Packit 6c4009
	puts ("%hhn overwrite more bytes");
Packit 6c4009
	result = 1;
Packit 6c4009
      }
Packit 6c4009
    if (bytes[3] != 3)
Packit 6c4009
      {
Packit 6c4009
	puts ("%hhn wrote incorrect value");
Packit 6c4009
	result = 1;
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  return result != 0;
Packit 6c4009
}
Packit 6c4009

Packit 6c4009
static void
Packit 6c4009
rfg1 (void)
Packit 6c4009
{
Packit 6c4009
  char buf[100];
Packit 6c4009
Packit 6c4009
  sprintf (buf, "%5.s", "xyz");
Packit 6c4009
  if (strcmp (buf, "     ") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "     ");
Packit 6c4009
  sprintf (buf, "%5.f", 33.3);
Packit 6c4009
  if (strcmp (buf, "   33") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "   33");
Packit 6c4009
  sprintf (buf, "%8.e", 33.3e7);
Packit 6c4009
  if (strcmp (buf, "   3e+08") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "   3e+08");
Packit 6c4009
  sprintf (buf, "%8.E", 33.3e7);
Packit 6c4009
  if (strcmp (buf, "   3E+08") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "   3E+08");
Packit 6c4009
  sprintf (buf, "%.g", 33.3);
Packit 6c4009
  if (strcmp (buf, "3e+01") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
Packit 6c4009
  sprintf (buf, "%.G", 33.3);
Packit 6c4009
  if (strcmp (buf, "3E+01") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
rfg2 (void)
Packit 6c4009
{
Packit 6c4009
  int prec;
Packit 6c4009
  char buf[100];
Packit 6c4009
Packit 6c4009
  prec = 0;
Packit 6c4009
  sprintf (buf, "%.*g", prec, 3.3);
Packit 6c4009
  if (strcmp (buf, "3") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "3");
Packit 6c4009
  prec = 0;
Packit 6c4009
  sprintf (buf, "%.*G", prec, 3.3);
Packit 6c4009
  if (strcmp (buf, "3") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "3");
Packit 6c4009
  prec = 0;
Packit 6c4009
  sprintf (buf, "%7.*G", prec, 3.33);
Packit 6c4009
  if (strcmp (buf, "      3") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "      3");
Packit 6c4009
  prec = 3;
Packit 6c4009
  sprintf (buf, "%04.*o", prec, 33);
Packit 6c4009
  if (strcmp (buf, " 041") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, " 041");
Packit 6c4009
  prec = 7;
Packit 6c4009
  sprintf (buf, "%09.*u", prec, 33);
Packit 6c4009
  if (strcmp (buf, "  0000033") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, "  0000033");
Packit 6c4009
  prec = 3;
Packit 6c4009
  sprintf (buf, "%04.*x", prec, 33);
Packit 6c4009
  if (strcmp (buf, " 021") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, " 021");
Packit 6c4009
  prec = 3;
Packit 6c4009
  sprintf (buf, "%04.*X", prec, 33);
Packit 6c4009
  if (strcmp (buf, " 021") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf, " 021");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
rfg3 (void)
Packit 6c4009
{
Packit 6c4009
  char buf[100];
Packit 6c4009
  double g = 5.0000001;
Packit 6c4009
  unsigned long l = 1234567890;
Packit 6c4009
  double d = 321.7654321;
Packit 6c4009
  const char s[] = "test-string";
Packit 6c4009
  int i = 12345;
Packit 6c4009
  int h = 1234;
Packit 6c4009
Packit 6c4009
  sprintf (buf,
Packit 6c4009
	   "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
Packit 6c4009
	   i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
Packit 6c4009
  if (strcmp (buf,
Packit 6c4009
	      "   12345  1234    11145401322     321.765432   3.217654e+02   5    test-string") != 0)
Packit 6c4009
    printf ("got: '%s', expected: '%s'\n", buf,
Packit 6c4009
	    "   12345  1234    11145401322     321.765432   3.217654e+02   5    test-string");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"