Update.
[platform/upstream/glibc.git] / libio / tst_putwc.c
1 /* Simple test of putwc in the C locale.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <errno.h>
22 #include <error.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <wchar.h>
26
27 static const char outname[] = OBJPFX "tst_putwc.temp";
28
29
30 #define TEST_FUNCTION do_test ()
31 int
32 do_test (void)
33 {
34   const wchar_t str[] = L"This is a test of putwc\n";
35   wchar_t buf[100];
36   size_t n = 0;
37   FILE *fp;
38   int res = 0;
39
40   fp = fopen (outname, "w+");
41   if (fp == NULL)
42     error (EXIT_FAILURE, errno, "cannot open temporary file");
43
44   for (n = 0; str[n] != L'\0'; ++n)
45     putwc (str[n], fp);
46
47   /* First try reading after rewinding.  */
48   rewind (fp);
49
50   wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
51   n = 0;
52   while (! feof (fp) && n < sizeof (buf) - 1)
53     {
54       buf[n] = getwc (fp);
55       if (buf[n] == WEOF)
56         break;
57       ++n;
58     }
59   buf[n] = L'\0';
60
61   if (wcscmp (buf, L"This is a test of putwc\n") != 0)
62     {
63       puts ("first comparison failed");
64       res = 1;
65     }
66
67   /* Now close the file, open it again, and read again.  */
68   if (fclose (fp) != 0)
69     {
70       printf ("failure during fclose(): %m");
71       res = 1;
72     }
73
74   fp = fopen (outname, "r");
75   if (fp == NULL)
76     error (EXIT_FAILURE, errno, "cannot reopen file");
77
78   /* We can remove the file now.  */
79   remove (outname);
80
81   wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
82   n = 0;
83   while (! feof (fp) && n < sizeof (buf) - 1)
84     {
85       buf[n] = getwc (fp);
86       if (buf[n] == WEOF)
87         break;
88       ++n;
89     }
90   buf[n] = L'\0';
91
92   if (wcscmp (buf, L"This is a test of putwc\n") != 0)
93     {
94       puts ("second comparison failed");
95       res = 1;
96     }
97
98   if (fclose (fp) != 0)
99     {
100       puts ("failure during fclose()");
101       res = 1;
102     }
103
104   return res;
105 }
106
107
108 #include "../test-skeleton.c"