Update.
[glibc.git] / libio / tst_putwc.c
blob60bb232a78af3061c8bc4b3e948324d190fc2a4d
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.
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.
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.
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. */
21 #include <errno.h>
22 #include <error.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <wchar.h>
27 static const char outname[] = OBJPFX "tst_putwc.temp";
30 #define TEST_FUNCTION do_test ()
31 int
32 do_test (void)
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;
40 fp = fopen (outname, "w+");
41 if (fp == NULL)
42 error (EXIT_FAILURE, errno, "cannot open temporary file");
44 for (n = 0; str[n] != L'\0'; ++n)
45 putwc (str[n], fp);
47 /* First try reading after rewinding. */
48 rewind (fp);
50 wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
51 n = 0;
52 while (! feof (fp) && n < sizeof (buf) - 1)
54 buf[n] = getwc (fp);
55 if (buf[n] == WEOF)
56 break;
57 ++n;
59 buf[n] = L'\0';
61 if (wcscmp (buf, L"This is a test of putwc\n") != 0)
63 puts ("first comparison failed");
64 res = 1;
67 /* Now close the file, open it again, and read again. */
68 if (fclose (fp) != 0)
70 printf ("failure during fclose(): %m");
71 res = 1;
74 fp = fopen (outname, "r");
75 if (fp == NULL)
76 error (EXIT_FAILURE, errno, "cannot reopen file");
78 /* We can remove the file now. */
79 remove (outname);
81 wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
82 n = 0;
83 while (! feof (fp) && n < sizeof (buf) - 1)
85 buf[n] = getwc (fp);
86 if (buf[n] == WEOF)
87 break;
88 ++n;
90 buf[n] = L'\0';
92 if (wcscmp (buf, L"This is a test of putwc\n") != 0)
94 puts ("second comparison failed");
95 res = 1;
98 if (fclose (fp) != 0)
100 puts ("failure during fclose()");
101 res = 1;
104 /* Next test: write a bit more than a few bytes. */
105 fp = fopen (outname, "w");
106 if (fp == NULL)
107 error (EXIT_FAILURE, errno, "cannot open temporary file");
109 for (n = 0; n < 4098; ++n)
110 putwc (n & 255, fp);
112 fclose (fp);
114 return res;
118 #include "../test-skeleton.c"