1 /* fmemopen tests for append and read mode.
2 Copyright (C) 2015-2016 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/>. */
22 #include <sys/types.h>
25 print_buffer (const char *s
, size_t n
)
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). */
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
);
44 fseek (fp
, 0, SEEK_SET
);
48 if (strcmp (buf
, exp
) != 0)
50 printf ("%s: check failed: %s != %s\n", __FUNCTION__
, buf
, exp
);
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. */
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
67 FILE *fp
= fmemopen (buf
, sizeof (buf
) - 2, "a");
71 fseek (fp
, 0, SEEK_SET
);
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
79 if (memcmp (buf
, exp
, sizeof (buf
)) != 0)
81 printf ("%s: check failed: ", __FUNCTION__
);
82 print_buffer (buf
, sizeof (buf
));
84 print_buffer (exp
, sizeof (exp
));
92 /* This test check for initial position and feek value for fmemopen objects
93 opened with append mode. */
95 do_test_read_append (void)
97 char buf
[32] = "testing buffer";
98 size_t buflen
= strlen (buf
);
101 /* POSIX defines for 'a+' the initial position is the first null byte. */
102 FILE *fp
= fmemopen (buf
, sizeof (buf
), "a+");
107 printf ("%s: ftell|SEEK_SET (fp) %li != strlen (%s) %zu\n",
108 __FUNCTION__
, fpos
, buf
, buflen
);
113 fseek (fp
, 0, SEEK_END
);
117 printf ("%s: ftell|SEEK_END (fp) %li != strlen (%s) %zu\n",
118 __FUNCTION__
, fpos
, buf
, buflen
);
124 /* Check if attempting to read past the current size, defined as strlen (buf)
126 fp
= fmemopen (buf
, sizeof (buf
), "a+");
129 printf ("%s: getc(fp) != EOF\n", __FUNCTION__
);
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. */
142 do_test_read_seek_neg (const char *mode
, const char *expected
)
144 char buf
[] = "abcdefghijklmnopqrstuvxz0123456789";
146 size_t tmps
= sizeof (tmps
);
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
);
160 printf (" expected: ");
161 print_buffer (expected
, tmps
);
172 do_test_read_seek_negative (void)
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");
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 ();
205 #define TEST_FUNCTION do_test ()
206 #include "../test-skeleton.c"