From 21c9d397bdbcb10413513be40374e605f8a185b1 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 14 Nov 2024 05:22:06 +0100 Subject: [PATCH] ftello: Add tests for large files. * tests/test-ftello-largefile.c: New file. * modules/ftello-extra-tests: New file. * modules/ftello-tests (Depends-on): Add ftello-extra-tests. --- ChangeLog | 7 +++ modules/ftello-extra-tests | 18 ++++++++ modules/ftello-tests | 1 + tests/test-ftello-largefile.c | 83 +++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 modules/ftello-extra-tests create mode 100644 tests/test-ftello-largefile.c diff --git a/ChangeLog b/ChangeLog index 421c2b277f..6e44c14648 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2024-11-14 Bruno Haible + + ftello: Add tests for large files. + * tests/test-ftello-largefile.c: New file. + * modules/ftello-extra-tests: New file. + * modules/ftello-tests (Depends-on): Add ftello-extra-tests. + 2024-11-14 Bruno Haible fseeko: Add tests for large files. diff --git a/modules/ftello-extra-tests b/modules/ftello-extra-tests new file mode 100644 index 0000000000..0f980f2518 --- /dev/null +++ b/modules/ftello-extra-tests @@ -0,0 +1,18 @@ +Status: +longrunning-test + +Files: +tests/test-ftello-largefile.c +tests/macros.h + +Depends-on: +fsync +fseeko +ftruncate +write + +configure.ac: + +Makefile.am: +TESTS += test-ftello-largefile +check_PROGRAMS += test-ftello-largefile diff --git a/modules/ftello-tests b/modules/ftello-tests index 90d269eae7..d709a6ebe5 100644 --- a/modules/ftello-tests +++ b/modules/ftello-tests @@ -12,6 +12,7 @@ m4/ungetc.m4 Depends-on: binary-io fdopen +ftello-extra-tests configure.ac: gl_FUNC_UNGETC_WORKS diff --git a/tests/test-ftello-largefile.c b/tests/test-ftello-largefile.c new file mode 100644 index 0000000000..4dd778b366 --- /dev/null +++ b/tests/test-ftello-largefile.c @@ -0,0 +1,83 @@ +/* Test of ftello() 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 "macros.h" + +#define TESTFILE "test-ftello.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 the value of ftello(). */ + { + FILE *fp = fopen (TESTFILE, "r"); + ASSERT (fp != NULL); + + ASSERT (fseeko (fp, 0, SEEK_END) == 0); + + off_t length = ftello (fp); + ASSERT (length > 0); + ASSERT (length == TESTFILE_ZEROES + TESTFILE_DATA_LEN); + + int ret = fclose (fp); + ASSERT (ret == 0); + } + + remove (TESTFILE); + + return test_exit_status; +} -- 2.39.5