1/* Simple test of putwc in the C locale.
2 Copyright (C) 2000-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 <errno.h>
20#include <error.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <wchar.h>
24
25static const char outname[] = OBJPFX "tst_putwc.temp";
26
27/* Prototype for our test function. */
28int do_test (void);
29#define TEST_FUNCTION do_test ()
30
31/* This defines the `main' function and some more. */
32#include <test-skeleton.c>
33
34int
35do_test (void)
36{
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;
42
43 add_temp_file (name: outname);
44
45 fp = fopen (outname, "w+");
46 if (fp == NULL)
47 error (EXIT_FAILURE, errno, format: "cannot open temporary file");
48
49 for (n = 0; str[n] != L'\0'; ++n)
50 putwc (str[n], fp);
51
52 /* First try reading after rewinding. */
53 rewind (fp);
54
55 wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
56 n = 0;
57 while (! feof (stream: fp) && n < sizeof (buf) - 1)
58 {
59 buf[n] = getwc (stream: fp);
60 if (buf[n] == WEOF)
61 break;
62 ++n;
63 }
64 buf[n] = L'\0';
65
66 if (wcscmp (s1: buf, s2: L"This is a test of putwc\n") != 0)
67 {
68 puts (s: "first comparison failed");
69 res = 1;
70 }
71
72 /* Now close the file, open it again, and read again. */
73 if (fclose (fp) != 0)
74 {
75 printf (format: "failure during fclose: %m\n");
76 res = 1;
77 }
78
79 fp = fopen (outname, "r");
80 if (fp == NULL)
81 {
82 printf (format: "cannot reopen file: %m\n");
83 return 1;
84 }
85
86 /* We can remove the file now. */
87 remove (outname);
88
89 wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
90 n = 0;
91 while (! feof (stream: fp) && n < sizeof (buf) - 1)
92 {
93 buf[n] = getwc (stream: fp);
94 if (buf[n] == WEOF)
95 break;
96 ++n;
97 }
98 buf[n] = L'\0';
99
100 if (wcscmp (s1: buf, s2: L"This is a test of putwc\n") != 0)
101 {
102 puts (s: "second comparison failed");
103 res = 1;
104 }
105
106 if (fclose (fp) != 0)
107 {
108 printf (format: "failure during fclose: %m\n");
109 res = 1;
110 }
111
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, format: "cannot open temporary file");
116
117 for (n = 0; n < 4098; ++n)
118 putwc (n & 255, fp);
119
120 fclose (fp);
121
122 return res;
123}
124

source code of glibc/libio/tst_putwc.c