[v3,0/7] Fix some libm static issues
[platform/upstream/glibc.git] / libio / bug-memstream1.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5
6 static int
7 do_test (void)
8 {
9   size_t size;
10   char *buf;
11   FILE *fp = open_memstream (&buf, &size);
12   if (fp == NULL)
13     {
14       puts ("open_memstream failed");
15       return 1;
16     }
17
18   off64_t off = ftello64 (fp);
19   if (off != 0)
20     {
21       puts ("initial position wrong");
22       return 1;
23     }
24
25   if (fseek (fp, 32768, SEEK_SET) != 0)
26     {
27       puts ("fseek failed");
28       return 1;
29     }
30
31   if (fputs ("foo", fp) == EOF)
32     {
33       puts ("fputs failed");
34       return 1;
35     }
36
37   if (fclose (fp) == EOF)
38     {
39       puts ("fclose failed");
40       return 1;
41     }
42
43   if (size != 32768 + 3)
44     {
45       printf ("expected size %d, got %zu\n", 32768 + 3, size);
46       return 1;
47     }
48
49   for (int i = 0; i < 32768; ++i)
50     if (buf[i] != '\0')
51       {
52         printf ("byte at offset %d is %#hhx\n", i, buf[i]);
53         return 1;
54       }
55
56   if (memcmp (buf + 32768, "foo", 3) != 0)
57     {
58       puts ("written string incorrect");
59       return 1;
60     }
61
62   /* Mark the buffer.  */
63   memset (buf, 'A', size);
64   free (buf);
65
66   /* Try again, this time with write mode enabled before the seek.  */
67   fp = open_memstream (&buf, &size);
68   if (fp == NULL)
69     {
70       puts ("2nd open_memstream failed");
71       return 1;
72     }
73
74   off = ftello64 (fp);
75   if (off != 0)
76     {
77       puts ("2nd initial position wrong");
78       return 1;
79     }
80
81   if (fputs ("bar", fp) == EOF)
82     {
83       puts ("2nd fputs failed");
84       return 1;
85     }
86
87   if (fseek (fp, 32768, SEEK_SET) != 0)
88     {
89       puts ("2nd fseek failed");
90       return 1;
91     }
92
93   if (fputs ("foo", fp) == EOF)
94     {
95       puts ("3rd fputs failed");
96       return 1;
97     }
98
99   if (fclose (fp) == EOF)
100     {
101       puts ("2nd fclose failed");
102       return 1;
103     }
104
105   if (size != 32768 + 3)
106     {
107       printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
108       return 1;
109     }
110
111   if (memcmp (buf, "bar", 3) != 0)
112     {
113       puts ("initial string incorrect in 2nd try");
114       return 1;
115     }
116
117   for (int i = 3; i < 32768; ++i)
118     if (buf[i] != '\0')
119       {
120         printf ("byte at offset %d is %#hhx in 2nd try\n", i, buf[i]);
121         return 1;
122       }
123
124   if (memcmp (buf + 32768, "foo", 3) != 0)
125     {
126       puts ("written string incorrect in 2nd try");
127       return 1;
128     }
129
130   return 0;
131 }
132
133 #define TEST_FUNCTION do_test ()
134 #include "../test-skeleton.c"