malloc/Makefile: Split and sort tests
[glibc.git] / libio / tst_putwc.c
blobf6139a32ce27986abc084b5d94e362f1071f09d6
1 /* Simple test of putwc in the C locale.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <errno.h>
20 #include <error.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <wchar.h>
25 static const char outname[] = OBJPFX "tst_putwc.temp";
27 /* Prototype for our test function. */
28 int do_test (void);
29 #define TEST_FUNCTION do_test ()
31 /* This defines the `main' function and some more. */
32 #include <test-skeleton.c>
34 int
35 do_test (void)
37 const wchar_t str[] = L"This is a test of putwc\n";
38 wchar_t buf[100];
39 size_t n = 0;
40 FILE *fp;
41 int res = 0;
43 add_temp_file (outname);
45 fp = fopen (outname, "w+");
46 if (fp == NULL)
47 error (EXIT_FAILURE, errno, "cannot open temporary file");
49 for (n = 0; str[n] != L'\0'; ++n)
50 putwc (str[n], fp);
52 /* First try reading after rewinding. */
53 rewind (fp);
55 wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
56 n = 0;
57 while (! feof (fp) && n < sizeof (buf) - 1)
59 buf[n] = getwc (fp);
60 if (buf[n] == WEOF)
61 break;
62 ++n;
64 buf[n] = L'\0';
66 if (wcscmp (buf, L"This is a test of putwc\n") != 0)
68 puts ("first comparison failed");
69 res = 1;
72 /* Now close the file, open it again, and read again. */
73 if (fclose (fp) != 0)
75 printf ("failure during fclose: %m\n");
76 res = 1;
79 fp = fopen (outname, "r");
80 if (fp == NULL)
82 printf ("cannot reopen file: %m\n");
83 return 1;
86 /* We can remove the file now. */
87 remove (outname);
89 wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
90 n = 0;
91 while (! feof (fp) && n < sizeof (buf) - 1)
93 buf[n] = getwc (fp);
94 if (buf[n] == WEOF)
95 break;
96 ++n;
98 buf[n] = L'\0';
100 if (wcscmp (buf, L"This is a test of putwc\n") != 0)
102 puts ("second comparison failed");
103 res = 1;
106 if (fclose (fp) != 0)
108 printf ("failure during fclose: %m\n");
109 res = 1;
112 /* Next test: write a bit more than a few bytes. */
113 fp = fopen (outname, "w");
114 if (fp == NULL)
115 error (EXIT_FAILURE, errno, "cannot open temporary file");
117 for (n = 0; n < 4098; ++n)
118 putwc (n & 255, fp);
120 fclose (fp);
122 return res;