Checks
Context |
Check |
Description |
redhat-pt-bot/TryBot-apply_patch |
success
|
Patch applied to master at the time it was sent
|
linaro-tcwg-bot/tcwg_glibc_build--master-arm |
success
|
Build passed
|
redhat-pt-bot/TryBot-32bit |
success
|
Build for i686
|
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 |
success
|
Build passed
|
linaro-tcwg-bot/tcwg_glibc_check--master-arm |
success
|
Test passed
|
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 |
success
|
Test passed
|
Commit Message
There seem to be no glibc tests specifically for the fread function.
Add basic tests of that function.
Tested for x86_64.
Comments
There are three includes you could do without, but leaving them in won't
hurt either. LGTM.
Reviewed-by: DJ Delorie <dj@redhat.com>
I wonder if it would be useful to have a test that makes sure
fread(x,-1,-1,y) doesn't blindly multply -1*-1 and read one
byte... (yes, I know they're unsigned).
Joseph Myers <josmyers@redhat.com> writes:
> diff --git a/stdio-common/Makefile b/stdio-common/Makefile
> + tst-fread \
Ok.
> diff --git a/stdio-common/tst-fread.c b/stdio-common/tst-fread.c
> +/* Test fread.
> + Copyright (C) 2024 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + The GNU C Library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <https://www.gnu.org/licenses/>. */
> +
> +#include <fcntl.h>
> +#include <stdint.h>
You don't need these two
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +
> +#include <support/check.h>
> +#include <support/file_contents.h>
You don't need this one.
> +#include <support/support.h>
> +#include <support/temp_file.h>
> +#include <support/test-driver.h>
> +#include <support/xstdio.h>
> +#include <support/xunistd.h>
Ok.
> +int
> +do_test (void)
> +{
> + char *temp_dir = support_create_temp_directory ("tst-fread");
> + char *file1 = xasprintf ("%s/file1", temp_dir);
> + support_write_file_string (file1, "file1");
> + add_temp_file (file1);
Ok.
> + FILE *fp;
> + size_t ret;
> + char buf[1024];
Ok.
> + verbose_printf ("test single-byte reads\n");
> + fp = xfopen (file1, "r");
> + memset (buf, 0, sizeof buf);
> + ret = fread (buf, 1, 2, fp);
"fi"
> + TEST_COMPARE (ret, 2);
> + TEST_COMPARE (buf[0], 'f');
> + TEST_COMPARE (buf[1], 'i');
> + TEST_COMPARE (feof (fp), 0);
> + TEST_COMPARE (ftell (fp), 2);
> + memset (buf, 0, sizeof buf);
> + ret = fread (buf, 1, 3, fp);
"le1"
> + TEST_COMPARE (ret, 3);
> + TEST_COMPARE (buf[0], 'l');
> + TEST_COMPARE (buf[1], 'e');
> + TEST_COMPARE (buf[2], '1');
> + TEST_COMPARE (ftell (fp), 5);
> + TEST_COMPARE (feof (fp), 0);
> + memset (buf, 0, sizeof buf);
> + ret = fread (buf, 1, 1, fp);
""
> + TEST_COMPARE (ret, 0);
> + TEST_COMPARE (!!feof (fp), 1);
> + TEST_COMPARE (ferror (fp), 0);
> + TEST_COMPARE (ftell (fp), 5);
> + xfclose (fp);
Ok.
> + verbose_printf ("test single-byte reads, EOF part way through\n");
> + fp = xfopen (file1, "r");
> + memset (buf, 0, sizeof buf);
> + ret = fread (buf, 1, sizeof buf, fp);
> + TEST_COMPARE (ret, 5);
> + TEST_COMPARE (buf[0], 'f');
> + TEST_COMPARE (buf[1], 'i');
> + TEST_COMPARE (buf[2], 'l');
> + TEST_COMPARE (buf[3], 'e');
> + TEST_COMPARE (buf[4], '1');
> + TEST_COMPARE (!!feof (fp), 1);
> + TEST_COMPARE (ferror (fp), 0);
> + TEST_COMPARE (ftell (fp), 5);
> + xfclose (fp);
Ok.
> + verbose_printf ("test multi-byte reads\n");
> + fp = xfopen (file1, "r");
> + memset (buf, 0, sizeof buf);
> + ret = fread (buf, 2, 2, fp);
"file"
> + TEST_COMPARE (ret, 2);
> + TEST_COMPARE (buf[0], 'f');
> + TEST_COMPARE (buf[1], 'i');
> + TEST_COMPARE (buf[2], 'l');
> + TEST_COMPARE (buf[3], 'e');
> + TEST_COMPARE (feof (fp), 0);
> + TEST_COMPARE (ftell (fp), 4);
> + memset (buf, 0, sizeof buf);
> + ret = fread (buf, 3, 3, fp);
> + TEST_COMPARE (ret, 0);
> + /* The bytes written for a partial element read are unspecified. */
> + TEST_COMPARE (!!feof (fp), 1);
> + TEST_COMPARE (ferror (fp), 0);
> + TEST_COMPARE (ftell (fp), 5);
> + xfclose (fp);
Ok.
> + verbose_printf ("test read error\n");
> + fp = xfopen (file1, "r");
> + xclose (fileno (fp));
> + memset (buf, 0, sizeof buf);
> + ret = fread (buf, 1, sizeof buf, fp);
> + TEST_COMPARE (ret, 0);
> + TEST_COMPARE (feof (fp), 0);
> + TEST_COMPARE (!!ferror (fp), 1);
> + fclose (fp);
Ok.
> + verbose_printf ("test zero size\n");
> + fp = xfopen (file1, "r");
> + ret = fread (buf, 0, SIZE_MAX, fp);
> + TEST_COMPARE (ret, 0);
> + TEST_COMPARE (feof (fp), 0);
> + TEST_COMPARE (ferror (fp), 0);
> + TEST_COMPARE (ftell (fp), 0);
> + xfclose (fp);
Ok.
> + verbose_printf ("test zero items\n");
> + fp = xfopen (file1, "r");
> + ret = fread (buf, SIZE_MAX, 0, fp);
> + TEST_COMPARE (ret, 0);
> + TEST_COMPARE (feof (fp), 0);
> + TEST_COMPARE (ferror (fp), 0);
> + TEST_COMPARE (ftell (fp), 0);
> + xfclose (fp);
Ok.
> + free (temp_dir);
> + free (file1);
> + return 0;
> +}
> +
> +#include <support/test-driver.c>
Ok.
@@ -217,6 +217,7 @@ tests := \
tst-fmemopen4 \
tst-fphex \
tst-fphex-wide \
+ tst-fread \
tst-freopen2 \
tst-freopen3 \
tst-freopen4 \
new file mode 100644
@@ -0,0 +1,137 @@
+/* Test fread.
+ Copyright (C) 2024 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <support/check.h>
+#include <support/file_contents.h>
+#include <support/support.h>
+#include <support/temp_file.h>
+#include <support/test-driver.h>
+#include <support/xstdio.h>
+#include <support/xunistd.h>
+
+int
+do_test (void)
+{
+ char *temp_dir = support_create_temp_directory ("tst-fread");
+ char *file1 = xasprintf ("%s/file1", temp_dir);
+ support_write_file_string (file1, "file1");
+ add_temp_file (file1);
+ FILE *fp;
+ size_t ret;
+ char buf[1024];
+
+ verbose_printf ("test single-byte reads\n");
+ fp = xfopen (file1, "r");
+ memset (buf, 0, sizeof buf);
+ ret = fread (buf, 1, 2, fp);
+ TEST_COMPARE (ret, 2);
+ TEST_COMPARE (buf[0], 'f');
+ TEST_COMPARE (buf[1], 'i');
+ TEST_COMPARE (feof (fp), 0);
+ TEST_COMPARE (ftell (fp), 2);
+ memset (buf, 0, sizeof buf);
+ ret = fread (buf, 1, 3, fp);
+ TEST_COMPARE (ret, 3);
+ TEST_COMPARE (buf[0], 'l');
+ TEST_COMPARE (buf[1], 'e');
+ TEST_COMPARE (buf[2], '1');
+ TEST_COMPARE (ftell (fp), 5);
+ TEST_COMPARE (feof (fp), 0);
+ memset (buf, 0, sizeof buf);
+ ret = fread (buf, 1, 1, fp);
+ TEST_COMPARE (ret, 0);
+ TEST_COMPARE (!!feof (fp), 1);
+ TEST_COMPARE (ferror (fp), 0);
+ TEST_COMPARE (ftell (fp), 5);
+ xfclose (fp);
+
+ verbose_printf ("test single-byte reads, EOF part way through\n");
+ fp = xfopen (file1, "r");
+ memset (buf, 0, sizeof buf);
+ ret = fread (buf, 1, sizeof buf, fp);
+ TEST_COMPARE (ret, 5);
+ TEST_COMPARE (buf[0], 'f');
+ TEST_COMPARE (buf[1], 'i');
+ TEST_COMPARE (buf[2], 'l');
+ TEST_COMPARE (buf[3], 'e');
+ TEST_COMPARE (buf[4], '1');
+ TEST_COMPARE (!!feof (fp), 1);
+ TEST_COMPARE (ferror (fp), 0);
+ TEST_COMPARE (ftell (fp), 5);
+ xfclose (fp);
+
+ verbose_printf ("test multi-byte reads\n");
+ fp = xfopen (file1, "r");
+ memset (buf, 0, sizeof buf);
+ ret = fread (buf, 2, 2, fp);
+ TEST_COMPARE (ret, 2);
+ TEST_COMPARE (buf[0], 'f');
+ TEST_COMPARE (buf[1], 'i');
+ TEST_COMPARE (buf[2], 'l');
+ TEST_COMPARE (buf[3], 'e');
+ TEST_COMPARE (feof (fp), 0);
+ TEST_COMPARE (ftell (fp), 4);
+ memset (buf, 0, sizeof buf);
+ ret = fread (buf, 3, 3, fp);
+ TEST_COMPARE (ret, 0);
+ /* The bytes written for a partial element read are unspecified. */
+ TEST_COMPARE (!!feof (fp), 1);
+ TEST_COMPARE (ferror (fp), 0);
+ TEST_COMPARE (ftell (fp), 5);
+ xfclose (fp);
+
+ verbose_printf ("test read error\n");
+ fp = xfopen (file1, "r");
+ xclose (fileno (fp));
+ memset (buf, 0, sizeof buf);
+ ret = fread (buf, 1, sizeof buf, fp);
+ TEST_COMPARE (ret, 0);
+ TEST_COMPARE (feof (fp), 0);
+ TEST_COMPARE (!!ferror (fp), 1);
+ fclose (fp);
+
+ verbose_printf ("test zero size\n");
+ fp = xfopen (file1, "r");
+ ret = fread (buf, 0, SIZE_MAX, fp);
+ TEST_COMPARE (ret, 0);
+ TEST_COMPARE (feof (fp), 0);
+ TEST_COMPARE (ferror (fp), 0);
+ TEST_COMPARE (ftell (fp), 0);
+ xfclose (fp);
+
+ verbose_printf ("test zero items\n");
+ fp = xfopen (file1, "r");
+ ret = fread (buf, SIZE_MAX, 0, fp);
+ TEST_COMPARE (ret, 0);
+ TEST_COMPARE (feof (fp), 0);
+ TEST_COMPARE (ferror (fp), 0);
+ TEST_COMPARE (ftell (fp), 0);
+ xfclose (fp);
+
+ free (temp_dir);
+ free (file1);
+ return 0;
+}
+
+#include <support/test-driver.c>