Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/* Test of POSIX compatible obstack_[v]zprintf() functions.
Copyright (C) 2007-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2024. */
/* This test exercises only a few POSIX compliance problems that are still
visible on platforms relevant in 2024. */
#include <config.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "obstack.h"
#include "macros.h"
#include "infinity.h"
#include "nan.h"
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
/* Test whether string[start_index..end_index-1] is a valid textual
representation of NaN. */
static int
strisnan (const char *string, size_t start_index, size_t end_index, int uppercase)
{
if (start_index < end_index)
{
if (string[start_index] == '-')
start_index++;
if (start_index + 3 <= end_index
&& memcmp (string + start_index, uppercase ? "NAN" : "nan", 3) == 0)
{
start_index += 3;
if (start_index == end_index
|| (string[start_index] == '(' && string[end_index - 1] == ')'))
return 1;
}
}
return 0;
}
static void
test_function (ptrdiff_t (*my_obstack_zprintf) (struct obstack *, const char *, ...))
{
struct obstack obs;
obstack_init (&obs);
#define RESULT_EQ(expected) \
(len == strlen (expected) && memcmp (result, expected, len) == 0)
/* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
output of floating-point numbers. */
{ /* This test would fail on Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%a %d", 3.1416015625, 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ ("0x1.922p+1 33")
|| RESULT_EQ ("0x3.244p+0 33")
|| RESULT_EQ ("0x6.488p-1 33")
|| RESULT_EQ ("0xc.91p-2 33"));
obstack_free (&obs, result);
}
{ /* This test would fail on Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%A %d", -3.1416015625, 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ ("-0X1.922P+1 33")
|| RESULT_EQ ("-0X3.244P+0 33")
|| RESULT_EQ ("-0X6.488P-1 33")
|| RESULT_EQ ("-0XC.91P-2 33"));
obstack_free (&obs, result);
}
{ /* This test would fail on FreeBSD 6.1, NetBSD 10.0. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%.2a %d", 1.51, 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ ("0x1.83p+0 33")
|| RESULT_EQ ("0x3.05p-1 33")
|| RESULT_EQ ("0x6.0ap-2 33")
|| RESULT_EQ ("0xc.14p-3 33"));
obstack_free (&obs, result);
}
{ /* This test would fail on macOS 14, FreeBSD 14.0, OpenBSD 7.5, AIX 7.3,
Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%.0a %d", 1.51, 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ ("0x2p+0 33")
|| RESULT_EQ ("0x3p-1 33")
|| RESULT_EQ ("0x6p-2 33")
|| RESULT_EQ ("0xcp-3 33"));
obstack_free (&obs, result);
}
/* Test the support of the %f format directive. */
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%f %d", Infinityd (), 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ ("inf 33")
|| RESULT_EQ ("infinity 33"));
obstack_free (&obs, result);
}
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%f %d", NaNd (), 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (len >= 3 + 3
&& strisnan (result, 0, len - 3, 0)
&& memcmp (result + len - 3, " 33", 3) == 0);
obstack_free (&obs, result);
}
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%010f %d", Infinityd (), 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ (" inf 33")
|| RESULT_EQ (" infinity 33"));
obstack_free (&obs, result);
}
/* Test the support of the %e format directive. */
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%e %d", Infinityd (), 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ ("inf 33")
|| RESULT_EQ ("infinity 33"));
obstack_free (&obs, result);
}
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%e %d", NaNd (), 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (len >= 3 + 3
&& strisnan (result, 0, len - 3, 0)
&& memcmp (result + len - 3, " 33", 3) == 0);
obstack_free (&obs, result);
}
/* Test the support of the %g format directive. */
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%g %d", Infinityd (), 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ ("inf 33")
|| RESULT_EQ ("infinity 33"));
obstack_free (&obs, result);
}
{ /* This test would fail on AIX 7.3, Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%g %d", NaNd (), 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (len >= 3 + 3
&& strisnan (result, 0, len - 3, 0)
&& memcmp (result + len - 3, " 33", 3) == 0);
obstack_free (&obs, result);
}
/* Test the support of large precision. */
{ /* This test would fail on AIX 7.1. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%.4000d %d", 1234567, 99);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (len == 4000 + 3);
size_t i;
for (i = 0; i < 4000 - 7; i++)
ASSERT (result[i] == '0');
ASSERT (memcmp (result + 4000 - 7, "1234567 99", 7 + 3) == 0);
obstack_free (&obs, result);
}
/* Test the support of the 'b' conversion specifier for binary output of
integers. */
{ /* This test would fail on glibc 2.34, musl libc, macOS 14,
FreeBSD 13.2, NetBSD 10.0, OpenBSD 7.5, AIX 7.3, Solaris 11.4. */
ptrdiff_t len =
my_obstack_zprintf (&obs, "%b %d", 12345, 33, 44, 55);
ASSERT (len >= 0);
char *result = obstack_finish (&obs);
ASSERT (RESULT_EQ ("11000000111001 33"));
obstack_free (&obs, result);
}
obstack_free (&obs, NULL);
}
static ptrdiff_t
my_obstack_zprintf (struct obstack *obs, const char *format, ...)
{
va_list args;
ptrdiff_t ret;
va_start (args, format);
ret = obstack_vzprintf (obs, format, args);
va_end (args);
return ret;
}
static void
test_obstack_vzprintf ()
{
test_function (my_obstack_zprintf);
}
static void
test_obstack_zprintf ()
{
test_function (obstack_zprintf);
}
int
main (int argc, char *argv[])
{
test_obstack_vzprintf ();
test_obstack_zprintf ();
return test_exit_status;
}