2 Copyright (C) 2015-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
23 #include <sys/types.h>
27 /* Check fmemopen with user provided buffer open for write. */
29 do_test_with_buffer (void)
33 const size_t nbuf
= sizeof (buf
);
35 FILE *fp
= fmemopen (buf
, nbuf
, "w");
38 printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__
);
42 /* Default write operation, check if file position is correct after it. */
43 static const char str
[] = "hello world";
44 const size_t nstr
= sizeof (str
) - 1;
46 off_t o
= ftello (fp
);
49 printf ("FAIL: first ftello returned %jd, expected %zu\n",
54 /* Rewind stream and seek tests, the position size should be equal to
55 buffer size provided in open function. */
60 printf ("FAIL: second ftello returned %jd, expected 0\n",
64 if (fseeko (fp
, 0, SEEK_END
) != 0)
66 printf ("FAIL: fseeko failed\n");
72 printf ("FAIL: third ftello returned %jd, expected %zu\n",
77 /* Rewind the stream and recheck by using a shorter string. */
79 static const char str2
[] = "just hello";
80 const size_t nstr2
= sizeof (str2
) - 1;
81 assert (nstr2
< nstr
);
86 printf ("FAIL: fourth ftello returned %jd, expected %zu\n",
92 /* Again, but now with a larger string. */
93 static const char str3
[] = "just hellod";
94 if (strcmp (buf
, str3
) != 0)
96 printf ("FAIL: final string is \"%s\", expected \"%s\"\n",
103 /* Check fmemopen without user provided buffer open for write. */
105 do_test_without_buffer (void)
108 const size_t nbuf
= 100;
110 FILE *fp
= fmemopen (NULL
, nbuf
, "w");
113 printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__
);
117 static const char str
[] = "hello world";
118 const size_t nstr
= sizeof (str
) - 1;
120 /* Default write operation, check if file position is correct after it. */
122 off_t o
= ftello (fp
);
125 printf ("FAIL: first ftello returned %jd, expected %zu\n",
129 if (fseeko (fp
, 0, SEEK_END
) != 0)
131 printf ("FAIL: fseeko failed\n");
137 printf ("FAIL: second ftello returned %jd, expected %zu\n",
142 /* Rewind the stream and recheck by using a shorter string. */
144 static const char str2
[] = "just hello";
145 const size_t nstr2
= sizeof (str2
) - 1;
146 assert (nstr2
< nstr
);
151 printf ("FAIL: third ftello returned %jd, expected %zu\n",
152 (intmax_t) o
, nstr2
);
160 /* Check fmemopen with a buffer lenght of zero. */
162 do_test_length_zero (void)
166 #define BUFCONTENTS "testing buffer"
167 char buf
[100] = BUFCONTENTS
;
168 const size_t nbuf
= 0;
171 fp
= fmemopen (buf
, nbuf
, "r");
174 printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__
);
178 /* Reading any data on a zero-length buffer should return EOF. */
179 if ((r
= fgetc (fp
)) != EOF
)
181 printf ("FAIL: fgetc on a zero-length returned: %d\n", r
);
184 off_t o
= ftello (fp
);
187 printf ("FAIL: first ftello returned %jd, expected 0\n",
193 /* Writing any data shall start at current position and shall not pass
194 current buffer size beyond the size in fmemopen call. */
195 fp
= fmemopen (buf
, nbuf
, "w");
198 printf ("FAIL: second fmemopen failed (%s)\n", __FUNCTION__
);
202 static const char str
[] = "hello world";
203 /* Because of buffering, the fputs call itself will not fail. However the
204 final buffer should be not changed because length 0 was passed to the
209 if (fflush (fp
) != EOF
)
211 printf ("FAIL: fflush did not return EOF\n");
217 printf ("FAIL: errno is %i (expected ENOSPC)\n", errno
);
224 if (strcmp (buf
, BUFCONTENTS
) != 0)
226 printf ("FAIL: strcmp (%s, %s) failed\n", buf
, BUFCONTENTS
);
230 /* Different than 'w' mode, 'w+' truncates the buffer. */
231 fp
= fmemopen (buf
, nbuf
, "w+");
234 printf ("FAIL: third fmemopen failed (%s)\n", __FUNCTION__
);
240 if (strcmp (buf
, "") != 0)
242 printf ("FAIL: strcmp (%s, \"\") failed\n", buf
);
254 ret
+= do_test_with_buffer ();
255 ret
+= do_test_without_buffer ();
256 ret
+= do_test_length_zero ();
261 #define TEST_FUNCTION do_test ()
262 #include "../test-skeleton.c"