summaryrefslogtreecommitdiff
path: root/libio/bug-wmemstream1.c
blob: 22d67f71e93f481a743377e8425480bb8eca6721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <string.h>
#include <wchar.h>


static int
do_test (void)
{
  size_t size;
  wchar_t *buf;
  FILE *fp = open_wmemstream (&buf, &size);
  if (fp == NULL)
    {
      puts ("open_wmemstream failed");
      return 1;
    }

  off64_t off = ftello64 (fp);
  if (off != 0)
    {
      puts ("initial position wrong");
      return 1;
    }

  if (fseek (fp, 32768, SEEK_SET) != 0)
    {
      puts ("fseek failed");
      return 1;
    }

  if (fputws (L"foo", fp) == EOF)
    {
      puts ("fputws failed");
      return 1;
    }

  if (fclose (fp) == EOF)
    {
      puts ("fclose failed");
      return 1;
    }

  if (size != 32768 + 3)
    {
      printf ("expected size %d, got %zu\n", 32768 + 3, size);
      return 1;
    }

  for (int i = 0; i < 32768; ++i)
    if (buf[i] != L'\0')
      {
	printf ("wide character at offset %d is %#x\n",
		i, (unsigned int) buf[i]);
	return 1;
      }

  if (wmemcmp (buf + 32768, L"foo", 3) != 0)
    {
      puts ("written string incorrect");
      return 1;
    }

  /* Mark the buffer.  */
  wmemset (buf, L'A', size);
  free (buf);

  /* Try again, this time with write mode enabled before the seek.  */
  fp = open_wmemstream (&buf, &size);
  if (fp == NULL)
    {
      puts ("2nd open_wmemstream failed");
      return 1;
    }

  off = ftello64 (fp);
  if (off != 0)
    {
      puts ("2nd initial position wrong");
      return 1;
    }

  if (fputws (L"bar", fp) == EOF)
    {
      puts ("2nd fputws failed");
      return 1;
    }

  if (fseek (fp, 32768, SEEK_SET) != 0)
    {
      puts ("2nd fseek failed");
      return 1;
    }

  if (fputws (L"foo", fp) == EOF)
    {
      puts ("3rd fputws failed");
      return 1;
    }

  if (fclose (fp) == EOF)
    {
      puts ("2nd fclose failed");
      return 1;
    }

  if (size != 32768 + 3)
    {
      printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
      return 1;
    }

  if (wmemcmp (buf, L"bar", 3) != 0)
    {
      puts ("initial string incorrect in 2nd try");
      return 1;
    }

  for (int i = 3; i < 32768; ++i)
    if (buf[i] != L'\0')
      {
	printf ("wide character at offset %d is %#x in 2nd try\n",
		i, (unsigned int) buf[i]);
	return 1;
      }

  if (wmemcmp (buf + 32768, L"foo", 3) != 0)
    {
      puts ("written string incorrect in 2nd try");
      return 1;
    }

  return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"