Blame gnulib-tests/test-inttostr.c

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