[v3,0/7] Fix some libm static issues
[platform/upstream/glibc.git] / libio / tst-bz24051.c
1 /* Test that assigning to stdout redirects puts, putchar, etc (BZ#24051)
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
20 /* Prevent putchar -> _IO_putc inline expansion.  */
21 #define __NO_INLINE__
22 #pragma GCC optimize("O0")
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <wchar.h>
27
28 #include <array_length.h>
29 #include <support/check.h>
30 #include <support/temp_file.h>
31 #include <support/test-driver.h>
32
33 #undef putchar
34 #undef putwchar
35
36 static int
37 do_test_narrow (void)
38 {
39   char buf[100];
40   int fd = create_temp_file ("tst-bz24051", NULL);
41   stdout = fdopen (fd, "w+");
42   TEST_VERIFY_EXIT (stdout != NULL);
43
44   printf ("ab%s", "cd");
45   putchar ('e');
46   putchar_unlocked ('f');
47   puts ("ghi");
48
49   rewind (stdout);
50   TEST_VERIFY_EXIT (fgets (buf, sizeof (buf), stdout) != NULL);
51   TEST_VERIFY (strcmp (buf, "abcdefghi\n") == 0);
52
53   return 0;
54 }
55
56 static int
57 do_test_wide (void)
58 {
59   wchar_t buf[100];
60   int fd = create_temp_file ("tst-bz24051w", NULL);
61   stdout = fdopen (fd, "w+");
62   TEST_VERIFY_EXIT (stdout != NULL);
63
64   wprintf (L"ab%ls", L"cd");
65   putwchar (L'e');
66   putwchar_unlocked (L'f');
67
68   rewind (stdout);
69   TEST_VERIFY_EXIT (fgetws (buf, array_length (buf), stdout) != NULL);
70   TEST_VERIFY (wcscmp (buf, L"abcdef") == 0);
71
72   return 0;
73 }
74
75 static int
76 do_test (void)
77 {
78   return do_test_narrow () + do_test_wide ();
79 }
80
81 #include <support/test-driver.c>