VirtualBox

source: kBuild/vendor/grep/2.12/gnulib-tests/test-inttostr.c@ 3576

Last change on this file since 3576 was 2595, checked in by bird, 13 years ago

gnu grep version 2.12 (grep-2.12.tar.xz, md5sum=8d2f0346d08b13c18afb81f0e8aa1e2f)

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1/* Test inttostr functions, and incidentally, INT_BUFSIZE_BOUND
2 Copyright (C) 2010-2012 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 <string.h>
27
28#include "macros.h"
29
30#define STREQ(a, b) (strcmp (a, b) == 0)
31#define FMT(T) (TYPE_SIGNED (T) ? "%jd" : "%ju")
32#define CAST_VAL(T,V) (TYPE_SIGNED (T) ? (intmax_t) (V) : (uintmax_t) (V))
33#define V_min(T) (CAST_VAL (T, TYPE_MINIMUM (T)))
34#define V_max(T) (CAST_VAL (T, TYPE_MAXIMUM (T)))
35#define IS_TIGHT(T) (_GL_SIGNED_TYPE_OR_EXPR (T) == TYPE_SIGNED (T))
36#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
37
38/* Verify that an inttostr function works as advertised.
39 Convert maximum and minimum (per-type, T) values using both snprintf --
40 with a cast to intmax_t or uintmax_t -- and FN, and compare the
41 resulting strings. Use malloc for the inttostr buffer, so that if
42 we ever exceed the usually-tight INT_BUFSIZE_BOUND, tools like
43 valgrind will detect the failure. */
44#define CK(T, Fn) \
45 do \
46 { \
47 char ref[100]; \
48 char *buf = malloc (INT_BUFSIZE_BOUND (T)); \
49 char const *p; \
50 ASSERT (buf); \
51 *buf = '\0'; \
52 ASSERT (snprintf (ref, sizeof ref, FMT (T), V_min (T)) < sizeof ref); \
53 ASSERT (STREQ ((p = Fn (TYPE_MINIMUM (T), buf)), ref)); \
54 /* Ensure that INT_BUFSIZE_BOUND is tight for signed types. */ \
55 ASSERT (! TYPE_SIGNED (T) || (p == buf && *p == '-')); \
56 ASSERT (snprintf (ref, sizeof ref, FMT (T), V_max (T)) < sizeof ref); \
57 ASSERT (STREQ ((p = Fn (TYPE_MAXIMUM (T), buf)), ref)); \
58 /* For unsigned types, the bound is not always tight. */ \
59 ASSERT (! IS_TIGHT (T) || TYPE_SIGNED (T) \
60 || (p == buf && ISDIGIT (*p))); \
61 free (buf); \
62 } \
63 while (0)
64
65int
66main (void)
67{
68 size_t b_size = 2;
69 char *b = malloc (b_size);
70 ASSERT (b);
71
72 /* Ideally we would rely on the snprintf-posix module, in which case
73 this guard would not be required, but due to limitations in gnulib's
74 implementation (see modules/snprintf-posix), we cannot. */
75 if (snprintf (b, b_size, "%ju", (uintmax_t) 3) == 1
76 && b[0] == '3' && b[1] == '\0')
77 {
78 CK (int, inttostr);
79 CK (unsigned int, uinttostr);
80 CK (off_t, offtostr);
81 CK (uintmax_t, umaxtostr);
82 CK (intmax_t, imaxtostr);
83 return 0;
84 }
85
86 /* snprintf doesn't accept %ju; skip this test. */
87 return 77;
88}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette