]>
Commit | Line | Data |
---|---|---|
421a1d34 GG |
1 | /* Test for the long double variants of *printf functions. |
2 | Copyright (C) 2019 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 | <http://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 | |
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 | |
81 | do_test_call_rarg (FILE *stream, const char *format, long double ld) | |
82 | { | |
83 | char *buffer = NULL; | |
84 | char string[128]; | |
85 | ||
86 | printf ("%15s", "asprintf: "); | |
87 | asprintf (&buffer, format, ld); | |
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); | |
99 | printf ("\n"); | |
100 | ||
101 | printf ("%15s", "fprintf: "); | |
102 | fprintf (stream, format, ld); | |
103 | printf ("\n"); | |
104 | ||
105 | printf ("%15s", "printf: "); | |
106 | printf (format, ld); | |
107 | printf ("\n"); | |
108 | ||
109 | printf ("%15s", "snprintf: "); | |
110 | snprintf (string, 127, format, ld); | |
111 | printf ("%s", string); | |
112 | printf ("\n"); | |
113 | ||
114 | printf ("%15s", "sprintf: "); | |
115 | sprintf (string, format, ld); | |
116 | printf ("%s", string); | |
117 | printf ("\n"); | |
118 | } | |
119 | ||
120 | static void | |
121 | do_test_call (void) | |
122 | { | |
123 | long double ld = -1; | |
124 | ||
125 | /* Print in decimal notation. */ | |
126 | do_test_call_rarg (stdout, "%.10Lf", ld); | |
127 | do_test_call_varg (stdout, "%.10Lf", ld); | |
128 | ||
129 | /* Print in hexadecimal notation. */ | |
130 | do_test_call_rarg (stdout, "%.10La", ld); | |
131 | do_test_call_varg (stdout, "%.10La", ld); | |
132 | } | |
133 | ||
134 | static int | |
135 | do_test (void) | |
136 | { | |
137 | struct support_capture_subprocess result; | |
138 | result = support_capture_subprocess ((void *) &do_test_call, NULL); | |
139 | ||
140 | /* Compare against the expected output. */ | |
141 | const char *expected = | |
142 | " asprintf: -1.0000000000\n" | |
143 | " dprintf: -1.0000000000\n" | |
144 | " fprintf: -1.0000000000\n" | |
145 | " printf: -1.0000000000\n" | |
146 | " snprintf: -1.0000000000\n" | |
147 | " sprintf: -1.0000000000\n" | |
148 | " vasprintf: -1.0000000000\n" | |
149 | " vdprintf: -1.0000000000\n" | |
150 | " vfprintf: -1.0000000000\n" | |
151 | " vprintf: -1.0000000000\n" | |
152 | " vsnprintf: -1.0000000000\n" | |
153 | " vsprintf: -1.0000000000\n" | |
154 | " asprintf: -0x1.0000000000p+0\n" | |
155 | " dprintf: -0x1.0000000000p+0\n" | |
156 | " fprintf: -0x1.0000000000p+0\n" | |
157 | " printf: -0x1.0000000000p+0\n" | |
158 | " snprintf: -0x1.0000000000p+0\n" | |
159 | " sprintf: -0x1.0000000000p+0\n" | |
160 | " vasprintf: -0x1.0000000000p+0\n" | |
161 | " vdprintf: -0x1.0000000000p+0\n" | |
162 | " vfprintf: -0x1.0000000000p+0\n" | |
163 | " vprintf: -0x1.0000000000p+0\n" | |
164 | " vsnprintf: -0x1.0000000000p+0\n" | |
165 | " vsprintf: -0x1.0000000000p+0\n"; | |
166 | TEST_COMPARE_STRING (expected, result.out.buffer); | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | #include <support/test-driver.c> |