1 /* Test for the long double variants of *printf functions.
2 Copyright (C) 2019-2022 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
27 static void
do_test_call_varg(FILE * stream,const char * format,...)28 do_test_call_varg (FILE *stream, const char *format, ...)
29 {
30 char *buffer = NULL;
31 char string[128];
32 va_list args;
33
34 printf ("%15s", "vasprintf: ");
35 va_start (args, format);
36 vasprintf (&buffer, format, args);
37 va_end (args);
38 if (buffer == NULL)
39 printf ("Error using vasprintf\n");
40 else
41 {
42 printf ("%s", buffer);
43 free (buffer);
44 }
45 printf ("\n");
46
47 printf ("%15s", "vdprintf: ");
48 va_start (args, format);
49 vdprintf (fileno (stream), format, args);
50 va_end (args);
51 printf ("\n");
52
53 printf ("%15s", "vfprintf: ");
54 va_start (args, format);
55 vfprintf (stream, format, args);
56 va_end (args);
57 printf ("\n");
58
59 printf ("%15s", "vprintf: ");
60 va_start (args, format);
61 vprintf (format, args);
62 va_end (args);
63 printf ("\n");
64
65 printf ("%15s", "vsnprintf: ");
66 va_start (args, format);
67 vsnprintf (string, 127, format, args);
68 va_end (args);
69 printf ("%s", string);
70 printf ("\n");
71
72 printf ("%15s", "vsprintf: ");
73 va_start (args, format);
74 vsprintf (string, format, args);
75 va_end (args);
76 printf ("%s", string);
77 printf ("\n");
78 }
79
80 static void
do_test_call_rarg(FILE * stream,const char * format,long double ld,double d)81 do_test_call_rarg (FILE *stream, const char *format, long double ld, double d)
82 {
83 char *buffer = NULL;
84 char string[128];
85
86 printf ("%15s", "asprintf: ");
87 asprintf (&buffer, format, ld, d);
88 if (buffer == NULL)
89 printf ("Error using asprintf\n");
90 else
91 {
92 printf ("%s", buffer);
93 free (buffer);
94 }
95 printf ("\n");
96
97 printf ("%15s", "dprintf: ");
98 dprintf (fileno (stream), format, ld, d);
99 printf ("\n");
100
101 printf ("%15s", "fprintf: ");
102 fprintf (stream, format, ld, d);
103 printf ("\n");
104
105 printf ("%15s", "printf: ");
106 printf (format, ld, d);
107 printf ("\n");
108
109 printf ("%15s", "snprintf: ");
110 snprintf (string, 127, format, ld, d);
111 printf ("%s", string);
112 printf ("\n");
113
114 printf ("%15s", "sprintf: ");
115 sprintf (string, format, ld, d);
116 printf ("%s", string);
117 printf ("\n");
118 }
119
120 static void
do_test_call(void)121 do_test_call (void)
122 {
123 long double ld = -1;
124 double d = -1;
125
126 /* Print in decimal notation. */
127 do_test_call_rarg (stdout, "%.10Lf, %.10f", ld, d);
128 do_test_call_varg (stdout, "%.10Lf, %.10f", ld, d);
129
130 /* Print in hexadecimal notation. */
131 do_test_call_rarg (stdout, "%.10La, %.10a", ld, d);
132 do_test_call_varg (stdout, "%.10La, %.10a", ld, d);
133
134 /* Test positional parameters. */
135 do_test_call_varg (stdout, "%3$Lf, %2$Lf, %1$f",
136 (double) 1, (long double) 2, (long double) 3);
137 }
138
139 static int
do_test(void)140 do_test (void)
141 {
142 struct support_capture_subprocess result;
143 result = support_capture_subprocess ((void *) &do_test_call, NULL);
144
145 /* Compare against the expected output. */
146 const char *expected =
147 " asprintf: -1.0000000000, -1.0000000000\n"
148 " dprintf: -1.0000000000, -1.0000000000\n"
149 " fprintf: -1.0000000000, -1.0000000000\n"
150 " printf: -1.0000000000, -1.0000000000\n"
151 " snprintf: -1.0000000000, -1.0000000000\n"
152 " sprintf: -1.0000000000, -1.0000000000\n"
153 " vasprintf: -1.0000000000, -1.0000000000\n"
154 " vdprintf: -1.0000000000, -1.0000000000\n"
155 " vfprintf: -1.0000000000, -1.0000000000\n"
156 " vprintf: -1.0000000000, -1.0000000000\n"
157 " vsnprintf: -1.0000000000, -1.0000000000\n"
158 " vsprintf: -1.0000000000, -1.0000000000\n"
159 " asprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
160 " dprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
161 " fprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
162 " printf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
163 " snprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
164 " sprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
165 " vasprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
166 " vdprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
167 " vfprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
168 " vprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
169 " vsnprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
170 " vsprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
171 " vasprintf: 3.000000, 2.000000, 1.000000\n"
172 " vdprintf: 3.000000, 2.000000, 1.000000\n"
173 " vfprintf: 3.000000, 2.000000, 1.000000\n"
174 " vprintf: 3.000000, 2.000000, 1.000000\n"
175 " vsnprintf: 3.000000, 2.000000, 1.000000\n"
176 " vsprintf: 3.000000, 2.000000, 1.000000\n";
177 TEST_COMPARE_STRING (expected, result.out.buffer);
178
179 return 0;
180 }
181
182 #include <support/test-driver.c>
183