S390: Move utf8-utf32-z9.c to multiarch folder and use s390_libc_ifunc_expr macro.
[glibc.git] / libio / bug-wmemstream1.c
blob709a3c6d66099ab9558ee830403e2103740ef084
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <wchar.h>
7 static int
8 do_test (void)
10 size_t size;
11 wchar_t *buf;
12 FILE *fp = open_wmemstream (&buf, &size);
13 if (fp == NULL)
15 puts ("open_wmemstream failed");
16 return 1;
19 off64_t off = ftello64 (fp);
20 if (off != 0)
22 puts ("initial position wrong");
23 return 1;
26 if (fseek (fp, 32768, SEEK_SET) != 0)
28 puts ("fseek failed");
29 return 1;
32 if (fputws (L"foo", fp) == EOF)
34 puts ("fputws failed");
35 return 1;
38 if (fclose (fp) == EOF)
40 puts ("fclose failed");
41 return 1;
44 if (size != 32768 + 3)
46 printf ("expected size %d, got %zu\n", 32768 + 3, size);
47 return 1;
50 for (int i = 0; i < 32768; ++i)
51 if (buf[i] != L'\0')
53 printf ("wide character at offset %d is %#x\n",
54 i, (unsigned int) buf[i]);
55 return 1;
58 if (wmemcmp (buf + 32768, L"foo", 3) != 0)
60 puts ("written string incorrect");
61 return 1;
64 /* Mark the buffer. */
65 wmemset (buf, L'A', size);
66 free (buf);
68 /* Try again, this time with write mode enabled before the seek. */
69 fp = open_wmemstream (&buf, &size);
70 if (fp == NULL)
72 puts ("2nd open_wmemstream failed");
73 return 1;
76 off = ftello64 (fp);
77 if (off != 0)
79 puts ("2nd initial position wrong");
80 return 1;
83 if (fputws (L"bar", fp) == EOF)
85 puts ("2nd fputws failed");
86 return 1;
89 if (fseek (fp, 32768, SEEK_SET) != 0)
91 puts ("2nd fseek failed");
92 return 1;
95 if (fputws (L"foo", fp) == EOF)
97 puts ("3rd fputws failed");
98 return 1;
101 if (fclose (fp) == EOF)
103 puts ("2nd fclose failed");
104 return 1;
107 if (size != 32768 + 3)
109 printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
110 return 1;
113 if (wmemcmp (buf, L"bar", 3) != 0)
115 puts ("initial string incorrect in 2nd try");
116 return 1;
119 for (int i = 3; i < 32768; ++i)
120 if (buf[i] != L'\0')
122 printf ("wide character at offset %d is %#x in 2nd try\n",
123 i, (unsigned int) buf[i]);
124 return 1;
127 if (wmemcmp (buf + 32768, L"foo", 3) != 0)
129 puts ("written string incorrect in 2nd try");
130 return 1;
133 return 0;
136 #define TEST_FUNCTION do_test ()
137 #include "../test-skeleton.c"