test-linkat: avoid failed assertion on "other" architectures
[gnulib.git] / tests / test-inttostr.c
1 /* Test inttostr functions, and incidentally, INT_BUFSIZE_BOUND
2    Copyright (C) 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Jim Meyering.  */
18
19 #include <config.h>
20
21 #include "inttostr.h"
22 #include "intprops.h"
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <assert.h>
27
28 #define STREQ(a, b) (strcmp (a, b) == 0)
29 #define FMT(T) (TYPE_SIGNED (T) ? "%jd" : "%ju")
30 #define CAST_VAL(T,V) (TYPE_SIGNED (T) ? (intmax_t) (V) : (uintmax_t) (V))
31 #define V_min(T) (CAST_VAL (T, TYPE_MINIMUM (T)))
32 #define V_max(T) (CAST_VAL (T, TYPE_MAXIMUM (T)))
33 #define IS_TIGHT(T) (signed_type_or_expr__(T) == TYPE_SIGNED (T))
34 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
35
36 /* Verify that an inttostr function works as advertised.
37    Convert maximum and minimum (per-type, T) values using both snprintf --
38    with a cast to intmax_t or uintmax_t -- and FN, and compare the
39    resulting strings.  Use malloc for the inttostr buffer, so that if
40    we ever exceed the usually-tight INT_BUFSIZE_BOUND, tools like
41    valgrind will detect the failure. */
42 #define CK(T, Fn)                                                       \
43   do                                                                    \
44     {                                                                   \
45       char ref[100];                                                    \
46       char *buf = malloc (INT_BUFSIZE_BOUND (T));                       \
47       char const *p;                                                    \
48       assert (buf);                                                     \
49       *buf = '\0';                                                      \
50       assert (snprintf (ref, sizeof ref, FMT (T), V_min (T)) < sizeof ref); \
51       assert (STREQ ((p = Fn (TYPE_MINIMUM (T), buf)), ref));           \
52       /* Ensure that INT_BUFSIZE_BOUND is tight for signed types.  */   \
53       assert (! TYPE_SIGNED (T) || (p == buf && *p == '-'));            \
54       assert (snprintf (ref, sizeof ref, FMT (T), V_max (T)) < sizeof ref); \
55       assert (STREQ ((p = Fn (TYPE_MAXIMUM (T), buf)), ref));           \
56       /* For unsigned types, the bound is not always tight.  */         \
57       assert (! IS_TIGHT (T) || TYPE_SIGNED (T)                         \
58               || (p == buf && ISDIGIT (*p)));                           \
59       free (buf);                                                       \
60     }                                                                   \
61   while (0)
62
63 int
64 main (void)
65 {
66   CK (int,          inttostr);
67   CK (unsigned int, uinttostr);
68   CK (off_t,        offtostr);
69   CK (uintmax_t,    umaxtostr);
70   CK (intmax_t,     imaxtostr);
71   return 0;
72 }