Blame gl/tests/test-inttostr.c

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