1 /* Test for the long double variants of *w*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 <wchar.h>
23 
24 #include <support/capture_subprocess.h>
25 #include <support/check.h>
26 
27 static void
do_test_call_varg(FILE * stream,const wchar_t * format,...)28 do_test_call_varg (FILE *stream, const wchar_t *format, ...)
29 {
30   wchar_t string[128];
31   va_list args;
32 
33   wprintf (L"%15Ls", L"vfwprintf: ");
34   va_start (args, format);
35   vfwprintf (stream, format, args);
36   va_end (args);
37   wprintf (L"\n");
38 
39   wprintf (L"%15Ls", L"vswprintf: ");
40   va_start (args, format);
41   vswprintf (string, 127, format, args);
42   va_end (args);
43   wprintf (L"%Ls", string);
44   wprintf (L"\n");
45 
46   wprintf (L"%15Ls", L"vwprintf: ");
47   va_start (args, format);
48   vwprintf (format, args);
49   va_end (args);
50   wprintf (L"\n");
51 }
52 
53 static void
do_test_call_rarg(FILE * stream,const wchar_t * format,long double ld,double d)54 do_test_call_rarg (FILE *stream, const wchar_t *format, long double ld,
55 		   double d)
56 {
57   wchar_t string[128];
58 
59   wprintf (L"%15Ls", L"fwprintf: ");
60   fwprintf (stream, format, ld, d);
61   wprintf (L"\n");
62 
63   wprintf (L"%15Ls", L"swprintf: ");
64   swprintf (string, 127, format, ld, d);
65   wprintf (L"%Ls", string);
66   wprintf (L"\n");
67 
68   wprintf (L"%15Ls", L"wprintf: ");
69   wprintf (format, ld, d);
70   wprintf (L"\n");
71 }
72 
73 static void
do_test_call(void)74 do_test_call (void)
75 {
76   long double ld = -1;
77   double d = -1;
78 
79   /* Print in decimal notation.  */
80   do_test_call_rarg (stdout, L"%.10Lf, %.10f", ld, d);
81   do_test_call_varg (stdout, L"%.10Lf, %.10f", ld, d);
82 
83   /* Print in hexadecimal notation.  */
84   do_test_call_rarg (stdout, L"%.10La, %.10a", ld, d);
85   do_test_call_varg (stdout, L"%.10La, %.10a", ld, d);
86 
87   /* Test positional parameters.  */
88   do_test_call_varg (stdout, L"%3$Lf, %2$Lf, %1$f",
89 		     (double) 1, (long double) 2, (long double) 3);
90 }
91 
92 static int
do_test(void)93 do_test (void)
94 {
95   struct support_capture_subprocess result;
96   result = support_capture_subprocess ((void *) &do_test_call, NULL);
97 
98   /* Compare against the expected output.  */
99   const char *expected =
100     "     fwprintf: -1.0000000000, -1.0000000000\n"
101     "     swprintf: -1.0000000000, -1.0000000000\n"
102     "      wprintf: -1.0000000000, -1.0000000000\n"
103     "    vfwprintf: -1.0000000000, -1.0000000000\n"
104     "    vswprintf: -1.0000000000, -1.0000000000\n"
105     "     vwprintf: -1.0000000000, -1.0000000000\n"
106     "     fwprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
107     "     swprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
108     "      wprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
109     "    vfwprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
110     "    vswprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
111     "     vwprintf: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
112     "    vfwprintf: 3.000000, 2.000000, 1.000000\n"
113     "    vswprintf: 3.000000, 2.000000, 1.000000\n"
114     "     vwprintf: 3.000000, 2.000000, 1.000000\n";
115   TEST_COMPARE_STRING (expected, result.out.buffer);
116 
117   return 0;
118 }
119 
120 #include <support/test-driver.c>
121