1/* Test for the long double variants of *printf functions.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <stdarg.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <support/capture_subprocess.h>
25#include <support/check.h>
26
27static void
28do_test_call_varg (FILE *stream, const char *format, ...)
29{
30 char *buffer = NULL;
31 char string[128];
32 va_list args;
33 int ret;
34
35 printf ("%15s", "vasprintf: ");
36 va_start (args, format);
37 ret = vasprintf (ptr: &buffer, fmt: format, ap: args);
38 va_end (args);
39 if (ret == -1 || buffer == NULL)
40 printf ("Error using vasprintf\n");
41 else
42 {
43 printf ("%s", buffer);
44 free (ptr: buffer);
45 }
46 printf ("\n");
47
48 printf ("%15s", "vdprintf: ");
49 va_start (args, format);
50 vdprintf (fd: fileno (stream: stream), fmt: format, ap: args);
51 va_end (args);
52 printf ("\n");
53
54 printf ("%15s", "vfprintf: ");
55 va_start (args, format);
56 vfprintf (stream: stream, fmt: format, ap: args);
57 va_end (args);
58 printf ("\n");
59
60 printf ("%15s", "vprintf: ");
61 va_start (args, format);
62 vprintf (fmt: format, ap: args);
63 va_end (args);
64 printf ("\n");
65
66 printf ("%15s", "vsnprintf: ");
67 va_start (args, format);
68 vsnprintf (s: string, n: 127, fmt: format, ap: args);
69 va_end (args);
70 printf ("%s", string);
71 printf ("\n");
72
73 printf ("%15s", "vsprintf: ");
74 va_start (args, format);
75 vsprintf (s: string, fmt: format, ap: args);
76 va_end (args);
77 printf ("%s", string);
78 printf ("\n");
79}
80
81static void
82do_test_call_rarg (FILE *stream, const char *format, long double ld, double d)
83{
84 char *buffer = NULL;
85 char string[128];
86 int ret;
87
88 printf ("%15s", "asprintf: ");
89 ret = asprintf (&buffer, format, ld, d);
90 if (ret == -1 || buffer == NULL)
91 printf ("Error using asprintf\n");
92 else
93 {
94 printf ("%s", buffer);
95 free (ptr: buffer);
96 }
97 printf ("\n");
98
99 printf ("%15s", "dprintf: ");
100 dprintf (fileno (stream), format, ld, d);
101 printf ("\n");
102
103 printf ("%15s", "fprintf: ");
104 fprintf (stream, format, ld, d);
105 printf ("\n");
106
107 printf ("%15s", "printf: ");
108 printf (format, ld, d);
109 printf ("\n");
110
111 printf ("%15s", "snprintf: ");
112 snprintf (string, 127, format, ld, d);
113 printf ("%s", string);
114 printf ("\n");
115
116 printf ("%15s", "sprintf: ");
117 sprintf (string, format, ld, d);
118 printf ("%s", string);
119 printf ("\n");
120}
121
122static void
123do_test_call (void)
124{
125 long double ld = -1;
126 double d = -1;
127
128 /* Print in decimal notation. */
129 do_test_call_rarg (stdout, format: "%.10Lf, %.10f", ld, d);
130 do_test_call_varg (stdout, format: "%.10Lf, %.10f", ld, d);
131
132 /* Print in hexadecimal notation. */
133 do_test_call_rarg (stdout, format: "%.10La, %.10a", ld, d);
134 do_test_call_varg (stdout, format: "%.10La, %.10a", ld, d);
135
136 /* Test positional parameters. */
137 do_test_call_varg (stdout, format: "%3$Lf, %2$Lf, %1$f",
138 (double) 1, (long double) 2, (long double) 3);
139}
140
141static int
142do_test (void)
143{
144 struct support_capture_subprocess result;
145 result = support_capture_subprocess (callback: (void *) &do_test_call, NULL);
146
147 /* Compare against the expected output. */
148 const char *expected =
149 " asprintf: -1.0000000000, -1.0000000000\n"
150 " dprintf: -1.0000000000, -1.0000000000\n"
151 " fprintf: -1.0000000000, -1.0000000000\n"
152 " printf: -1.0000000000, -1.0000000000\n"
153 " snprintf: -1.0000000000, -1.0000000000\n"
154 " sprintf: -1.0000000000, -1.0000000000\n"
155 " vasprintf: -1.0000000000, -1.0000000000\n"
156 " vdprintf: -1.0000000000, -1.0000000000\n"
157 " vfprintf: -1.0000000000, -1.0000000000\n"
158 " vprintf: -1.0000000000, -1.0000000000\n"
159 " vsnprintf: -1.0000000000, -1.0000000000\n"
160 " vsprintf: -1.0000000000, -1.0000000000\n"
161 " asprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
162 " dprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
163 " fprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
164 " printf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
165 " snprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
166 " sprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
167 " vasprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
168 " vdprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
169 " vfprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
170 " vprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
171 " vsnprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
172 " vsprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
173 " vasprintf: 3.000000, 2.000000, 1.000000\n"
174 " vdprintf: 3.000000, 2.000000, 1.000000\n"
175 " vfprintf: 3.000000, 2.000000, 1.000000\n"
176 " vprintf: 3.000000, 2.000000, 1.000000\n"
177 " vsnprintf: 3.000000, 2.000000, 1.000000\n"
178 " vsprintf: 3.000000, 2.000000, 1.000000\n";
179 TEST_COMPARE_STRING (expected, result.out.buffer);
180
181 return 0;
182}
183
184#include <support/test-driver.c>
185

source code of glibc/sysdeps/ieee754/ldbl-128ibm-compat/test-printf-ldbl-compat.c