Blame gnulib-tests/test-inttostr.c

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