]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/tst-memstream3.c
Update copyright dates not handled by scripts/update-copyrights.
[thirdparty/glibc.git] / libio / tst-memstream3.c
1 /* Test for open_memstream implementation.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <mcheck.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <errno.h>
24
25
26 #ifndef CHAR_T
27 # define CHAR_T char
28 # define W(o) o
29 # define OPEN_MEMSTREAM open_memstream
30 # define PRINTF printf
31 # define FWRITE fwrite
32 # define FPUTC fputc
33 # define STRCMP strcmp
34 #endif
35
36 #define S(s) S1 (s)
37 #define S1(s) #s
38
39 static void
40 mcheck_abort (enum mcheck_status ev)
41 {
42 printf ("mecheck failed with status %d\n", (int) ev);
43 exit (1);
44 }
45
46 static void
47 error_printf (int line, const char *fmt, ...)
48 {
49 va_list ap;
50
51 printf ("error: %s:%i: ", __FILE__, line);
52 va_start (ap, fmt);
53 vprintf (fmt, ap);
54 va_end (ap);
55 }
56
57 #define ERROR_RET1(...) \
58 { error_printf(__LINE__, __VA_ARGS__); return 1; }
59
60 static int
61 do_test_bz18241 (void)
62 {
63 CHAR_T *buf;
64 size_t size;
65
66 FILE *fp = OPEN_MEMSTREAM (&buf, &size);
67 if (fp == NULL)
68 ERROR_RET1 ("%s failed\n", S(OPEN_MEMSTREAM));
69
70 if (FPUTC (W('a'), fp) != W('a'))
71 ERROR_RET1 ("%s failed (errno = %d)\n", S(FPUTC), errno);
72 if (fflush (fp) != 0)
73 ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
74 if (fseek (fp, -2, SEEK_SET) != -1)
75 ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
76 if (errno != EINVAL)
77 ERROR_RET1 ("errno != EINVAL\n");
78 if (ftell (fp) != 1)
79 ERROR_RET1 ("ftell failed (errno = %d)\n", errno);
80 if (ferror (fp) != 0)
81 ERROR_RET1 ("ferror != 0\n");
82
83 if (fseek (fp, -1, SEEK_CUR) == -1)
84 ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
85 if (ftell (fp) != 0)
86 ERROR_RET1 ("ftell failed (errno = %d)\n", errno);
87 if (ferror (fp) != 0)
88 ERROR_RET1 ("ferror != 0\n");
89 if (FPUTC (W('b'), fp) != W('b'))
90 ERROR_RET1 ("%s failed (errno = %d)\n", S(FPUTC), errno);
91 if (fflush (fp) != 0)
92 ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
93
94 if (fclose (fp) != 0)
95 ERROR_RET1 ("fclose failed (errno = %d\n", errno);
96
97 if (STRCMP (buf, W("b")) != 0)
98 ERROR_RET1 ("%s failed\n", S(STRCMP));
99
100 free (buf);
101
102 return 0;
103 }
104
105 static int
106 do_test_bz20181 (void)
107 {
108 CHAR_T *buf;
109 size_t size;
110 size_t ret;
111
112 FILE *fp = OPEN_MEMSTREAM (&buf, &size);
113 if (fp == NULL)
114 ERROR_RET1 ("%s failed\n", S(OPEN_MEMSTREAM));
115
116 if ((ret = FWRITE (W("abc"), 1, 3, fp)) != 3)
117 ERROR_RET1 ("%s failed (errno = %d)\n", S(FWRITE), errno);
118
119 if (fseek (fp, 0, SEEK_SET) != 0)
120 ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
121
122 if (FWRITE (W("z"), 1, 1, fp) != 1)
123 ERROR_RET1 ("%s failed (errno = %d)\n", S(FWRITE), errno);
124
125 if (fflush (fp) != 0)
126 ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
127
128 /* Avoid truncating the buffer on close. */
129 if (fseek (fp, 3, SEEK_SET) != 0)
130 ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
131
132 if (fclose (fp) != 0)
133 ERROR_RET1 ("fclose failed (errno = %d\n", errno);
134
135 if (size != 3)
136 ERROR_RET1 ("size != 3\n");
137
138 if (buf[0] != W('z')
139 || buf[1] != W('b')
140 || buf[2] != W('c'))
141 {
142 PRINTF (W("error: buf {%c,%c,%c} != {z,b,c}\n"),
143 buf[0], buf[1], buf[2]);
144 return 1;
145 }
146
147 free (buf);
148
149 return 0;
150 }
151
152 static int
153 do_test (void)
154 {
155 int ret = 0;
156
157 mcheck_pedantic (mcheck_abort);
158
159 ret += do_test_bz18241 ();
160 ret += do_test_bz20181 ();
161
162 return ret;
163 }
164
165 #define TEST_FUNCTION do_test ()
166 #include "../test-skeleton.c"