Remove i486 subdirectory
[glibc.git] / stdio-common / tst-fmemopen3.c
blobbe481539caa563f6130994f2ebe1d8b7f6e4194f
1 /* fmemopen tests for append and read mode.
2 Copyright (C) 2015 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/>. */
19 #include <assert.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/types.h>
24 static void
25 print_buffer (const char *s, size_t n)
27 size_t i;
28 for (i=0; i<n; ++i)
29 printf ("0x%02X (%c), ", s[i], s[i]);
32 /* This test check append mode initial position (a/a+) based on POSIX defition
33 (BZ#6544 and BZ#13151). */
34 static int
35 do_test_write_append (const char *mode)
37 char buf[32] = "testing buffer";
38 char exp[32] = "testing bufferXX";
40 FILE *fp = fmemopen (buf, sizeof (buf), mode);
42 fflush (fp);
43 fprintf (fp, "X");
44 fseek (fp, 0, SEEK_SET);
45 fprintf (fp, "X");
46 fclose (fp);
48 if (strcmp (buf, exp) != 0)
50 printf ("%s: check failed: %s != %s\n", __FUNCTION__, buf, exp);
51 return 1;
54 return 0;
57 /* This test check append mode initial position (a/a+) based on POSIX defition
58 (BZ#6544 and BZ#13151) for buffer without null byte end. */
59 static int
60 do_test_write_append_without_null (const char *mode)
62 char buf[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
63 char exp[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
65 /* If '\0' is not found in buffer, POSIX states that SEEK_SET should be
66 the size argument. */
67 FILE *fp = fmemopen (buf, sizeof (buf) - 2, "a");
69 fflush (fp);
70 fputc (0x70, fp);
71 fseek (fp, 0, SEEK_SET);
72 fputc (0x70, fp);
73 fputc (0x70, fp);
74 fclose (fp);
76 /* POSIX also states that a write operation on the stream shall not advance
77 the current buffer size beyond the size given in fmemopen, so the string
78 should be same. */
79 if (memcmp (buf, exp, sizeof (buf)) != 0)
81 printf ("%s: check failed: ", __FUNCTION__);
82 print_buffer (buf, sizeof (buf));
83 printf ("!= ");
84 print_buffer (exp, sizeof (exp));
85 printf ("\n");
86 return 1;
89 return 0;
92 /* This test check for initial position and feek value for fmemopen objects
93 opened with append mode. */
94 static int
95 do_test_read_append (void)
97 char buf[32] = "testing buffer";
98 size_t buflen = strlen (buf);
99 long fpos;
101 /* POSIX defines for 'a+' the initial position is the first null byte. */
102 FILE *fp = fmemopen (buf, sizeof (buf), "a+");
104 fpos = ftell (fp);
105 if (fpos != buflen)
107 printf ("%s: ftell|SEEK_SET (fp) %li != strlen (%s) %zu\n",
108 __FUNCTION__, fpos, buf, buflen);
109 fclose (fp);
110 return 1;
113 fseek (fp, 0, SEEK_END);
115 if (fpos != buflen)
117 printf ("%s: ftell|SEEK_END (fp) %li != strlen (%s) %zu\n",
118 __FUNCTION__, fpos, buf, buflen);
119 fclose (fp);
120 return 1;
122 fclose (fp);
124 /* Check if attempting to read past the current size, defined as strlen (buf)
125 yield an EOF. */
126 fp = fmemopen (buf, sizeof (buf), "a+");
127 if (getc(fp) != EOF)
129 printf ("%s: getc(fp) != EOF\n", __FUNCTION__);
130 fclose (fp);
131 return -1;
134 fclose (fp);
136 return 0;
139 /* This test check for fseek (SEEK_END) using negative offsets (BZ#14292). The
140 starting position of descriptor is different base on the opening mode. */
141 static int
142 do_test_read_seek_neg (const char *mode, const char *expected)
144 char buf[] = "abcdefghijklmnopqrstuvxz0123456789";
145 char tmp[10];
146 size_t tmps = sizeof (tmps);
147 long offset = -11;
149 FILE *fp = fmemopen (buf, sizeof (buf), mode);
150 fseek (fp, offset, SEEK_END);
151 fread (tmp, tmps, 1, fp);
153 if (memcmp (tmp, expected, tmps) != 0)
155 printf ("%s: fmemopen(%s) - fseek (fp, %li, SEEK_END):\n",
156 __FUNCTION__, mode, offset);
157 printf (" returned: ");
158 print_buffer (tmp, tmps);
159 printf ("\n");
160 printf (" expected: ");
161 print_buffer (expected, tmps);
162 printf ("\n");
163 return 1;
166 fclose (fp);
168 return 0;
171 static int
172 do_test_read_seek_negative (void)
174 int ret = 0;
176 /* 'r' and 'w' modes defines the initial position at the buffer start and
177 seek with SEEK_END shall seek relative to its size give in fmemopen
178 call. The expected tmp result is 0 to 9 *without* the ending null */
179 ret += do_test_read_seek_neg ("r", "0123456789");
180 /* 'a+' mode sets the initial position at the first null byte in buffer and
181 SEEK_END shall seek relative to its size as well. The expected result is
182 z012345678, since SEEK_END plus a+ start at '\0', not size. */
183 ret += do_test_read_seek_neg ("a+", "z012345678");
185 return ret;
188 static int
189 do_test (void)
191 int ret = 0;
193 ret += do_test_write_append ("a");
194 ret += do_test_write_append_without_null ("a");
195 ret += do_test_write_append ("a+");
196 ret += do_test_write_append_without_null ("a+");
198 ret += do_test_read_append ();
200 ret += do_test_read_seek_negative ();
202 return ret;
205 #define TEST_FUNCTION do_test ()
206 #include "../test-skeleton.c"