From: Bruno Haible Date: Thu, 14 Nov 2024 04:20:33 +0000 (+0100) Subject: fseeko: Add tests for large files. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=b18f628def8ff2187ae2912bac003a2549299fb8;p=gnulib.git fseeko: Add tests for large files. * tests/test-fseeko-largefile.c: New file. * modules/fseeko-extra-tests: New file. * modules/fseeko-tests (Depends-on): Add fseeko-extra-tests. --- diff --git a/ChangeLog b/ChangeLog index 671d025e30..421c2b277f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2024-11-14 Bruno Haible + + fseeko: Add tests for large files. + * tests/test-fseeko-largefile.c: New file. + * modules/fseeko-extra-tests: New file. + * modules/fseeko-tests (Depends-on): Add fseeko-extra-tests. + 2024-11-14 Bruno Haible ftello: Fix override on mingw. diff --git a/modules/fseeko-extra-tests b/modules/fseeko-extra-tests new file mode 100644 index 0000000000..e59b800e66 --- /dev/null +++ b/modules/fseeko-extra-tests @@ -0,0 +1,17 @@ +Status: +longrunning-test + +Files: +tests/test-fseeko-largefile.c +tests/macros.h + +Depends-on: +fsync +ftruncate +write + +configure.ac: + +Makefile.am: +TESTS += test-fseeko-largefile +check_PROGRAMS += test-fseeko-largefile diff --git a/modules/fseeko-tests b/modules/fseeko-tests index 22e1e2b472..4fc539364a 100644 --- a/modules/fseeko-tests +++ b/modules/fseeko-tests @@ -12,6 +12,7 @@ m4/ungetc.m4 Depends-on: fdopen +fseeko-extra-tests configure.ac: gl_FUNC_UNGETC_WORKS diff --git a/tests/test-fseeko-largefile.c b/tests/test-fseeko-largefile.c new file mode 100644 index 0000000000..7cb7edc264 --- /dev/null +++ b/tests/test-fseeko-largefile.c @@ -0,0 +1,86 @@ +/* Test of fseeko() function on large files. + Copyright (C) 2024 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2024. */ + +#include + +/* Specification. */ +#include + +#include +#include +#include + +#include "macros.h" + +#define TESTFILE "test-fseeko.data" +#define TESTFILE_ZEROES 3000000000LL +#define TESTFILE_DATA "GNU" +#define TESTFILE_DATA_LEN 3 + +int +main (void) +{ + remove (TESTFILE); + + if (sizeof (off_t) <= 4) + { + fprintf (stderr, "off_t is only 32 bits.\n"); + return 77; + } + /* off_t is larger than 32-bit. */ + + /* Create a file that is larger than 2 GiB. + On file systems such as ext2, the file will have "holes" and thus + not consume much disk space. */ + { + int fd = open (TESTFILE, O_CREAT | O_TRUNC | O_RDWR, 0600); + ASSERT (fd >= 0); + if (ftruncate (fd, TESTFILE_ZEROES) < 0 + || lseek (fd, TESTFILE_ZEROES, SEEK_SET) < 0 + || write (fd, TESTFILE_DATA, TESTFILE_DATA_LEN) < 0 + || fsync (fd) < 0) + { + close (fd); + remove (TESTFILE); + fprintf (stderr, "Could not create 3 GB large file.\n"); + return 77; + } + close (fd); + } + + /* Check that fseeko() works. */ + { + FILE *fp = fopen (TESTFILE, "r"); + ASSERT (fp != NULL); + + int ret; + ret = fseeko (fp, TESTFILE_ZEROES, SEEK_SET); + ASSERT (ret == 0); + + char buf[TESTFILE_DATA_LEN]; + ASSERT (fread (buf, 1, TESTFILE_DATA_LEN, fp) == TESTFILE_DATA_LEN); + ASSERT (memcmp (buf, TESTFILE_DATA, TESTFILE_DATA_LEN) == 0); + + ret = fclose (fp); + ASSERT (ret == 0); + } + + remove (TESTFILE); + + return test_exit_status; +}