Blame gnulib-tests/test-inttostr.c

Packit 33f14e
/* Test inttostr functions, and incidentally, INT_BUFSIZE_BOUND
Packit 33f14e
   Copyright (C) 2010-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software: you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3 of the License, or
Packit 33f14e
   (at your option) any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
/* Written by Jim Meyering.  */
Packit 33f14e
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
#include "inttostr.h"
Packit 33f14e
#include "intprops.h"
Packit 33f14e
#include <inttypes.h>
Packit 33f14e
#include <stdio.h>
Packit 33f14e
#include <stdlib.h>
Packit 33f14e
#include <string.h>
Packit 33f14e
Packit 33f14e
#include "macros.h"
Packit 33f14e
Packit 33f14e
#define STREQ(a, b) (strcmp (a, b) == 0)
Packit 33f14e
#define IS_TIGHT(T) (_GL_SIGNED_TYPE_OR_EXPR (T) == TYPE_SIGNED (T))
Packit 33f14e
#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
Packit 33f14e
Packit 33f14e
/* Verify that an inttostr function works as advertised.
Packit 33f14e
   Convert maximum and minimum (per-type, T) values using both snprintf --
Packit 33f14e
   with a cast to intmax_t or uintmax_t -- and FN, and compare the
Packit 33f14e
   resulting strings.  Use malloc for the inttostr buffer, so that if
Packit 33f14e
   we ever exceed the usually-tight INT_BUFSIZE_BOUND, tools like
Packit 33f14e
   valgrind will detect the failure. */
Packit 33f14e
#define CK(T, Fn)                                                       \
Packit 33f14e
  do                                                                    \
Packit 33f14e
    {                                                                   \
Packit 33f14e
      char ref[100];                                                    \
Packit 33f14e
      char *buf = malloc (INT_BUFSIZE_BOUND (T));                       \
Packit 33f14e
      char const *p;                                                    \
Packit 33f14e
      ASSERT (buf);                                                     \
Packit 33f14e
      *buf = '\0';                                                      \
Packit 33f14e
      ASSERT                                                            \
Packit 33f14e
        ((TYPE_SIGNED (T)                                               \
Packit 33f14e
          ? snprintf (ref, sizeof ref, "%jd", (intmax_t) TYPE_MINIMUM (T)) \
Packit 33f14e
          : snprintf (ref, sizeof ref, "%ju", (uintmax_t) TYPE_MINIMUM (T))) \
Packit 33f14e
         < sizeof ref);                                                 \
Packit 33f14e
      ASSERT (STREQ ((p = Fn (TYPE_MINIMUM (T), buf)), ref));           \
Packit 33f14e
      /* Ensure that INT_BUFSIZE_BOUND is tight for signed types.  */   \
Packit 33f14e
      ASSERT (! TYPE_SIGNED (T) || (p == buf && *p == '-'));            \
Packit 33f14e
      ASSERT                                                            \
Packit 33f14e
        ((TYPE_SIGNED (T)                                               \
Packit 33f14e
          ? snprintf (ref, sizeof ref, "%jd", (intmax_t) TYPE_MAXIMUM (T)) \
Packit 33f14e
          : snprintf (ref, sizeof ref, "%ju", (uintmax_t) TYPE_MAXIMUM (T))) \
Packit 33f14e
         < sizeof ref);                                                 \
Packit 33f14e
      ASSERT (STREQ ((p = Fn (TYPE_MAXIMUM (T), buf)), ref));           \
Packit 33f14e
      /* For unsigned types, the bound is not always tight.  */         \
Packit 33f14e
      ASSERT (! IS_TIGHT (T) || TYPE_SIGNED (T)                         \
Packit 33f14e
              || (p == buf && ISDIGIT (*p)));                           \
Packit 33f14e
      free (buf);                                                       \
Packit 33f14e
    }                                                                   \
Packit 33f14e
  while (0)
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
main (void)
Packit 33f14e
{
Packit 33f14e
  size_t b_size = 2;
Packit 33f14e
  char *b = malloc (b_size);
Packit 33f14e
  ASSERT (b);
Packit 33f14e
Packit 33f14e
  /* Ideally we would rely on the snprintf-posix module, in which case
Packit 33f14e
     this guard would not be required, but due to limitations in gnulib's
Packit 33f14e
     implementation (see modules/snprintf-posix), we cannot.  */
Packit 33f14e
  if (snprintf (b, b_size, "%ju", (uintmax_t) 3) == 1
Packit 33f14e
      && b[0] == '3' && b[1] == '\0')
Packit 33f14e
    {
Packit 33f14e
      CK (int,          inttostr);
Packit 33f14e
      CK (unsigned int, uinttostr);
Packit 33f14e
      CK (off_t,        offtostr);
Packit 33f14e
      CK (uintmax_t,    umaxtostr);
Packit 33f14e
      CK (intmax_t,     imaxtostr);
Packit 33f14e
      free (b);
Packit 33f14e
      return 0;
Packit 33f14e
    }
Packit 33f14e
Packit 33f14e
  /* snprintf doesn't accept %ju; skip this test.  */
Packit 33f14e
  free (b);
Packit 33f14e
  return 77;
Packit 33f14e
}