Back to index

glibc  2.9
bug-wmemstream1.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <wchar.h>
00004 
00005 
00006 static int
00007 do_test (void)
00008 {
00009   size_t size;
00010   wchar_t *buf;
00011   FILE *fp = open_wmemstream (&buf, &size);
00012   if (fp == NULL)
00013     {
00014       puts ("open_wmemstream failed");
00015       return 1;
00016     }
00017 
00018   off64_t off = ftello64 (fp);
00019   if (off != 0)
00020     {
00021       puts ("initial position wrong");
00022       return 1;
00023     }
00024 
00025   if (fseek (fp, 32768, SEEK_SET) != 0)
00026     {
00027       puts ("fseek failed");
00028       return 1;
00029     }
00030 
00031   if (fputws (L"foo", fp) == EOF)
00032     {
00033       puts ("fputws failed");
00034       return 1;
00035     }
00036 
00037   if (fclose (fp) == EOF)
00038     {
00039       puts ("fclose failed");
00040       return 1;
00041     }
00042 
00043   if (size != 32768 + 3)
00044     {
00045       printf ("expected size %d, got %zu\n", 32768 + 3, size);
00046       return 1;
00047     }
00048 
00049   for (int i = 0; i < 32768; ++i)
00050     if (buf[i] != L'\0')
00051       {
00052        printf ("wide character at offset %d is %#x\n",
00053               i, (unsigned int) buf[i]);
00054        return 1;
00055       }
00056 
00057   if (wmemcmp (buf + 32768, L"foo", 3) != 0)
00058     {
00059       puts ("written string incorrect");
00060       return 1;
00061     }
00062 
00063   /* Mark the buffer.  */
00064   wmemset (buf, L'A', size);
00065   free (buf);
00066 
00067   /* Try again, this time with write mode enabled before the seek.  */
00068   fp = open_wmemstream (&buf, &size);
00069   if (fp == NULL)
00070     {
00071       puts ("2nd open_wmemstream failed");
00072       return 1;
00073     }
00074 
00075   off = ftello64 (fp);
00076   if (off != 0)
00077     {
00078       puts ("2nd initial position wrong");
00079       return 1;
00080     }
00081 
00082   if (fputws (L"bar", fp) == EOF)
00083     {
00084       puts ("2nd fputws failed");
00085       return 1;
00086     }
00087 
00088   if (fseek (fp, 32768, SEEK_SET) != 0)
00089     {
00090       puts ("2nd fseek failed");
00091       return 1;
00092     }
00093 
00094   if (fputws (L"foo", fp) == EOF)
00095     {
00096       puts ("3rd fputws failed");
00097       return 1;
00098     }
00099 
00100   if (fclose (fp) == EOF)
00101     {
00102       puts ("2nd fclose failed");
00103       return 1;
00104     }
00105 
00106   if (size != 32768 + 3)
00107     {
00108       printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
00109       return 1;
00110     }
00111 
00112   if (wmemcmp (buf, L"bar", 3) != 0)
00113     {
00114       puts ("initial string incorrect in 2nd try");
00115       return 1;
00116     }
00117 
00118   for (int i = 3; i < 32768; ++i)
00119     if (buf[i] != L'\0')
00120       {
00121        printf ("wide character at offset %d is %#x in 2nd try\n",
00122               i, (unsigned int) buf[i]);
00123        return 1;
00124       }
00125 
00126   if (wmemcmp (buf + 32768, L"foo", 3) != 0)
00127     {
00128       puts ("written string incorrect in 2nd try");
00129       return 1;
00130     }
00131 
00132   return 0;
00133 }
00134 
00135 #define TEST_FUNCTION do_test ()
00136 #include "../test-skeleton.c"