Back to index

glibc  2.9
tst_putwc.c
Go to the documentation of this file.
00001 /* Simple test of putwc in the C locale.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <error.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <wchar.h>
00026 
00027 static const char outname[] = OBJPFX "tst_putwc.temp";
00028 
00029 /* Prototype for our test function.  */
00030 int do_test (void);
00031 #define TEST_FUNCTION do_test ()
00032 
00033 /* This defines the `main' function and some more.  */
00034 #include <test-skeleton.c>
00035 
00036 int
00037 do_test (void)
00038 {
00039   const wchar_t str[] = L"This is a test of putwc\n";
00040   wchar_t buf[100];
00041   size_t n = 0;
00042   FILE *fp;
00043   int res = 0;
00044 
00045   add_temp_file (outname);
00046 
00047   fp = fopen (outname, "w+");
00048   if (fp == NULL)
00049     error (EXIT_FAILURE, errno, "cannot open temporary file");
00050 
00051   for (n = 0; str[n] != L'\0'; ++n)
00052     putwc (str[n], fp);
00053 
00054   /* First try reading after rewinding.  */
00055   rewind (fp);
00056 
00057   wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
00058   n = 0;
00059   while (! feof (fp) && n < sizeof (buf) - 1)
00060     {
00061       buf[n] = getwc (fp);
00062       if (buf[n] == WEOF)
00063        break;
00064       ++n;
00065     }
00066   buf[n] = L'\0';
00067 
00068   if (wcscmp (buf, L"This is a test of putwc\n") != 0)
00069     {
00070       puts ("first comparison failed");
00071       res = 1;
00072     }
00073 
00074   /* Now close the file, open it again, and read again.  */
00075   if (fclose (fp) != 0)
00076     {
00077       printf ("failure during fclose(): %m");
00078       res = 1;
00079     }
00080 
00081   fp = fopen (outname, "r");
00082   if (fp == NULL)
00083     error (EXIT_FAILURE, errno, "cannot reopen file");
00084 
00085   /* We can remove the file now.  */
00086   remove (outname);
00087 
00088   wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
00089   n = 0;
00090   while (! feof (fp) && n < sizeof (buf) - 1)
00091     {
00092       buf[n] = getwc (fp);
00093       if (buf[n] == WEOF)
00094        break;
00095       ++n;
00096     }
00097   buf[n] = L'\0';
00098 
00099   if (wcscmp (buf, L"This is a test of putwc\n") != 0)
00100     {
00101       puts ("second comparison failed");
00102       res = 1;
00103     }
00104 
00105   if (fclose (fp) != 0)
00106     {
00107       puts ("failure during fclose()");
00108       res = 1;
00109     }
00110 
00111   /* Next test: write a bit more than a few bytes.  */
00112   fp = fopen (outname, "w");
00113   if (fp == NULL)
00114     error (EXIT_FAILURE, errno, "cannot open temporary file");
00115 
00116   for (n = 0; n < 4098; ++n)
00117     putwc (n & 255, fp);
00118 
00119   fclose (fp);
00120 
00121   return res;
00122 }